-
-
Save iosandroiddev/c4c83102a3ad035b096c43e2d3fabf00 to your computer and use it in GitHub Desktop.
Kotlin extension functions for creating AlertDialog in a DSL way
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
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