Skip to content

Commit

Permalink
Applied ktlint
Browse files Browse the repository at this point in the history
Applied ktlint for code linter and formatting
  • Loading branch information
Dhaval2404 committed May 13, 2019
1 parent 704510f commit 3a034d6
Show file tree
Hide file tree
Showing 17 changed files with 112 additions and 126 deletions.
2 changes: 2 additions & 0 deletions imagepicker/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

apply from: "../ktlint.gradle"

android {
compileSdkVersion 28

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,4 @@ class ExampleInstrumentedTest {

assertEquals("com.github.dhaval2404.imagepicker.test", appContext.packageName)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import java.io.File
open class ImagePicker {

companion object {
//Default Request Code to Pick Image
// Default Request Code to Pick Image
const val REQUEST_CODE = 2404
const val RESULT_ERROR = 64

Expand Down Expand Up @@ -78,19 +78,18 @@ open class ImagePicker {
*/
fun getFile(data: Intent?): File? {
val path = getFilePath(data)
if(path!=null){
if (path != null) {
return File(path)
}
return null
}

}

class Builder(private val activity: Activity) {

private var fragment: Fragment? = null

//Image Provider
// Image Provider
private var imageProvider = ImageProvider.BOTH

/*
Expand Down Expand Up @@ -128,7 +127,7 @@ open class ImagePicker {
/**
* Only Capture image using Camera.
*/
//@Deprecated("Please use provider(ImageProvider.CAMERA) instead")
// @Deprecated("Please use provider(ImageProvider.CAMERA) instead")
fun cameraOnly(): Builder {
this.imageProvider = ImageProvider.CAMERA
return this
Expand All @@ -137,7 +136,7 @@ open class ImagePicker {
/**
* Only Pick image from gallery.
*/
//@Deprecated("Please use provider(ImageProvider.GALLERY) instead")
// @Deprecated("Please use provider(ImageProvider.GALLERY) instead")
fun galleryOnly(): Builder {
this.imageProvider = ImageProvider.GALLERY
return this
Expand Down Expand Up @@ -195,7 +194,7 @@ open class ImagePicker {
*/
fun start(reqCode: Int) {
if (imageProvider == ImageProvider.BOTH) {
//Pick Image Provider if not specified
// Pick Image Provider if not specified
showImageProviderDialog(reqCode)
} else {
startActivity(reqCode)
Expand All @@ -207,7 +206,7 @@ open class ImagePicker {
*/
fun start(completionHandler: ((resultCode: Int, data: Intent?) -> Unit)? = null) {
if (imageProvider == ImageProvider.BOTH) {
//Pick Image Provider if not specified
// Pick Image Provider if not specified
showImageProviderDialog(completionHandler)
} else {
startActivity(completionHandler)
Expand Down Expand Up @@ -285,14 +284,13 @@ open class ImagePicker {
completionHandler?.invoke(result.resultCode, result.data)
}
}
} catch (e : Exception){
} catch (e: Exception) {
if (e is ClassNotFoundException) {
Toast.makeText(if (fragment!= null) fragment!!.context else activity, "InlineActivityResult library not installed falling back to default method, please install " +
Toast.makeText(if (fragment != null) fragment!!.context else activity, "InlineActivityResult library not installed falling back to default method, please install " +
"it from https://github.com/florent37/InlineActivityResult if you want to get inline activity results.", Toast.LENGTH_LONG).show()
startActivity(REQUEST_CODE)
}
}

}

/**
Expand All @@ -307,9 +305,5 @@ open class ImagePicker {
activity.startActivityForResult(intent, reqCode)
}
}


}


}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ class ImagePickerActivity : FragmentActivity() {
intent.putExtra(ImagePicker.EXTRA_ERROR, message)
return intent
}

}

private var mGalleryProvider: GalleryProvider? = null
Expand All @@ -57,7 +56,7 @@ class ImagePickerActivity : FragmentActivity() {
val bundle = intent.extras!!
val provider = bundle.getSerializable(ImagePicker.EXTRA_IMAGE_PROVIDER) as ImageProvider

//Create provider object and start process
// Create provider object and start process
when (provider) {
ImageProvider.GALLERY -> {
mGalleryProvider = GalleryProvider(this)
Expand All @@ -73,9 +72,11 @@ class ImagePickerActivity : FragmentActivity() {
/**
* Dispatch incoming result to the correct provider.
*/
override fun onRequestPermissionsResult(requestCode: Int,
permissions: Array<String>,
grantResults: IntArray) {
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<String>,
grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
mCameraProvider?.onRequestPermissionsResult(requestCode)
mGalleryProvider?.onRequestPermissionsResult(requestCode)
Expand Down Expand Up @@ -123,15 +124,15 @@ class ImagePickerActivity : FragmentActivity() {
mCropFile = file

mCameraProvider?.let {
//Delete Camera file after crop. Else there will be two image for the same action.
//In case of Gallery Provider, we will get original image path, so we will not delete that.
// Delete Camera file after crop. Else there will be two image for the same action.
// In case of Gallery Provider, we will get original image path, so we will not delete that.
mOriginalFile?.delete()
mOriginalFile = null
}

if(mCompressionProvider.isCompressionRequired(file)){
if (mCompressionProvider.isCompressionRequired(file)) {
mCompressionProvider.compress(file)
}else{
} else {
setResult(file)
}
}
Expand All @@ -142,14 +143,14 @@ class ImagePickerActivity : FragmentActivity() {
* @param file Compressed image file
*/
fun setCompressedImage(file: File) {
//This is the case when Crop is not enabled
// This is the case when Crop is not enabled
mCameraProvider?.let {
//Delete Camera file after Compress. Else there will be two image for the same action.
//In case of Gallery Provider, we will get original image path, so we will not delete that.
// Delete Camera file after Compress. Else there will be two image for the same action.
// In case of Gallery Provider, we will get original image path, so we will not delete that.
mOriginalFile?.delete()
}

//If crop file is not null, Delete it after crop
// If crop file is not null, Delete it after crop
mCropFile?.delete()
mCropFile = null

Expand Down Expand Up @@ -188,5 +189,4 @@ class ImagePickerActivity : FragmentActivity() {
setResult(ImagePicker.RESULT_ERROR, intent)
finish()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,4 @@ package com.github.dhaval2404.imagepicker.listener
internal interface ResultListener<T> {

fun onResult(t: T?)

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ abstract class BaseProvider(protected val activity: ImagePickerActivity) : Conte
*
* @param errorRes Error Message
*/
protected fun setError(errorRes:Int){
protected fun setError(errorRes: Int) {
setError(getString(errorRes))
}

Expand All @@ -37,23 +37,22 @@ abstract class BaseProvider(protected val activity: ImagePickerActivity) : Conte
*
* @param messageRes String message resource
*/
protected fun showToast(messageRes:Int){
protected fun showToast(messageRes: Int) {
Toast.makeText(this, messageRes, Toast.LENGTH_SHORT).show()
}

/**
* Call this method when task is cancel in between the operation.
* E.g. user hit back-press
*/
protected fun setResultCancel(){
protected fun setResultCancel() {
onFailure()
activity.setResultCancel()
}

/**
* This method will be Call on Error, It can be used for clean up Tasks
*/
protected open fun onFailure(){
protected open fun onFailure() {
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ class CameraProvider(activity: ImagePickerActivity) : BaseProvider(activity) {
* Create Temporary File object and Pass it to Camera Intent
*/
private fun startCameraIntent() {
//Create and get empty file to store capture image content
// Create and get empty file to store capture image content
mCameraFile = FileUtil.getCameraFile()

//Check if file exists
// Check if file exists
if (mCameraFile != null && mCameraFile!!.exists()) {
val cameraIntent = IntentUtils.getCameraIntent(this, mCameraFile!!)
activity.startActivityForResult(cameraIntent, CAMERA_INTENT_REQ_CODE)
Expand All @@ -82,13 +82,13 @@ class CameraProvider(activity: ImagePickerActivity) : BaseProvider(activity) {
* Handle Requested Permission Result
*/
fun onRequestPermissionsResult(requestCode: Int) {
if(requestCode==PERMISSION_INTENT_REQ_CODE) {
//Check again if permission is granted
if (requestCode == PERMISSION_INTENT_REQ_CODE) {
// Check again if permission is granted
if (isPermissionGranted(this, REQUIRED_PERMISSIONS)) {
//Permission is granted, Start Camera Intent
// Permission is granted, Start Camera Intent
startCameraIntent()
} else {
//Exit with error message
// Exit with error message
val error = getString(R.string.permission_camera_denied)
setError(error)
}
Expand All @@ -98,8 +98,8 @@ class CameraProvider(activity: ImagePickerActivity) : BaseProvider(activity) {
/**
* Handle Camera Intent Activity Result
*
* @param requestCode It must be {@link CameraProvider#CAMERA_INTENT_REQ_CODE}
* @param resultCode For success it should be {@link Activity#RESULT_OK}
* @param requestCode It must be {@link CameraProvider#CAMERA_INTENT_REQ_CODE}
* @param resultCode For success it should be {@link Activity#RESULT_OK}
* @param data Result Intent
*/
fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
Expand All @@ -122,8 +122,7 @@ class CameraProvider(activity: ImagePickerActivity) : BaseProvider(activity) {
/**
* Delete Camera file is exists
*/
override fun onFailure(){
override fun onFailure() {
mCameraFile?.delete()
}

}
Loading

0 comments on commit 3a034d6

Please sign in to comment.