From 3354bb7da400c4c4a226182e7b85516bd27f7c5f Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Wed, 27 May 2020 22:57:12 +0200 Subject: [PATCH] OKDialogs ok button guard --- .../androidaps/utils/alertDialogs/OKDialog.kt | 138 ++++++++++++------ .../utils/alertDialogs/WarningDialog.kt | 36 +++-- 2 files changed, 112 insertions(+), 62 deletions(-) diff --git a/core/src/main/java/info/nightscout/androidaps/utils/alertDialogs/OKDialog.kt b/core/src/main/java/info/nightscout/androidaps/utils/alertDialogs/OKDialog.kt index 2d45d2910d..c0fc646258 100644 --- a/core/src/main/java/info/nightscout/androidaps/utils/alertDialogs/OKDialog.kt +++ b/core/src/main/java/info/nightscout/androidaps/utils/alertDialogs/OKDialog.kt @@ -12,6 +12,7 @@ import info.nightscout.androidaps.utils.extensions.runOnUiThread object OKDialog { @SuppressLint("InflateParams") fun show(context: Context, title: String, message: String, runnable: Runnable? = null) { + var okClicked = false var notEmptytitle = title if (notEmptytitle.isEmpty()) notEmptytitle = context.getString(R.string.message) @@ -19,18 +20,21 @@ object OKDialog { .setCustomTitle(AlertDialogHelper.buildCustomTitle(context, notEmptytitle)) .setMessage(message) .setPositiveButton(context.getString(R.string.ok)) { dialog: DialogInterface, _: Int -> - dialog.dismiss() - SystemClock.sleep(100) - runOnUiThread(runnable) + if (okClicked) return@setPositiveButton + else { + okClicked = true + dialog.dismiss() + SystemClock.sleep(100) + runOnUiThread(runnable) + } } .show() .setCanceledOnTouchOutside(false) } @SuppressLint("InflateParams") - @JvmStatic - @JvmOverloads fun show(activity: Activity, title: String, message: Spanned, runnable: Runnable? = null) { + var okClicked = false var notEmptytitle = title if (notEmptytitle.isEmpty()) notEmptytitle = activity.getString(R.string.message) @@ -38,9 +42,13 @@ object OKDialog { .setCustomTitle(AlertDialogHelper.buildCustomTitle(activity, notEmptytitle)) .setMessage(message) .setPositiveButton(activity.getString(R.string.ok)) { dialog: DialogInterface, _: Int -> - dialog.dismiss() - SystemClock.sleep(100) - runnable?.let { activity.runOnUiThread(it) } + if (okClicked) return@setPositiveButton + else { + okClicked = true + dialog.dismiss() + SystemClock.sleep(100) + runnable?.let { activity.runOnUiThread(it) } + } } .show() .setCanceledOnTouchOutside(false) @@ -57,71 +65,89 @@ object OKDialog { } @SuppressLint("InflateParams") - @JvmStatic - @JvmOverloads fun showConfirmation(activity: Activity, title: String, message: Spanned, ok: Runnable?, cancel: Runnable? = null) { + var okClicked = false AlertDialogHelper.Builder(activity) .setMessage(message) .setCustomTitle(AlertDialogHelper.buildCustomTitle(activity, title)) .setPositiveButton(android.R.string.ok) { dialog: DialogInterface, _: Int -> - dialog.dismiss() - SystemClock.sleep(100) - ok?.let { activity.runOnUiThread(it) } + if (okClicked) return@setPositiveButton + else { + okClicked = true + dialog.dismiss() + SystemClock.sleep(100) + ok?.let { activity.runOnUiThread(it) } + } } .setNegativeButton(android.R.string.cancel) { dialog: DialogInterface, _: Int -> - dialog.dismiss() - SystemClock.sleep(100) - cancel?.let { activity.runOnUiThread(it) } + if (okClicked) return@setNegativeButton + else { + okClicked = true + dialog.dismiss() + SystemClock.sleep(100) + cancel?.let { activity.runOnUiThread(it) } + } } - .setNegativeButton(android.R.string.cancel, null) .show() .setCanceledOnTouchOutside(false) } @SuppressLint("InflateParams") - @JvmStatic fun showConfirmation(activity: Activity, title: String, message: String, ok: Runnable?, cancel: Runnable? = null) { + var okClicked = false AlertDialogHelper.Builder(activity) .setMessage(message) .setCustomTitle(AlertDialogHelper.buildCustomTitle(activity, title)) .setPositiveButton(android.R.string.ok) { dialog: DialogInterface, _: Int -> - dialog.dismiss() - SystemClock.sleep(100) - ok?.let { activity.runOnUiThread(it) } + if (okClicked) return@setPositiveButton + else { + okClicked = true + dialog.dismiss() + SystemClock.sleep(100) + ok?.let { activity.runOnUiThread(it) } + } } .setNegativeButton(android.R.string.cancel) { dialog: DialogInterface, _: Int -> - dialog.dismiss() - SystemClock.sleep(100) - cancel?.let { activity.runOnUiThread(it) } + if (okClicked) return@setNegativeButton + else { + okClicked = true + dialog.dismiss() + SystemClock.sleep(100) + cancel?.let { activity.runOnUiThread(it) } + } } .show() .setCanceledOnTouchOutside(false) } - @JvmStatic - @JvmOverloads fun showConfirmation(context: Context, message: Spanned, ok: Runnable?, cancel: Runnable? = null) { showConfirmation(context, context.getString(R.string.confirmation), message, ok, cancel) } @SuppressLint("InflateParams") - @JvmStatic - @JvmOverloads fun showConfirmation(context: Context, title: String, message: Spanned, ok: Runnable?, cancel: Runnable? = null) { + var okClicked = false AlertDialogHelper.Builder(context) .setMessage(message) .setCustomTitle(AlertDialogHelper.buildCustomTitle(context, title)) .setPositiveButton(android.R.string.ok) { dialog: DialogInterface, _: Int -> - dialog.dismiss() - SystemClock.sleep(100) - runOnUiThread(ok) + if (okClicked) return@setPositiveButton + else { + okClicked = true + dialog.dismiss() + SystemClock.sleep(100) + runOnUiThread(ok) + } } .setNegativeButton(android.R.string.cancel) { dialog: DialogInterface, _: Int -> - dialog.dismiss() - SystemClock.sleep(100) - runOnUiThread(cancel) + if (okClicked) return@setNegativeButton + else { + okClicked = true + dialog.dismiss() + SystemClock.sleep(100) + runOnUiThread(cancel) + } } - .setNegativeButton(android.R.string.cancel, null) .show() .setCanceledOnTouchOutside(false) } @@ -136,18 +162,27 @@ object OKDialog { @JvmStatic @JvmOverloads fun showConfirmation(context: Context, title: String, message: String, ok: Runnable?, cancel: Runnable? = null) { + var okClicked = false AlertDialogHelper.Builder(context) .setMessage(message) .setCustomTitle(AlertDialogHelper.buildCustomTitle(context, title)) .setPositiveButton(android.R.string.ok) { dialog: DialogInterface, _: Int -> - dialog.dismiss() - SystemClock.sleep(100) - runOnUiThread(ok) + if (okClicked) return@setPositiveButton + else { + okClicked = true + dialog.dismiss() + SystemClock.sleep(100) + runOnUiThread(ok) + } } .setNegativeButton(android.R.string.cancel) { dialog: DialogInterface, _: Int -> - dialog.dismiss() - SystemClock.sleep(100) - runOnUiThread(cancel) + if (okClicked) return@setNegativeButton + else { + okClicked = true + dialog.dismiss() + SystemClock.sleep(100) + runOnUiThread(cancel) + } } .show() .setCanceledOnTouchOutside(false) @@ -157,18 +192,27 @@ object OKDialog { @JvmStatic @JvmOverloads fun showConfirmation(context: Context, title: String, message: String, ok: DialogInterface.OnClickListener?, cancel: DialogInterface.OnClickListener? = null) { + var okClicked = false AlertDialogHelper.Builder(context) .setMessage(message) .setCustomTitle(AlertDialogHelper.buildCustomTitle(context, title)) .setPositiveButton(android.R.string.ok) { dialog: DialogInterface, which: Int -> - dialog.dismiss() - SystemClock.sleep(100) - ok?.onClick(dialog, which) + if (okClicked) return@setPositiveButton + else { + okClicked = true + dialog.dismiss() + SystemClock.sleep(100) + ok?.onClick(dialog, which) + } } .setNegativeButton(android.R.string.cancel) { dialog: DialogInterface, which: Int -> - dialog.dismiss() - SystemClock.sleep(100) - cancel?.onClick(dialog, which) + if (okClicked) return@setNegativeButton + else { + okClicked = true + dialog.dismiss() + SystemClock.sleep(100) + cancel?.onClick(dialog, which) + } } .show() .setCanceledOnTouchOutside(false) diff --git a/core/src/main/java/info/nightscout/androidaps/utils/alertDialogs/WarningDialog.kt b/core/src/main/java/info/nightscout/androidaps/utils/alertDialogs/WarningDialog.kt index 847287d57a..2894f1b8d4 100644 --- a/core/src/main/java/info/nightscout/androidaps/utils/alertDialogs/WarningDialog.kt +++ b/core/src/main/java/info/nightscout/androidaps/utils/alertDialogs/WarningDialog.kt @@ -13,31 +13,37 @@ import info.nightscout.androidaps.utils.extensions.runOnUiThread object WarningDialog { @SuppressLint("InflateParams") - @JvmStatic - @JvmOverloads fun showWarning(context: Context, title: String, message: String, @StringRes positiveButton: Int = -1, ok: (() -> Unit)? = null, cancel: (() -> Unit)? = null) { - + var okClicked = false val builder = AlertDialogHelper.Builder(context, R.style.AppThemeWarningDialog) .setMessage(message) .setCustomTitle(AlertDialogHelper.buildCustomTitle(context, title, R.drawable.ic_header_warning, R.style.AppThemeWarningDialog)) .setNegativeButton(R.string.dismiss) { dialog: DialogInterface, _: Int -> - dialog.dismiss() - SystemClock.sleep(100) - if (cancel != null) { - runOnUiThread(Runnable { - cancel() - }) + if (okClicked) return@setNegativeButton + else { + okClicked = true + dialog.dismiss() + SystemClock.sleep(100) + if (cancel != null) { + runOnUiThread(Runnable { + cancel() + }) + } } } if (positiveButton != -1) { builder.setPositiveButton(positiveButton) { dialog: DialogInterface, _: Int -> - dialog.dismiss() - SystemClock.sleep(100) - if (ok != null) { - runOnUiThread(Runnable { - ok() - }) + if (okClicked) return@setPositiveButton + else { + okClicked = true + dialog.dismiss() + SystemClock.sleep(100) + if (ok != null) { + runOnUiThread(Runnable { + ok() + }) + } } } }