appstore: gate library and updates on enabled sources (#845)
- Commit
ff5c891b39284131a9d538fdc9397d22cffc5398- Author
- vayun-mathur <[email protected]>
- When
- 2026-10-22 02:44 UTC (1 month ago)
- Parents
35642d7
Diff
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 79cbe4e6dc..e3a1b8f34a 100644--- a/appstore/src/main/java/com/vayunmathur/appstore/data/UpdateCheckWorker.kt+++ b/appstore/src/main/java/com/vayunmathur/appstore/data/UpdateCheckWorker.kt@@ -102,6 +102,9 @@ class UpdateCheckWorker( ) val enabled = env.settings.readEnabledSources() + // Drop disabled sources' cached rows before the index is read: the last sync+ // may predate the toggle, and nothing else purges on this path (#845).+ env.catalog.purgeDisabled(enabled) env.catalog.sync(enabled) env.installedRepo.refresh() if (AppSource.PLAYSTORE in enabled) env.play.restore()
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 0351d7a046..7c041f764a 100644--- a/appstore/src/main/java/com/vayunmathur/appstore/tv/TvStoreViewModel.kt+++ b/appstore/src/main/java/com/vayunmathur/appstore/tv/TvStoreViewModel.kt@@ -195,6 +195,10 @@ class TvStoreViewModel( init { viewModelScope.launch { enabledSourcesFlow.value = settings.readEnabledSources()+ // Drop disabled sources' cached rows before anything reads the catalogue:+ // sync only purges when it runs, so a disabled offline source's rows would+ // otherwise survive every restart underneath the toggle (#845).+ catalog.purgeDisabled(enabledSourcesFlow.value) installedRepo.refresh() if (AppSource.PLAYSTORE in enabledSourcesFlow.value) play.restore() syncIfNeverSynced()@@ -441,6 +445,9 @@ class TvStoreViewModel( playUpdatesFlow.value = installed.mapNotNull { inst -> remote[inst.packageName]?.takeIf { it.versionCode > inst.versionCode } }+ } else {+ // A check with Play off must not leave a stale snapshot behind (#845).+ playUpdatesFlow.value = emptyList() } if (AppSource.ACCRESCENT in enabled) {@@ -449,6 +456,8 @@ class TvStoreViewModel( accrescentUpdatesFlow.value = installed .filter { it.packageName in accrescentIds } .mapNotNull { inst -> accrescentUpdate(inst.packageName, inst.versionCode) }+ } else {+ accrescentUpdatesFlow.value = emptyList() } lastUpdateCheckFlow.value = System.currentTimeMillis()
appstore/src/main/java/com/vayunmathur/appstore/util/AppStoreHomeOps.kt
diff --git a/appstore/src/main/java/com/vayunmathur/appstore/util/AppStoreHomeOps.kt b/appstore/src/main/java/com/vayunmathur/appstore/util/AppStoreHomeOps.ktindex 13c6f75ac2..9e12452629 100644--- a/appstore/src/main/java/com/vayunmathur/appstore/util/AppStoreHomeOps.kt+++ b/appstore/src/main/java/com/vayunmathur/appstore/util/AppStoreHomeOps.kt@@ -185,7 +185,9 @@ internal fun AppStoreViewModel.syncIfNeverSynced(enabled: Set<AppSource>) { fun AppStoreViewModel.syncSources() { if (isSyncingFlow.value) return viewModelScope.launch {- val enabled = enabledSources.value+ // The persisted choice, not the flow's optimistic default: a manual sync fired+ // on a cold start must not re-fetch a source the user switched off (#845).+ val enabled = settings.readEnabledSources() isSyncingFlow.value = true val report = catalog.sync(enabled) { step -> statusMessageFlow.value = context.getString(
appstore/src/main/java/com/vayunmathur/appstore/util/AppStoreSearchOps.kt
diff --git a/appstore/src/main/java/com/vayunmathur/appstore/util/AppStoreSearchOps.kt b/appstore/src/main/java/com/vayunmathur/appstore/util/AppStoreSearchOps.ktindex d5afbf2015..5f0863165b 100644--- a/appstore/src/main/java/com/vayunmathur/appstore/util/AppStoreSearchOps.kt+++ b/appstore/src/main/java/com/vayunmathur/appstore/util/AppStoreSearchOps.kt@@ -29,11 +29,15 @@ internal fun AppStoreViewModel.setSearchImpl(query: String) { delay(SEARCH_DEBOUNCE_MS) isSearchingFlow.value = true searchPlayErrorFlow.value = ""- val enabled = enabledSources.value+ // The persisted choice, not the flow's optimistic default: a search fired on a+ // cold start must not query a source the user switched off (#845).+ val enabled = settings.readEnabledSources() // Local first and published immediately: the F-Droid catalogue is on disk, so // there is no reason to make the user wait on Play before seeing anything.- val local = catalog.searchLocal(query)+ // Filtered to enabled sources: the Room cache can still hold a disabled+ // source's rows before the startup purge lands.+ val local = catalog.searchLocal(query).filter { it.source in enabled } searchResultsFlow.value = rank(local, query) // Accrescent search is client-side over the listings already cached from the home
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 dec4a7a2cc..1b17cbbca9 100644--- a/appstore/src/main/java/com/vayunmathur/appstore/util/AppStoreUpdateOps.kt+++ b/appstore/src/main/java/com/vayunmathur/appstore/util/AppStoreUpdateOps.kt@@ -20,7 +20,10 @@ internal fun AppStoreViewModel.checkForUpdatesImpl() { if (isCheckingUpdatesFlow.value) return viewModelScope.launch { try {- val enabled = enabledSources.value+ // The persisted choice, not the flow's optimistic default: this runs from+ // HomePage on every cold start, before DataStore has delivered the stored+ // value, and the default has every source switched on (#845).+ val enabled = settings.readEnabledSources() isCheckingUpdatesFlow.value = true statusMessageFlow.value = context.getString(R.string.updates_checking) @@ -43,6 +46,10 @@ internal fun AppStoreViewModel.checkForUpdatesImpl() { // The same response tells us which of these packages Play actually hosts, which // the library uses to tell a genuine Play app from a sideloaded one. if (remote.isNotEmpty()) playInstalledPackagesFlow.value = remote.keys.toSet()+ } else {+ // A cold-start check with Play off must not leave a stale snapshot behind.+ playUpdatesFlow.value = emptyList()+ playInstalledPackagesFlow.value = emptySet() } // Accrescent: refresh the signed allowlist, then ask its API for a newer build of@@ -54,6 +61,9 @@ internal fun AppStoreViewModel.checkForUpdatesImpl() { accrescentUpdatesFlow.value = installed .filter { it.packageName in accrescentIds } .mapNotNull { inst -> accrescentUpdate(inst.packageName, inst.versionCode) }+ } else {+ accrescentUpdatesFlow.value = emptyList()+ accrescentPackagesFlow.value = emptySet() } lastUpdateCheckFlow.value = System.currentTimeMillis()
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 2c17fe32d8..0d1debed50 100644--- a/appstore/src/main/java/com/vayunmathur/appstore/util/AppStoreViewModel.kt+++ b/appstore/src/main/java/com/vayunmathur/appstore/util/AppStoreViewModel.kt@@ -166,13 +166,23 @@ class AppStoreViewModel( }.stateIn(viewModelScope, SharingStarted.Eagerly, "") val updates: StateFlow<List<UnifiedApp>> = combine(- catalogUpdatesFlow,- playUpdatesFlow,- accrescentUpdatesFlow,+ combine(+ catalogUpdatesFlow,+ playUpdatesFlow,+ accrescentUpdatesFlow,+ ) { catalogUpdates, playUpdates, accrescentUpdates ->+ catalogUpdates + playUpdates + accrescentUpdates+ }, installedRepo.apps,- ) { catalogUpdates, playUpdates, accrescentUpdates, installed ->+ enabledSources,+ ) { pending, installed, enabled -> val installedVersions = installed.associate { it.packageName to it.versionCode }- (catalogUpdates + playUpdates + accrescentUpdates)+ pending+ // A disabled source contributes nothing, even if its in-memory snapshot has not+ // been cleared yet (issue #845): the toggle clears Play/Accrescent snapshots+ // via forgetSource, but a cold start can repopulate them before the persisted+ // choice arrives, so the merged list filters rather than trusting them.+ .filter { it.source in enabled } // The surviving row's source decides which download-and-verify path the update // takes, so it has to be the same precedence search and the library use. .sortedBy { it.source.priority }@@ -303,21 +313,38 @@ class AppStoreViewModel( val library: StateFlow<LibraryUiState> = combine( chrome,- catalog.packageIndex,- playInstalledPackagesFlow,- accrescentPackagesFlow,- libraryFilterFlow,- ) { rows, index, playPackages, accrescentPackages, filter ->+ combine(+ catalog.packageIndex,+ playInstalledPackagesFlow,+ accrescentPackagesFlow,+ libraryFilterFlow,+ enabledSources,+ ) { index, playPackages, accrescentPackages, filter, enabled ->+ LibraryInputs(index, playPackages, accrescentPackages, filter, enabled)+ },+ ) { rows, inputs ->+ val (index, playPackages, accrescentPackages, filter, enabled) = inputs // Source attribution, highest priority first: // 1. Whatever the offline catalogue (F-Droid / Modern Apps / Proprietary) recorded. // 2. Accrescent, for packages its signed allowlist vouches for. // 3. Play, but only for packages Play confirmed it hosts (see playInstalledPackagesFlow). // A package no source vouches for — sideloaded, or from a store we don't track — is // left out entirely rather than mislabelled as Play.- fun sourceOf(pkg: String): AppSource? =+ //+ // Every branch is gated on the source still being enabled (issue #845): the Room+ // cache can still hold a disabled offline source's rows on a cold start before+ // purgeDisabled runs, and the Play/Accrescent snapshots are in-memory.+ fun sourceOf(pkg: String): AppSource? { index[pkg]?.source?.let { runCatching { AppSource.valueOf(it) }.getOrNull() }- ?: AppSource.ACCRESCENT.takeIf { pkg in accrescentPackages }- ?: AppSource.PLAYSTORE.takeIf { pkg in playPackages }+ ?.takeIf { it in enabled }?.let { return it }+ if (AppSource.ACCRESCENT in enabled && pkg in accrescentPackages) {+ return AppSource.ACCRESCENT+ }+ if (AppSource.PLAYSTORE in enabled && pkg in playPackages) {+ return AppSource.PLAYSTORE+ }+ return null+ } val all = rows.installed.mapNotNull { info -> sourceOf(info.packageName)?.let { info.toUnifiedApp(it) }@@ -340,6 +367,11 @@ class AppStoreViewModel( // user switched off must not be contacted once on every cold start while the // stored value is still on its way. val enabled = settings.readEnabledSources()+ // Drop disabled sources' cached rows before anything reads the catalogue:+ // sync only purges when it runs, and a non-empty cache skips the startup+ // sync entirely, so without this a disabled offline source's rows survive+ // every restart and keep feeding search, updates and the library (#845).+ catalog.purgeDisabled(enabled) installedRepo.refresh() if (AppSource.PLAYSTORE in enabled) play.restore() loadHome(enabled)@@ -578,6 +610,14 @@ class AppStoreViewModel( val stages: Map<String, InstallStage> = emptyMap(), ) + private data class LibraryInputs(+ val index: Map<String, com.vayunmathur.appstore.data.PackageIndexRow> = emptyMap(),+ val playPackages: Set<String> = emptySet(),+ val accrescentPackages: Set<String> = emptySet(),+ val filter: SourceFilter = SourceFilter.ALL,+ val enabled: Set<AppSource> = emptySet(),+ )+ private data class HomeChrome( val updateCount: Int, val isSyncing: Boolean,