fix(prefs): reload the grid with the value just set (#7404)
* fix(prefs): reload the grid with the value just set Most grid preferences reload the grid in their onSet. onSet runs right after the DataStore write, but the device profiles read the preferences through PreferenceManager2's cache (firstCached), which is updated by its own collector on the main thread. That collector may not have run yet when the reload runs inline, so the grid could be rebuilt from the previous value: the change shows up only on the next reload, for example after the next change of any grid preference. ReloadHelper.reloadGrid now refreshes the cache from the DataStore, which already has the new value, before reloading the grid. This fixes every preference whose onSet calls reloadGrid, and the other callers (restart(), the folder settings), with no change at the call sites. The reload now always runs on the main thread, through PreferenceManager2's Main.immediate scope. Co-Authored-By: Claude <noreply@anthropic.com> (cherry picked from commit cbe549ade0bac9d655a965ed8f802c8c19573ef5) * fix(prefs): keep a newer cached value when the fresh read finishes late With two quick grid changes, the first refresh could finish after the collector had already stored the second value and put the older snapshot back. Store the read result only if the cache hasn't changed meanwhile. Suggested in review by CodeRabbit. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
1 parent
6c2c2c22ef
commit
d04b3b1c8d
2 files changed
+19
-1
No files matched your search
@@ -85,6 +85,7 @@ import kotlinx.coroutines.flow.first
|
||||
import kotlinx.coroutines.flow.launchIn
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.flow.onEach
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.runBlocking
|
||||
|
||||
@LauncherAppSingleton
|
||||
@@ -118,6 +119,21 @@ class PreferenceManager2 @Inject constructor(
|
||||
|
||||
fun getCachedPreferences(): Preferences = cachedPreferences
|
||||
|
||||
/**
|
||||
* Runs [block] on the main thread once the cache has every value written so far. A preference's
|
||||
* onSet runs right after its DataStore write, but the cache is updated by its own collector,
|
||||
* which may not have run yet: refresh it from the DataStore, which already has the new value.
|
||||
* If the collector stores a newer value while the read is suspended, that value is kept.
|
||||
*/
|
||||
internal fun withFreshCache(block: () -> Unit) {
|
||||
scope.launch {
|
||||
val cacheBeforeRead = cachedPreferences
|
||||
val freshPreferences = preferencesDataStore.data.first()
|
||||
if (cachedPreferences === cacheBeforeRead) cachedPreferences = freshPreferences
|
||||
block()
|
||||
}
|
||||
}
|
||||
|
||||
private val reloadHelper = ReloadHelper(context)
|
||||
|
||||
val darkStatusBar = preference(
|
||||
|
||||
@@ -33,7 +33,9 @@ class ReloadHelper(private val context: Context) {
|
||||
private val tisBinder = TISBindHelper(context) { tis = it }
|
||||
|
||||
fun reloadGrid() {
|
||||
idp.onPreferencesChanged(context)
|
||||
// The grid reads the preferences from the cache of PreferenceManager2, which may not have
|
||||
// the value just set yet.
|
||||
PreferenceManager2.getInstance(context).withFreshCache { idp.onPreferencesChanged(context) }
|
||||
}
|
||||
|
||||
fun recreate() {
|
||||
|
||||
Reference in new issue
Block a user