Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. abstract class CoroutineDialog<T>(context : Context, private val channel: Channel<DialogMessage<T>>) : AlertDialog(context) {
  2.  
  3. init {
  4. setOnCancelListener {
  5. channel.offer(DialogMessage.Cancelled)
  6. }
  7. }
  8.  
  9. fun positiveChannel(text : String, block : (() -> T)? = null) {
  10. setButton(AlertDialog.BUTTON_POSITIVE, text){d, _ ->
  11. channel.offer(DialogMessage.Positive(block?.invoke() ?: Unit as T))
  12. d.dismiss()
  13. }
  14. }
  15.  
  16. fun negativeChannel(text : String) {
  17. setButton(AlertDialog.BUTTON_NEGATIVE, text){d, _ ->
  18. channel.offer(DialogMessage.Negative)
  19. d.dismiss()
  20. }
  21. }
  22.  
  23. suspend fun showAndReceive() : DialogMessage<T>{
  24. show()
  25. return channel.receive()
  26. }
  27.  
  28. sealed class DialogMessage<out T> {
  29. class Positive<T>(val value : T) : DialogMessage<T>()
  30. object Negative : DialogMessage<Nothing>()
  31. object Cancelled : DialogMessage<Nothing>()
  32. }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement