Skip to content

Instantly share code, notes, and snippets.

@jeluchu
Forked from oakkub/DialogExtensions.kt
Created November 17, 2019 19:45
Show Gist options
  • Save jeluchu/4676111c76c3ebbaff7ea05b3954f8b3 to your computer and use it in GitHub Desktop.
Save jeluchu/4676111c76c3ebbaff7ea05b3954f8b3 to your computer and use it in GitHub Desktop.
Kotlin extension functions for creating AlertDialog in a DSL way
import android.annotation.SuppressLint
import android.app.Activity
import android.app.AlertDialog
import android.content.Context
import android.support.annotation.StringRes
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.widget.Button
import android.widget.TextView
import com.eggdigital.trueyouedc.R
/**
* Created by oakkub on 10/5/2017 AD.
*/
inline fun Activity.alert(title: CharSequence? = null, message: CharSequence? = null, func: AlertDialogHelper.() -> Unit): AlertDialog {
return AlertDialogHelper(this, title, message).apply {
func()
}.create()
}
inline fun Activity.alert(titleResource: Int = 0, messageResource: Int = 0, func: AlertDialogHelper.() -> Unit): AlertDialog {
val title = if (titleResource == 0) null else getString(titleResource)
val message = if (messageResource == 0) null else getString(messageResource)
return AlertDialogHelper(this, title, message).apply {
func()
}.create()
}
inline fun Fragment.alert(title: CharSequence? = null, message: CharSequence? = null, func: AlertDialogHelper.() -> Unit): AlertDialog {
return AlertDialogHelper(context, title, message).apply {
func()
}.create()
}
inline fun Fragment.alert(titleResource: Int = 0, messageResource: Int = 0, func: AlertDialogHelper.() -> Unit): AlertDialog {
val title = if (titleResource == 0) null else getString(titleResource)
val message = if (messageResource == 0) null else getString(messageResource)
return AlertDialogHelper(context, title, message).apply {
func()
}.create()
}
@SuppressLint("InflateParams")
class AlertDialogHelper(context: Context, title: CharSequence?, message: CharSequence?) {
private val dialogView: View by lazyFast {
LayoutInflater.from(context).inflate(R.layout.dialog_info, null)
}
private val builder: AlertDialog.Builder = AlertDialog.Builder(context)
.setView(dialogView)
private val title: TextView by lazyFast {
dialogView.findViewById<TextView>(R.id.dialogInfoTitleTextView)
}
private val message: TextView by lazyFast {
dialogView.findViewById<TextView>(R.id.dialogInfoMessageTextView)
}
private val positiveButton: Button by lazyFast {
dialogView.findViewById<Button>(R.id.dialogInfoPositiveButton)
}
private val negativeButton: Button by lazyFast {
dialogView.findViewById<Button>(R.id.dialogInfoNegativeButton)
}
private var dialog: AlertDialog? = null
var cancelable: Boolean = true
init {
this.title.text = title
this.message.text = message
}
fun positiveButton(@StringRes textResource: Int, func: (() -> Unit)? = null) {
with(positiveButton) {
text = builder.context.getString(textResource)
setClickListenerToDialogButton(func)
}
}
fun positiveButton(text: CharSequence, func: (() -> Unit)? = null) {
with(positiveButton) {
this.text = text
setClickListenerToDialogButton(func)
}
}
fun negativeButton(@StringRes textResource: Int, func: (() -> Unit)? = null) {
with(negativeButton) {
text = builder.context.getString(textResource)
setClickListenerToDialogButton(func)
}
}
fun negativeButton(text: CharSequence, func: (() -> Unit)? = null) {
with(negativeButton) {
this.text = text
setClickListenerToDialogButton(func)
}
}
fun onCancel(func: () -> Unit) {
builder.setOnCancelListener { func() }
}
fun create(): AlertDialog {
title.goneIfTextEmpty()
message.goneIfTextEmpty()
positiveButton.goneIfTextEmpty()
negativeButton.goneIfTextEmpty()
dialog = builder
.setCancelable(cancelable)
.create()
return dialog!!
}
private fun TextView.goneIfTextEmpty() {
visibility = if (text.isNullOrEmpty()) {
View.GONE
} else {
View.VISIBLE
}
}
private fun Button.setClickListenerToDialogButton(func: (() -> Unit)?) {
setOnClickListener {
func?.invoke()
dialog?.dismiss()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment