AndroidAPS/app/src/main/java/info/nightscout/androidaps/utils/OKDialog.kt

209 lines
9.6 KiB
Kotlin
Raw Normal View History

2019-12-22 21:37:26 +01:00
package info.nightscout.androidaps.utils
2020-03-08 22:42:36 +01:00
import android.annotation.SuppressLint
2019-12-22 21:37:26 +01:00
import android.app.Activity
import android.content.Context
import android.content.DialogInterface
import android.os.Handler
import android.os.SystemClock
import android.text.Spanned
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.view.ContextThemeWrapper
import info.nightscout.androidaps.MainApp
import info.nightscout.androidaps.R
object OKDialog {
2020-03-08 22:42:36 +01:00
@SuppressLint("InflateParams")
2019-12-22 21:37:26 +01:00
@JvmStatic
@JvmOverloads
fun show(context: Context, title: String, message: String, runnable: Runnable? = null) {
var notEmptytitle = title
2020-03-08 22:42:36 +01:00
if (notEmptytitle.isEmpty()) notEmptytitle = context.getString(R.string.message)
2019-12-22 21:37:26 +01:00
val titleLayout = LayoutInflater.from(context).inflate(R.layout.dialog_alert_custom, null)
(titleLayout.findViewById<View>(R.id.alertdialog_title) as TextView).text = notEmptytitle
(titleLayout.findViewById<View>(R.id.alertdialog_icon) as ImageView).setImageResource(R.drawable.ic_check_while_48dp)
AlertDialog.Builder(ContextThemeWrapper(context, R.style.AppTheme))
.setCustomTitle(titleLayout)
.setMessage(message)
2020-03-08 22:42:36 +01:00
.setPositiveButton(context.getString(R.string.ok)) { dialog: DialogInterface, _: Int ->
2019-12-22 21:37:26 +01:00
dialog.dismiss()
SystemClock.sleep(100)
runOnUiThread(runnable)
}
.show()
.setCanceledOnTouchOutside(false)
}
fun runOnUiThread(theRunnable: Runnable?) {
val mainHandler = Handler(MainApp.instance().applicationContext.mainLooper)
theRunnable?.let { mainHandler.post(it) }
}
2020-03-08 22:42:36 +01:00
@SuppressLint("InflateParams")
2019-12-22 21:37:26 +01:00
@JvmStatic
@JvmOverloads
fun show(activity: Activity, title: String, message: Spanned, runnable: Runnable? = null) {
var notEmptytitle = title
2020-03-08 22:42:36 +01:00
if (notEmptytitle.isEmpty()) notEmptytitle = activity.getString(R.string.message)
2019-12-22 21:37:26 +01:00
val titleLayout = activity.layoutInflater.inflate(R.layout.dialog_alert_custom, null)
(titleLayout.findViewById<View>(R.id.alertdialog_title) as TextView).text = notEmptytitle
(titleLayout.findViewById<View>(R.id.alertdialog_icon) as ImageView).setImageResource(R.drawable.ic_check_while_48dp)
AlertDialog.Builder(ContextThemeWrapper(activity, R.style.AppTheme))
.setCustomTitle(titleLayout)
.setMessage(message)
2020-03-08 22:42:36 +01:00
.setPositiveButton(activity.getString(R.string.ok)) { dialog: DialogInterface, _: Int ->
2019-12-22 21:37:26 +01:00
dialog.dismiss()
SystemClock.sleep(100)
runnable?.let { activity.runOnUiThread(it) }
}
.show()
.setCanceledOnTouchOutside(false)
}
@JvmStatic
fun showConfirmation(activity: Activity, message: String, ok: Runnable?) {
2020-03-08 22:42:36 +01:00
showConfirmation(activity, activity.getString(R.string.confirmation), message, ok, null)
2019-12-22 21:37:26 +01:00
}
@JvmStatic
fun showConfirmation(activity: Activity, message: Spanned, ok: Runnable?) {
2020-03-08 22:42:36 +01:00
showConfirmation(activity, activity.getString(R.string.confirmation), message, ok, null)
2019-12-22 21:37:26 +01:00
}
2020-03-08 22:42:36 +01:00
@SuppressLint("InflateParams")
2019-12-22 21:37:26 +01:00
@JvmStatic
@JvmOverloads
fun showConfirmation(activity: Activity, title: String, message: Spanned, ok: Runnable?, cancel: Runnable? = null) {
val titleLayout = activity.layoutInflater.inflate(R.layout.dialog_alert_custom, null)
(titleLayout.findViewById<View>(R.id.alertdialog_title) as TextView).text = title
(titleLayout.findViewById<View>(R.id.alertdialog_icon) as ImageView).setImageResource(R.drawable.ic_check_while_48dp)
AlertDialog.Builder(ContextThemeWrapper(activity, R.style.AppTheme))
.setMessage(message)
.setCustomTitle(titleLayout)
.setPositiveButton(android.R.string.ok) { dialog: DialogInterface, _: Int ->
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) }
}
.setNegativeButton(android.R.string.cancel, null)
.show()
.setCanceledOnTouchOutside(false)
}
2020-03-08 22:42:36 +01:00
@SuppressLint("InflateParams")
2019-12-22 21:37:26 +01:00
@JvmStatic
fun showConfirmation(activity: Activity, title: String, message: String, ok: Runnable?, cancel: Runnable? = null) {
val titleLayout = activity.layoutInflater.inflate(R.layout.dialog_alert_custom, null)
(titleLayout.findViewById<View>(R.id.alertdialog_title) as TextView).text = title
(titleLayout.findViewById<View>(R.id.alertdialog_icon) as ImageView).setImageResource(R.drawable.ic_check_while_48dp)
AlertDialog.Builder(ContextThemeWrapper(activity, R.style.AppTheme))
.setMessage(message)
.setCustomTitle(titleLayout)
.setPositiveButton(android.R.string.ok) { dialog: DialogInterface, _: Int ->
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) }
}
.show()
.setCanceledOnTouchOutside(false)
}
@JvmStatic
@JvmOverloads
fun showConfirmation(context: Context, message: Spanned, ok: Runnable?, cancel: Runnable? = null) {
2020-03-08 22:42:36 +01:00
showConfirmation(context, context.getString(R.string.confirmation), message, ok, cancel)
2019-12-22 21:37:26 +01:00
}
2020-03-08 22:42:36 +01:00
@SuppressLint("InflateParams")
2019-12-22 21:37:26 +01:00
@JvmStatic
@JvmOverloads
fun showConfirmation(context: Context, title: String, message: Spanned, ok: Runnable?, cancel: Runnable? = null) {
val titleLayout = LayoutInflater.from(context).inflate(R.layout.dialog_alert_custom, null)
(titleLayout.findViewById<View>(R.id.alertdialog_title) as TextView).text = title
(titleLayout.findViewById<View>(R.id.alertdialog_icon) as ImageView).setImageResource(R.drawable.ic_check_while_48dp)
AlertDialog.Builder(ContextThemeWrapper(context, R.style.AppTheme))
.setMessage(message)
.setCustomTitle(titleLayout)
.setPositiveButton(android.R.string.ok) { dialog: DialogInterface, _: Int ->
dialog.dismiss()
SystemClock.sleep(100)
runOnUiThread(ok)
}
.setNegativeButton(android.R.string.cancel) { dialog: DialogInterface, _: Int ->
dialog.dismiss()
SystemClock.sleep(100)
runOnUiThread(cancel)
}
.setNegativeButton(android.R.string.cancel, null)
.show()
.setCanceledOnTouchOutside(false)
}
@JvmStatic
@JvmOverloads
fun showConfirmation(context: Context, message: String, ok: Runnable?, cancel: Runnable? = null) {
2020-03-08 22:42:36 +01:00
showConfirmation(context, context.getString(R.string.confirmation), message, ok, cancel)
2019-12-22 21:37:26 +01:00
}
2020-03-08 22:42:36 +01:00
@SuppressLint("InflateParams")
2019-12-22 21:37:26 +01:00
@JvmStatic
@JvmOverloads
fun showConfirmation(context: Context, title: String, message: String, ok: Runnable?, cancel: Runnable? = null) {
val titleLayout = LayoutInflater.from(context).inflate(R.layout.dialog_alert_custom, null)
(titleLayout.findViewById<View>(R.id.alertdialog_title) as TextView).text = title
(titleLayout.findViewById<View>(R.id.alertdialog_icon) as ImageView).setImageResource(R.drawable.ic_check_while_48dp)
AlertDialog.Builder(ContextThemeWrapper(context, R.style.AppTheme))
.setMessage(message)
.setCustomTitle(titleLayout)
.setPositiveButton(android.R.string.ok) { dialog: DialogInterface, _: Int ->
dialog.dismiss()
SystemClock.sleep(100)
runOnUiThread(ok)
}
.setNegativeButton(android.R.string.cancel) { dialog: DialogInterface, _: Int ->
dialog.dismiss()
SystemClock.sleep(100)
runOnUiThread(cancel)
}
.show()
.setCanceledOnTouchOutside(false)
}
2020-03-08 22:42:36 +01:00
@SuppressLint("InflateParams")
2019-12-22 21:37:26 +01:00
@JvmStatic
@JvmOverloads
fun showConfirmation(context: Context, title: String, message: String, ok: DialogInterface.OnClickListener?, cancel: DialogInterface.OnClickListener? = null) {
val titleLayout = LayoutInflater.from(context).inflate(R.layout.dialog_alert_custom, null)
(titleLayout.findViewById<View>(R.id.alertdialog_title) as TextView).text = title
(titleLayout.findViewById<View>(R.id.alertdialog_icon) as ImageView).setImageResource(R.drawable.ic_check_while_48dp)
AlertDialog.Builder(ContextThemeWrapper(context, R.style.AppTheme))
.setMessage(message)
.setCustomTitle(titleLayout)
.setPositiveButton(android.R.string.ok) { dialog: DialogInterface, which: Int ->
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)
}
.show()
.setCanceledOnTouchOutside(false)
}
}