This commit is contained in:
1 parent
415622797d
commit
4796cf9856
3 files changed
+111
-1
No files matched your search
@@ -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<Long>) = dataStore.edit { p ->
|
||||
val mode = store.name
|
||||
fun prune(key: Preferences.Key<Set<String>>, 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<String>, kind: ListEntry, mode: String, present: Set<Long>): Set<String> =
|
||||
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<String>, mode: String): Map<String, TaskGrouping> =
|
||||
entries.mapNotNull { entry ->
|
||||
|
||||
@@ -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<List<TaskList>> = observing { dataSource.taskLists() }
|
||||
override fun taskLists(): Flow<List<TaskList>> = observing {
|
||||
val mode = providerResolver.mode()
|
||||
dataSource.taskLists().also { lists -> if (providerResolver.mode() == mode) forgetGoneLists(mode, lists) }
|
||||
}
|
||||
|
||||
private val listsPrunedFor = AtomicReference<Pair<StorageMode, Set<Long>>?>(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<TaskList>) {
|
||||
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<FilteredTasks> = when (filter) {
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user