项目作者: xeinebiu

项目描述 :
A helper library for Android to display Dialogs by suspending the coroutine till finish of the dialog.
高级语言: Kotlin
项目地址: git://github.com/xeinebiu/android-suspend-dialogs.git
创建时间: 2021-07-22T15:10:55Z
项目社区:https://github.com/xeinebiu/android-suspend-dialogs

开源协议:MIT License

下载


Android Suspendable Dialogs

A helper library for Android to display Dialogs by suspending the coroutine till finish of the dialog.

Installation

  1. allprojects {
  2. repositories {
  3. ...
  4. maven { url 'https://www.jitpack.io' }
  5. }
  6. }
  1. dependencies {
  2. implementation 'com.github.xeinebiu:android-suspend-dialogs:1.6.2'
  3. }

Some Differences with and without suspend

Confirm

The below example shows how a normal dialog is shown without any suspension

  1. MaterialAlertDialogBuilder(this@MainActivity)
  2. .setTitle("Title")
  3. .setMessage("Message")
  4. .setPositiveButton("Positive") { _, _ ->
  5. // code to execute
  6. }
  7. .setNegativeButton("Negative") { _, _ ->
  8. // code to execute
  9. }
  10. .setNeutralButton("Neutral") { _, _ ->
  11. // code to execute
  12. }
  13. .show()
  14. // this code is executed before the dialog is finished
  15. println("Hello World")

Now, using the suspend dialog, we can wait for the dialog to be finish after continue with rest of the code flow

  1. val result = SuspendAlertDialog.confirm(
  2. positiveButtonText = "Positive",
  3. negativeButtonText = "Negative",
  4. neutralButtonText = "Neutral"
  5. ) {
  6. MaterialAlertDialogBuilder(this@MainActivity)
  7. .setTitle("Title")
  8. .setMessage("Message")
  9. }
  10. // this line is executed after the dialog above is finish
  11. tvResult.text = result.toString()

If you are fan of extension functions, the below approach can be used to achieve the same behavior as above.

  1. val result = MaterialAlertDialogBuilder(this@MainActivity)
  2. .setTitle("Title")
  3. .setMessage("Message")
  4. .confirm(
  5. positiveButtonText = "Save",
  6. negativeButtonText = "Cancel",
  7. neutralButtonText = "Neutral",
  8. )
  9. tvResult.text = result.toString()

Alert

  1. SuspendAlertDialog.alert("Ok") {
  2. MaterialAlertDialogBuilder(this@MainActivity).setTitle("Selected Option")
  3. }
  4. tvResult.text = getString(R.string.alert_finished)

Using extension function

  1. MaterialAlertDialogBuilder(this@MainActivity)
  2. .setTitle("Selected Option")
  3. .alert("Ok")
  4. tvResult.text = getString(R.string.alert_finished)

Multi Choice Items

  1. val multiChoiceResult = SuspendAlertDialog.setMultiChoiceItems(
  2. positiveButtonText = "Save",
  3. negativeButtonText = "Cancel",
  4. neutralButtonText = "Minimize",
  5. items = SuspendAlertDialog.MultiChoiceItems(
  6. items = listOf("Hello", "World", "Berlin", "Germany"),
  7. checked = listOf(false, false, false, false)
  8. )
  9. ) {
  10. MaterialAlertDialogBuilder(this@MainActivity).setTitle("Title")
  11. }
  12. tvResult.text = multiChoiceResult.toString()

Using extension function

  1. val result = MaterialAlertDialogBuilder(this@MainActivity)
  2. .setTitle("Title")
  3. .setMultiChoiceItems(
  4. positiveButtonText = "Save",
  5. negativeButtonText = "Cancel",
  6. neutralButtonText = "Minimize",
  7. items = SuspendAlertDialog.MultiChoiceItems(
  8. items = listOf("Hello", "World", "Berlin", "Germany"),
  9. checked = listOf(false, false, false, false)
  10. )
  11. )
  12. tvResult.text = result.toString()

Single Choice Items

  1. val singleChoiceResult = SuspendAlertDialog.setSingleChoiceItems(
  2. positiveButtonText = "Save",
  3. negativeButtonText = "Cancel",
  4. neutralButtonText = "Minimize",
  5. items = SuspendAlertDialog.SingleChoiceItems(
  6. items = listOf("Hello", "World", "Berlin", "Germany"),
  7. selectedIndex = 1
  8. )
  9. ) {
  10. MaterialAlertDialogBuilder(this@MainActivity).setTitle("Title")
  11. }
  12. tvResult.text = singleChoiceResult.toString()

Using extension function

  1. val result = MaterialAlertDialogBuilder(this@MainActivity)
  2. .setTitle("Title")
  3. .setSingleChoiceItems(
  4. positiveButtonText = "Save",
  5. negativeButtonText = "Cancel",
  6. neutralButtonText = "Minimize",
  7. items = SuspendAlertDialog.SingleChoiceItems(
  8. items = listOf("Hello", "World", "Berlin", "Germany"),
  9. selectedIndex = 1
  10. )
  11. )
  12. tvResult.text = result.toString()

Custom Dialogs (DialogFragment & BottomSheetDialogFragment)

While above we read how to suspend calls to some dialogs, here we will discuss about suspend of calls to the DialogFragment and BottomSheetDialogFragment.

showAwait

Show await suspends the call till the dialog is destroyed. Returns an Unit.

  1. DemoDialogFragment().showAwait(
  2. fragmentManager = supportFragmentManager,
  3. tag = DemoDialogFragment::class.java.canonicalName
  4. )
  5. tvResult.text = "${DemoDialogFragment::class.java.canonicalName} finished"

showAwaitResult

Dialogs that must return results, an implementation of SuspendDialogResult interface is a must. SuspendDialogResult provides a member called result which one is returned
from the dialog after destroy. Make sure to assign a value to result before calling dismiss.

For more details, check the example app.

  1. val result = DemoResultDialogFragment().showAwaitResult(
  2. fragmentManager = supportFragmentManager,
  3. tag = DemoResultDialogFragment::class.java.canonicalName
  4. )
  5. tvResult.text = result