Skip to content

Commit 40876f9

Browse files
committed
Update
1 parent d95fb31 commit 40876f9

5 files changed

Lines changed: 210 additions & 146 deletions

File tree

app/src/main/java/com/omarea/common/ui/ProgressBarDialog.kt

Lines changed: 49 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import com.omarea.common.shell.AsynSuShellUnit
1515
open 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
}

app/src/main/java/com/omarea/krscript/ui/ActionListFragment.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class ActionListFragment : androidx.fragment.app.Fragment(), PageLayoutRender.On
4444

4545
private var actionInfos: ArrayList<NodeInfoBase>? = null
4646
private lateinit var progressBarDialog: ProgressBarDialog
47+
private var activeLoadJob: Job? = null
4748
private var krScriptActionHandler: KrScriptActionHandler? = null
4849
private var autoRunTask: AutoRunTask? = null
4950
private var themeMode: ThemeMode? = null
@@ -240,9 +241,12 @@ class ActionListFragment : androidx.fragment.app.Fragment(), PageLayoutRender.On
240241
separator = item.separator
241242
}
242243

244+
progressBarDialog.setCancelCallback {
245+
activeLoadJob?.cancel()
246+
}
243247
progressBarDialog.showDialog(getString(R.string.kr_param_options_load))
244248

245-
viewLifecycleOwner.lifecycleScope.launch(Dispatchers.IO) {
249+
activeLoadJob = viewLifecycleOwner.lifecycleScope.launch(Dispatchers.IO) {
246250
if (item.getState != null) {
247251
paramInfo.valueFromShell = executeScriptGetResult(item.getState!!, item)
248252
}
@@ -304,9 +308,12 @@ class ActionListFragment : androidx.fragment.app.Fragment(), PageLayoutRender.On
304308
val layoutInflater = LayoutInflater.from(requireContext())
305309
val linearLayout = layoutInflater.inflate(R.layout.kr_params_list, null) as LinearLayout
306310

311+
progressBarDialog.setCancelCallback {
312+
activeLoadJob?.cancel()
313+
}
307314
progressBarDialog.showDialog(getString(R.string.onloading))
308315

309-
viewLifecycleOwner.lifecycleScope.launch(Dispatchers.IO) {
316+
activeLoadJob = viewLifecycleOwner.lifecycleScope.launch(Dispatchers.IO) {
310317
for (param in actionParamInfos) {
311318
withContext(Dispatchers.Main) {
312319
progressBarDialog.showDialog(getString(R.string.kr_param_load) + (param.label ?: param.name))

app/src/main/java/com/tool/tree/ActionPage.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class ActionPage : AppCompatActivity() {
5656
private var menuOptions: ArrayList<PageMenuOption>? = null
5757
private var menuCheckboxRefreshing = false
5858
private var checkboxRefreshJob: Job? = null
59+
private var loadPageJob: Job? = null
5960

6061
override fun onCreate(savedInstanceState: Bundle?) {
6162
super.onCreate(savedInstanceState)
@@ -373,7 +374,13 @@ class ActionPage : AppCompatActivity() {
373374
private fun loadPageConfig(showLoading: Boolean = true) {
374375
val config = currentPageConfig ?: return
375376

376-
lifecycleScope.launch(Dispatchers.IO) {
377+
loadPageJob?.cancel()
378+
progressBarDialog.setCancelCallback {
379+
loadPageJob?.cancel()
380+
finish()
381+
}
382+
383+
loadPageJob = lifecycleScope.launch(Dispatchers.IO) {
377384
if (config.beforeRead.isNotEmpty()) {
378385
withContext(Dispatchers.Main) {
379386
progressBarDialog.showDialog(getString(R.string.kr_page_before_load))

app/src/main/java/com/tool/tree/TextEditorActivity.kt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,8 @@ class TextEditorActivity : AppCompatActivity() {
145145
filePath = extraFile
146146
configDir = intent.getStringExtra(EXTRA_DIR).orEmpty()
147147
absoluteFilePath = resolveAbsolutePath(configDir, filePath)
148-
149-
val extraTitle = intent.getStringExtra(EXTRA_TITLE).orEmpty()
150148
wrapEnabled = intent.getBooleanExtra(EXTRA_WRAP, true)
151149

152-
title = extraTitle.ifEmpty { File(absoluteFilePath).name }
153-
154150
applyWrapState()
155151
setupCursorAutoScroll()
156152
setupUndoRedoTracking()

0 commit comments

Comments
 (0)