Skip to content

Commit

Permalink
Added dialog dissmiss event listener
Browse files Browse the repository at this point in the history
Added helper method to allow the creator of a dialog to run some code when the dialog is dismissed.

#141
  • Loading branch information
Dhaval2404 committed Dec 19, 2020
1 parent eac8256 commit 1de8b03
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 3 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand Down Expand Up @@ -118,6 +121,11 @@ open class ImagePicker {

private var imageProviderInterceptor: ((ImageProvider) -> Unit)? = null

/**
* Dialog dismiss event listener
*/
private var dismissListener: DismissListener? = null

/**
* File Directory
*
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -292,7 +320,7 @@ open class ImagePicker {
startActivity(reqCode)
}
}
})
}, dismissListener)
}

/**
Expand All @@ -310,7 +338,7 @@ open class ImagePicker {
completionHandler?.invoke(Activity.RESULT_CANCELED, intent)
}
}
})
}, dismissListener)
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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()

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

Expand All @@ -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<ImageProvider>) {
fun showChooseAppDialog(
context: Context,
listener: ResultListener<ImageProvider>,
dismissListener: DismissListener?
) {
val layoutInflater = LayoutInflater.from(context)
val customView = layoutInflater.inflate(R.layout.dialog_choose_app, null)

Expand All @@ -34,6 +39,9 @@ internal object DialogHelper {
.setNegativeButton(R.string.action_cancel) { _, _ ->
listener.onResult(null)
}
.setOnDismissListener {
dismissListener?.onDismiss()
}
.show()

// Handle Camera option click
Expand Down

0 comments on commit 1de8b03

Please # to comment.