|
| 1 | +package fr.free.nrw.commons.utils |
| 2 | + |
| 3 | +import android.app.Activity |
| 4 | +import android.app.AlertDialog |
| 5 | +import android.app.Dialog |
| 6 | +import android.view.View |
| 7 | +import fr.free.nrw.commons.R |
| 8 | +import timber.log.Timber |
| 9 | + |
| 10 | +object DialogUtil { |
| 11 | + /** |
| 12 | + * Shows a dialog safely. |
| 13 | + * @param activity the activity |
| 14 | + * @param dialog the dialog to be shown |
| 15 | + */ |
| 16 | + private fun showSafely(activity: Activity?, dialog: Dialog?) { |
| 17 | + |
| 18 | + if (activity == null || dialog == null) { |
| 19 | + Timber.d("Show called with null activity / dialog. Ignoring.") |
| 20 | + return |
| 21 | + } |
| 22 | + |
| 23 | + if (activity.isFinishing || activity.isDestroyed) { |
| 24 | + Timber.e("Activity is not running. Could not show dialog. ") |
| 25 | + return |
| 26 | + } |
| 27 | + try { |
| 28 | + dialog.show() |
| 29 | + } catch (e: IllegalStateException) { |
| 30 | + Timber.e(e, "Could not show dialog.") |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + @JvmStatic |
| 35 | + fun showAlertDialog( |
| 36 | + activity: Activity, |
| 37 | + title: String, |
| 38 | + message: String, |
| 39 | + onPositiveBtnClick: Runnable?, |
| 40 | + onNegativeBtnClick: Runnable? |
| 41 | + ) { |
| 42 | + createAndShowDialogSafely( |
| 43 | + activity = activity, |
| 44 | + title = title, |
| 45 | + message = message, |
| 46 | + positiveButtonText = activity.getString(R.string.yes), |
| 47 | + negativeButtonText = activity.getString(R.string.no), |
| 48 | + onPositiveBtnClick = onPositiveBtnClick, |
| 49 | + onNegativeBtnClick = onNegativeBtnClick |
| 50 | + ) |
| 51 | + } |
| 52 | + |
| 53 | + @JvmStatic |
| 54 | + fun showAlertDialog( |
| 55 | + activity: Activity, |
| 56 | + title: String, |
| 57 | + message: String, |
| 58 | + positiveButtonText: String?, |
| 59 | + negativeButtonText: String?, |
| 60 | + onPositiveBtnClick: Runnable?, |
| 61 | + onNegativeBtnClick: Runnable? |
| 62 | + ) { |
| 63 | + createAndShowDialogSafely( |
| 64 | + activity = activity, |
| 65 | + title = title, |
| 66 | + message = message, |
| 67 | + positiveButtonText = positiveButtonText, |
| 68 | + negativeButtonText = negativeButtonText, |
| 69 | + onPositiveBtnClick = onPositiveBtnClick, |
| 70 | + onNegativeBtnClick = onNegativeBtnClick |
| 71 | + ) |
| 72 | + } |
| 73 | + |
| 74 | + @JvmStatic |
| 75 | + fun showAlertDialog( |
| 76 | + activity: Activity, |
| 77 | + title: String, |
| 78 | + message: String, |
| 79 | + onPositiveBtnClick: Runnable?, |
| 80 | + onNegativeBtnClick: Runnable?, |
| 81 | + customView: View?, |
| 82 | + cancelable: Boolean |
| 83 | + ) { |
| 84 | + createAndShowDialogSafely( |
| 85 | + activity = activity, |
| 86 | + title = title, |
| 87 | + message = message, |
| 88 | + positiveButtonText = activity.getString(R.string.yes), |
| 89 | + negativeButtonText = activity.getString(R.string.no), |
| 90 | + onPositiveBtnClick = onPositiveBtnClick, |
| 91 | + onNegativeBtnClick = onNegativeBtnClick, |
| 92 | + customView = customView, |
| 93 | + cancelable = cancelable |
| 94 | + ) |
| 95 | + } |
| 96 | + |
| 97 | + @JvmStatic |
| 98 | + fun showAlertDialog( |
| 99 | + activity: Activity, |
| 100 | + title: String, |
| 101 | + message: String, |
| 102 | + positiveButtonText: String?, |
| 103 | + onPositiveBtnClick: Runnable?, |
| 104 | + cancelable: Boolean |
| 105 | + ) { |
| 106 | + createAndShowDialogSafely( |
| 107 | + activity = activity, |
| 108 | + title = title, |
| 109 | + message = message, |
| 110 | + positiveButtonText = positiveButtonText, |
| 111 | + onPositiveBtnClick = onPositiveBtnClick, |
| 112 | + cancelable = cancelable |
| 113 | + ) |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * show a dialog |
| 118 | + * @param activity |
| 119 | + * @param title |
| 120 | + * @param message |
| 121 | + * @param positiveButtonText |
| 122 | + * @param negativeButtonText |
| 123 | + * @param onPositiveBtnClick |
| 124 | + * @param onNegativeBtnClick |
| 125 | + * @param customView |
| 126 | + * @param cancelable |
| 127 | + */ |
| 128 | + private fun createAndShowDialogSafely( |
| 129 | + activity: Activity, |
| 130 | + title: String, |
| 131 | + message: String, |
| 132 | + positiveButtonText: String? = null, |
| 133 | + negativeButtonText: String? = null, |
| 134 | + onPositiveBtnClick: Runnable? = null, |
| 135 | + onNegativeBtnClick: Runnable? = null, |
| 136 | + customView: View? = null, |
| 137 | + cancelable: Boolean = true |
| 138 | + ) { |
| 139 | + |
| 140 | + /* If the custom view already has a parent, there is already a dialog showing with the view |
| 141 | + * This happens for on resume - return to avoid creating a second dialog - the first one |
| 142 | + * will still show |
| 143 | + */ |
| 144 | + if (customView?.parent != null) { |
| 145 | + return |
| 146 | + } |
| 147 | + |
| 148 | + showSafely(activity, AlertDialog.Builder(activity).apply { |
| 149 | + setTitle(title) |
| 150 | + setMessage(message) |
| 151 | + setView(customView) |
| 152 | + setCancelable(cancelable) |
| 153 | + positiveButtonText?.let { |
| 154 | + setPositiveButton(it) { _, _ -> onPositiveBtnClick?.run() } |
| 155 | + } |
| 156 | + negativeButtonText?.let { |
| 157 | + setNegativeButton(it) { _, _ -> onNegativeBtnClick?.run() } |
| 158 | + } |
| 159 | + }.create()) |
| 160 | + } |
| 161 | +} |
0 commit comments