A helper library for Android to display Dialogs by suspending the coroutine till finish of the dialog.
allprojects {
repositories {
...
maven { url 'https://www.jitpack.io' }
}
}
dependencies {
implementation 'com.github.xeinebiu:android-suspend-dialogs:1.6.2'
}
The below example shows how a normal dialog is shown without any suspension
MaterialAlertDialogBuilder(this@MainActivity)
.setTitle("Title")
.setMessage("Message")
.setPositiveButton("Positive") { _, _ ->
// code to execute
}
.setNegativeButton("Negative") { _, _ ->
// code to execute
}
.setNeutralButton("Neutral") { _, _ ->
// code to execute
}
.show()
// this code is executed before the dialog is finished
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
val result = SuspendAlertDialog.confirm(
positiveButtonText = "Positive",
negativeButtonText = "Negative",
neutralButtonText = "Neutral"
) {
MaterialAlertDialogBuilder(this@MainActivity)
.setTitle("Title")
.setMessage("Message")
}
// this line is executed after the dialog above is finish
tvResult.text = result.toString()
If you are fan of extension functions, the below approach can be used to achieve the same behavior as above.
val result = MaterialAlertDialogBuilder(this@MainActivity)
.setTitle("Title")
.setMessage("Message")
.confirm(
positiveButtonText = "Save",
negativeButtonText = "Cancel",
neutralButtonText = "Neutral",
)
tvResult.text = result.toString()
SuspendAlertDialog.alert("Ok") {
MaterialAlertDialogBuilder(this@MainActivity).setTitle("Selected Option")
}
tvResult.text = getString(R.string.alert_finished)
Using extension function
MaterialAlertDialogBuilder(this@MainActivity)
.setTitle("Selected Option")
.alert("Ok")
tvResult.text = getString(R.string.alert_finished)
val multiChoiceResult = SuspendAlertDialog.setMultiChoiceItems(
positiveButtonText = "Save",
negativeButtonText = "Cancel",
neutralButtonText = "Minimize",
items = SuspendAlertDialog.MultiChoiceItems(
items = listOf("Hello", "World", "Berlin", "Germany"),
checked = listOf(false, false, false, false)
)
) {
MaterialAlertDialogBuilder(this@MainActivity).setTitle("Title")
}
tvResult.text = multiChoiceResult.toString()
Using extension function
val result = MaterialAlertDialogBuilder(this@MainActivity)
.setTitle("Title")
.setMultiChoiceItems(
positiveButtonText = "Save",
negativeButtonText = "Cancel",
neutralButtonText = "Minimize",
items = SuspendAlertDialog.MultiChoiceItems(
items = listOf("Hello", "World", "Berlin", "Germany"),
checked = listOf(false, false, false, false)
)
)
tvResult.text = result.toString()
val singleChoiceResult = SuspendAlertDialog.setSingleChoiceItems(
positiveButtonText = "Save",
negativeButtonText = "Cancel",
neutralButtonText = "Minimize",
items = SuspendAlertDialog.SingleChoiceItems(
items = listOf("Hello", "World", "Berlin", "Germany"),
selectedIndex = 1
)
) {
MaterialAlertDialogBuilder(this@MainActivity).setTitle("Title")
}
tvResult.text = singleChoiceResult.toString()
Using extension function
val result = MaterialAlertDialogBuilder(this@MainActivity)
.setTitle("Title")
.setSingleChoiceItems(
positiveButtonText = "Save",
negativeButtonText = "Cancel",
neutralButtonText = "Minimize",
items = SuspendAlertDialog.SingleChoiceItems(
items = listOf("Hello", "World", "Berlin", "Germany"),
selectedIndex = 1
)
)
tvResult.text = result.toString()
While above we read how to suspend calls to some dialogs, here we will discuss about suspend of calls to the DialogFragment and BottomSheetDialogFragment.
Show await suspends the call till the dialog is destroyed. Returns an Unit.
DemoDialogFragment().showAwait(
fragmentManager = supportFragmentManager,
tag = DemoDialogFragment::class.java.canonicalName
)
tvResult.text = "${DemoDialogFragment::class.java.canonicalName} finished"
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.
val result = DemoResultDialogFragment().showAwaitResult(
fragmentManager = supportFragmentManager,
tag = DemoResultDialogFragment::class.java.canonicalName
)
tvResult.text = result