From fedf52b36e7260f8b1ddfbcf72ac9537bc60f9da Mon Sep 17 00:00:00 2001 From: Dhaval Patel Date: Mon, 13 May 2019 23:31:27 +0530 Subject: [PATCH] Code Cleanup Code Cleanup --- .../dhaval2404/imagepicker/ImagePicker.kt | 66 +++++++++---------- .../imagepicker/ImagePickerActivity.kt | 19 ++++-- imagepicker/src/main/res/values/strings.xml | 1 + .../sample/MainActivity.kt | 3 +- 4 files changed, 46 insertions(+), 43 deletions(-) diff --git a/imagepicker/src/main/kotlin/com/github/dhaval2404/imagepicker/ImagePicker.kt b/imagepicker/src/main/kotlin/com/github/dhaval2404/imagepicker/ImagePicker.kt index b5fb799b..26c8c013 100644 --- a/imagepicker/src/main/kotlin/com/github/dhaval2404/imagepicker/ImagePicker.kt +++ b/imagepicker/src/main/kotlin/com/github/dhaval2404/imagepicker/ImagePicker.kt @@ -35,7 +35,6 @@ open class ImagePicker { internal const val EXTRA_ERROR = "extra.error" internal const val EXTRA_FILE_PATH = "extra.file_path" - internal const val EXTRA_CAMERA = "extra.camera" /** * Use this to use ImagePicker in Activity Class @@ -127,20 +126,20 @@ open class ImagePicker { } /** - * Only Capture image using Camera + * Only Capture image using Camera. */ - @Deprecated("Please use provider(ImageProvider.CAMERA) instead") + //@Deprecated("Please use provider(ImageProvider.CAMERA) instead") fun cameraOnly(): Builder { - imageProvider = ImageProvider.CAMERA + this.imageProvider = ImageProvider.CAMERA return this } /** - * Only Pick image from gallery + * Only Pick image from gallery. */ - @Deprecated("Please use provider(ImageProvider.GALLERY) instead") + //@Deprecated("Please use provider(ImageProvider.GALLERY) instead") fun galleryOnly(): Builder { - imageProvider = ImageProvider.GALLERY + this.imageProvider = ImageProvider.GALLERY return this } @@ -221,8 +220,8 @@ open class ImagePicker { private fun showImageProviderDialog(reqCode: Int) { DialogHelper.showChooseAppDialog(activity, object : ResultListener { override fun onResult(t: ImageProvider?) { - if (t != null) { - imageProvider = t + t?.let { + imageProvider = it startActivity(reqCode) } } @@ -239,32 +238,39 @@ open class ImagePicker { imageProvider = t startActivity(completionHandler) } else { - completionHandler?.invoke(Activity.RESULT_CANCELED, Intent()) + val intent = ImagePickerActivity.getCancelledIntent(activity) + completionHandler?.invoke(Activity.RESULT_CANCELED, intent) } } }) } /** - * Start ImagePickerActivity with given Argument + * Get Bundle for ImagePickerActivity */ - private fun startActivity(completionHandler: ((resultCode: Int, data: Intent?) -> Unit)? = null) { + private fun getBundle(): Bundle { + val bundle = Bundle() + bundle.putSerializable(EXTRA_IMAGE_PROVIDER, imageProvider) - try { - val bundle = Bundle() - bundle.putSerializable(EXTRA_IMAGE_PROVIDER, imageProvider) - //bundle.putBoolean(EXTRA_ASK_PERMISSION, askPermission) + bundle.putFloat(EXTRA_CROP_X, cropX) + bundle.putFloat(EXTRA_CROP_Y, cropY) - bundle.putFloat(EXTRA_CROP_X, cropX) - bundle.putFloat(EXTRA_CROP_Y, cropY) + bundle.putInt(EXTRA_MAX_WIDTH, maxWidth) + bundle.putInt(EXTRA_MAX_HEIGHT, maxHeight) - bundle.putInt(EXTRA_MAX_WIDTH, maxWidth) - bundle.putInt(EXTRA_MAX_HEIGHT, maxHeight) + bundle.putLong(EXTRA_IMAGE_MAX_SIZE, maxSize) - bundle.putLong(EXTRA_IMAGE_MAX_SIZE, maxSize) + return bundle + } + /** + * Start ImagePickerActivity with given Argument + */ + private fun startActivity(completionHandler: ((resultCode: Int, data: Intent?) -> Unit)? = null) { + + try { val intent = Intent(activity, ImagePickerActivity::class.java) - intent.putExtras(bundle) + intent.putExtras(getBundle()) if (fragment != null) { fragment?.startForResult(intent) { result -> @@ -275,7 +281,7 @@ open class ImagePicker { } else { (activity as AppCompatActivity).startForResult(intent) { result -> completionHandler?.invoke(result.resultCode, result.data) - }?.onFailed { result -> + }.onFailed { result -> completionHandler?.invoke(result.resultCode, result.data) } } @@ -293,20 +299,8 @@ open class ImagePicker { * Start ImagePickerActivity with given Argument */ private fun startActivity(reqCode: Int) { - val bundle = Bundle() - bundle.putSerializable(EXTRA_IMAGE_PROVIDER, imageProvider) - //bundle.putBoolean(EXTRA_ASK_PERMISSION, askPermission) - - bundle.putFloat(EXTRA_CROP_X, cropX) - bundle.putFloat(EXTRA_CROP_Y, cropY) - - bundle.putInt(EXTRA_MAX_WIDTH, maxWidth) - bundle.putInt(EXTRA_MAX_HEIGHT, maxHeight) - - bundle.putLong(EXTRA_IMAGE_MAX_SIZE, maxSize) - val intent = Intent(activity, ImagePickerActivity::class.java) - intent.putExtras(bundle) + intent.putExtras(getBundle()) if (fragment != null) { fragment?.startActivityForResult(intent, reqCode) } else { diff --git a/imagepicker/src/main/kotlin/com/github/dhaval2404/imagepicker/ImagePickerActivity.kt b/imagepicker/src/main/kotlin/com/github/dhaval2404/imagepicker/ImagePickerActivity.kt index d2ea1340..8adf8b85 100644 --- a/imagepicker/src/main/kotlin/com/github/dhaval2404/imagepicker/ImagePickerActivity.kt +++ b/imagepicker/src/main/kotlin/com/github/dhaval2404/imagepicker/ImagePickerActivity.kt @@ -1,6 +1,7 @@ package com.github.dhaval2404.imagepicker import android.app.Activity +import android.content.Context import android.content.Intent import android.net.Uri import android.os.Bundle @@ -23,6 +24,14 @@ class ImagePickerActivity : FragmentActivity() { companion object { private const val TAG = "image_picker" + + internal fun getCancelledIntent(context: Context): Intent { + val intent = Intent() + val message = context.getString(R.string.error_task_cancelled) + intent.putExtra(ImagePicker.EXTRA_ERROR, message) + return intent + } + } private var mGalleryProvider: GalleryProvider? = null @@ -86,8 +95,7 @@ class ImagePickerActivity : FragmentActivity() { * Handle Activity Back Press */ override fun onBackPressed() { - setResultCancel(false) //needs to be set before onBackPressed or else won't work - super.onBackPressed() + setResultCancel() } /** @@ -164,10 +172,9 @@ class ImagePickerActivity : FragmentActivity() { /** * User has cancelled the task */ - fun setResultCancel(finish: Boolean = true) { - setResult(Activity.RESULT_CANCELED, Intent()) - if (finish) - finish() + fun setResultCancel() { + setResult(Activity.RESULT_CANCELED, getCancelledIntent(this)) + finish() } /** diff --git a/imagepicker/src/main/res/values/strings.xml b/imagepicker/src/main/res/values/strings.xml index b9089c2a..012c0eef 100644 --- a/imagepicker/src/main/res/values/strings.xml +++ b/imagepicker/src/main/res/values/strings.xml @@ -19,6 +19,7 @@ Failed to pick Gallery image Failed to crop image Failed to compress image + Task Cancelled diff --git a/sample/src/main/kotlin/com.github.dhaval2404.imagepicker/sample/MainActivity.kt b/sample/src/main/kotlin/com.github.dhaval2404.imagepicker/sample/MainActivity.kt index d2e350b4..998aa16c 100644 --- a/sample/src/main/kotlin/com.github.dhaval2404.imagepicker/sample/MainActivity.kt +++ b/sample/src/main/kotlin/com.github.dhaval2404.imagepicker/sample/MainActivity.kt @@ -7,6 +7,7 @@ import android.util.Log import android.widget.Toast import androidx.appcompat.app.AppCompatActivity import com.github.dhaval2404.imagepicker.ImagePicker +import com.github.dhaval2404.imagepicker.constant.ImageProvider import kotlinx.android.synthetic.main.activity_main.* import kotlinx.android.synthetic.main.content_camera_only.* import kotlinx.android.synthetic.main.content_gallery_only.* @@ -44,7 +45,7 @@ class MainActivity : AppCompatActivity() { fab_add_camera_photo.setOnClickListener { ImagePicker.with(this) - .cameraOnly() //User can only capture image using Camera(Optional) + .provider(ImageProvider.CAMERA) //Default will be ImageProvider.BOTH .compress(1024) //Final image size will be less than 1 MB(Optional) .start(CAMERA_IMAGE_REQ_CODE) }