Skip to content

Commit

Permalink
Code Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ahnv committed Sep 30, 2020
1 parent 023f81a commit 92c2ed2
Show file tree
Hide file tree
Showing 12 changed files with 92 additions and 89 deletions.
3 changes: 2 additions & 1 deletion imagekit/src/main/java/com/imagekit/android/ImageKit.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
104 changes: 57 additions & 47 deletions imagekit/src/main/java/com/imagekit/android/ImagekitUrlConstructor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -23,7 +23,8 @@ class ImagekitUrlConstructor constructor(
private val transformationMap = HashMap<String, Any>()
private var path: String? = null
private var isSource: Boolean = true
private var queryParams: HashMap<String, String> = hashMapOf(IK_VERSION_KEY to "android-${BuildConfig.API_VERSION}")
private var queryParams: HashMap<String, String> =
hashMapOf(IK_VERSION_KEY to "android-${BuildConfig.API_VERSION}")

constructor(
context: Context,
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -738,7 +740,7 @@ class ImagekitUrlConstructor constructor(
* @return the current ImagekitUrlConstructor object.
*/
fun addCustomQueryParameter(params: HashMap<String, String>): ImagekitUrlConstructor {
params.forEach{ (key, value) -> queryParams[key] = value }
params.forEach { (key, value) -> queryParams[key] = value }
return this
}

Expand All @@ -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()

}

Expand Down
33 changes: 13 additions & 20 deletions imagekit/src/main/java/com/imagekit/android/data/Repository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
@@ -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) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -24,5 +24,5 @@ class ContextModule (val context: Context){
* @return the Context to be provided
*/
@Provides
fun provideContext(): Context = context
fun provideContext(): Context = context
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -25,7 +24,7 @@ class SignatureApi @Inject constructor(
}

return NetworkManager
.getApiInterface()
.getSignature(endPoint, expire)
.getApiInterface()
.getSignature(endPoint, expire)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,20 @@ 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(
KEY_CLIENT_AUTHENTICATION_ENDPOINT_KEY,
clientAuthenticationEndpoint
).apply()

fun getClientAuthenticationEndpoint() = mPref.getString(KEY_CLIENT_AUTHENTICATION_ENDPOINT_KEY, "")!!
fun getClientAuthenticationEndpoint() =
mPref.getString(KEY_CLIENT_AUTHENTICATION_ENDPOINT_KEY, "")!!

}
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 92c2ed2

Please sign in to comment.