From 92c2ed2ea902a3c9f80e42f121b05e1e3be56e0e Mon Sep 17 00:00:00 2001 From: Abhinav Dhiman Date: Wed, 30 Sep 2020 13:26:46 +0530 Subject: [PATCH] Code Cleanup --- .../java/com/imagekit/android/ImageKit.kt | 3 +- .../android/ImagekitUrlConstructor.kt | 104 ++++++++++-------- .../com/imagekit/android/data/Repository.kt | 33 +++--- .../ApplicationContextExpectedException.kt | 4 +- .../android/injection/module/ContextModule.kt | 4 +- .../android/retrofit/ApiInterface.java | 3 - .../android/retrofit/NetworkManager.java | 9 +- .../imagekit/android/retrofit/SignatureApi.kt | 5 +- .../com/imagekit/android/util/BitmapUtil.kt | 2 +- .../java/com/imagekit/android/util/LogUtil.kt | 2 +- .../imagekit/android/util/SharedPrefUtil.kt | 10 +- .../android/util/TranformationMapping.kt | 2 +- 12 files changed, 92 insertions(+), 89 deletions(-) diff --git a/imagekit/src/main/java/com/imagekit/android/ImageKit.kt b/imagekit/src/main/java/com/imagekit/android/ImageKit.kt index 8bce54d..3390cbd 100644 --- a/imagekit/src/main/java/com/imagekit/android/ImageKit.kt +++ b/imagekit/src/main/java/com/imagekit/android/ImageKit.kt @@ -82,7 +82,8 @@ class ImageKit private constructor( private lateinit var appComponent: UtilComponent } - @JvmOverloads fun url( + @JvmOverloads + fun url( path: String, urlEndpoint: String = mSharedPrefUtil.getImageKitUrlEndpoint(), transformationPosition: TransformationPosition = mSharedPrefUtil.getTransformationPosition() diff --git a/imagekit/src/main/java/com/imagekit/android/ImagekitUrlConstructor.kt b/imagekit/src/main/java/com/imagekit/android/ImagekitUrlConstructor.kt index 58b5897..5c8a438 100644 --- a/imagekit/src/main/java/com/imagekit/android/ImagekitUrlConstructor.kt +++ b/imagekit/src/main/java/com/imagekit/android/ImagekitUrlConstructor.kt @@ -9,8 +9,8 @@ import com.imagekit.android.util.TranformationMapping import com.imagekit.android.util.TranformationMapping.overlayTransparency import java.lang.Math.abs import java.net.URI -import java.util.* import java.net.URLEncoder +import java.util.* @Suppress("unused") @@ -23,7 +23,8 @@ class ImagekitUrlConstructor constructor( private val transformationMap = HashMap() private var path: String? = null private var isSource: Boolean = true - private var queryParams: HashMap = hashMapOf(IK_VERSION_KEY to "android-${BuildConfig.API_VERSION}") + private var queryParams: HashMap = + hashMapOf(IK_VERSION_KEY to "android-${BuildConfig.API_VERSION}") constructor( context: Context, @@ -341,7 +342,7 @@ class ImagekitUrlConstructor constructor( * or the overlay focus has already been applied */ fun overlayX(overlayX: Int): ImagekitUrlConstructor { - val s = if(overlayX < 0) + val s = if (overlayX < 0) String.format("%s-N%s", TranformationMapping.overlayX, abs(overlayX)) else String.format("%s-%d", TranformationMapping.overlayX, overlayX) @@ -362,7 +363,7 @@ class ImagekitUrlConstructor constructor( * or the overlay focus has already been applied */ fun overlayY(overlayY: Int): ImagekitUrlConstructor { - val s = if(overlayY < 0) + val s = if (overlayY < 0) String.format("%s-N%s", TranformationMapping.overlayY, abs(overlayY)) else String.format("%s-%d", TranformationMapping.overlayY, overlayY) @@ -412,7 +413,8 @@ class ImagekitUrlConstructor constructor( * @return the current ImagekitUrlConstructor object. */ fun overlayText(overlayText: String): ImagekitUrlConstructor { - transformationMap[TranformationMapping.overlayText] = URLEncoder.encode(overlayText, "UTF-8") + transformationMap[TranformationMapping.overlayText] = + URLEncoder.encode(overlayText, "UTF-8") transformationList.add( String.format( "%s-%s", @@ -662,7 +664,7 @@ class ImagekitUrlConstructor constructor( * @return the current ImagekitUrlConstructor object. */ fun effectSharpen(value: Int = 0): ImagekitUrlConstructor { - if (value == 0){ + if (value == 0) { transformationMap[TranformationMapping.sharpen] = true transformationList.add(String.format("%s", TranformationMapping.sharpen)) } else { @@ -738,7 +740,7 @@ class ImagekitUrlConstructor constructor( * @return the current ImagekitUrlConstructor object. */ fun addCustomQueryParameter(params: HashMap): ImagekitUrlConstructor { - params.forEach{ (key, value) -> queryParams[key] = value } + params.forEach { (key, value) -> queryParams[key] = value } return this } @@ -747,56 +749,64 @@ class ImagekitUrlConstructor constructor( * @return the Url used to fetch an image after applying the specified transformations. */ fun create(): String { - var url = source.trim('/') - path = path?.trim('/') + var url = source.trim('/') + path = path?.trim('/') - if (url.isEmpty()) { - return "" - } + if (url.isEmpty()) { + return "" + } - if (transformationList.isNotEmpty()) { - var transforms = transformationList.map{ transformation -> transformation }.joinToString(",").replace(",:,", ":") - if (isSource){ - transformationPosition = TransformationPosition.QUERY - if (url.contains("?tr=")) { - url = url.substring(0, url.indexOf("?tr=")) - } - if (url.contains("/tr:")) { - url = url.replace( - url.substring(url.indexOf("/tr:"), url.lastIndexOf("/")), - "" - ) - } - queryParams["tr"] = transforms - } else { - if (transformationPosition == TransformationPosition.PATH) { - url = String.format("%s/%s", addPathParams(url), path) - } - if (transformationPosition == TransformationPosition.QUERY) { - url = String.format("%s/%s", url, path) - queryParams["tr"] = transforms - } + if (transformationList.isNotEmpty()) { + var transforms = + transformationList.map { transformation -> transformation }.joinToString(",") + .replace(",:,", ":") + if (isSource) { + transformationPosition = TransformationPosition.QUERY + if (url.contains("?tr=")) { + url = url.substring(0, url.indexOf("?tr=")) + } + if (url.contains("/tr:")) { + url = url.replace( + url.substring(url.indexOf("/tr:"), url.lastIndexOf("/")), + "" + ) } + queryParams["tr"] = transforms } else { - if (!isSource){ - url = String.format( "%s/%s", url, path ) + if (transformationPosition == TransformationPosition.PATH) { + url = String.format("%s/%s", addPathParams(url), path) + } + if (transformationPosition == TransformationPosition.QUERY) { + url = String.format("%s/%s", url, path) + queryParams["tr"] = transforms } } + } else { + if (!isSource) { + url = String.format("%s/%s", url, path) + } + } - var u = URI(url) - val sb = StringBuilder( - if (u.query == null) - "" - else - u.query - ) + var u = URI(url) + val sb = StringBuilder( + if (u.query == null) + "" + else + u.query + ) - if (sb.isNotEmpty()) - sb.append('&') + if (sb.isNotEmpty()) + sb.append('&') - sb.append(queryParams.map{ (key, value) -> String.format("%s=%s", key, value) }.joinToString("&")) + sb.append(queryParams.map { (key, value) -> + String.format( + "%s=%s", + key, + value + ) + }.joinToString("&")) - return URI(u.scheme, u.authority, u.path, sb.toString(), u.fragment).toString() + return URI(u.scheme, u.authority, u.path, sb.toString(), u.fragment).toString() } diff --git a/imagekit/src/main/java/com/imagekit/android/data/Repository.kt b/imagekit/src/main/java/com/imagekit/android/data/Repository.kt index 6b2399a..8bdf4cd 100644 --- a/imagekit/src/main/java/com/imagekit/android/data/Repository.kt +++ b/imagekit/src/main/java/com/imagekit/android/data/Repository.kt @@ -6,18 +6,11 @@ import android.graphics.Bitmap import com.google.gson.Gson import com.imagekit.android.ImageKitCallback import com.imagekit.android.R -import com.imagekit.android.entity.SignatureResponse import com.imagekit.android.entity.UploadError import com.imagekit.android.entity.UploadResponse import com.imagekit.android.retrofit.SignatureApi import com.imagekit.android.retrofit.UploadApi import com.imagekit.android.util.BitmapUtil.bitmapToFile -import io.reactivex.Observable -import io.reactivex.Single -import io.reactivex.android.schedulers.AndroidSchedulers -import io.reactivex.functions.BiFunction -import io.reactivex.schedulers.Schedulers -import okhttp3.ResponseBody import retrofit2.HttpException import java.io.File import java.util.concurrent.TimeUnit @@ -46,20 +39,20 @@ class Repository @Inject constructor( responseFields: String?, imageKitCallback: ImageKitCallback ) = upload( - bitmapToFile( - context, - fileName, - image - ), + bitmapToFile( + context, fileName, - useUniqueFilename, - tags, - folder, - isPrivateFile, - customCoordinates, - responseFields, - imageKitCallback - ) + image + ), + fileName, + useUniqueFilename, + tags, + folder, + isPrivateFile, + customCoordinates, + responseFields, + imageKitCallback + ) //Takes file @SuppressLint("CheckResult") diff --git a/imagekit/src/main/java/com/imagekit/android/exception/ApplicationContextExpectedException.kt b/imagekit/src/main/java/com/imagekit/android/exception/ApplicationContextExpectedException.kt index 3ee96b7..c830597 100644 --- a/imagekit/src/main/java/com/imagekit/android/exception/ApplicationContextExpectedException.kt +++ b/imagekit/src/main/java/com/imagekit/android/exception/ApplicationContextExpectedException.kt @@ -1,5 +1,5 @@ package com.imagekit.android.exception -class ApplicationContextExpectedException: Exception { - constructor(message: String = "Application Context Expected!!"): super(message) +class ApplicationContextExpectedException(message: String = "Application Context Expected!!") : + Exception(message) { } \ No newline at end of file diff --git a/imagekit/src/main/java/com/imagekit/android/injection/module/ContextModule.kt b/imagekit/src/main/java/com/imagekit/android/injection/module/ContextModule.kt index ac5d031..a3aaa6d 100644 --- a/imagekit/src/main/java/com/imagekit/android/injection/module/ContextModule.kt +++ b/imagekit/src/main/java/com/imagekit/android/injection/module/ContextModule.kt @@ -8,7 +8,7 @@ import dagger.Provides @Module // Safe here as we are dealing with a Dagger 2 module @Suppress("unused") -class ContextModule (val context: Context){ +class ContextModule(val context: Context) { /** * Provides the Application Context @@ -24,5 +24,5 @@ class ContextModule (val context: Context){ * @return the Context to be provided */ @Provides - fun provideContext(): Context = context + fun provideContext(): Context = context } \ No newline at end of file diff --git a/imagekit/src/main/java/com/imagekit/android/retrofit/ApiInterface.java b/imagekit/src/main/java/com/imagekit/android/retrofit/ApiInterface.java index fda667b..42dbda9 100644 --- a/imagekit/src/main/java/com/imagekit/android/retrofit/ApiInterface.java +++ b/imagekit/src/main/java/com/imagekit/android/retrofit/ApiInterface.java @@ -2,13 +2,10 @@ import com.imagekit.android.entity.SignatureResponse; -import java.util.Map; - import io.reactivex.Single; import okhttp3.MultipartBody; import okhttp3.ResponseBody; import retrofit2.http.GET; -import retrofit2.http.HeaderMap; import retrofit2.http.Multipart; import retrofit2.http.POST; import retrofit2.http.Part; diff --git a/imagekit/src/main/java/com/imagekit/android/retrofit/NetworkManager.java b/imagekit/src/main/java/com/imagekit/android/retrofit/NetworkManager.java index 6708f3c..090841d 100644 --- a/imagekit/src/main/java/com/imagekit/android/retrofit/NetworkManager.java +++ b/imagekit/src/main/java/com/imagekit/android/retrofit/NetworkManager.java @@ -18,8 +18,7 @@ public static void initialize() { createRetrofitObject(); } - private static void createRetrofitObject() - { + private static void createRetrofitObject() { HttpLoggingInterceptor logging = new HttpLoggingInterceptor(); logging.setLevel(HttpLoggingInterceptor.Level.BODY); @@ -43,10 +42,8 @@ public static String getBaseURL() { return "https://api.imagekit.io/"; } - public static ApiInterface getApiInterface() - { - if (apiInterface == null) - { + public static ApiInterface getApiInterface() { + if (apiInterface == null) { Log.i(TAG, "Api interface is null"); createRetrofitObject(); } diff --git a/imagekit/src/main/java/com/imagekit/android/retrofit/SignatureApi.kt b/imagekit/src/main/java/com/imagekit/android/retrofit/SignatureApi.kt index b654c8b..757bfb1 100644 --- a/imagekit/src/main/java/com/imagekit/android/retrofit/SignatureApi.kt +++ b/imagekit/src/main/java/com/imagekit/android/retrofit/SignatureApi.kt @@ -1,7 +1,6 @@ package com.imagekit.android.retrofit import android.content.Context -import com.imagekit.android.R import com.imagekit.android.entity.SignatureResponse import com.imagekit.android.util.LogUtil import com.imagekit.android.util.SharedPrefUtil @@ -25,7 +24,7 @@ class SignatureApi @Inject constructor( } return NetworkManager - .getApiInterface() - .getSignature(endPoint, expire) + .getApiInterface() + .getSignature(endPoint, expire) } } \ No newline at end of file diff --git a/imagekit/src/main/java/com/imagekit/android/util/BitmapUtil.kt b/imagekit/src/main/java/com/imagekit/android/util/BitmapUtil.kt index a56d696..92256b3 100644 --- a/imagekit/src/main/java/com/imagekit/android/util/BitmapUtil.kt +++ b/imagekit/src/main/java/com/imagekit/android/util/BitmapUtil.kt @@ -7,7 +7,7 @@ import java.io.File import java.io.FileOutputStream import java.io.IOException -object BitmapUtil{ +object BitmapUtil { @Throws(IOException::class) fun bitmapToFile(context: Context, filename: String, bitmap: Bitmap): File { val f = File(context.cacheDir, filename) diff --git a/imagekit/src/main/java/com/imagekit/android/util/LogUtil.kt b/imagekit/src/main/java/com/imagekit/android/util/LogUtil.kt index 35c8ead..8b28f79 100644 --- a/imagekit/src/main/java/com/imagekit/android/util/LogUtil.kt +++ b/imagekit/src/main/java/com/imagekit/android/util/LogUtil.kt @@ -5,7 +5,7 @@ import android.util.Log object LogUtil { private const val TAG = "ImageKitError" - fun logError(error: String){ + fun logError(error: String) { Log.e(TAG, error) } } \ No newline at end of file diff --git a/imagekit/src/main/java/com/imagekit/android/util/SharedPrefUtil.kt b/imagekit/src/main/java/com/imagekit/android/util/SharedPrefUtil.kt index 9bc9a12..f725c6c 100644 --- a/imagekit/src/main/java/com/imagekit/android/util/SharedPrefUtil.kt +++ b/imagekit/src/main/java/com/imagekit/android/util/SharedPrefUtil.kt @@ -36,7 +36,12 @@ class SharedPrefUtil @Inject constructor(context: Context) { fun setTransformationPosition(transformationPosition: TransformationPosition) = mPref.edit().putString(KEY_TRANSFORMATION_POSITION_KEY, transformationPosition.name).apply() - fun getTransformationPosition() = TransformationPosition.valueOf(mPref.getString(KEY_TRANSFORMATION_POSITION_KEY, TransformationPosition.PATH.name)!!) + fun getTransformationPosition() = TransformationPosition.valueOf( + mPref.getString( + KEY_TRANSFORMATION_POSITION_KEY, + TransformationPosition.PATH.name + )!! + ) fun setClientAuthenticationEndpoint(clientAuthenticationEndpoint: String?) = mPref.edit().putString( @@ -44,6 +49,7 @@ class SharedPrefUtil @Inject constructor(context: Context) { clientAuthenticationEndpoint ).apply() - fun getClientAuthenticationEndpoint() = mPref.getString(KEY_CLIENT_AUTHENTICATION_ENDPOINT_KEY, "")!! + fun getClientAuthenticationEndpoint() = + mPref.getString(KEY_CLIENT_AUTHENTICATION_ENDPOINT_KEY, "")!! } \ No newline at end of file diff --git a/imagekit/src/main/java/com/imagekit/android/util/TranformationMapping.kt b/imagekit/src/main/java/com/imagekit/android/util/TranformationMapping.kt index 4927413..230e364 100644 --- a/imagekit/src/main/java/com/imagekit/android/util/TranformationMapping.kt +++ b/imagekit/src/main/java/com/imagekit/android/util/TranformationMapping.kt @@ -19,7 +19,7 @@ internal object TranformationMapping { const val trimEdges = "t" const val overlayImage = "oi" const val overlayFocus = "ofo" - const val overlayX = "ox" + const val overlayX = "ox" const val overlayY = "oy" const val overlayWidth = "ow" const val overlayHeight = "oh"