diff --git a/app/src/full/java/de/jeanlucmakiola/agendula/data/sync/AccountRepository.kt b/app/src/full/java/de/jeanlucmakiola/agendula/data/sync/AccountRepository.kt index 58fd9b9..81a332b 100644 --- a/app/src/full/java/de/jeanlucmakiola/agendula/data/sync/AccountRepository.kt +++ b/app/src/full/java/de/jeanlucmakiola/agendula/data/sync/AccountRepository.kt @@ -376,7 +376,7 @@ class AccountRepository @Inject constructor( cadence.forget(dropped) quarantine.forget(dropped) change.drop.forEach { notices.forgetList(accountId, it.name) } - if (!keepUnticked) change.drop.forEach { runCatching { settings.forgetList(it.id, StorageMode.OWN) } } + if (!keepUnticked) runCatching { settings.forgetDeletedLists(change.drop.map { it.id }, StorageMode.OWN) } if (change.attach.isNotEmpty()) syncTrigger.enqueue(account.displayName, expedited = true) } } @@ -573,7 +573,7 @@ class AccountRepository @Inject constructor( if (deleteLocalData) { val deleted = database.taskLists().idsForAccount(accountId) database.taskLists().deleteForAccount(accountId) - deleted.forEach { runCatching { settings.forgetList(it, StorageMode.OWN) } } + runCatching { settings.forgetDeletedLists(deleted, StorageMode.OWN) } } accountState.setNeedsSignIn(accountId, false) 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 aad28dc..0b2a497 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 @@ -1,6 +1,7 @@ package de.jeanlucmakiola.agendula.data.prefs import androidx.datastore.core.DataStore +import androidx.datastore.preferences.core.MutablePreferences import androidx.datastore.preferences.core.Preferences import androidx.datastore.preferences.core.booleanPreferencesKey import androidx.datastore.preferences.core.edit @@ -239,8 +240,8 @@ data class Settings( start = dates.start, due = dates.due, isAllDay = dates.isAllDay, - ) startsNow = dates.start == now, + ) } fun persistenceFor(listId: Long): ListPersistence = perListPersistentReminders[listId] ?: ListPersistence.INHERIT @@ -273,6 +274,7 @@ class SettingsPrefs @Inject constructor( private val resolver: ProviderResolver, ) { val settings: Flow = dataStore.data.map { p -> + val scope = scopeOf(p) Settings( themeMode = p[THEME_MODE]?.let { runCatching { ThemeMode.valueOf(it) }.getOrNull() } ?: ThemeMode.SYSTEM, @@ -302,14 +304,14 @@ class SettingsPrefs @Inject constructor( ?.let { runCatching { ReminderDescription.valueOf(it) }.getOrNull() } ?: ReminderDescription.SHORT, persistentReminders = p[PERSISTENT_REMINDERS] ?: false, - perListPersistentReminders = parseListPersistence(p[LIST_PERSISTENT_REMINDERS].orEmpty(), modeOf(p).name), + perListPersistentReminders = parseListPersistence(p[LIST_PERSISTENT_REMINDERS].orEmpty(), scope), timeFormat = p[TIME_FORMAT]?.let { runCatching { TimeFormatPref.valueOf(it) }.getOrNull() } ?: TimeFormatPref.AUTO, autofocusTitle = p[AUTOFOCUS_TITLE] ?: true, syncIntervalMinutes = p[SYNC_INTERVAL]?.takeIf { it == 0 || it >= 15 } ?: DEFAULT_SYNC_INTERVAL_MINUTES, weekStart = p[WEEK_START]?.let { runCatching { DayOfWeek.valueOf(it) }.getOrNull() }, pushEnabled = p[PUSH_ENABLED] ?: true, - hiddenFromSmartLists = modeOf(p).name.let { mode -> + hiddenFromSmartLists = scope.let { mode -> p[SMART_LIST_HIDDEN].orEmpty() .mapNotNull { entry -> entry.substringAfter("$mode:", "").toLongOrNull() } .toSet() @@ -320,8 +322,8 @@ class SettingsPrefs @Inject constructor( defaultStartDate = p[DEFAULT_START_DATE]?.let { runCatching { DefaultStartDate.valueOf(it) }.getOrNull() } ?: if (p[DEFAULT_START_TODAY] == true) DefaultStartDate.TODAY else DefaultStartDate.NONE, collapsedGroups = (p[COLLAPSED_SECTIONS] ?: DEFAULT_COLLAPSED_GROUPS) - .mapNotNullTo(HashSet()) { unscopeListKey(it, modeOf(p).name) }, - groupings = decodeGroupings(p[GROUPINGS].orEmpty(), modeOf(p).name), + .mapNotNullTo(HashSet()) { unscopeListKey(it, scope) }, + groupings = decodeGroupings(p[GROUPINGS].orEmpty(), scope), upcomingRange = p[UPCOMING_RANGE]?.let { runCatching { UpcomingRange.valueOf(it) }.getOrNull() } ?: UpcomingRange.ALL, ) @@ -329,14 +331,14 @@ class SettingsPrefs @Inject constructor( /** Folds or unfolds the group with [id]; day groups that have passed are dropped on the way. */ suspend fun setGroupCollapsed(id: String, collapsed: Boolean) = dataStore.edit { p -> - val stored = scopeListKey(id, modeOf(p).name) + val stored = scopeListKey(id, claimedScope(p)) val current = dropPastDays(p[COLLAPSED_SECTIONS] ?: DEFAULT_COLLAPSED_GROUPS, LocalDate.now()) p[COLLAPSED_SECTIONS] = if (collapsed) current + stored else current - stored } /** Remembers [grouping] for [filter]'s view. */ suspend fun setGrouping(filter: TaskFilter, grouping: TaskGrouping) = dataStore.edit { p -> - val key = filter.groupingKey()?.let { scopeListKey(it, modeOf(p).name) } ?: return@edit + val key = filter.groupingKey()?.let { scopeListKey(it, claimedScope(p)) } ?: return@edit val kept = p[GROUPINGS].orEmpty().filterTo(HashSet()) { it.substringAfter(GROUPING_SEPARATOR) != key } p[GROUPINGS] = if (grouping == TaskGrouping.DUE) kept else kept + "${grouping.name}$GROUPING_SEPARATOR$key" } @@ -354,33 +356,39 @@ class SettingsPrefs @Inject constructor( /** Sets (or clears, via [ListPersistence.INHERIT]) a list's persistent-reminder override in the current store. */ suspend fun setListPersistence(listId: Long, persistence: ListPersistence) = dataStore.edit { p -> - val prefix = listPersistencePrefix(modeOf(p).name, listId) + val prefix = listPersistencePrefix(claimedScope(p), listId) val rest = p[LIST_PERSISTENT_REMINDERS].orEmpty().filterNotTo(HashSet()) { it.startsWith(prefix) } p[LIST_PERSISTENT_REMINDERS] = if (persistence == ListPersistence.INHERIT) rest else rest + "$prefix${persistence.name}" } /** - * Drops what was remembered about a list deleted from [store] (the current - * one by default), so a later list given its id starts fresh. The reminder - * overrides are keyed by bare id, shared by every store, so they stay. + * Drops what was remembered about [listIds], deleted from [store], so a later + * list given one of their ids starts fresh. The one way to forget a list + * explicitly: [forgetListsExcept], run on every read of the lists, cannot see + * the last list deleted (a read finding none proves nothing) nor an account's + * lists removed while another store is current. The reminder overrides are + * keyed by bare id, shared by every store, so they stay. */ - suspend fun forgetList(listId: Long, store: StorageMode? = null) = dataStore.edit { p -> - val mode = (store ?: modeOf(p)).name - 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 } + suspend fun forgetDeletedLists(listIds: Collection, 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 } + } } /** * 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. + * ones a successful read of it, through [authority] for an external one, + * just found. Another store's or provider'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 + suspend fun forgetListsExcept(store: StorageMode, authority: String?, present: Set) = dataStore.edit { p -> + val mode = claimedScope(p, store, authority) fun prune(key: Preferences.Key>, kind: ListEntry) { val entries = p[key] ?: return val kept = withoutGoneLists(entries, kind, mode, present) @@ -456,6 +464,19 @@ 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) + } + + private fun authorityOf(store: StorageMode): String? = + if (store == StorageMode.EXTERNAL) resolver.resolveExternal()?.authority else null + suspend fun setStorageMode(mode: StorageMode) = dataStore.edit { it[STORAGE_MODE] = mode.name } /** @@ -490,7 +511,7 @@ class SettingsPrefs @Inject constructor( } suspend fun setHiddenFromSmartLists(listId: Long, hidden: Boolean) = dataStore.edit { p -> - val entry = "${modeOf(p).name}:$listId" + val entry = "${claimedScope(p)}:$listId" val current = p[SMART_LIST_HIDDEN].orEmpty() p[SMART_LIST_HIDDEN] = if (hidden) current + entry else current - entry } @@ -543,6 +564,9 @@ class SettingsPrefs @Inject constructor( val BOTTOM_ADD_BAR = booleanPreferencesKey("bottom_add_bar") val ONBOARDING_DONE = booleanPreferencesKey("reminder_onboarding_done") val STORAGE_MODE = stringPreferencesKey("storage_mode") + + /** The external provider the bare `EXTERNAL` list entries belong to. */ + val EXTERNAL_LISTS_OWNER = stringPreferencesKey("external_lists_owner") val LIST_REMINDER_OVERRIDE = stringPreferencesKey("list_reminder_override") val DEFAULT_EDIT_FIELDS = stringSetPreferencesKey("default_edit_fields") val SORT_ORDER = stringPreferencesKey("sort_order") @@ -603,6 +627,16 @@ private fun List.inWholeDays(): List = private const val GROUPING_SEPARATOR = '|' +/** + * What a store's list entries are stored under. A provider's list ids mean + * nothing to another, so an external provider other than [owner], the one the + * bare `EXTERNAL` entries of earlier versions belong to, gets its own: + * `EXTERNAL@org.tasks.opentasks`. + */ +internal fun listScope(store: StorageMode, authority: String?, owner: String?): String = + if (store != StorageMode.EXTERNAL || authority == null || owner == null || owner == authority) store.name + else "${store.name}@$authority" + /** List ids belong to one store, so a stored list key carries the store's [mode]: `list:5` → `list:OWN:5`. */ private fun scopeListKey(key: String, mode: String): String = if (key.startsWith(TaskGroupKey.LIST_PREFIX)) "${TaskGroupKey.LIST_PREFIX}$mode:${key.removePrefix(TaskGroupKey.LIST_PREFIX)}" else key 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 9b19214..924592e 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 @@ -56,22 +56,28 @@ class TasksRepositoryImpl @Inject constructor( ) : TasksRepository { override fun taskLists(): Flow> = observing { - val mode = providerResolver.mode() - dataSource.taskLists().also { lists -> if (providerResolver.mode() == mode) forgetGoneLists(mode, lists) } + val store = listStore() + dataSource.taskLists().also { lists -> if (listStore() == store) forgetGoneLists(store, lists) } } - private val listsPrunedFor = AtomicReference>?>(null) + /** The store a read of the lists goes to, with the provider for an external one. */ + private fun listStore(): Pair { + val mode = providerResolver.mode() + return mode to if (mode == StorageMode.EXTERNAL) providerResolver.resolveExternal()?.authority else null + } + + private val listsPrunedFor = AtomicReference, Set>?>(null) /** - * Forgets the settings of [mode]'s lists a read no longer found, once per + * Forgets the settings of [store]'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) { + private fun forgetGoneLists(store: Pair, lists: List) { if (lists.isEmpty()) return - val read = mode to lists.mapTo(HashSet()) { it.id } + val read = store to lists.mapTo(HashSet()) { it.id } if (listsPrunedFor.getAndSet(read) == read) return appScope.launch(io) { - runCatching { settingsPrefs.forgetListsExcept(read.first, read.second) } + runCatching { settingsPrefs.forgetListsExcept(store.first, store.second, read.second) } .onFailure { listsPrunedFor.compareAndSet(read, null) Log.w(TAG, "Forgetting gone lists failed", it) @@ -302,7 +308,13 @@ class TasksRepositoryImpl @Inject constructor( withContext(io) { dataSource.updateList(listId, name, color) } override suspend fun deleteList(listId: Long) = - withContext(io) { dataSource.deleteList(listId) } + withContext(io) { + val store = providerResolver.mode() + dataSource.deleteList(listId) + runCatching { settingsPrefs.forgetDeletedLists(listOf(listId), store) } + .onFailure { Log.w(TAG, "Forgetting a deleted list failed", it) } + Unit + } override suspend fun reorderLists(listIds: List) = withContext(io) { dataSource.reorderLists(listIds) } diff --git a/app/src/main/java/de/jeanlucmakiola/agendula/ui/tasklist/TaskListViewModel.kt b/app/src/main/java/de/jeanlucmakiola/agendula/ui/tasklist/TaskListViewModel.kt index 16790f4..b9aa548 100644 --- a/app/src/main/java/de/jeanlucmakiola/agendula/ui/tasklist/TaskListViewModel.kt +++ b/app/src/main/java/de/jeanlucmakiola/agendula/ui/tasklist/TaskListViewModel.kt @@ -8,6 +8,7 @@ import de.jeanlucmakiola.agendula.data.prefs.DEFAULT_COLLAPSED_GROUPS import de.jeanlucmakiola.agendula.data.prefs.SettingsPrefs import de.jeanlucmakiola.agendula.data.sync.RemoteLists import de.jeanlucmakiola.agendula.data.tasks.FilteredTasks +import de.jeanlucmakiola.agendula.data.tasks.StorageMode import de.jeanlucmakiola.agendula.data.tasks.TasksRepository import de.jeanlucmakiola.agendula.data.tasks.recoveringFromProviderFailure import de.jeanlucmakiola.agendula.domain.ListSearchResult @@ -566,10 +567,7 @@ class TaskListViewModel @Inject constructor( fun deleteList(listId: Long, accountId: Long?) = viewModelScope.launch { if (accountId == null) { runCatching { repository.deleteList(listId) } - .onSuccess { - _listDeleted.value = true - forgetList(listId) - } + .onSuccess { _listDeleted.value = true } .onFailure { _listWriteFailure.value = ListWriteFailure.DELETE } return@launch } @@ -583,15 +581,15 @@ class TaskListViewModel @Inject constructor( val failure = outcome.asFailure(ListWriteFailure.DELETE) if (failure == null) { _listDeleted.value = true - forgetList(listId) + forgetAccountList(listId) } else { _listWriteFailure.value = failure } } - /** In the app scope: the screen leaves as soon as the list is gone. */ - private fun forgetList(listId: Long) { - appScope.launch { runCatching { settingsPrefs.forgetList(listId) } } + /** In the app scope: the screen leaves as soon as the list is gone. An account's lists are our own store's. */ + private fun forgetAccountList(listId: Long) { + appScope.launch { runCatching { settingsPrefs.forgetDeletedLists(listOf(listId), StorageMode.OWN) } } } /** Inline "add subtask" from an expanded list group — files it under [parent]. */ 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 index 1c60692..abe21fb 100644 --- a/app/src/test/java/de/jeanlucmakiola/agendula/data/prefs/GoneListsTest.kt +++ b/app/src/test/java/de/jeanlucmakiola/agendula/data/prefs/GoneListsTest.kt @@ -1,6 +1,7 @@ package de.jeanlucmakiola.agendula.data.prefs import com.google.common.truth.Truth.assertThat +import de.jeanlucmakiola.agendula.data.tasks.StorageMode import org.junit.jupiter.api.Test class GoneListsTest { @@ -33,4 +34,20 @@ class GoneListsTest { assertThat(withoutGoneLists(entries, ListEntry.COLLAPSED, "OWN", present)) .containsExactly("list:OWN:2", "list:EXTERNAL:5", "COMPLETED", "day:2026-11-20") } + + @Test + fun `another external provider's lists are kept apart, the first one's stay where they were`() { + assertThat(listScope(StorageMode.OWN, null, "org.dmfs.tasks")).isEqualTo("OWN") + assertThat(listScope(StorageMode.EXTERNAL, "org.dmfs.tasks", null)).isEqualTo("EXTERNAL") + assertThat(listScope(StorageMode.EXTERNAL, "org.dmfs.tasks", "org.dmfs.tasks")).isEqualTo("EXTERNAL") + val other = listScope(StorageMode.EXTERNAL, "org.tasks.opentasks", "org.dmfs.tasks") + assertThat(other).isEqualTo("EXTERNAL@org.tasks.opentasks") + + val entries = setOf("EXTERNAL:3=ALWAYS", "$other:3=NEVER", "$other:4=ALWAYS") + assertThat(withoutGoneLists(entries, ListEntry.PERSISTENCE, other, setOf(3L))) + .containsExactly("EXTERNAL:3=ALWAYS", "$other:3=NEVER") + assertThat(withoutGoneLists(entries, ListEntry.PERSISTENCE, "EXTERNAL", setOf(9L))) + .containsExactly("$other:3=NEVER", "$other:4=ALWAYS") + assertThat(parseListPersistence(entries, "EXTERNAL")).containsExactly(3L, ListPersistence.ALWAYS) + } }