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 {
@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 ->
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,10 +42,14 @@ object OKDialog {
.setCustomTitle(AlertDialogHelper.buildCustomTitle(activity, notEmptytitle))
.setMessage(message)
.setPositiveButton(activity.getString(R.string.ok)) { dialog: DialogInterface, _: Int ->
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 ->
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 ->
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 ->
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 ->
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 ->
if (okClicked) return@setPositiveButton
else {
okClicked = true
dialog.dismiss()
SystemClock.sleep(100)
runOnUiThread(ok)
}
}
.setNegativeButton(android.R.string.cancel) { dialog: DialogInterface, _: Int ->
if (okClicked) return@setNegativeButton
else {
okClicked = true
dialog.dismiss()
SystemClock.sleep(100)
runOnUiThread(cancel)
}
.setNegativeButton(android.R.string.cancel, null)
}
.show()
.setCanceledOnTouchOutside(false)
}
@ -136,19 +162,28 @@ 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 ->
if (okClicked) return@setPositiveButton
else {
okClicked = true
dialog.dismiss()
SystemClock.sleep(100)
runOnUiThread(ok)
}
}
.setNegativeButton(android.R.string.cancel) { dialog: DialogInterface, _: Int ->
if (okClicked) return@setNegativeButton
else {
okClicked = true
dialog.dismiss()
SystemClock.sleep(100)
runOnUiThread(cancel)
}
}
.show()
.setCanceledOnTouchOutside(false)
}
@ -157,19 +192,28 @@ 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 ->
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 ->
if (okClicked) return@setNegativeButton
else {
okClicked = true
dialog.dismiss()
SystemClock.sleep(100)
cancel?.onClick(dialog, which)
}
}
.show()
.setCanceledOnTouchOutside(false)
}

View file

@ -13,14 +13,15 @@ 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 ->
if (okClicked) return@setNegativeButton
else {
okClicked = true
dialog.dismiss()
SystemClock.sleep(100)
if (cancel != null) {
@ -29,9 +30,13 @@ object WarningDialog {
})
}
}
}
if (positiveButton != -1) {
builder.setPositiveButton(positiveButton) { dialog: DialogInterface, _: Int ->
if (okClicked) return@setPositiveButton
else {
okClicked = true
dialog.dismiss()
SystemClock.sleep(100)
if (ok != null) {
@ -41,6 +46,7 @@ object WarningDialog {
}
}
}
}
val dialog = builder.show()
dialog.setCanceledOnTouchOutside(true)