Skip to content

Commit 19dc35a

Browse files
refactor: Improve list building and sorting logic
This commit refactors the list processing logic to improve its safety and efficiency. The building of the visible list and the mapping of pinned statuses are now performed in a single loop. This avoids potential `IndexOutOfBoundsException` errors that could occur if the sizes of the item list and the pinned status map were mismatched. The sorting logic remains the same but now operates on this more safely constructed list. Additionally, a check to skip duplicate items has been added within the loop.
1 parent ccafe0b commit 19dc35a

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

app/src/main/java/com/github/droidworksstudio/mlauncher/MainViewModel.kt

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -461,22 +461,28 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
461461
): MutableList<R> = withContext(Dispatchers.IO) {
462462

463463
val list = mutableListOf<R>()
464+
val pinnedStatusMap = mutableMapOf<R, Boolean>()
464465
val scrollMap = mutableMapOf<String, Int>()
465466

466-
// Build the visible list
467+
// Build the visible list AND pinned map together (this avoids mismatched sizes)
467468
items.forEach { raw ->
468469
val key = getKey(raw)
470+
471+
// Skip duplicates
469472
if (!seenKey.add(key)) return@forEach
473+
474+
// Skip hidden items unless included
470475
if (isHidden(raw) && !includeHidden) return@forEach
471-
list.add(buildItem(raw))
472-
}
473476

474-
// Build a map between built items and their pinned status
475-
val pinnedStatusMap = items.zip(list).associate { (item, built) ->
476-
built to isPinned(item)
477+
// Build the final item
478+
val built = buildItem(raw)
479+
list.add(built)
480+
481+
// Map pin state to the built item
482+
pinnedStatusMap[built] = isPinned(raw)
477483
}
478484

479-
// Sort safely: no index lookups during sorting
485+
// Stable, safe sorting — no null comparisons
480486
list.sortWith(
481487
compareByDescending<R> { pinnedStatusMap[it] == true }
482488
.thenBy { normalize(getLabel(it)) }

0 commit comments

Comments
 (0)