Improve dialog extension

This commit is contained in:
Rahul Kumar Patel
2021-03-01 22:14:21 +05:30
parent 90113ab338
commit 6a1e0ec875
2 changed files with 31 additions and 38 deletions

View File

@@ -27,40 +27,39 @@ import androidx.fragment.app.Fragment
import com.google.android.material.dialog.MaterialAlertDialogBuilder
fun Context.showDialog(@StringRes titleId: Int, @StringRes messageId: Int) {
runOnUiThread {
val backgroundColor: Int = getStyledAttributeColor(android.R.attr.colorBackground)
val builder = MaterialAlertDialogBuilder(this).apply {
setTitle(titleId)
setMessage(messageId)
setPositiveButton(android.R.string.ok) { dialog: DialogInterface, _ -> dialog.dismiss() }
background = ColorDrawable(backgroundColor)
}.create()
builder.show()
}
showDialog(getString(titleId), getString(messageId), null, null)
}
fun Context.showDialog(title: String?, message: String?) {
runOnUiThread {
val backgroundColor: Int = getStyledAttributeColor(android.R.attr.colorBackground)
val builder = MaterialAlertDialogBuilder(this).apply {
setTitle(title)
setMessage(message)
setPositiveButton(android.R.string.ok) { dialog: DialogInterface, _ -> dialog.dismiss() }
background = ColorDrawable(backgroundColor)
}.create()
builder.show()
}
showDialog(title, message, null, null)
}
fun Context.showDialog(title: String?, message: String?, listener: DialogInterface.OnDismissListener) {
fun Context.showDialog(
title: String?,
message: String?,
positiveListener: DialogInterface.OnClickListener?,
negativeListener: DialogInterface.OnClickListener?
) {
runOnUiThread {
val backgroundColor: Int = getStyledAttributeColor(android.R.attr.colorBackground)
val builder = MaterialAlertDialogBuilder(this).apply {
setTitle(title)
setMessage(message)
setPositiveButton(android.R.string.ok) { dialog: DialogInterface, _ -> listener }
if (positiveListener != null) {
setPositiveButton(android.R.string.ok, positiveListener)
} else {
setPositiveButton(android.R.string.ok) { dialog, _ -> dialog.dismiss() }
}
positiveListener?.let {
}
negativeListener?.let {
setNegativeButton(android.R.string.cancel, negativeListener)
}
background = ColorDrawable(backgroundColor)
}.create()