diff --git a/README.md b/README.md index 13af77ba..196579d6 100644 --- a/README.md +++ b/README.md @@ -214,6 +214,16 @@ Almost 90% of the app that I have developed has an Image upload feature. Along w } .start() ``` + * Intercept Dialog dismiss event + + ```kotlin + ImagePicker.with(this) + .setDismissListener { + // Handle dismiss event + Log.d("ImagePicker", "onDismiss"); + } + .start() + ``` * Specify Directory to store captured, cropped or compressed images 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 c1e03e6f..3cf4c597 100644 --- a/imagepicker/src/main/kotlin/com/github/dhaval2404/imagepicker/ImagePicker.kt +++ b/imagepicker/src/main/kotlin/com/github/dhaval2404/imagepicker/ImagePicker.kt @@ -7,6 +7,7 @@ import android.widget.Toast import androidx.appcompat.app.AppCompatActivity import androidx.fragment.app.Fragment import com.github.dhaval2404.imagepicker.constant.ImageProvider +import com.github.dhaval2404.imagepicker.listener.DismissListener import com.github.dhaval2404.imagepicker.listener.ResultListener import com.github.dhaval2404.imagepicker.util.DialogHelper import com.github.florent37.inlineactivityresult.kotlin.startForResult @@ -27,6 +28,8 @@ open class ImagePicker { const val RESULT_ERROR = 64 internal const val EXTRA_IMAGE_PROVIDER = "extra.image_provider" + internal const val EXTRA_CAMERA_DEVICE = "extra.camera_device" + internal const val EXTRA_IMAGE_MAX_SIZE = "extra.image_max_size" internal const val EXTRA_CROP = "extra.crop" internal const val EXTRA_CROP_X = "extra.crop_x" @@ -118,6 +121,11 @@ open class ImagePicker { private var imageProviderInterceptor: ((ImageProvider) -> Unit)? = null + /** + * Dialog dismiss event listener + */ + private var dismissListener: DismissListener? = null + /** * File Directory * @@ -249,6 +257,26 @@ open class ImagePicker { return this } + /** + * Sets the callback that will be called when the dialog is dismissed for any reason. + */ + fun setDismissListener(listener: DismissListener): Builder { + this.dismissListener = listener + return this + } + + /** + * Sets the callback that will be called when the dialog is dismissed for any reason. + */ + fun setDismissListener(listener: (() -> Unit)): Builder { + this.dismissListener = object : DismissListener { + override fun onDismiss() { + listener.invoke() + } + } + return this + } + /** * Start Image Picker Activity */ @@ -292,7 +320,7 @@ open class ImagePicker { startActivity(reqCode) } } - }) + }, dismissListener) } /** @@ -310,7 +338,7 @@ open class ImagePicker { completionHandler?.invoke(Activity.RESULT_CANCELED, intent) } } - }) + }, dismissListener) } /** diff --git a/imagepicker/src/main/kotlin/com/github/dhaval2404/imagepicker/listener/DismissListener.kt b/imagepicker/src/main/kotlin/com/github/dhaval2404/imagepicker/listener/DismissListener.kt new file mode 100644 index 00000000..981240bb --- /dev/null +++ b/imagepicker/src/main/kotlin/com/github/dhaval2404/imagepicker/listener/DismissListener.kt @@ -0,0 +1,18 @@ +package com.github.dhaval2404.imagepicker.listener + +/** + * Interface used to allow the creator of a dialog to run some code when the + * dialog is dismissed. + * + * @author Dhaval Patel + * @version 1.8 + * @since 19 December 2020 + */ +interface DismissListener { + + /** + * This method will be invoked when the dialog is dismissed. + */ + fun onDismiss() + +} diff --git a/imagepicker/src/main/kotlin/com/github/dhaval2404/imagepicker/util/DialogHelper.kt b/imagepicker/src/main/kotlin/com/github/dhaval2404/imagepicker/util/DialogHelper.kt index ad116543..8375e261 100644 --- a/imagepicker/src/main/kotlin/com/github/dhaval2404/imagepicker/util/DialogHelper.kt +++ b/imagepicker/src/main/kotlin/com/github/dhaval2404/imagepicker/util/DialogHelper.kt @@ -5,6 +5,7 @@ import android.view.LayoutInflater import androidx.appcompat.app.AlertDialog import com.github.dhaval2404.imagepicker.R import com.github.dhaval2404.imagepicker.constant.ImageProvider +import com.github.dhaval2404.imagepicker.listener.DismissListener import com.github.dhaval2404.imagepicker.listener.ResultListener import kotlinx.android.synthetic.main.dialog_choose_app.view.* @@ -21,7 +22,11 @@ internal object DialogHelper { * Show Image Provide Picker Dialog. This will streamline the code to pick/capture image * */ - fun showChooseAppDialog(context: Context, listener: ResultListener) { + fun showChooseAppDialog( + context: Context, + listener: ResultListener, + dismissListener: DismissListener? + ) { val layoutInflater = LayoutInflater.from(context) val customView = layoutInflater.inflate(R.layout.dialog_choose_app, null) @@ -34,6 +39,9 @@ internal object DialogHelper { .setNegativeButton(R.string.action_cancel) { _, _ -> listener.onResult(null) } + .setOnDismissListener { + dismissListener?.onDismiss() + } .show() // Handle Camera option click