Modern Apps git

← Modern-Apps.git

launcher-wear: duplicate-package favorites collapse to one row per package

Commit
ec6a30a3aee39a14eabf08a64cd9a4eb0f2fa491
Author
Vayun Mathur <[email protected]>
When
2026-12-01 10:54 UTC (5 days ago)
Parents
ae9627b
buildWearLauncherSections sorted the snapshot's favorites
by rank with no package dedup, trusting producer-side
uniqueness of decoded/persisted JSON — while the installed
list directly above dedupes for exactly this invariant. A
snapshot carrying one package twice (different components)
produced two favorite rows for one package, and the screen
keys favorites by packageName: duplicate Lazy keys (Compose
throws) or the same app twice. The missing-favorites and
empty-installed paths passed duplicates through raw too.

The section builder now enforces its own one-row-per-package
invariant: rank-sorted distinctBy package on the normal path
(lowest rank wins, mirroring the phone builder; exact-
component preference preserved), and distinctBy on the
empty-installed path. Three regression tests cover all three
paths. No protocol change. Verified: :launcher:wear:
testDebugUnitTest 15 tests and lint BUILD SUCCESSFUL.

Diff

launcher-wear/src/main/java/com/vayunmathur/launcher/wear/domain/WearLauncherModel.kt
diff --git a/launcher-wear/src/main/java/com/vayunmathur/launcher/wear/domain/WearLauncherModel.kt b/launcher-wear/src/main/java/com/vayunmathur/launcher/wear/domain/WearLauncherModel.ktindex e21f7bb7e9..ba52f305c9 100644--- a/launcher-wear/src/main/java/com/vayunmathur/launcher/wear/domain/WearLauncherModel.kt+++ b/launcher-wear/src/main/java/com/vayunmathur/launcher/wear/domain/WearLauncherModel.kt@@ -46,6 +46,8 @@ data class WearLauncherSections(  * enumeration and icon loading stay in `platform/`.  *  * - Duplicate packages keep the first label-sorted component.+ * - Duplicate favorite packages keep the lowest rank: the snapshot is+ *   decoded/persisted JSON, so its package uniqueness is not assumed here.  * - The launcher itself ([ownPackageName]) is excluded: it is the surface.  * - A favorite matches by package, preferring its exact component.  * - Recents exclude favorites (a pinned app needs no second row) and cap@@ -66,12 +68,14 @@ fun buildWearLauncherSections(     // The drawer shows one row per package; favorites may name any component.     val apps = sorted.distinctBy { it.packageName }     if (apps.isEmpty()) {-        return WearLauncherSections(missingFavorites = snapshot.favorites)+        return WearLauncherSections(+            missingFavorites = snapshot.favorites.distinctBy { it.packageName },+        )     }     val byPackage = apps.associateBy { it.packageName }     val favorites = mutableListOf<WatchApp>()     val missing = mutableListOf<WearFavorite>()-    snapshot.favorites.sortedBy { it.rank }.forEach { favorite ->+    snapshot.favorites.sortedBy { it.rank }.distinctBy { it.packageName }.forEach { favorite ->         val exact = sorted.firstOrNull {             it.packageName == favorite.packageName && it.className == favorite.className         }
launcher-wear/src/test/java/com/vayunmathur/launcher/wear/domain/WearLauncherModelTest.kt
diff --git a/launcher-wear/src/test/java/com/vayunmathur/launcher/wear/domain/WearLauncherModelTest.kt b/launcher-wear/src/test/java/com/vayunmathur/launcher/wear/domain/WearLauncherModelTest.ktindex 3b412692c5..ea06a69577 100644--- a/launcher-wear/src/test/java/com/vayunmathur/launcher/wear/domain/WearLauncherModelTest.kt+++ b/launcher-wear/src/test/java/com/vayunmathur/launcher/wear/domain/WearLauncherModelTest.kt@@ -71,6 +71,63 @@ class WearLauncherModelTest {         assertEquals("com.example.multi.Second", sections.favorites.single().className)     } +    @Test+    fun duplicateFavoritePackagesCollapseToLowestRank() {+        val two = listOf(+            WatchApp("com.example.multi", "com.example.multi.Second", "Multi"),+            WatchApp("com.example.multi", "com.example.multi.First", "Multi"),+        )+        val sections = buildWearLauncherSections(+            two,+            WearFavoritesSnapshot(+                1L,+                listOf(+                    WearFavorite("com.example.multi", "com.example.multi.Second", "Multi", 1),+                    WearFavorite("com.example.multi", "com.example.multi.First", "Multi", 0),+                ),+            ),+            emptyList(),+            ownPackageName = "com.vayunmathur.launcher",+        )+        // One row per package (the UI keys favorite rows by package), lowest rank wins.+        assertEquals(listOf("com.example.multi.First"), sections.favorites.map { it.className })+        assertTrue(sections.missingFavorites.isEmpty())+    }++    @Test+    fun duplicateMissingFavoritePackagesCollapseToLowestRank() {+        val sections = buildWearLauncherSections(+            installed,+            WearFavoritesSnapshot(+                1L,+                listOf(+                    WearFavorite("com.example.absent", "com.example.absent.Other", "Absent other", 1),+                    WearFavorite("com.example.absent", "", "Absent", 0),+                ),+            ),+            emptyList(),+            ownPackageName = "com.vayunmathur.launcher",+        )+        assertEquals(listOf("Absent"), sections.missingFavorites.map { it.label })+    }++    @Test+    fun duplicateMissingFavoritesCollapseWhenNothingInstalled() {+        val sections = buildWearLauncherSections(+            emptyList(),+            WearFavoritesSnapshot(+                1L,+                listOf(+                    WearFavorite("com.example.absent", "", "Absent", 0),+                    WearFavorite("com.example.absent", "", "Absent duplicate", 1),+                ),+            ),+            emptyList(),+            ownPackageName = "com.vayunmathur.launcher",+        )+        assertEquals(listOf("com.example.absent"), sections.missingFavorites.map { it.packageName })+    }+     @Test     fun capabilityAbsentYieldsEmptySectionsExceptMissing() {         val sections = buildWearLauncherSections(