Skip to content

Instantly share code, notes, and snippets.

@soulduse
Last active January 11, 2018 13:51
Show Gist options
  • Save soulduse/05e59967a0f29a6bb8c2c9ae4e43df0f to your computer and use it in GitHub Desktop.
Save soulduse/05e59967a0f29a6bb8c2c9ae4e43df0f to your computer and use it in GitHub Desktop.
Dialog Helper with Kotlin
/**
* 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