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 ->
dialog.dismiss() if (okClicked) return@setPositiveButton
SystemClock.sleep(100) else {
runOnUiThread(runnable) okClicked = true
dialog.dismiss()
SystemClock.sleep(100)
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,9 +42,13 @@ 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 ->
dialog.dismiss() if (okClicked) return@setPositiveButton
SystemClock.sleep(100) else {
runnable?.let { activity.runOnUiThread(it) } okClicked = true
dialog.dismiss()
SystemClock.sleep(100)
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 ->
dialog.dismiss() if (okClicked) return@setPositiveButton
SystemClock.sleep(100) else {
ok?.let { activity.runOnUiThread(it) } okClicked = true
dialog.dismiss()
SystemClock.sleep(100)
ok?.let { activity.runOnUiThread(it) }
}
} }
.setNegativeButton(android.R.string.cancel) { dialog: DialogInterface, _: Int -> .setNegativeButton(android.R.string.cancel) { dialog: DialogInterface, _: Int ->
dialog.dismiss() if (okClicked) return@setNegativeButton
SystemClock.sleep(100) else {
cancel?.let { activity.runOnUiThread(it) } okClicked = true
dialog.dismiss()
SystemClock.sleep(100)
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 ->
dialog.dismiss() if (okClicked) return@setPositiveButton
SystemClock.sleep(100) else {
ok?.let { activity.runOnUiThread(it) } okClicked = true
dialog.dismiss()
SystemClock.sleep(100)
ok?.let { activity.runOnUiThread(it) }
}
} }
.setNegativeButton(android.R.string.cancel) { dialog: DialogInterface, _: Int -> .setNegativeButton(android.R.string.cancel) { dialog: DialogInterface, _: Int ->
dialog.dismiss() if (okClicked) return@setNegativeButton
SystemClock.sleep(100) else {
cancel?.let { activity.runOnUiThread(it) } okClicked = true
dialog.dismiss()
SystemClock.sleep(100)
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 ->
dialog.dismiss() if (okClicked) return@setPositiveButton
SystemClock.sleep(100) else {
runOnUiThread(ok) okClicked = true
dialog.dismiss()
SystemClock.sleep(100)
runOnUiThread(ok)
}
} }
.setNegativeButton(android.R.string.cancel) { dialog: DialogInterface, _: Int -> .setNegativeButton(android.R.string.cancel) { dialog: DialogInterface, _: Int ->
dialog.dismiss() if (okClicked) return@setNegativeButton
SystemClock.sleep(100) else {
runOnUiThread(cancel) okClicked = true
dialog.dismiss()
SystemClock.sleep(100)
runOnUiThread(cancel)
}
} }
.setNegativeButton(android.R.string.cancel, null)
.show() .show()
.setCanceledOnTouchOutside(false) .setCanceledOnTouchOutside(false)
} }
@ -136,18 +162,27 @@ 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 ->
dialog.dismiss() if (okClicked) return@setPositiveButton
SystemClock.sleep(100) else {
runOnUiThread(ok) okClicked = true
dialog.dismiss()
SystemClock.sleep(100)
runOnUiThread(ok)
}
} }
.setNegativeButton(android.R.string.cancel) { dialog: DialogInterface, _: Int -> .setNegativeButton(android.R.string.cancel) { dialog: DialogInterface, _: Int ->
dialog.dismiss() if (okClicked) return@setNegativeButton
SystemClock.sleep(100) else {
runOnUiThread(cancel) okClicked = true
dialog.dismiss()
SystemClock.sleep(100)
runOnUiThread(cancel)
}
} }
.show() .show()
.setCanceledOnTouchOutside(false) .setCanceledOnTouchOutside(false)
@ -157,18 +192,27 @@ 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 ->
dialog.dismiss() if (okClicked) return@setPositiveButton
SystemClock.sleep(100) else {
ok?.onClick(dialog, which) okClicked = true
dialog.dismiss()
SystemClock.sleep(100)
ok?.onClick(dialog, which)
}
} }
.setNegativeButton(android.R.string.cancel) { dialog: DialogInterface, which: Int -> .setNegativeButton(android.R.string.cancel) { dialog: DialogInterface, which: Int ->
dialog.dismiss() if (okClicked) return@setNegativeButton
SystemClock.sleep(100) else {
cancel?.onClick(dialog, which) okClicked = true
dialog.dismiss()
SystemClock.sleep(100)
cancel?.onClick(dialog, which)
}
} }
.show() .show()
.setCanceledOnTouchOutside(false) .setCanceledOnTouchOutside(false)

View file

@ -13,31 +13,37 @@ 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 ->
dialog.dismiss() if (okClicked) return@setNegativeButton
SystemClock.sleep(100) else {
if (cancel != null) { okClicked = true
runOnUiThread(Runnable { dialog.dismiss()
cancel() SystemClock.sleep(100)
}) if (cancel != null) {
runOnUiThread(Runnable {
cancel()
})
}
} }
} }
if (positiveButton != -1) { if (positiveButton != -1) {
builder.setPositiveButton(positiveButton) { dialog: DialogInterface, _: Int -> builder.setPositiveButton(positiveButton) { dialog: DialogInterface, _: Int ->
dialog.dismiss() if (okClicked) return@setPositiveButton
SystemClock.sleep(100) else {
if (ok != null) { okClicked = true
runOnUiThread(Runnable { dialog.dismiss()
ok() SystemClock.sleep(100)
}) if (ok != null) {
runOnUiThread(Runnable {
ok()
})
}
} }
} }
} }