Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- abstract class CoroutineDialog<T>(context : Context, private val channel: Channel<DialogMessage<T>>) : AlertDialog(context) {
- init {
- setOnCancelListener {
- channel.offer(DialogMessage.Cancelled)
- }
- }
- fun positiveChannel(text : String, block : (() -> T)? = null) {
- setButton(AlertDialog.BUTTON_POSITIVE, text){d, _ ->
- channel.offer(DialogMessage.Positive(block?.invoke() ?: Unit as T))
- d.dismiss()
- }
- }
- fun negativeChannel(text : String) {
- setButton(AlertDialog.BUTTON_NEGATIVE, text){d, _ ->
- channel.offer(DialogMessage.Negative)
- d.dismiss()
- }
- }
- suspend fun showAndReceive() : DialogMessage<T>{
- show()
- return channel.receive()
- }
- sealed class DialogMessage<out T> {
- class Positive<T>(val value : T) : DialogMessage<T>()
- object Negative : DialogMessage<Nothing>()
- object Cancelled : DialogMessage<Nothing>()
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement