@@ -7,7 +7,6 @@ import android.view.View
77import android.widget.TextView
88import androidx.fragment.app.FragmentActivity
99import androidx.lifecycle.lifecycleScope
10- import com.omarea.common.model.SelectItem
1110import com.omarea.common.ui.AdapterAppChooser
1211import com.omarea.common.ui.DialogAppChooser
1312import com.omarea.krscript.R
@@ -32,11 +31,13 @@ class ParamsAppChooserRender(
3231 private lateinit var packages: ArrayList <AdapterAppChooser .AppInfo >
3332
3433 fun render (): View {
35- val layout = LayoutInflater .from(context).inflate(R .layout.kr_param_app, null )
34+ val layout = LayoutInflater .from(context)
35+ .inflate(R .layout.kr_param_app, null )
36+
3637 valueView = layout.findViewById(R .id.kr_param_app_package)
3738 nameView = layout.findViewById(R .id.kr_param_app_name)
3839
39- // giữ hành vi cũ
40+ // ✅ GIỮ HÀNH VI CŨ: KHÔNG LOAD PACKAGE Ở ĐÂY
4041 setTextView()
4142
4243 layout.findViewById<View >(R .id.kr_param_app_btn).setOnClickListener {
@@ -51,7 +52,7 @@ class ParamsAppChooserRender(
5152 }
5253
5354 // =======================
54- // OPEN DIALOG + LOAD ASYNC
55+ // OPEN DIALOG (KHÔNG BLOCK UI)
5556 // =======================
5657 private fun openAppChooser () {
5758 packages = ArrayList ()
@@ -63,15 +64,22 @@ class ParamsAppChooserRender(
6364 this
6465 )
6566
67+ // ✅ SHOW NGAY
6668 dialog.show(context.supportFragmentManager, " app-chooser" )
69+
70+ // ✅ LOAD SAU KHI DIALOG ĐÃ HIỆN
6771 loadPackagesAsync(dialog, actionParamInfo.type == " packages" )
6872 }
6973
74+ // =======================
75+ // LOAD PACKAGE ASYNC + BATCH
76+ // =======================
7077 private fun loadPackagesAsync (
7178 dialog : DialogAppChooser ,
7279 includeMissing : Boolean
7380 ) {
7481 val pm = context.packageManager
82+
7583 val filterSet = actionParamInfo.optionsFromShell
7684 ?.mapNotNull { it.value }
7785 ?.toHashSet()
@@ -87,13 +95,14 @@ class ParamsAppChooserRender(
8795
8896 if (filterSet == null || filterSet.contains(pkg)) {
8997 val info = AdapterAppChooser .AppInfo ().apply {
90- packageName = pkg // GIỮ NULL
98+ packageName = pkg // GIỮ NULL
9199 appName = app.loadLabel(pm)?.toString() // GIỮ NULL
92100 }
93101 result[pkg] = info
94102 batch.add(info)
95103 }
96104
105+ // đổ dần
97106 if (batch.size == 10 || index == apps.lastIndex) {
98107 val copy = ArrayList (batch)
99108 batch.clear()
@@ -106,7 +115,7 @@ class ParamsAppChooserRender(
106115 }
107116 }
108117
109- // thêm app bị thiếu (giữ nguyên hành vi cũ)
118+ // thêm app bị thiếu (giữ hành vi cũ)
110119 if (includeMissing && actionParamInfo.optionsFromShell != null ) {
111120 val missing = ArrayList <AdapterAppChooser .AppInfo >()
112121 for (item in actionParamInfo.optionsFromShell!! ) {
@@ -144,62 +153,47 @@ class ParamsAppChooserRender(
144153 packages.forEach { it.selected = false }
145154
146155 val currentValue = valueView.text.toString()
156+
147157 if (actionParamInfo.multiple) {
148- currentValue.split(actionParamInfo.separator).forEach { value ->
149- packages.find { it.packageName == value }?.selected = true
150- }
158+ currentValue
159+ .split(actionParamInfo.separator)
160+ .forEach { value ->
161+ packages.find { it.packageName == value }?.selected = true
162+ }
151163 } else {
152164 packages.find { it.packageName == currentValue }?.selected = true
153165 }
154166 }
155167
156168 // =======================
157- // INIT UI VALUE (GIỮ NGUYÊN HÀNH VI)
169+ // INIT UI VALUE (GIỮ NGUYÊN HÀNH VI CŨ )
158170 // =======================
159171 private fun setTextView () {
160- packages = loadPackages(actionParamInfo.type == " packages" )
161-
162- val packageMap = packages
163- .filter { it.packageName != null }
164- .associateBy { it.packageName!! }
165-
166172 if (actionParamInfo.multiple) {
167- ActionParamsLayoutRender .getParamValues(actionParamInfo)
168- ?.forEach { value ->
169- packageMap[value]?.selected = true
170- }
171- onConfirm(packages.filter { it.selected })
173+ val values = ActionParamsLayoutRender
174+ .getParamValues(actionParamInfo)
175+ ?.joinToString(actionParamInfo.separator)
176+ ? : " "
177+
178+ valueView.text = values
179+ nameView.text = values
172180 } else {
173- val validOptions = ArrayList <SelectItem >(packages.size)
174- packages.forEach {
175- validOptions.add(
176- SelectItem ().apply {
177- title = it.appName ? : " "
178- value = it.packageName ? : " "
179- }
180- )
181- }
181+ val value = ActionParamsLayoutRender
182+ .getParamValue(actionParamInfo)
183+ ? : " "
182184
183- val currentIndex =
184- ActionParamsLayoutRender .getParamOptionsCurrentIndex(
185- actionParamInfo,
186- validOptions
187- )
188-
189- if (currentIndex >= 0 ) {
190- val item = packages[currentIndex]
191- valueView.text = item.packageName ? : " "
192- nameView.text = item.appName ? : " "
193- } else {
194- valueView.text = " "
195- nameView.text = " "
196- }
185+ valueView.text = value
186+ nameView.text = value
197187 }
198188 }
199189
200190 // =======================
201- // GIỮ HÀM CŨ (FIX Unresolved reference )
191+ // HÀM CŨ – KHÔNG DÙNG (TRÁNH BLOCK UI )
202192 // =======================
193+ @Deprecated(
194+ message = " DO NOT USE - blocks UI thread. Use loadPackagesAsync instead" ,
195+ level = DeprecationLevel .WARNING
196+ )
203197 private fun loadPackages (includeMissing : Boolean ): ArrayList <AdapterAppChooser .AppInfo > {
204198 val pm = context.packageManager
205199 val list = ArrayList <AdapterAppChooser .AppInfo >()
0 commit comments