OKDialogs ok button guard

This commit is contained in:
Milos Kozak 2020-05-27 22:57:12 +02:00
parent 6f7932f1c7
commit 3354bb7da4
2 changed files with 112 additions and 62 deletions

View file

@ -12,6 +12,7 @@ import info.nightscout.androidaps.utils.extensions.runOnUiThread
object OKDialog { object OKDialog {
@SuppressLint("InflateParams") @SuppressLint("InflateParams")
fun show(context: Context, title: String, message: String, runnable: Runnable? = null) { fun show(context: Context, title: String, message: String, runnable: Runnable? = null) {
var okClicked = false
var notEmptytitle = title var notEmptytitle = title
if (notEmptytitle.isEmpty()) notEmptytitle = context.getString(R.string.message) if (notEmptytitle.isEmpty()) notEmptytitle = context.getString(R.string.message)
@ -19,18 +20,21 @@ object OKDialog {
.setCustomTitle(AlertDialogHelper.buildCustomTitle(context, notEmptytitle)) .setCustomTitle(AlertDialogHelper.buildCustomTitle(context, notEmptytitle))
.setMessage(message) .setMessage(message)
.setPositiveButton(context.getString(R.string.ok)) { dialog: DialogInterface, _: Int -> .setPositiveButton(context.getString(R.string.ok)) { dialog: DialogInterface, _: Int ->
if (okClicked) return@setPositiveButton
else {
okClicked = true
dialog.dismiss() dialog.dismiss()
SystemClock.sleep(100) SystemClock.sleep(100)
runOnUiThread(runnable) runOnUiThread(runnable)
} }
}
.show() .show()
.setCanceledOnTouchOutside(false) .setCanceledOnTouchOutside(false)
} }
@SuppressLint("InflateParams") @SuppressLint("InflateParams")
@JvmStatic
@JvmOverloads
fun show(activity: Activity, title: String, message: Spanned, runnable: Runnable? = null) { fun show(activity: Activity, title: String, message: Spanned, runnable: Runnable? = null) {
var okClicked = false
var notEmptytitle = title var notEmptytitle = title
if (notEmptytitle.isEmpty()) notEmptytitle = activity.getString(R.string.message) if (notEmptytitle.isEmpty()) notEmptytitle = activity.getString(R.string.message)
@ -38,10 +42,14 @@ object OKDialog {
.setCustomTitle(AlertDialogHelper.buildCustomTitle(activity, notEmptytitle)) .setCustomTitle(AlertDialogHelper.buildCustomTitle(activity, notEmptytitle))
.setMessage(message) .setMessage(message)
.setPositiveButton(activity.getString(R.string.ok)) { dialog: DialogInterface, _: Int -> .setPositiveButton(activity.getString(R.string.ok)) { dialog: DialogInterface, _: Int ->
if (okClicked) return@setPositiveButton
else {
okClicked = true
dialog.dismiss() dialog.dismiss()
SystemClock.sleep(100) SystemClock.sleep(100)
runnable?.let { activity.runOnUiThread(it) } runnable?.let { activity.runOnUiThread(it) }
} }
}
.show() .show()
.setCanceledOnTouchOutside(false) .setCanceledOnTouchOutside(false)
} }
@ -57,71 +65,89 @@ object OKDialog {
} }
@SuppressLint("InflateParams") @SuppressLint("InflateParams")
@JvmStatic
@JvmOverloads
fun showConfirmation(activity: Activity, title: String, message: Spanned, ok: Runnable?, cancel: Runnable? = null) { fun showConfirmation(activity: Activity, title: String, message: Spanned, ok: Runnable?, cancel: Runnable? = null) {
var okClicked = false
AlertDialogHelper.Builder(activity) AlertDialogHelper.Builder(activity)
.setMessage(message) .setMessage(message)
.setCustomTitle(AlertDialogHelper.buildCustomTitle(activity, title)) .setCustomTitle(AlertDialogHelper.buildCustomTitle(activity, title))
.setPositiveButton(android.R.string.ok) { dialog: DialogInterface, _: Int -> .setPositiveButton(android.R.string.ok) { dialog: DialogInterface, _: Int ->
if (okClicked) return@setPositiveButton
else {
okClicked = true
dialog.dismiss() dialog.dismiss()
SystemClock.sleep(100) SystemClock.sleep(100)
ok?.let { activity.runOnUiThread(it) } ok?.let { activity.runOnUiThread(it) }
} }
}
.setNegativeButton(android.R.string.cancel) { dialog: DialogInterface, _: Int -> .setNegativeButton(android.R.string.cancel) { dialog: DialogInterface, _: Int ->
if (okClicked) return@setNegativeButton
else {
okClicked = true
dialog.dismiss() dialog.dismiss()
SystemClock.sleep(100) SystemClock.sleep(100)
cancel?.let { activity.runOnUiThread(it) } cancel?.let { activity.runOnUiThread(it) }
} }
.setNegativeButton(android.R.string.cancel, null) }
.show() .show()
.setCanceledOnTouchOutside(false) .setCanceledOnTouchOutside(false)
} }
@SuppressLint("InflateParams") @SuppressLint("InflateParams")
@JvmStatic
fun showConfirmation(activity: Activity, title: String, message: String, ok: Runnable?, cancel: Runnable? = null) { fun showConfirmation(activity: Activity, title: String, message: String, ok: Runnable?, cancel: Runnable? = null) {
var okClicked = false
AlertDialogHelper.Builder(activity) AlertDialogHelper.Builder(activity)
.setMessage(message) .setMessage(message)
.setCustomTitle(AlertDialogHelper.buildCustomTitle(activity, title)) .setCustomTitle(AlertDialogHelper.buildCustomTitle(activity, title))
.setPositiveButton(android.R.string.ok) { dialog: DialogInterface, _: Int -> .setPositiveButton(android.R.string.ok) { dialog: DialogInterface, _: Int ->
if (okClicked) return@setPositiveButton
else {
okClicked = true
dialog.dismiss() dialog.dismiss()
SystemClock.sleep(100) SystemClock.sleep(100)
ok?.let { activity.runOnUiThread(it) } ok?.let { activity.runOnUiThread(it) }
} }
}
.setNegativeButton(android.R.string.cancel) { dialog: DialogInterface, _: Int -> .setNegativeButton(android.R.string.cancel) { dialog: DialogInterface, _: Int ->
if (okClicked) return@setNegativeButton
else {
okClicked = true
dialog.dismiss() dialog.dismiss()
SystemClock.sleep(100) SystemClock.sleep(100)
cancel?.let { activity.runOnUiThread(it) } cancel?.let { activity.runOnUiThread(it) }
} }
}
.show() .show()
.setCanceledOnTouchOutside(false) .setCanceledOnTouchOutside(false)
} }
@JvmStatic
@JvmOverloads
fun showConfirmation(context: Context, message: Spanned, ok: Runnable?, cancel: Runnable? = null) { fun showConfirmation(context: Context, message: Spanned, ok: Runnable?, cancel: Runnable? = null) {
showConfirmation(context, context.getString(R.string.confirmation), message, ok, cancel) showConfirmation(context, context.getString(R.string.confirmation), message, ok, cancel)
} }
@SuppressLint("InflateParams") @SuppressLint("InflateParams")
@JvmStatic
@JvmOverloads
fun showConfirmation(context: Context, title: String, message: Spanned, ok: Runnable?, cancel: Runnable? = null) { fun showConfirmation(context: Context, title: String, message: Spanned, ok: Runnable?, cancel: Runnable? = null) {
var okClicked = false
AlertDialogHelper.Builder(context) AlertDialogHelper.Builder(context)
.setMessage(message) .setMessage(message)
.setCustomTitle(AlertDialogHelper.buildCustomTitle(context, title)) .setCustomTitle(AlertDialogHelper.buildCustomTitle(context, title))
.setPositiveButton(android.R.string.ok) { dialog: DialogInterface, _: Int -> .setPositiveButton(android.R.string.ok) { dialog: DialogInterface, _: Int ->
if (okClicked) return@setPositiveButton
else {
okClicked = true
dialog.dismiss() dialog.dismiss()
SystemClock.sleep(100) SystemClock.sleep(100)
runOnUiThread(ok) runOnUiThread(ok)
} }
}
.setNegativeButton(android.R.string.cancel) { dialog: DialogInterface, _: Int -> .setNegativeButton(android.R.string.cancel) { dialog: DialogInterface, _: Int ->
if (okClicked) return@setNegativeButton
else {
okClicked = true
dialog.dismiss() dialog.dismiss()
SystemClock.sleep(100) SystemClock.sleep(100)
runOnUiThread(cancel) runOnUiThread(cancel)
} }
.setNegativeButton(android.R.string.cancel, null) }
.show() .show()
.setCanceledOnTouchOutside(false) .setCanceledOnTouchOutside(false)
} }
@ -136,19 +162,28 @@ object OKDialog {
@JvmStatic @JvmStatic
@JvmOverloads @JvmOverloads
fun showConfirmation(context: Context, title: String, message: String, ok: Runnable?, cancel: Runnable? = null) { fun showConfirmation(context: Context, title: String, message: String, ok: Runnable?, cancel: Runnable? = null) {
var okClicked = false
AlertDialogHelper.Builder(context) AlertDialogHelper.Builder(context)
.setMessage(message) .setMessage(message)
.setCustomTitle(AlertDialogHelper.buildCustomTitle(context, title)) .setCustomTitle(AlertDialogHelper.buildCustomTitle(context, title))
.setPositiveButton(android.R.string.ok) { dialog: DialogInterface, _: Int -> .setPositiveButton(android.R.string.ok) { dialog: DialogInterface, _: Int ->
if (okClicked) return@setPositiveButton
else {
okClicked = true
dialog.dismiss() dialog.dismiss()
SystemClock.sleep(100) SystemClock.sleep(100)
runOnUiThread(ok) runOnUiThread(ok)
} }
}
.setNegativeButton(android.R.string.cancel) { dialog: DialogInterface, _: Int -> .setNegativeButton(android.R.string.cancel) { dialog: DialogInterface, _: Int ->
if (okClicked) return@setNegativeButton
else {
okClicked = true
dialog.dismiss() dialog.dismiss()
SystemClock.sleep(100) SystemClock.sleep(100)
runOnUiThread(cancel) runOnUiThread(cancel)
} }
}
.show() .show()
.setCanceledOnTouchOutside(false) .setCanceledOnTouchOutside(false)
} }
@ -157,19 +192,28 @@ object OKDialog {
@JvmStatic @JvmStatic
@JvmOverloads @JvmOverloads
fun showConfirmation(context: Context, title: String, message: String, ok: DialogInterface.OnClickListener?, cancel: DialogInterface.OnClickListener? = null) { fun showConfirmation(context: Context, title: String, message: String, ok: DialogInterface.OnClickListener?, cancel: DialogInterface.OnClickListener? = null) {
var okClicked = false
AlertDialogHelper.Builder(context) AlertDialogHelper.Builder(context)
.setMessage(message) .setMessage(message)
.setCustomTitle(AlertDialogHelper.buildCustomTitle(context, title)) .setCustomTitle(AlertDialogHelper.buildCustomTitle(context, title))
.setPositiveButton(android.R.string.ok) { dialog: DialogInterface, which: Int -> .setPositiveButton(android.R.string.ok) { dialog: DialogInterface, which: Int ->
if (okClicked) return@setPositiveButton
else {
okClicked = true
dialog.dismiss() dialog.dismiss()
SystemClock.sleep(100) SystemClock.sleep(100)
ok?.onClick(dialog, which) ok?.onClick(dialog, which)
} }
}
.setNegativeButton(android.R.string.cancel) { dialog: DialogInterface, which: Int -> .setNegativeButton(android.R.string.cancel) { dialog: DialogInterface, which: Int ->
if (okClicked) return@setNegativeButton
else {
okClicked = true
dialog.dismiss() dialog.dismiss()
SystemClock.sleep(100) SystemClock.sleep(100)
cancel?.onClick(dialog, which) cancel?.onClick(dialog, which)
} }
}
.show() .show()
.setCanceledOnTouchOutside(false) .setCanceledOnTouchOutside(false)
} }

View file

@ -13,14 +13,15 @@ import info.nightscout.androidaps.utils.extensions.runOnUiThread
object WarningDialog { object WarningDialog {
@SuppressLint("InflateParams") @SuppressLint("InflateParams")
@JvmStatic
@JvmOverloads
fun showWarning(context: Context, title: String, message: String, @StringRes positiveButton: Int = -1, ok: (() -> Unit)? = null, cancel: (() -> Unit)? = null) { 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) val builder = AlertDialogHelper.Builder(context, R.style.AppThemeWarningDialog)
.setMessage(message) .setMessage(message)
.setCustomTitle(AlertDialogHelper.buildCustomTitle(context, title, R.drawable.ic_header_warning, R.style.AppThemeWarningDialog)) .setCustomTitle(AlertDialogHelper.buildCustomTitle(context, title, R.drawable.ic_header_warning, R.style.AppThemeWarningDialog))
.setNegativeButton(R.string.dismiss) { dialog: DialogInterface, _: Int -> .setNegativeButton(R.string.dismiss) { dialog: DialogInterface, _: Int ->
if (okClicked) return@setNegativeButton
else {
okClicked = true
dialog.dismiss() dialog.dismiss()
SystemClock.sleep(100) SystemClock.sleep(100)
if (cancel != null) { if (cancel != null) {
@ -29,9 +30,13 @@ object WarningDialog {
}) })
} }
} }
}
if (positiveButton != -1) { if (positiveButton != -1) {
builder.setPositiveButton(positiveButton) { dialog: DialogInterface, _: Int -> builder.setPositiveButton(positiveButton) { dialog: DialogInterface, _: Int ->
if (okClicked) return@setPositiveButton
else {
okClicked = true
dialog.dismiss() dialog.dismiss()
SystemClock.sleep(100) SystemClock.sleep(100)
if (ok != null) { if (ok != null) {
@ -41,6 +46,7 @@ object WarningDialog {
} }
} }
} }
}
val dialog = builder.show() val dialog = builder.show()
dialog.setCanceledOnTouchOutside(true) dialog.setCanceledOnTouchOutside(true)