Modern Apps git

← Modern-Apps.git

news: move retired prefs keys out of NewsPrefs

Commit
a778940409369b9bd06c9fbf3a1c3920d89da650
Author
Vayun Mathur <[email protected]>
When
2026-12-05 03:19 UTC (1 day ago)
Parents
68dd663
NewsPrefsTest.noFeedSortPreferenceExists failed on main because
LEGACY_FEED_SORT, a deletion-only key for the removed feed-sort
feature, was declared in the NewsPrefs companion. Reflection over
every NewsPrefs member therefore found sort state, even though no
feed-sort preference exists: the key is never read or written, only
removed on upgrade by clearLegacySourcePrefs.

Move both retired keys (disabled_sources and feed_sort, with their
stored names and types unchanged) into a new internal
NewsLegacyPrefs object in data/, documented as not-preferences that
exist only as deletion targets. NewsPrefs remains the app's sole
DataStore opener and still removes both keys, so upgrade cleanup is
unchanged; it now declares only current preferences, which is what
the test asserts. The test itself is unchanged.

Verified: ./gradlew :news:lint :news:lintVitalRelease
:news:testDevUnitTest fully green (NewsPrefsTest 4/4, including
noFeedSortPreferenceExists and the legacy-cleanup test).

Diff

news/src/main/java/com/vayunmathur/news/data/NewsLegacyPrefs.kt
diff --git a/news/src/main/java/com/vayunmathur/news/data/NewsLegacyPrefs.kt b/news/src/main/java/com/vayunmathur/news/data/NewsLegacyPrefs.ktnew file mode 100644index 0000000000..eb0e8ca126--- /dev/null+++ b/news/src/main/java/com/vayunmathur/news/data/NewsLegacyPrefs.kt@@ -0,0 +1,18 @@+package com.vayunmathur.news.data++import androidx.datastore.preferences.core.stringPreferencesKey+import androidx.datastore.preferences.core.stringSetPreferencesKey++/**+ * Retired preference keys from removed features. These are not+ * preferences: no code reads their values or writes new ones. They+ * exist only so [NewsPrefs.clearLegacySourcePrefs] — the app's sole+ * DataStore opener — can delete any value an old install left behind+ * under these names on upgrade. Keeping them here, outside the+ * current-preferences owner, means [NewsPrefs] exposes no state for+ * features that no longer exist.+ */+internal object NewsLegacyPrefs {+    val DISABLED_SOURCES = stringSetPreferencesKey("disabled_sources")+    val FEED_SORT = stringPreferencesKey("feed_sort")+}
news/src/main/java/com/vayunmathur/news/data/NewsPrefs.kt
diff --git a/news/src/main/java/com/vayunmathur/news/data/NewsPrefs.kt b/news/src/main/java/com/vayunmathur/news/data/NewsPrefs.ktindex 90c40e61af..67f74fd4cb 100644--- a/news/src/main/java/com/vayunmathur/news/data/NewsPrefs.kt+++ b/news/src/main/java/com/vayunmathur/news/data/NewsPrefs.kt@@ -5,7 +5,6 @@ import androidx.datastore.preferences.core.booleanPreferencesKey import androidx.datastore.preferences.core.edit import androidx.datastore.preferences.core.intPreferencesKey import androidx.datastore.preferences.core.stringPreferencesKey-import androidx.datastore.preferences.core.stringSetPreferencesKey import androidx.datastore.preferences.preferencesDataStore import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.catch@@ -28,10 +27,11 @@ private val Context.newsFeedCacheDataStore by preferencesDataStore(name = "news_  * catch in [safeData].  *  * There is no per-source enable/disable any more: every source is- * always in the corpus, and categories + search are the filters. The- * legacy `disabled_sources` key (and the stale `reader_web_view` value)- * is never read; [clearLegacySourcePrefs] removes it when present so- * stale values cannot linger or crash anything.+ * always in the corpus, and categories + search are the filters.+ * Retired keys (see [NewsLegacyPrefs]) are never read;+ * [clearLegacySourcePrefs] removes them when present so stale values+ * cannot linger or crash anything. Only current preferences are+ * declared here; retired keys live with their cleanup owner.  */ class NewsPrefs(context: Context) {     private val appContext = context.applicationContext@@ -78,8 +78,10 @@ class NewsPrefs(context: Context) {     /** Drops retired preference state, if an old install left any. */     suspend fun clearLegacySourcePrefs() {         appContext.newsDataStore.edit { prefs ->-            if (prefs.contains(LEGACY_DISABLED_SOURCES)) prefs.remove(LEGACY_DISABLED_SOURCES)-            if (prefs.contains(LEGACY_FEED_SORT)) prefs.remove(LEGACY_FEED_SORT)+            if (prefs.contains(NewsLegacyPrefs.DISABLED_SOURCES)) {+                prefs.remove(NewsLegacyPrefs.DISABLED_SOURCES)+            }+            if (prefs.contains(NewsLegacyPrefs.FEED_SORT)) prefs.remove(NewsLegacyPrefs.FEED_SORT)         }     } @@ -103,8 +105,6 @@ class NewsPrefs(context: Context) {         const val DEFAULT_TEXT_SP = 17         const val MIN_TEXT_SP = 13         const val MAX_TEXT_SP = 24-        private val LEGACY_DISABLED_SOURCES = stringSetPreferencesKey("disabled_sources")-        private val LEGACY_FEED_SORT = stringPreferencesKey("feed_sort")         private val LOCAL_LOCATION = stringPreferencesKey("local_location")         private val SERVER_FEED_STAMP = stringPreferencesKey("server_feed_stamp")         private val TEXT_SP = intPreferencesKey("reader_text_sp")