Skip to content

Commit d04d371

Browse files
Fix(AppDrawer): Resolve icon loading and sorting bugs
This commit addresses two key issues in the app drawer: 1. **Fixes an icon loading race condition:** * Previously, due to view recycling, fast scrolling could cause incorrect icons to be displayed for apps. * This is resolved by tagging each app view with its package name. An icon is now only applied if the view's tag matches the package name for which the icon was loaded, preventing mismatched icons. 2. **Corrects app list sorting with aliases:** * The app drawer now correctly sorts applications based on their assigned alias, not just their original label. * The scrollbar index has also been updated to use aliases, ensuring that tapping a letter in the scrollbar navigates to the correct position in the alphabetized list. Additionally, a minor bug in the app renaming logic has been fixed.
1 parent d7345d0 commit d04d371

File tree

2 files changed

+27
-11
lines changed

2 files changed

+27
-11
lines changed

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

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -625,24 +625,30 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
625625
category = raw.category
626626
)
627627
}
628-
// 🔹 Sort pinned apps first, then regular/recent alphabetically
629-
.sortedWith(
630-
compareByDescending<AppListItem> { it.category == AppCategory.PINNED }
631-
.thenBy { normalizeForSort(it.activityLabel) }
632-
)
633-
.toMutableList()
628+
// 1. Perform the sort using Alias priority
629+
val sortedApps = allApps.sortedWith(
630+
compareByDescending<AppListItem> { it.category == AppCategory.PINNED }
631+
.thenBy { item ->
632+
val alias = prefs.getAppAlias(item.activityPackage)
633+
val displayName = alias.takeIf { it.isNotBlank() } ?: item.activityLabel
634+
normalizeForSort(displayName)
635+
}
636+
).toMutableList()
634637

635-
// 🔹 Build scroll map and finalize
638+
// 2. Build the scroll map using the same Alias priority
636639
buildList(
637-
items = allApps,
640+
items = sortedApps, // Use the sorted list here
638641
seenKey = mutableSetOf(),
639642
scrollMapLiveData = _appScrollMap,
640643
includeHidden = includeHiddenApps,
641644
getKey = { "${it.activityPackage}|${it.activityClass}|${it.user.hashCode()}" },
642645
isHidden = { it.activityPackage in hiddenAppsSet },
643646
isPinned = { it.activityPackage in pinnedPackages },
644647
buildItem = { it },
645-
getLabel = { it.activityLabel },
648+
// UPDATE: Use the alias here so the scroll index matches the sort
649+
getLabel = { item ->
650+
prefs.getAppAlias(item.activityPackage).takeIf { it.isNotBlank() } ?: item.activityLabel
651+
},
646652
normalize = ::normalizeForSort
647653
)
648654
}

app/src/main/java/com/github/codeworkscreativehub/mlauncher/ui/adapter/AppDrawerAdapter.kt

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ class AppDrawerAdapter(
169169
appRenameListener(appModel.activityPackage, emptyString()) // empty string = default
170170
}
171171

172-
currentText == appModel.activityLabel -> { // Rename state
172+
currentText != appModel.activityLabel -> { // Rename state
173173
AppLogger.d("AppListDebug", "✏️ Renaming ${appModel.activityPackage} to $currentText")
174174
appRenameListener(appModel.activityPackage, currentText)
175175
}
@@ -474,6 +474,9 @@ class AppDrawerAdapter(
474474
setAppTitleIcon(appTitle, cachedIcon ?: placeholderIcon, prefs)
475475

476476
if (cachedIcon == null && packageName.isNotBlank() && prefs.iconPackAppList != Constants.IconPacks.Disabled) {
477+
// 1. Tag the view with the package name to prevent "wrong icon" bugs
478+
appTitle.tag = packageName
479+
477480
iconLoadingScope.launch {
478481
val icon = withContext(Dispatchers.IO) {
479482
val nonNullDrawable: Drawable = getSafeAppIcon(
@@ -485,8 +488,15 @@ class AppDrawerAdapter(
485488
)
486489
getSystemIcons(context, prefs, IconCacheTarget.APP_LIST, nonNullDrawable) ?: nonNullDrawable
487490
}
491+
492+
// 2. Update cache (Ensure iconCache is thread-safe, e.g., ConcurrentHashMap)
488493
iconCache[packageName] = icon
489-
if (appTitle.text == appListItem.activityLabel) setAppTitleIcon(appTitle, icon, prefs)
494+
495+
// 3. ONLY update the UI if the view is still intended for THIS package
496+
// This prevents the wrong icon from appearing after scrolling
497+
if (appTitle.tag == packageName) {
498+
setAppTitleIcon(appTitle, icon, prefs)
499+
}
490500
}
491501
}
492502

0 commit comments

Comments
 (0)