Look the provider up once per settings or lists read, forget deleted lists in one pass (#100, #77)

This commit is contained in:
makiolaj committed 2026-11-20 12:58:05 +01:00
1 parent 8cc9567984
commit a5accc5fef
4 files changed
+103 -29

No files matched your search

@@ -370,15 +370,19 @@ class SettingsPrefs @Inject constructor(
* keyed by bare id, shared by every store, so they stay.
*/
suspend fun forgetDeletedLists(listIds: Collection<Long>, store: StorageMode) = dataStore.edit { p ->
val mode = claimedScope(p, store)
listIds.forEach { listId ->
p[LIST_PERSISTENT_REMINDERS] = p[LIST_PERSISTENT_REMINDERS].orEmpty()
.filterNotTo(HashSet()) { it.startsWith(listPersistencePrefix(mode, listId)) }
p[SMART_LIST_HIDDEN] = p[SMART_LIST_HIDDEN].orEmpty() - "$mode:$listId"
val key = scopeListKey("${TaskGroupKey.LIST_PREFIX}$listId", mode)
p[GROUPINGS]?.let { all -> p[GROUPINGS] = all.filterTo(HashSet()) { it.substringAfter(GROUPING_SEPARATOR) != key } }
p[COLLAPSED_SECTIONS]?.let { p[COLLAPSED_SECTIONS] = it - key }
val mode = claimedScope(p, withAuthority(store))
val prefixes = listIds.map { listPersistencePrefix(mode, it) }
val hidden = listIds.mapTo(HashSet()) { "$mode:$it" }
val keys = listIds.mapTo(HashSet()) { scopeListKey("${TaskGroupKey.LIST_PREFIX}$it", mode) }
fun drop(key: Preferences.Key<Set<String>>, gone: (String) -> Boolean) {
val entries = p[key] ?: return
val kept = entries.filterNotTo(HashSet(), gone)
if (kept.size != entries.size) p[key] = kept
}
drop(LIST_PERSISTENT_REMINDERS) { entry -> prefixes.any(entry::startsWith) }
drop(SMART_LIST_HIDDEN) { it in hidden }
drop(GROUPINGS) { it.substringAfter(GROUPING_SEPARATOR) in keys }
drop(COLLAPSED_SECTIONS) { it in keys }
}
/**
@@ -388,7 +392,7 @@ class SettingsPrefs @Inject constructor(
* overrides keyed by bare id, stay.
*/
suspend fun forgetListsExcept(store: StorageMode, authority: String?, present: Set<Long>) = dataStore.edit { p ->
val mode = claimedScope(p, store, authority)
val mode = claimedScope(p, store to authority)
fun prune(key: Preferences.Key<Set<String>>, kind: ListEntry) {
val entries = p[key] ?: return
val kept = withoutGoneLists(entries, kind, mode, present)
@@ -464,18 +468,31 @@ class SettingsPrefs @Inject constructor(
/** The store list ids belong to; each store numbers its lists on its own. */
private fun modeOf(p: Preferences): StorageMode = storedMode(p[STORAGE_MODE]) ?: resolver.autoMode()
/** What [store]'s list entries are stored under; see [listScope]. */
private fun scopeOf(p: Preferences, store: StorageMode = modeOf(p), authority: String? = authorityOf(store)): String =
listScope(store, authority, p[EXTERNAL_LISTS_OWNER])
/** [scopeOf], first making [authority] the owner of the bare external entries if none is yet. */
private fun claimedScope(p: MutablePreferences, store: StorageMode = modeOf(p), authority: String? = authorityOf(store)): String {
if (store == StorageMode.EXTERNAL && authority != null && p[EXTERNAL_LISTS_OWNER] == null) p[EXTERNAL_LISTS_OWNER] = authority
return scopeOf(p, store, authority)
/**
* What the current store's list entries are stored under; see [listScope].
* Asks the resolver, a PackageManager lookup, only when the answer depends
* on it: every write to this file maps the settings again.
*/
private fun scopeOf(p: Preferences): String {
val stored = storedMode(p[STORAGE_MODE])
val owner = p[EXTERNAL_LISTS_OWNER]
if (stored == StorageMode.OWN || (stored == StorageMode.EXTERNAL && owner == null)) return stored.name
val (store, authority) = resolver.active(stored)
return listScope(store, authority, owner)
}
private fun authorityOf(store: StorageMode): String? =
if (store == StorageMode.EXTERNAL) resolver.resolveExternal()?.authority else null
/** The store [p] names, or the resolver's guess, with its provider. */
private fun activeIn(p: Preferences): Pair<StorageMode, String?> = resolver.active(storedMode(p[STORAGE_MODE]))
/** [store]'s scope, first making its provider the owner of the bare external entries if none is yet. */
private fun claimedScope(p: MutablePreferences, store: Pair<StorageMode, String?> = activeIn(p)): String {
val (mode, authority) = store
if (mode == StorageMode.EXTERNAL && authority != null && p[EXTERNAL_LISTS_OWNER] == null) p[EXTERNAL_LISTS_OWNER] = authority
return listScope(mode, authority, p[EXTERNAL_LISTS_OWNER])
}
private fun withAuthority(store: StorageMode): Pair<StorageMode, String?> =
store to if (store == StorageMode.EXTERNAL) resolver.resolveExternal()?.authority else null
suspend fun setStorageMode(mode: StorageMode) = dataStore.edit { it[STORAGE_MODE] = mode.name }
@@ -93,9 +93,18 @@ class ProviderResolver @Inject constructor(
* Deliberately cheap and synchronous: a PackageManager lookup and a permission
* check, no database probe. Settings overrides it either way.
*/
fun autoMode(): StorageMode {
fun autoMode(external: TaskProvider? = resolveExternal()): StorageMode =
if (external != null && hasPermission(external)) StorageMode.EXTERNAL else StorageMode.OWN
/**
* The store [stored] names, or [autoMode]'s when none, with the external
* provider's authority when that is the store; one lookup at most.
*/
fun active(stored: StorageMode? = storageMode): Pair<StorageMode, String?> {
if (stored == StorageMode.OWN) return StorageMode.OWN to null
val external = resolveExternal()
return if (external != null && hasPermission(external)) StorageMode.EXTERNAL else StorageMode.OWN
val mode = stored ?: autoMode(external)
return mode to external?.authority?.takeIf { mode == StorageMode.EXTERNAL }
}
/** The first installed external candidate, or `null` when none is present. */
@@ -56,14 +56,9 @@ class TasksRepositoryImpl @Inject constructor(
) : TasksRepository {
override fun taskLists(): Flow<List<TaskList>> = observing {
val store = listStore()
dataSource.taskLists().also { lists -> if (listStore() == store) forgetGoneLists(store, lists) }
}
/** The store a read of the lists goes to, with the provider for an external one. */
private fun listStore(): Pair<StorageMode, String?> {
val mode = providerResolver.mode()
return mode to if (mode == StorageMode.EXTERNAL) providerResolver.resolveExternal()?.authority else null
val chosen = providerResolver.storageMode
val store = providerResolver.active(chosen)
dataSource.taskLists().also { lists -> if (providerResolver.storageMode == chosen) forgetGoneLists(store, lists) }
}
private val listsPrunedFor = AtomicReference<Pair<Pair<StorageMode, String?>, Set<Long>>?>(null)
@@ -0,0 +1,53 @@
package de.jeanlucmakiola.agendula.data.prefs
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.emptyPreferences
import com.google.common.truth.Truth.assertThat
import de.jeanlucmakiola.agendula.data.tasks.ProviderEnvironment
import de.jeanlucmakiola.agendula.data.tasks.ProviderResolver
import de.jeanlucmakiola.agendula.data.tasks.StorageMode
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.runBlocking
import org.junit.jupiter.api.Test
class ForgetDeletedListsTest {
private class MemoryDataStore : DataStore<Preferences> {
override val data = MutableStateFlow(emptyPreferences())
override suspend fun updateData(transform: suspend (t: Preferences) -> Preferences) =
transform(data.value).also { data.value = it }
}
private object NoProviders : ProviderEnvironment {
override fun packageDeclaring(authority: String): String? = null
override fun isGranted(permission: String) = false
override fun appLabel(packageName: String): String? = null
}
private val dataStore = MemoryDataStore()
private val prefs = SettingsPrefs(dataStore, ProviderResolver(NoProviders))
@Test
fun `forgets each deleted list and nothing else`() = runBlocking<Unit> {
prefs.setStorageMode(StorageMode.OWN)
listOf(1L, 2L, 3L).forEach { prefs.setHiddenFromSmartLists(it, true) }
prefs.setListPersistence(1, ListPersistence.ALWAYS)
prefs.setListPersistence(3, ListPersistence.NEVER)
prefs.forgetDeletedLists(listOf(1L, 2L), StorageMode.OWN)
val settings = prefs.settings.first()
assertThat(settings.hiddenFromSmartLists).containsExactly(3L)
assertThat(settings.perListPersistentReminders).containsExactly(3L, ListPersistence.NEVER)
}
@Test
fun `writes no setting the lists never had`() = runBlocking<Unit> {
prefs.setStorageMode(StorageMode.OWN)
val before = dataStore.data.value.asMap().keys
prefs.forgetDeletedLists(listOf(1L, 2L), StorageMode.OWN)
assertThat(dataStore.data.value.asMap().keys).isEqualTo(before)
}
}