|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Shaan Narendran <shaannaren06@gmail.com> |
| 3 | + * |
| 4 | + * This program is free software; you can redistribute it and/or modify it under |
| 5 | + * the terms of the GNU General Public License as published by the Free Software |
| 6 | + * Foundation; either version 3 of the License, or (at your option) any later |
| 7 | + * version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, but WITHOUT ANY |
| 10 | + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 11 | + * PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 12 | + * |
| 13 | + * You should have received a copy of the GNU General Public License along with |
| 14 | + * this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.ichi2.anki.dialogs |
| 18 | + |
| 19 | +import android.content.Context |
| 20 | +import android.content.DialogInterface |
| 21 | +import android.os.Handler |
| 22 | +import android.os.Looper |
| 23 | +import android.view.LayoutInflater |
| 24 | +import android.widget.TextView |
| 25 | +import androidx.appcompat.app.AlertDialog |
| 26 | +import com.google.android.material.dialog.MaterialAlertDialogBuilder |
| 27 | +import com.google.android.material.progressindicator.CircularProgressIndicator |
| 28 | +import com.ichi2.anki.R |
| 29 | + |
| 30 | +/** |
| 31 | + * Replacement for [android.app.ProgressDialog] deprecation on API 26+. This class should be used |
| 32 | + * instead of the platform [android.app.ProgressDialog]. |
| 33 | + * |
| 34 | + * @see android.app.ProgressDialog |
| 35 | + */ |
| 36 | +class ProgressCompat( |
| 37 | + private val context: Context, |
| 38 | +) { |
| 39 | + companion object { |
| 40 | + private val PROGRESS_PATTERN = Regex("(\\d+)\\s*/\\s*(\\d+)") |
| 41 | + } |
| 42 | + |
| 43 | + private var dialog: AlertDialog? = null |
| 44 | + private var circularIndicator: CircularProgressIndicator? = null |
| 45 | + private var messageView: TextView? = null |
| 46 | + private var pendingMessage: CharSequence? = null |
| 47 | + private var negativeButtonAction: Pair<String, (DialogInterface, Int) -> Unit>? = null |
| 48 | + private var onCancelListener: DialogInterface.OnCancelListener? = null |
| 49 | + private var isCancelable = false |
| 50 | + private var max = 100 |
| 51 | + private var progress = 0 |
| 52 | + |
| 53 | + private val handler by lazy { Handler(Looper.getMainLooper()) } |
| 54 | + |
| 55 | + private inline fun runOnUi(crossinline action: () -> Unit) { |
| 56 | + if (Looper.myLooper() == Looper.getMainLooper()) action() else handler.post { action() } |
| 57 | + } |
| 58 | + |
| 59 | + fun setCancelable(flag: Boolean) { |
| 60 | + isCancelable = flag |
| 61 | + } |
| 62 | + |
| 63 | + fun setOnCancelListener(listener: DialogInterface.OnCancelListener?) { |
| 64 | + onCancelListener = listener |
| 65 | + } |
| 66 | + |
| 67 | + fun setButton( |
| 68 | + whichButton: Int, |
| 69 | + text: CharSequence, |
| 70 | + listener: (DialogInterface, Int) -> Unit, |
| 71 | + ) { |
| 72 | + if (whichButton == DialogInterface.BUTTON_NEGATIVE) { |
| 73 | + negativeButtonAction = text.toString() to listener |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Updates the message text. |
| 79 | + * |
| 80 | + * This method attempts to parse progress numbers from the [message] string. |
| 81 | + * If the message contains a pattern like "50 / 100", the progress bar will |
| 82 | + * automatically update to match those values. |
| 83 | + * |
| 84 | + * @param message The text to display, or null to clear. |
| 85 | + */ |
| 86 | + fun setMessage(message: CharSequence?) = |
| 87 | + runOnUi { |
| 88 | + pendingMessage = message |
| 89 | + messageView?.text = message |
| 90 | + |
| 91 | + message?.let { msg -> |
| 92 | + PROGRESS_PATTERN.find(msg)?.destructured?.let { (curr, total) -> |
| 93 | + runCatching { |
| 94 | + val t = total.toInt() |
| 95 | + // FIX: Changed _max to max |
| 96 | + if (max != t) setMaxInternal(t) |
| 97 | + setProgressInternal(curr.toInt()) |
| 98 | + } |
| 99 | + return@runOnUi |
| 100 | + } |
| 101 | + // If no digits found, ensure we are in indeterminate mode (spinner) |
| 102 | + if (circularIndicator?.isIndeterminate == false && progress == 0) { |
| 103 | + circularIndicator?.isIndeterminate = true |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + fun setMax(max: Int) = runOnUi { setMaxInternal(max) } |
| 109 | + |
| 110 | + private fun setMaxInternal(max: Int) { |
| 111 | + this.max = max |
| 112 | + circularIndicator?.max = max |
| 113 | + } |
| 114 | + |
| 115 | + fun setProgress(value: Int) = runOnUi { setProgressInternal(value) } |
| 116 | + |
| 117 | + private fun setProgressInternal(value: Int) { |
| 118 | + progress = value |
| 119 | + circularIndicator?.apply { |
| 120 | + if (isIndeterminate) isIndeterminate = false |
| 121 | + setProgressCompat(value, true) |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + fun incrementProgressBy(diff: Int) = runOnUi { setProgressInternal(progress + diff) } |
| 126 | + |
| 127 | + var isIndeterminate: Boolean |
| 128 | + get() = circularIndicator?.isIndeterminate ?: false |
| 129 | + set(value) = runOnUi { circularIndicator?.isIndeterminate = value } |
| 130 | + |
| 131 | + fun show() = |
| 132 | + runOnUi { |
| 133 | + if (dialog?.isShowing == true) return@runOnUi |
| 134 | + |
| 135 | + val view = LayoutInflater.from(context).inflate(R.layout.dialog_circular_progress, null) |
| 136 | + circularIndicator = |
| 137 | + view.findViewById<CircularProgressIndicator>(R.id.circular_progress).apply { |
| 138 | + isIndeterminate = false |
| 139 | + // FIX: Explicitly reference the class member using 'this@ProgressCompat.max' |
| 140 | + max = this@ProgressCompat.max |
| 141 | + setProgressCompat(progress, false) |
| 142 | + } |
| 143 | + messageView = view.findViewById<TextView>(R.id.progress_message).apply { text = pendingMessage } |
| 144 | + |
| 145 | + dialog = |
| 146 | + MaterialAlertDialogBuilder(context) |
| 147 | + .setView(view) |
| 148 | + .setCancelable(isCancelable) |
| 149 | + .setOnCancelListener(onCancelListener) |
| 150 | + .apply { |
| 151 | + negativeButtonAction?.let { (text, listener) -> setNegativeButton(text, listener) } |
| 152 | + }.create() |
| 153 | + |
| 154 | + dialog?.show() |
| 155 | + } |
| 156 | + |
| 157 | + fun dismiss() = |
| 158 | + runOnUi { |
| 159 | + runCatching { dialog?.dismiss() } |
| 160 | + } |
| 161 | +} |
0 commit comments