Last active
January 11, 2018 13:51
-
-
Save soulduse/05e59967a0f29a6bb8c2c9ae4e43df0f to your computer and use it in GitHub Desktop.
Dialog Helper with Kotlin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by Soulduse on 2018. 1. 11.. | |
* | |
* ex) | |
* CustomDialog(CustomDialog.Items().apply { | |
* this.context = context | |
* this.message = "message" | |
* this.positiveTitle = "ok" | |
* this.negativeTitle = "cancel" | |
* }).show({ | |
* // do positive something | |
* },{ | |
* // do negative something | |
* }) | |
*/ | |
class CustomDialog(private val items: Items) { | |
fun show(positive: ()-> Unit, negative: (()-> Unit)?= null) = AlertDialog.Builder(items.context).apply { | |
with(items){ | |
setMessage(message) | |
setPositiveButton(positiveTitle, { _, _ -> | |
positive() | |
}) | |
negative?.let{ | |
setNegativeButton(negativeTitle, { dialog, _ -> | |
dialog.dismiss() | |
negative() | |
}) | |
} | |
} | |
}.create().show() | |
class Items { | |
lateinit var context: Context | |
lateinit var message: String | |
lateinit var positiveTitle: String | |
lateinit var negativeTitle: String | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment