@@ -43,6 +43,9 @@ class ActionPage : AppCompatActivity() {
4343 private lateinit var binding: ActivityActionPageBinding
4444 private var openedSubPage = false
4545
46+ // Lưu danh sách các itemId (index) vừa mới được click để tạm chặn việc script ghi đè sai trạng thái
47+ private val justClickedItems = HashSet <Int >()
48+
4649 override fun onCreate (savedInstanceState : Bundle ? ) {
4750 super .onCreate(savedInstanceState)
4851
@@ -183,19 +186,19 @@ class ActionPage : AppCompatActivity() {
183186 return super .onPrepareOptionsMenu(menu)
184187 }
185188
186- // SỬA LỖI LOẠN VỊ TRÍ: Ánh xạ chính xác bằng originalIndex gốc của mảng menuOptions
187189 private fun refreshCheckboxMenuStates () {
188190 val config = currentPageConfig ? : return
189191
190- // Tạo một danh sách Pair chứa cặp (Vị trí gốc trong menuOptions, Đối tượng Option)
191192 val checkboxOptionsWithIndex = menuOptions?.mapIndexed { index, option -> index to option }
192- ?.filter { (_, option) -> option.type == " checkbox" && option.checkedSh.isNotEmpty() }
193+ ?.filter { (index, option) ->
194+ // SỬA: Nếu checkbox này vừa mới được click và đang đợi script chạy, TẠM THỜI bỏ qua không quét từ hệ thống nữa
195+ option.type == " checkbox" && option.checkedSh.isNotEmpty() && ! justClickedItems.contains(index)
196+ }
193197
194198 if (checkboxOptionsWithIndex.isNullOrEmpty() || menuCheckboxRefreshing) return
195199 menuCheckboxRefreshing = true
196200
197201 lifecycleScope.launch(Dispatchers .IO ) {
198- // Thực thi script và giữ nguyên vị trí index gốc ban đầu
199202 val results = checkboxOptionsWithIndex.map { (originalIndex, option) ->
200203 val result = ScriptEnvironmen .executeResultRoot(this @ActionPage, option.checkedSh, config)?.trim()
201204 Triple (originalIndex, option, result)
@@ -206,9 +209,12 @@ class ActionPage : AppCompatActivity() {
206209 var changed = false
207210
208211 results.forEach { (originalIndex, option, result) ->
209- val newChecked = result == " 1" || result?.lowercase() == " true"
210- if (option.checked != newChecked) changed = true
211- option.checked = newChecked
212+ // Kiểm tra lại một lần nữa trước khi ghi đè dữ liệu lên option nhằm tránh xung đột chạy song song
213+ if (! justClickedItems.contains(originalIndex)) {
214+ val newChecked = result == " 1" || result?.lowercase() == " true"
215+ if (option.checked != newChecked) changed = true
216+ option.checked = newChecked
217+ }
212218 }
213219
214220 if (changed && ! isFinishing) {
@@ -240,6 +246,14 @@ class ActionPage : AppCompatActivity() {
240246 if (option.type == " checkbox" ) {
241247 option.checked = ! option.checked
242248 item.isChecked = option.checked
249+
250+ // Đánh dấu ô này vừa click xong để lock (khóa) trạng thái, tránh bị hàm check đồng bộ đè dữ liệu cũ lên
251+ justClickedItems.add(index)
252+
253+ // Mở khóa sau 1.5 giây - khoảng thời gian dư dả để lệnh Shell cũ thực thi xong hoàn toàn
254+ handler.postDelayed({
255+ justClickedItems.remove(index)
256+ }, 1500 )
243257 }
244258
245259 onMenuItemClick(option)
@@ -498,6 +512,7 @@ class ActionPage : AppCompatActivity() {
498512 }
499513
500514 override fun onDestroy () {
515+ handler.removeCallbacksAndMessages(null )
501516 setExcludeFromRecents()
502517 super .onDestroy()
503518 }
0 commit comments