appstore: fix phantom F-Droid to Play updates on cold start
- Commit
3f46825ad50d8cae40f8f31669e15acbfd87d55f- Author
- vayun-mathur <[email protected]>
- When
- 2026-10-23 17:25 UTC (1 month ago)
- Parents
dfe073c
Diff
appstore/src/main/java/com/vayunmathur/appstore/data/AppDatabase.kt
diff --git a/appstore/src/main/java/com/vayunmathur/appstore/data/AppDatabase.kt b/appstore/src/main/java/com/vayunmathur/appstore/data/AppDatabase.ktindex 40ae8813cb..94f1d1c56d 100644--- a/appstore/src/main/java/com/vayunmathur/appstore/data/AppDatabase.kt+++ b/appstore/src/main/java/com/vayunmathur/appstore/data/AppDatabase.kt@@ -268,6 +268,18 @@ interface CachedAppDao { @Query("SELECT packageName, source, versionCode FROM CachedAppEntity") fun indexFlow(): Flow<List<PackageIndexRow>> + /**+ * Same rows as [indexFlow] as a one-shot read.+ *+ * The flow's [StateFlow] cache starts as an empty map until Room emits, so a cold-start+ * update check reading `.value` sees no packages and asks Play about every installed+ * app — including ones the offline catalogue lists. Those phantom Play rows then+ * vanish on the next refresh once the flow has populated. A direct query always+ * reflects what is actually cached.+ */+ @Query("SELECT packageName, source, versionCode FROM CachedAppEntity")+ suspend fun indexList(): List<PackageIndexRow>+ @Query("SELECT * FROM CachedAppEntity WHERE source = :source ORDER BY name ASC") fun bySourceFlow(source: String): Flow<List<CachedAppEntity>> @@ -331,6 +343,18 @@ interface CachedAppDao { @Upsert suspend fun upsert(app: CachedAppEntity) + /**+ * Replace one repo's rows atomically. Delete-then-upsert without a transaction+ * leaves a window where the index is empty and a concurrent update check sees no+ * packages — asking Play about every installed app, including ones this repo+ * lists. Those phantom Play rows then vanish on the next refresh.+ */+ @androidx.room3.Transaction+ suspend fun replaceByRepo(repoUrl: String, apps: List<CachedAppEntity>) {+ deleteByRepo(repoUrl)+ upsertAll(apps)+ }+ @Query("DELETE FROM CachedAppEntity WHERE repoUrl = :repoUrl") suspend fun deleteByRepo(repoUrl: String)
appstore/src/main/java/com/vayunmathur/appstore/data/CatalogRepository.kt
diff --git a/appstore/src/main/java/com/vayunmathur/appstore/data/CatalogRepository.kt b/appstore/src/main/java/com/vayunmathur/appstore/data/CatalogRepository.ktindex d2c96fd82a..092a6faa38 100644--- a/appstore/src/main/java/com/vayunmathur/appstore/data/CatalogRepository.kt+++ b/appstore/src/main/java/com/vayunmathur/appstore/data/CatalogRepository.kt@@ -175,7 +175,7 @@ class CatalogRepository( /** Rows for packages that are installed but older than what the catalogue offers. */ suspend fun updatesFor(installed: List<InstalledInfo>): List<UnifiedApp> {- val index = packageIndex.value+ val index = indexSnapshot() val stale = installed.filter { inst -> val known = index[inst.packageName] ?: return@filter false known.versionCode > inst.versionCode@@ -183,6 +183,20 @@ class CatalogRepository( return byPackages(stale).filterTv() } + /**+ * Package → what the catalogue knows about it, read straight from the DB.+ *+ * One-shot checks (update checks, Play candidate filtering) must use this, not+ * [packageIndex].value: the flow's StateFlow cache starts empty until Room emits,+ * so a cold-start check reading `.value` sees no packages and asks Play about+ * every installed app — including ones the offline catalogue lists. Those phantom+ * Play rows then vanish on the next refresh once the flow has populated.+ */+ suspend fun indexSnapshot(): Map<String, PackageIndexRow> = withContext(Dispatchers.IO) {+ runCatching { db.cachedAppDao().indexList().associateBy { it.packageName } }+ .getOrDefault(emptyMap())+ }+ /** * Refresh all offline sources. *
appstore/src/main/java/com/vayunmathur/appstore/data/FDroidAppProvider.kt
diff --git a/appstore/src/main/java/com/vayunmathur/appstore/data/FDroidAppProvider.kt b/appstore/src/main/java/com/vayunmathur/appstore/data/FDroidAppProvider.ktindex 5d3c9dc44d..b29b776751 100644--- a/appstore/src/main/java/com/vayunmathur/appstore/data/FDroidAppProvider.kt+++ b/appstore/src/main/java/com/vayunmathur/appstore/data/FDroidAppProvider.kt@@ -46,8 +46,7 @@ class FDroidAppProvider( return@withContext 0 } val repo = db.repoDao().all().find { it.url == descriptor.url }- db.cachedAppDao().deleteByRepo(descriptor.url)- db.cachedAppDao().upsertAll(result.apps.map { it.toEntity() })+ db.cachedAppDao().replaceByRepo(descriptor.url, result.apps.map { it.toEntity() }) db.repoDao().upsert( (repo ?: descriptor.toEntity()).copy( fingerprint = result.signerSha256,
appstore/src/main/java/com/vayunmathur/appstore/data/UpdateCheckWorker.kt
diff --git a/appstore/src/main/java/com/vayunmathur/appstore/data/UpdateCheckWorker.kt b/appstore/src/main/java/com/vayunmathur/appstore/data/UpdateCheckWorker.ktindex e3a1b8f34a..43980a3233 100644--- a/appstore/src/main/java/com/vayunmathur/appstore/data/UpdateCheckWorker.kt+++ b/appstore/src/main/java/com/vayunmathur/appstore/data/UpdateCheckWorker.kt@@ -150,7 +150,10 @@ class UpdateCheckWorker( ): List<UnifiedApp> { // Only Play can answer for packages the offline catalogues have never heard of. if (AppSource.PLAYSTORE !in enabled) return emptyList()- val index = env.catalog.packageIndex.value+ // Direct DB read, not packageIndex.value: the worker's flow has no collector yet+ // on a cold start, so .value would be empty and Play would be asked about every+ // installed app — including ones the offline catalogue lists.+ val index = env.catalog.indexSnapshot() val unknown = installed .filter { it.packageName !in index } .map { it.packageName }
appstore/src/main/java/com/vayunmathur/appstore/tv/TvStoreViewModel.kt
diff --git a/appstore/src/main/java/com/vayunmathur/appstore/tv/TvStoreViewModel.kt b/appstore/src/main/java/com/vayunmathur/appstore/tv/TvStoreViewModel.ktindex 7c041f764a..0cde2c602d 100644--- a/appstore/src/main/java/com/vayunmathur/appstore/tv/TvStoreViewModel.kt+++ b/appstore/src/main/java/com/vayunmathur/appstore/tv/TvStoreViewModel.kt@@ -435,7 +435,10 @@ class TvStoreViewModel( installedRepo.refresh() catalogUpdatesFlow.value = catalog.updatesFor(installedRepo.updatable.value) - val index = catalog.packageIndex.value+ // Direct DB read, not packageIndex.value: the flow's StateFlow cache starts+ // empty until Room emits, so a cold-start check reading .value sees no+ // packages and asks Play about every installed app.+ val index = catalog.indexSnapshot() val installed = installedRepo.updatable.value if (AppSource.PLAYSTORE in enabled) { val playCandidates = installed
appstore/src/main/java/com/vayunmathur/appstore/util/AppStoreUpdateOps.kt
diff --git a/appstore/src/main/java/com/vayunmathur/appstore/util/AppStoreUpdateOps.kt b/appstore/src/main/java/com/vayunmathur/appstore/util/AppStoreUpdateOps.ktindex 1b17cbbca9..98658c3cfe 100644--- a/appstore/src/main/java/com/vayunmathur/appstore/util/AppStoreUpdateOps.kt+++ b/appstore/src/main/java/com/vayunmathur/appstore/util/AppStoreUpdateOps.kt@@ -32,7 +32,11 @@ internal fun AppStoreViewModel.checkForUpdatesImpl() { // Only ask Play about packages neither offline source lists — for the rest the // catalogue already answered, and Play would just re-answer it over the network.- val index = catalog.packageIndex.value+ // A direct DB read, not packageIndex.value: the flow's StateFlow cache starts+ // empty until Room emits, so a cold-start check reading .value sees no packages+ // and asks Play about every installed app — including ones the offline+ // catalogue lists. Those phantom Play rows then vanish on the next refresh.+ val index = catalog.indexSnapshot() val installed = installedRepo.updatable.value if (AppSource.PLAYSTORE in enabled) { val playCandidates = installed
appstore/src/main/java/com/vayunmathur/appstore/util/AppStoreViewModel.kt
diff --git a/appstore/src/main/java/com/vayunmathur/appstore/util/AppStoreViewModel.kt b/appstore/src/main/java/com/vayunmathur/appstore/util/AppStoreViewModel.ktindex ffa8de598d..8bb8565221 100644--- a/appstore/src/main/java/com/vayunmathur/appstore/util/AppStoreViewModel.kt+++ b/appstore/src/main/java/com/vayunmathur/appstore/util/AppStoreViewModel.kt@@ -458,7 +458,10 @@ class AppStoreViewModel( playInstalledPackagesFlow.value = emptySet() return }- val index = catalog.packageIndex.value+ // Direct DB read, not packageIndex.value: the flow's StateFlow cache starts empty+ // until Room emits, so a cold-start read of .value would ask Play about every+ // installed package, including ones the offline catalogue already attributes.+ val index = catalog.indexSnapshot() val candidates = installedRepo.apps.value .map { it.packageName } .filter { it !in index }