OKDialogs ok button guard
This commit is contained in:
parent
6f7932f1c7
commit
3354bb7da4
2 changed files with 112 additions and 62 deletions
|
@ -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)
|
||||
|
|
|
@ -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()
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue