diff --git a/app/src/main/java/de/jeanlucmakiola/agendula/data/prefs/SettingsPrefs.kt b/app/src/main/java/de/jeanlucmakiola/agendula/data/prefs/SettingsPrefs.kt index 9a5fe14..2b2ee5b 100644 --- a/app/src/main/java/de/jeanlucmakiola/agendula/data/prefs/SettingsPrefs.kt +++ b/app/src/main/java/de/jeanlucmakiola/agendula/data/prefs/SettingsPrefs.kt @@ -373,6 +373,24 @@ class SettingsPrefs @Inject constructor( p[COLLAPSED_SECTIONS]?.let { p[COLLAPSED_SECTIONS] = it - key } } + /** + * Drops what was remembered about [store]'s lists other than [present], the + * ones a successful read of it just found. Another store's entries, and the + * reminder overrides keyed by bare id, stay. + */ + suspend fun forgetListsExcept(store: StorageMode, present: Set) = dataStore.edit { p -> + val mode = store.name + fun prune(key: Preferences.Key>, kind: ListEntry) { + val entries = p[key] ?: return + val kept = withoutGoneLists(entries, kind, mode, present) + if (kept.size != entries.size) p[key] = kept + } + prune(LIST_PERSISTENT_REMINDERS, ListEntry.PERSISTENCE) + prune(SMART_LIST_HIDDEN, ListEntry.HIDDEN) + prune(GROUPINGS, ListEntry.GROUPING) + prune(COLLAPSED_SECTIONS, ListEntry.COLLAPSED) + } + suspend fun setSyncIntervalMinutes(minutes: Int) = dataStore.edit { it[SYNC_INTERVAL] = minutes } suspend fun setPushEnabled(enabled: Boolean) = dataStore.edit { it[PUSH_ENABLED] = enabled } @@ -595,6 +613,39 @@ internal fun unscopeListKey(stored: String, mode: String): String? { return "${TaskGroupKey.LIST_PREFIX}$id" } +/** The shapes a stored set entry naming a list of one store comes in. */ +internal enum class ListEntry { + /** `MODE:id=ALWAYS` */ + PERSISTENCE, + + /** `MODE:id` */ + HIDDEN, + + /** `GROUPING|list:MODE:id` */ + GROUPING, + + /** `list:MODE:id` */ + COLLAPSED; + + /** The list of [mode] [entry] names; null for another store's, or an entry naming no list. */ + fun listId(entry: String, mode: String): Long? = when (this) { + PERSISTENCE -> storedListId(entry.substringBefore('='), mode) + HIDDEN -> storedListId(entry, mode) + GROUPING -> scopedListId(entry.substringAfter(GROUPING_SEPARATOR), mode) + COLLAPSED -> scopedListId(entry, mode) + } +} + +/** [entries] without those naming a list of [mode] that is not [present]. */ +internal fun withoutGoneLists(entries: Set, kind: ListEntry, mode: String, present: Set): Set = + entries.filterTo(HashSet()) { entry -> kind.listId(entry, mode)?.let { it in present } ?: true } + +private fun storedListId(entry: String, mode: String): Long? = + entry.removePrefix("$mode:").takeIf { it != entry }?.toLongOrNull() + +private fun scopedListId(stored: String, mode: String): Long? = + stored.removePrefix(TaskGroupKey.LIST_PREFIX).takeIf { it != stored }?.let { storedListId(it, mode) } + /** `GROUPING|key` entries → view key to grouping, keeping only the lists of the current [mode]. */ internal fun decodeGroupings(entries: Set, mode: String): Map = entries.mapNotNull { entry -> diff --git a/app/src/main/java/de/jeanlucmakiola/agendula/data/tasks/TasksRepositoryImpl.kt b/app/src/main/java/de/jeanlucmakiola/agendula/data/tasks/TasksRepositoryImpl.kt index c7f09e8..0da2a8d 100644 --- a/app/src/main/java/de/jeanlucmakiola/agendula/data/tasks/TasksRepositoryImpl.kt +++ b/app/src/main/java/de/jeanlucmakiola/agendula/data/tasks/TasksRepositoryImpl.kt @@ -38,6 +38,7 @@ import kotlinx.coroutines.flow.flowOn import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import java.time.ZoneId +import java.util.concurrent.atomic.AtomicReference import javax.inject.Inject import javax.inject.Singleton import kotlin.time.Clock @@ -54,7 +55,29 @@ class TasksRepositoryImpl @Inject constructor( @IoDispatcher private val io: CoroutineDispatcher, ) : TasksRepository { - override fun taskLists(): Flow> = observing { dataSource.taskLists() } + override fun taskLists(): Flow> = observing { + val mode = providerResolver.mode() + dataSource.taskLists().also { lists -> if (providerResolver.mode() == mode) forgetGoneLists(mode, lists) } + } + + private val listsPrunedFor = AtomicReference>?>(null) + + /** + * Forgets the settings of [mode]'s lists a read no longer found, once per + * change of what it finds. A read finding no list proves nothing. + */ + private fun forgetGoneLists(mode: StorageMode, lists: List) { + if (lists.isEmpty()) return + val read = mode to lists.mapTo(HashSet()) { it.id } + if (listsPrunedFor.getAndSet(read) == read) return + appScope.launch(io) { + runCatching { settingsPrefs.forgetListsExcept(read.first, read.second) } + .onFailure { + listsPrunedFor.compareAndSet(read, null) + Log.w(TAG, "Forgetting gone lists failed", it) + } + } + } @OptIn(ExperimentalCoroutinesApi::class) override fun filteredTasks(filter: TaskFilter): Flow = when (filter) { diff --git a/app/src/test/java/de/jeanlucmakiola/agendula/data/prefs/GoneListsTest.kt b/app/src/test/java/de/jeanlucmakiola/agendula/data/prefs/GoneListsTest.kt new file mode 100644 index 0000000..1c60692 --- /dev/null +++ b/app/src/test/java/de/jeanlucmakiola/agendula/data/prefs/GoneListsTest.kt @@ -0,0 +1,36 @@ +package de.jeanlucmakiola.agendula.data.prefs + +import com.google.common.truth.Truth.assertThat +import org.junit.jupiter.api.Test + +class GoneListsTest { + + private val present = setOf(1L, 2L) + + @Test + fun `a gone list's persistence goes, another store's stays`() { + val entries = setOf("OWN:1=ALWAYS", "OWN:3=NEVER", "EXTERNAL:3=ALWAYS", "junk") + assertThat(withoutGoneLists(entries, ListEntry.PERSISTENCE, "OWN", present)) + .containsExactly("OWN:1=ALWAYS", "EXTERNAL:3=ALWAYS", "junk") + } + + @Test + fun `a gone list is no longer hidden from smart lists`() { + assertThat(withoutGoneLists(setOf("OWN:2", "OWN:9", "EXTERNAL:9"), ListEntry.HIDDEN, "OWN", present)) + .containsExactly("OWN:2", "EXTERNAL:9") + } + + @Test + fun `a gone list's grouping goes, other views' stay`() { + val entries = setOf("DAY|list:OWN:1", "PRIORITY|list:OWN:7", "LIST|list:EXTERNAL:7", "DAY|smart:UPCOMING", "DAY|tag:7") + assertThat(withoutGoneLists(entries, ListEntry.GROUPING, "OWN", present)) + .containsExactly("DAY|list:OWN:1", "LIST|list:EXTERNAL:7", "DAY|smart:UPCOMING", "DAY|tag:7") + } + + @Test + fun `a gone list's folded group goes, other groups stay`() { + val entries = setOf("list:OWN:2", "list:OWN:5", "list:EXTERNAL:5", "COMPLETED", "day:2026-11-20") + assertThat(withoutGoneLists(entries, ListEntry.COLLAPSED, "OWN", present)) + .containsExactly("list:OWN:2", "list:EXTERNAL:5", "COMPLETED", "day:2026-11-20") + } +}