@@ -15,6 +15,8 @@ import com.omarea.common.shell.AsynSuShellUnit
1515open class ProgressBarDialog (private var context : Activity , private var uniqueId : String? = null ) {
1616 private var alert: DialogHelper .DialogWrap ? = null
1717 private var textView: TextView ? = null
18+ private var cancelButton: Button ? = null
19+ private var cancelCallback: (() -> Unit )? = null
1820
1921 companion object {
2022 private val dialogs = LinkedHashMap <String , DialogHelper .DialogWrap >()
@@ -52,7 +54,6 @@ open class ProgressBarDialog(private var context: Activity, private var uniqueId
5254 val textView: TextView = (dialog.findViewById(R .id.dialog_text))
5355 textView.text = context.getString(R .string.execute_wait)
5456 alert = DialogHelper .customDialog(context, dialog, false )
55- // AlertDialog.Builder(context).setView(dialog).setCancelable(false).create()
5657 if (handler == null ) {
5758 AsynSuShellUnit (DefaultHandler (alert)).exec(cmd).waitFor()
5859 } else {
@@ -69,14 +70,17 @@ open class ProgressBarDialog(private var context: Activity, private var uniqueId
6970 }
7071
7172 fun hideDialog () {
73+ cancelCallback = null
7274 try {
7375 if (alert != null ) {
7476 alert?.dismiss()
7577 alert = null
7678 }
7779 } catch (_: Exception ) {
7880 }
79-
81+ textView = null
82+ cancelButton = null
83+
8084 uniqueId?.run {
8185 if (dialogs.containsKey(this )) {
8286 dialogs.remove(this )
@@ -85,52 +89,68 @@ open class ProgressBarDialog(private var context: Activity, private var uniqueId
8589 }
8690
8791 /* *
88- * Hiển thị dialog loading kèm nút Hủy .
89- * [onCancel] được gọi trên main thread khi người dùng bấm Hủy;
90- * dialog tự đóng ngay sau đó .
92+ * Đặt callback hủy cho dialog đang hiển thị .
93+ * Nếu dialog chưa mở thì callback được lưu lại và áp dụng khi [showDialog] được gọi.
94+ * Gọi với null để ẩn nút Hủy .
9195 */
92- fun showDialogWithCancel (text : String , onCancel : () -> Unit ): ProgressBarDialog {
93- val layoutInflater = LayoutInflater .from(context)
94- val dialog = layoutInflater.inflate(R .layout.dialog_loading, null )
95-
96- textView = dialog.findViewById(R .id.dialog_text)
97- textView?.text = text
98-
99- val cancelButton = dialog.findViewById<Button >(R .id.dialog_cancel_button)
100- cancelButton?.visibility = View .VISIBLE
101- cancelButton?.setOnClickListener {
102- hideDialog()
103- onCancel()
104- }
105-
106- alert = DialogHelper .customDialog(context, dialog, false )
107-
108- uniqueId?.run {
109- dialogs.remove(this )
110- alert?.let { dialogs[this ] = it }
96+ fun setCancelCallback (onCancel : (() -> Unit )? ) {
97+ cancelCallback = onCancel
98+ val btn = cancelButton ? : return
99+ if (onCancel != null ) {
100+ btn.visibility = View .VISIBLE
101+ btn.setOnClickListener {
102+ val cb = cancelCallback
103+ hideDialog()
104+ cb?.invoke()
105+ }
106+ } else {
107+ btn.visibility = View .GONE
111108 }
109+ }
112110
111+ /* *
112+ * Hiển thị dialog loading kèm nút Hủy ngay từ đầu.
113+ * Các lần gọi [showDialog] sau đó chỉ cập nhật text, nút Hủy vẫn được giữ nguyên.
114+ */
115+ fun showDialogWithCancel (text : String , onCancel : () -> Unit ): ProgressBarDialog {
116+ cancelCallback = onCancel
117+ showDialog(text)
113118 return this
114119 }
115120
116121 fun showDialog (text : String = "Loading , please wait..."): ProgressBarDialog {
117- if (textView != null && alert != null ) {
122+ if (textView != null && alert?.isShowing == true ) {
123+ // Dialog đang mở — chỉ cập nhật text, giữ nguyên nút Hủy
118124 textView?.text = text
119125 } else {
126+ // Tạo mới dialog
120127 val layoutInflater = LayoutInflater .from(context)
121128 val dialog = layoutInflater.inflate(R .layout.dialog_loading, null )
122-
129+
123130 textView = dialog.findViewById(R .id.dialog_text)
124131 textView?.text = text
125-
132+
133+ cancelButton = dialog.findViewById(R .id.dialog_cancel_button)
134+ val cb = cancelCallback
135+ if (cb != null ) {
136+ cancelButton?.visibility = View .VISIBLE
137+ cancelButton?.setOnClickListener {
138+ val captured = cancelCallback
139+ hideDialog()
140+ captured?.invoke()
141+ }
142+ } else {
143+ cancelButton?.visibility = View .GONE
144+ }
145+
126146 alert = DialogHelper .customDialog(context, dialog, false )
127147 }
128-
148+
129149 uniqueId?.run {
130150 dialogs.remove(this )
131151 alert?.let { dialogs[this ] = it }
132152 }
133-
153+
134154 return this
135155 }
136156}
0 commit comments