From bfd202cf580331abf125ed8989a41a1e5902c271 Mon Sep 17 00:00:00 2001 From: williamrai Date: Thu, 17 Oct 2024 13:58:48 -0400 Subject: [PATCH 1/2] - adds a SimpleAlertDialog to simplify the creation and display of alert dialog --- .../org/wikipedia/views/SimpleAlertDialog.kt | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 app/src/main/java/org/wikipedia/views/SimpleAlertDialog.kt diff --git a/app/src/main/java/org/wikipedia/views/SimpleAlertDialog.kt b/app/src/main/java/org/wikipedia/views/SimpleAlertDialog.kt new file mode 100644 index 00000000000..1da77cb6919 --- /dev/null +++ b/app/src/main/java/org/wikipedia/views/SimpleAlertDialog.kt @@ -0,0 +1,69 @@ +package org.wikipedia.views + +import android.content.Context +import androidx.annotation.DrawableRes +import androidx.annotation.StringRes +import androidx.annotation.StyleRes +import androidx.appcompat.app.AppCompatActivity +import androidx.lifecycle.lifecycleScope +import com.google.android.material.dialog.MaterialAlertDialogBuilder +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.delay +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext + +class SimpleAlertDialog(private val context: Context) { + @StringRes + var title: Int? = null + @StringRes + var message: Int? = null + @DrawableRes + var icon: Int? = null + @StyleRes + var themeResId: Int = com.google.android.material.R.style.ThemeOverlay_Material3_Dialog_Alert + var delayMillis: Long = 0 + var isCancellable: Boolean = false + + private var positiveButton: Pair Unit>? = null + private var negativeButton: Pair Unit>? = null + private var neutralButton: Pair Unit>? = null + + fun positiveButton(text: String, action: () -> Unit) { + this.positiveButton = text to action + } + + fun negativeButton(text: String, action: () -> Unit) { + this.negativeButton = text to action + } + + fun neutralButton(text: String, action: () -> Unit) { + this.neutralButton = text to action + } + + internal fun build() = MaterialAlertDialogBuilder(context, themeResId).apply { + title?.let { setTitle(it) } + message?.let { setMessage(it) } + icon?.let { setIcon(it) } + positiveButton?.let { (text, action) -> + setPositiveButton(text) { _, _ -> action() } + } + negativeButton?.let { (text, action) -> + setNegativeButton(text) { _, _ -> action() } + } + neutralButton?.let { (text, action) -> + setNeutralButton(text) { _, _ -> action() } + } + setCancelable(isCancellable) + }.create() +} + +fun AppCompatActivity.showSimpleAlertDialog(block: SimpleAlertDialog.() -> Unit) { + val builder = SimpleAlertDialog(this) + builder.block() + lifecycleScope.launch { + delay(builder.delayMillis) + withContext(Dispatchers.Main) { + builder.build().show() + } + } +} \ No newline at end of file From 37f64288989d28332c14984b9fbafa346b2196f7 Mon Sep 17 00:00:00 2001 From: williamrai Date: Thu, 17 Oct 2024 14:28:56 -0400 Subject: [PATCH 2/2] - fix ci issue --- app/src/main/java/org/wikipedia/views/SimpleAlertDialog.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/org/wikipedia/views/SimpleAlertDialog.kt b/app/src/main/java/org/wikipedia/views/SimpleAlertDialog.kt index 1da77cb6919..3cadef07fb6 100644 --- a/app/src/main/java/org/wikipedia/views/SimpleAlertDialog.kt +++ b/app/src/main/java/org/wikipedia/views/SimpleAlertDialog.kt @@ -66,4 +66,4 @@ fun AppCompatActivity.showSimpleAlertDialog(block: SimpleAlertDialog.() -> Unit) builder.build().show() } } -} \ No newline at end of file +}