Require the week start for rules, read flat filters only in the migration (#172)
This commit is contained in:
1 parent
bfbe7398d4
commit
f9926a80b3
15 files changed
+123
-84
No files matched your search
@@ -19,8 +19,8 @@ import java.net.URLEncoder
|
||||
* URL-encoded, and list ids are qualified by the store they belong to
|
||||
* (`OWN:3`), since each store numbers its lists on its own.
|
||||
*
|
||||
* Lines without the `v2` mark are the flat format of 1.4 and earlier,
|
||||
* `id|name|due|lists|tags|match|priority`, still read; [migrate] rewrites them.
|
||||
* The flat format of 1.4 and earlier, `id|name|due|lists|tags|match|priority`
|
||||
* under another key, is read only by [migrate], which rewrites it.
|
||||
*
|
||||
* Fixed once released — don't change without a data migration.
|
||||
*/
|
||||
@@ -32,11 +32,17 @@ internal object SavedFilterCodec {
|
||||
fun decode(raw: String?, mode: String): List<SavedFilter> =
|
||||
lines(raw).mapNotNull { runCatching { decodeLine(it, mode) }.getOrNull() }
|
||||
|
||||
/** [raw] with [filter] replacing the line of the same id, or appended. */
|
||||
/**
|
||||
* [raw] with [filter] replacing the line of the same id, or appended. Other
|
||||
* stores' lists the stored line no longer has, e.g. pruned while the filter
|
||||
* was being edited, stay gone.
|
||||
*/
|
||||
fun upsert(raw: String?, filter: SavedFilter, mode: String): String {
|
||||
val existing = lines(raw)
|
||||
val index = existing.indexOfFirst { idOf(it) == filter.id }
|
||||
val line = encodeLine(filter, mode)
|
||||
val stored = existing.getOrNull(index)?.let { runCatching { decodeLine(it, mode) }.getOrNull() }
|
||||
val kept = stored?.let { filter.copy(rule = filter.rule.keepingElsewhere(stored.rule.elsewhereRefs()).withoutLists { false }) }
|
||||
val line = encodeLine(kept ?: filter, mode)
|
||||
val updated = if (index >= 0) existing.toMutableList().also { it[index] = line } else existing + line
|
||||
return updated.joinToString("\n")
|
||||
}
|
||||
@@ -71,7 +77,23 @@ internal object SavedFilterCodec {
|
||||
|
||||
private fun isV2(line: String): Boolean = line.startsWith("$V2|")
|
||||
|
||||
private fun idOf(line: String): Long? = fields(line).getOrNull(if (isV2(line)) 1 else 0)?.toLongOrNull()
|
||||
private fun idOf(line: String): Long? = fields(line).takeIf { isV2(line) }?.getOrNull(1)?.toLongOrNull()
|
||||
|
||||
private fun Rule.elsewhereRefs(): Set<String> = when (this) {
|
||||
is Rule.InList -> elsewhere
|
||||
is Rule.All -> children.flatMapTo(HashSet()) { it.elsewhereRefs() }
|
||||
is Rule.Any -> children.flatMapTo(HashSet()) { it.elsewhereRefs() }
|
||||
is Rule.Not -> child.elsewhereRefs()
|
||||
is Rule.HasTag, is Rule.MinPriority, is Rule.Due -> emptySet()
|
||||
}
|
||||
|
||||
private fun Rule.keepingElsewhere(kept: Set<String>): Rule = when (this) {
|
||||
is Rule.InList -> copy(elsewhere = elsewhere intersect kept)
|
||||
is Rule.All -> Rule.All(children.map { it.keepingElsewhere(kept) })
|
||||
is Rule.Any -> Rule.Any(children.map { it.keepingElsewhere(kept) })
|
||||
is Rule.Not -> Rule.Not(child.keepingElsewhere(kept))
|
||||
is Rule.HasTag, is Rule.MinPriority, is Rule.Due -> this
|
||||
}
|
||||
|
||||
private fun splitList(value: String): List<String> = value.split(',').filter { it.isNotEmpty() }
|
||||
|
||||
@@ -79,7 +101,7 @@ internal object SavedFilterCodec {
|
||||
listOf(V2, filter.id.toString(), encode(filter.name), encodeRule(filter.rule, mode)).joinToString("|")
|
||||
|
||||
private fun decodeLine(line: String, mode: String): SavedFilter? {
|
||||
if (!isV2(line)) return decodeFlatLine(line, mode)
|
||||
if (!isV2(line)) return null
|
||||
val f = fields(line)
|
||||
if (f.size != 4) return null
|
||||
val id = f[1].toLongOrNull() ?: return null
|
||||
@@ -120,7 +142,7 @@ internal object SavedFilterCodec {
|
||||
}
|
||||
is Rule.HasTag -> "tag(${encode(rule.tag)})"
|
||||
is Rule.MinPriority -> "prio(${rule.priority.name})"
|
||||
is Rule.Due -> when (val spec = rule.window) {
|
||||
is Rule.Due -> when (val spec = rule.spec) {
|
||||
is DueSpec.Window -> "due(${spec.window.name})"
|
||||
DueSpec.Today -> "range(today)"
|
||||
DueSpec.ThisWeek -> "range(week)"
|
||||
|
||||
@@ -90,7 +90,7 @@ class TasksRepositoryImpl @Inject constructor(
|
||||
override fun filteredTasks(filter: TaskFilter): Flow<FilteredTasks> = when (filter) {
|
||||
// A deleted filter shows nothing rather than failing the screen or widget on it.
|
||||
is TaskFilter.Saved -> settingsPrefs.savedFilter(filter.id)
|
||||
.combine(settingsPrefs.settings.map { it.effectiveWeekStart }.distinctUntilChanged(), ::Pair)
|
||||
.combine(settingsPrefs.settings.map { it.weekStart }.distinctUntilChanged(), ::Pair)
|
||||
.flatMapLatest { (saved, weekStart) ->
|
||||
if (saved == null) flowOf(FilteredTasks(emptyList()))
|
||||
else observing {
|
||||
@@ -120,7 +120,8 @@ class TasksRepositoryImpl @Inject constructor(
|
||||
}
|
||||
}
|
||||
|
||||
private fun loadTasks(filter: TaskFilter, saved: SavedFilter? = null, weekStart: DayOfWeek = localeWeekStart()): List<Task> {
|
||||
/** [weekStart] is the setting, read against the locale on each load when unset. */
|
||||
private fun loadTasks(filter: TaskFilter, saved: SavedFilter? = null, weekStart: DayOfWeek? = null): List<Task> {
|
||||
val query = when (filter) {
|
||||
is TaskFilter.OfList -> TaskQuery(listId = filter.listId, includeCompleted = true)
|
||||
// Read completed tasks too, then let [TaskFiltering.matches] enforce each
|
||||
@@ -136,7 +137,7 @@ class TasksRepositoryImpl @Inject constructor(
|
||||
}
|
||||
val zone = ZoneId.systemDefault()
|
||||
val (todayStart, todayEnd) = DayWindow.today(Clock.System.now(), zone)
|
||||
val clock = saved?.let { RuleClock(todayStart, todayEnd, zone, weekStart) }
|
||||
val clock = saved?.let { RuleClock(todayStart, todayEnd, zone, weekStart ?: localeWeekStart()) }
|
||||
val all = SeriesCollapse.visible(dataSource.tasks(query))
|
||||
// (done, total) of direct children per parent, over the unfiltered set.
|
||||
val progress: Map<Long, Pair<Int, Int>> = all
|
||||
|
||||
@@ -42,39 +42,39 @@ data class FilterCriteria(
|
||||
fun of(rule: Rule): FilterCriteria? {
|
||||
val parts = if (rule is Rule.All) rule.children else listOf(rule)
|
||||
var criteria = FilterCriteria()
|
||||
var seen = emptySet<String>()
|
||||
fun claim(kind: String): Boolean = (kind !in seen).also { seen = seen + kind }
|
||||
val seen = mutableSetOf<Kind>()
|
||||
fun claim(kind: Kind): Boolean = seen.add(kind)
|
||||
for (part in parts) {
|
||||
criteria = when {
|
||||
part is Rule.Due && part.window is DueSpec.Window -> {
|
||||
if (!claim("due")) return null
|
||||
criteria.copy(due = setOf(part.window.window))
|
||||
part is Rule.Due && part.spec is DueSpec.Window -> {
|
||||
if (!claim(Kind.DUE)) return null
|
||||
criteria.copy(due = setOf(part.spec.window))
|
||||
}
|
||||
part is Rule.InList -> {
|
||||
if (!claim("lists") || part.listIds.isEmpty() && part.elsewhere.isEmpty()) return null
|
||||
if (!claim(Kind.LISTS) || part.listIds.isEmpty() && part.elsewhere.isEmpty()) return null
|
||||
criteria.copy(listIds = part.listIds, listsElsewhere = part.elsewhere)
|
||||
}
|
||||
part is Rule.MinPriority -> {
|
||||
if (!claim("priority")) return null
|
||||
if (!claim(Kind.PRIORITY)) return null
|
||||
criteria.copy(minPriority = part.priority)
|
||||
}
|
||||
// Tags straight under the top read as all of them.
|
||||
part is Rule.HasTag -> {
|
||||
if ("tag group" in seen) return null
|
||||
seen = seen + "tags"
|
||||
if (Kind.TAG_GROUP in seen) return null
|
||||
seen += Kind.TAGS
|
||||
criteria.copy(tags = criteria.tags + part.tag, tagMatch = TagMatch.ALL)
|
||||
}
|
||||
else -> {
|
||||
val group = groupOf(part) ?: return null
|
||||
val windows = group.mapNotNull { ((it as? Rule.Due)?.window as? DueSpec.Window)?.window }
|
||||
val windows = group.mapNotNull { ((it as? Rule.Due)?.spec as? DueSpec.Window)?.window }
|
||||
val tags = group.mapNotNull { (it as? Rule.HasTag)?.tag }
|
||||
when {
|
||||
part is Rule.Any && windows.size == group.size -> {
|
||||
if (!claim("due")) return null
|
||||
if (!claim(Kind.DUE)) return null
|
||||
criteria.copy(due = windows.toSet())
|
||||
}
|
||||
tags.size == group.size -> {
|
||||
if (!claim("tags") || !claim("tag group")) return null
|
||||
if (!claim(Kind.TAGS) || !claim(Kind.TAG_GROUP)) return null
|
||||
criteria.copy(tags = tags.toSet(), tagMatch = if (part is Rule.All) TagMatch.ALL else TagMatch.ANY)
|
||||
}
|
||||
else -> return null
|
||||
@@ -85,6 +85,8 @@ data class FilterCriteria(
|
||||
return criteria
|
||||
}
|
||||
|
||||
private enum class Kind { DUE, LISTS, PRIORITY, TAGS, TAG_GROUP }
|
||||
|
||||
/** The children of a non-empty [Rule.All] or [Rule.Any]; `null` for anything else. */
|
||||
private fun groupOf(rule: Rule): List<Rule>? = when (rule) {
|
||||
is Rule.All -> rule.children
|
||||
|
||||
@@ -2,6 +2,7 @@ package de.jeanlucmakiola.agendula.domain
|
||||
|
||||
import java.time.DayOfWeek
|
||||
import java.time.LocalDate
|
||||
import java.time.ZoneId
|
||||
import kotlin.time.Instant
|
||||
|
||||
/** The due date a new task starts with, counted from the day it is created. */
|
||||
@@ -79,12 +80,12 @@ fun NewTaskDates.showsIn(
|
||||
savedFilter: SavedFilter?,
|
||||
todayStart: Instant,
|
||||
todayEnd: Instant,
|
||||
weekStart: DayOfWeek,
|
||||
upcomingEnd: Instant? = null,
|
||||
weekStart: DayOfWeek = localeWeekStart(),
|
||||
): Boolean {
|
||||
val probe = openTaskDated(start, due, isAllDay)
|
||||
return when {
|
||||
savedFilter != null -> savedFilter.admitsDueOf(probe, todayStart, todayEnd, weekStart)
|
||||
savedFilter != null -> savedFilter.admitsDueOf(probe, RuleClock(todayStart, todayEnd, ZoneId.systemDefault(), weekStart))
|
||||
smartList != null -> TaskFiltering.matches(probe, TaskFilter.Smart(smartList), todayStart, todayEnd, upcomingEnd = upcomingEnd)
|
||||
else -> true
|
||||
}
|
||||
|
||||
@@ -31,7 +31,7 @@ sealed interface Rule {
|
||||
/** At [priority] or higher. */
|
||||
data class MinPriority(val priority: Priority) : Rule
|
||||
|
||||
data class Due(val window: DueSpec) : Rule
|
||||
data class Due(val spec: DueSpec) : Rule
|
||||
|
||||
companion object {
|
||||
/** The rule every task passes. */
|
||||
@@ -86,8 +86,8 @@ sealed interface DueSpec {
|
||||
data class RuleClock(
|
||||
val todayStart: Instant,
|
||||
val todayEnd: Instant,
|
||||
val zone: ZoneId = ZoneId.systemDefault(),
|
||||
val weekStart: DayOfWeek = localeWeekStart(),
|
||||
val zone: ZoneId,
|
||||
val weekStart: DayOfWeek,
|
||||
) {
|
||||
val today: LocalDate = todayStart.calendarDate(allDay = false, zone = zone)
|
||||
}
|
||||
@@ -103,7 +103,7 @@ fun Rule.matches(task: Task, clock: RuleClock): Boolean = when (this) {
|
||||
is Rule.InList -> task.listId in listIds
|
||||
is Rule.HasTag -> tag in task.tags
|
||||
is Rule.MinPriority -> task.priority >= priority
|
||||
is Rule.Due -> window.matches(task, clock)
|
||||
is Rule.Due -> spec.matches(task, clock)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -111,7 +111,7 @@ fun Rule.matches(task: Task, clock: RuleClock): Boolean = when (this) {
|
||||
* does, `null` when the rule's other conditions decide.
|
||||
*/
|
||||
fun Rule.dueVerdict(task: Task, clock: RuleClock): Boolean? = when (this) {
|
||||
is Rule.Due -> window.matches(task, clock)
|
||||
is Rule.Due -> spec.matches(task, clock)
|
||||
is Rule.Not -> child.dueVerdict(task, clock)?.not()
|
||||
is Rule.All -> children.map { it.dueVerdict(task, clock) }.let { v ->
|
||||
if (false in v) false else if (null in v) null else true
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package de.jeanlucmakiola.agendula.domain
|
||||
|
||||
import java.time.DayOfWeek
|
||||
import java.time.LocalDate
|
||||
import java.time.ZoneId
|
||||
import kotlin.time.Instant
|
||||
@@ -20,28 +19,19 @@ data class SavedFilter(
|
||||
constructor(id: Long, name: String, criteria: FilterCriteria) : this(id, name, criteria.toRule())
|
||||
|
||||
/** [rule] in the flat form the filter editor shows; `null` when it has none. */
|
||||
val criteria: FilterCriteria? get() = FilterCriteria.of(rule)
|
||||
val criteria: FilterCriteria? by lazy(LazyThreadSafetyMode.PUBLICATION) { FilterCriteria.of(rule) }
|
||||
|
||||
/** This filter without the list ids missing from [existing], e.g. lists since deleted. */
|
||||
/**
|
||||
* This filter without the list ids missing from [existing], e.g. lists since
|
||||
* deleted. A read finding no list at all proves nothing, so it keeps them.
|
||||
*/
|
||||
fun withListsIn(existing: Set<Long>): SavedFilter =
|
||||
copy(rule = rule.withoutLists { it !in existing })
|
||||
if (existing.isEmpty()) this else copy(rule = rule.withoutLists { it !in existing })
|
||||
|
||||
fun matches(task: Task, clock: RuleClock): Boolean = !task.isClosed && rule.matches(task, clock)
|
||||
|
||||
fun matches(
|
||||
task: Task,
|
||||
todayStart: Instant,
|
||||
todayEnd: Instant,
|
||||
weekStart: DayOfWeek = localeWeekStart(),
|
||||
): Boolean = matches(task, RuleClock(todayStart, todayEnd, weekStart = weekStart))
|
||||
|
||||
/** Whether an open task dated like [probe] can pass this filter, going by its due conditions only. */
|
||||
fun admitsDueOf(
|
||||
probe: Task,
|
||||
todayStart: Instant,
|
||||
todayEnd: Instant,
|
||||
weekStart: DayOfWeek = localeWeekStart(),
|
||||
): Boolean = rule.dueVerdict(probe, RuleClock(todayStart, todayEnd, weekStart = weekStart)) != false
|
||||
fun admitsDueOf(probe: Task, clock: RuleClock): Boolean = rule.dueVerdict(probe, clock) != false
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -35,7 +35,7 @@ object TaskFiltering {
|
||||
/**
|
||||
* [saved] is the definition behind a [TaskFilter.Saved]; without one it matches nothing.
|
||||
* [upcomingEnd] caps Upcoming to dues before it (see [UpcomingRange.end]); `null` = no cap.
|
||||
* [ruleClock] reads a saved filter's rule; without one it is today with the locale's weeks.
|
||||
* [ruleClock] reads a saved filter's rule; without one a saved filter matches nothing.
|
||||
*/
|
||||
fun matches(
|
||||
task: Task,
|
||||
@@ -51,7 +51,8 @@ object TaskFiltering {
|
||||
is TaskFilter.OfLists -> task.listId in filter.listIds
|
||||
is TaskFilter.Smart -> matchesSmart(task, filter.list, todayStart, todayEnd, upcomingEnd)
|
||||
is TaskFilter.Tagged -> filter.tag in task.tags
|
||||
is TaskFilter.Saved -> saved?.takeIf { it.id == filter.id }?.matches(task, ruleClock ?: RuleClock(todayStart, todayEnd)) ?: false
|
||||
is TaskFilter.Saved -> saved?.takeIf { it.id == filter.id }
|
||||
?.let { s -> ruleClock?.let { s.matches(task, it) } } ?: false
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -247,7 +247,7 @@ class TaskEditViewModel @Inject constructor(
|
||||
val upcomingEnd = settings.upcomingRange.end(now, zone)
|
||||
val presetDue = preset?.dueDate ?: presetSmartList?.dueWindows?.presetDue(today)
|
||||
val dates = settings.defaultNewTaskDates(today, now, presetDue, subtask = parentId != null) {
|
||||
it.showsIn(presetSmartList, filter, todayStart, todayEnd, upcomingEnd, settings.effectiveWeekStart)
|
||||
it.showsIn(presetSmartList, filter, todayStart, todayEnd, settings.effectiveWeekStart, upcomingEnd)
|
||||
}
|
||||
// A read-only or vanished default — or a widget's preset list, if it was
|
||||
// since deleted or turned read-only — would give the task a list it
|
||||
|
||||
@@ -67,9 +67,7 @@ class FilterEditViewModel @Inject constructor(
|
||||
viewModelScope.launch {
|
||||
val saved = settingsPrefs.savedFilter(filterId).first()
|
||||
// Show it as it matches: lists deleted since drop out, as they do in the repository.
|
||||
// A read finding no list proves nothing, so it prunes nothing.
|
||||
val existing = runCatching { repository.taskLists().first().mapTo(HashSet()) { it.id } }.getOrNull()
|
||||
?.takeIf { it.isNotEmpty() }
|
||||
load(saved?.let { if (existing == null) it else it.withListsIn(existing) } ?: SavedFilter(id = 0, name = ""))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ import de.jeanlucmakiola.agendula.domain.TaskFilter
|
||||
import de.jeanlucmakiola.agendula.domain.TaskFiltering
|
||||
import de.jeanlucmakiola.agendula.domain.TaskList
|
||||
import de.jeanlucmakiola.agendula.domain.UpcomingRange
|
||||
import de.jeanlucmakiola.agendula.domain.localeWeekStart
|
||||
import de.jeanlucmakiola.agendula.domain.tagsInUse
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.SharingStarted
|
||||
@@ -148,7 +149,7 @@ class ListsViewModel @Inject constructor(
|
||||
// Open smart lists drop completed tasks, but the Today ring needs the
|
||||
// ones already ticked off to show "x of y done", so read them too.
|
||||
repository.tasks(TaskFilter.Smart(SmartList.COMPLETED)),
|
||||
settingsPrefs.settings.map { Triple(it.hiddenFromSmartLists, it.upcomingRange, it.effectiveWeekStart) }.distinctUntilChanged(),
|
||||
settingsPrefs.settings.map { Triple(it.hiddenFromSmartLists, it.upcomingRange, it.weekStart) }.distinctUntilChanged(),
|
||||
settingsPrefs.savedFilters,
|
||||
) { lists, openTasks, completedTasks, (hidden, upcomingRange, weekStart), filters ->
|
||||
buildContent(lists, openTasks, completedTasks, hidden, upcomingRange, filters, weekStart) as ListsUiState
|
||||
@@ -162,7 +163,7 @@ class ListsViewModel @Inject constructor(
|
||||
hidden: Set<Long>,
|
||||
upcomingRange: UpcomingRange,
|
||||
savedFilters: List<SavedFilter>,
|
||||
weekStart: DayOfWeek,
|
||||
weekStart: DayOfWeek?,
|
||||
): ListsUiState.Content {
|
||||
val now = Clock.System.now()
|
||||
val zone = ZoneId.systemDefault()
|
||||
@@ -215,12 +216,12 @@ class ListsViewModel @Inject constructor(
|
||||
val ids = tagged.mapTo(HashSet()) { it.taskId }
|
||||
TagOverview(tag, tagged.count { !it.isClosed && (!it.isSubtask || it.parentId !in ids) })
|
||||
}
|
||||
val clock = RuleClock(todayStart, todayEnd, zone, weekStart)
|
||||
val clock = RuleClock(todayStart, todayEnd, zone, weekStart ?: localeWeekStart())
|
||||
// Lists deleted since drop out, as they do when the filter is opened.
|
||||
val listIds = lists.mapTo(HashSet()) { it.id }
|
||||
// Same folding as the tags: a subtask counts only when its parent isn't in the filter too.
|
||||
val filters = savedFilters.map { saved ->
|
||||
val filter = if (listIds.isEmpty()) saved else saved.withListsIn(listIds)
|
||||
val filter = saved.withListsIn(listIds)
|
||||
val matching = openTasks.filter { filter.matches(it, clock) }
|
||||
val ids = matching.mapTo(HashSet()) { it.taskId }
|
||||
FilterOverview(saved, matching.count { !it.isSubtask || it.parentId !in ids })
|
||||
|
||||
@@ -73,12 +73,6 @@ class SavedFilterCodecTest {
|
||||
assertThat(SavedFilterCodec.decode(raw, "OWN").map { it.name }).containsExactly("renamed", filter.name).inOrder()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `upsert replaces a flat line by id`() {
|
||||
val raw = SavedFilterCodec.upsert(legacyLine(id = 3, refs = listOf("OWN:1")), filter.copy(name = "new"), "OWN")
|
||||
assertThat(SavedFilterCodec.decode(raw, "OWN").map { it.name }).containsExactly("new")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `list ids belong to their store`() {
|
||||
val raw = SavedFilterCodec.upsert(null, filter, "OWN")
|
||||
@@ -126,11 +120,25 @@ class SavedFilterCodecTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `remove drops only that filter, in either format`() {
|
||||
fun `remove drops only that filter`() {
|
||||
var raw = SavedFilterCodec.upsert(null, filter.copy(id = 1), "OWN")
|
||||
raw = SavedFilterCodec.upsert(raw, filter, "OWN") + "\n" + legacyLine(id = 7)
|
||||
assertThat(SavedFilterCodec.decode(SavedFilterCodec.remove(raw, 1), "OWN").map { it.id }).containsExactly(3L, 7L)
|
||||
assertThat(SavedFilterCodec.decode(SavedFilterCodec.remove(raw, 7), "OWN").map { it.id }).containsExactly(1L, 3L)
|
||||
raw = SavedFilterCodec.upsert(raw, filter, "OWN")
|
||||
assertThat(SavedFilterCodec.decode(SavedFilterCodec.remove(raw, 1), "OWN").map { it.id }).containsExactly(3L)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `saving keeps no other store's list pruned meanwhile`() {
|
||||
val both = SavedFilter(1, "f", FilterCriteria(listIds = setOf(1L), listsElsewhere = setOf("EXTERNAL:5", "EXTERNAL:6")))
|
||||
var raw = SavedFilterCodec.upsert(null, both, "OWN")
|
||||
val loaded = SavedFilterCodec.decode(raw, "OWN").single()
|
||||
raw = SavedFilterCodec.withoutLists(raw, "EXTERNAL") { it == 5L }
|
||||
raw = SavedFilterCodec.upsert(raw, loaded.copy(name = "renamed"), "OWN")
|
||||
assertThat(SavedFilterCodec.decode(raw, "OWN").single().criteria!!.listsElsewhere).containsExactly("EXTERNAL:6")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `flat lines are only read by the migration`() {
|
||||
assertThat(SavedFilterCodec.decode(legacyLine(id = 1), "OWN")).isEmpty()
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -151,7 +159,8 @@ class SavedFilterCodecTest {
|
||||
match = "ALL",
|
||||
priority = "MEDIUM",
|
||||
)
|
||||
val own = SavedFilterCodec.decode(raw, "OWN").single()
|
||||
val migrated = SavedFilterCodec.migrate(raw)
|
||||
val own = SavedFilterCodec.decode(migrated, "OWN").single()
|
||||
assertThat(own.name).isEqualTo("Work | today")
|
||||
assertThat(own.flat()).isEqualTo(
|
||||
FilterCriteria(
|
||||
@@ -163,7 +172,7 @@ class SavedFilterCodecTest {
|
||||
minPriority = Priority.MEDIUM,
|
||||
),
|
||||
)
|
||||
assertThat(SavedFilterCodec.decode(raw, "EXTERNAL").single().flat().listIds).containsExactly(2L)
|
||||
assertThat(SavedFilterCodec.decode(migrated, "EXTERNAL").single().flat().listIds).containsExactly(2L)
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -178,9 +187,17 @@ class SavedFilterCodecTest {
|
||||
assertThat(migrated.lines()).hasSize(3)
|
||||
assertThat(migrated.lines().all { it.startsWith("v2|") }).isTrue()
|
||||
assertThat(SavedFilterCodec.migrate(migrated)).isEqualTo(migrated)
|
||||
for (mode in listOf("OWN", "EXTERNAL")) {
|
||||
assertThat(SavedFilterCodec.decode(migrated, mode)).isEqualTo(SavedFilterCodec.decode(raw, mode))
|
||||
}
|
||||
val upcomingX = FilterCriteria(due = setOf(DueWindow.UPCOMING), tags = setOf("x"))
|
||||
assertThat(SavedFilterCodec.decode(migrated, "OWN").map { it.criteria }).containsExactly(
|
||||
upcomingX.copy(listIds = setOf(1L), listsElsewhere = setOf("EXTERNAL:5")),
|
||||
criteria,
|
||||
FilterCriteria(),
|
||||
).inOrder()
|
||||
assertThat(SavedFilterCodec.decode(migrated, "EXTERNAL").map { it.criteria }).containsExactly(
|
||||
upcomingX.copy(listIds = setOf(5L), listsElsewhere = setOf("OWN:1")),
|
||||
criteria.copy(listIds = emptySet(), listsElsewhere = setOf("OWN:4", "OWN:9")),
|
||||
FilterCriteria(listsElsewhere = setOf("OWN:nope")),
|
||||
).inOrder()
|
||||
// An unreadable list of this store reads as no list condition, as it did before.
|
||||
assertThat(SavedFilterCodec.decode(migrated, "OWN").single { it.id == 2L }.rule).isEqualTo(Rule.Everything)
|
||||
}
|
||||
@@ -198,13 +215,13 @@ class SavedFilterCodecTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `dropping lists prunes them from rules and flat lines alike`() {
|
||||
fun `dropping lists prunes them from every filter`() {
|
||||
val nested = SavedFilter(
|
||||
id = 5,
|
||||
name = "n",
|
||||
rule = Rule.Any(listOf(Rule.InList(setOf(1L)), Rule.Not(Rule.InList(setOf(1L, 2L))))),
|
||||
)
|
||||
val raw = SavedFilterCodec.upsert(null, nested, "OWN") + "\n" + legacyLine(id = 6, refs = listOf("OWN:1", "OWN:3"))
|
||||
val raw = SavedFilterCodec.upsert(null, nested, "OWN") + "\n" + SavedFilterCodec.migrate(legacyLine(id = 6, refs = listOf("OWN:1", "OWN:3")))
|
||||
val pruned = SavedFilterCodec.decode(SavedFilterCodec.withoutLists(raw, "OWN") { it == 1L }, "OWN")
|
||||
assertThat(pruned[0].rule).isEqualTo(Rule.Any(listOf(Rule.InList(emptySet()), Rule.Not(Rule.InList(setOf(2L))))))
|
||||
assertThat(pruned[1].flat().listIds).containsExactly(3L)
|
||||
|
||||
+5
-1
@@ -3,12 +3,15 @@ package de.jeanlucmakiola.agendula.data.prefs
|
||||
import com.google.common.truth.Truth.assertWithMessage
|
||||
import de.jeanlucmakiola.agendula.domain.DueWindow
|
||||
import de.jeanlucmakiola.agendula.domain.Priority
|
||||
import de.jeanlucmakiola.agendula.domain.RuleClock
|
||||
import de.jeanlucmakiola.agendula.domain.TagMatch
|
||||
import de.jeanlucmakiola.agendula.domain.Task
|
||||
import de.jeanlucmakiola.agendula.domain.TaskStatus
|
||||
import de.jeanlucmakiola.agendula.domain.dueWindowOf
|
||||
import de.jeanlucmakiola.agendula.domain.testTask
|
||||
import org.junit.jupiter.api.Test
|
||||
import java.time.DayOfWeek
|
||||
import java.time.ZoneId
|
||||
import kotlin.time.Instant
|
||||
|
||||
/** Every flat filter, migrated to a rule, keeps the tasks the 1.4 filter kept, in every store. */
|
||||
@@ -17,6 +20,7 @@ class SavedFilterMigrationEquivalenceTest {
|
||||
private val dayMs = 86_400_000L
|
||||
private val todayStart = Instant.fromEpochMilliseconds(1000 * dayMs)
|
||||
private val todayEnd = Instant.fromEpochMilliseconds(1001 * dayMs)
|
||||
private val clock = RuleClock(todayStart, todayEnd, ZoneId.systemDefault(), DayOfWeek.MONDAY)
|
||||
|
||||
/** The 1.4 filter as its codec read it for one store, and its `matches`. */
|
||||
private data class Legacy(
|
||||
@@ -105,7 +109,7 @@ class SavedFilterMigrationEquivalenceTest {
|
||||
val legacy = Legacy.read(refs, mode, due, tags, match, priority)
|
||||
val now = SavedFilterCodec.decode(migrated, mode).single()
|
||||
for (task in tasks) {
|
||||
if (now.matches(task, todayStart, todayEnd) != legacy.matches(task, todayStart, todayEnd)) {
|
||||
if (now.matches(task, clock) != legacy.matches(task, todayStart, todayEnd)) {
|
||||
mismatches += "$line in $mode: $task"
|
||||
}
|
||||
checked++
|
||||
|
||||
@@ -2,6 +2,7 @@ package de.jeanlucmakiola.agendula.domain
|
||||
|
||||
import com.google.common.truth.Truth.assertThat
|
||||
import org.junit.jupiter.api.Test
|
||||
import java.time.DayOfWeek
|
||||
import java.time.LocalDate
|
||||
import de.jeanlucmakiola.floret.time.DayWindow
|
||||
import java.time.ZoneId
|
||||
@@ -63,11 +64,11 @@ class NewTaskDefaultsTest {
|
||||
val upcomingEnd = UpcomingRange.WEEK.end(now, zone)
|
||||
val preset = SmartList.UPCOMING.dueWindows.presetDue(localToday)
|
||||
val dates = newTaskDates(DefaultDueDate.NEXT_MONTH, DefaultStartDate.NONE, preset, localToday, now) {
|
||||
it.showsIn(SmartList.UPCOMING, null, start, end, upcomingEnd)
|
||||
it.showsIn(SmartList.UPCOMING, null, start, end, DayOfWeek.MONDAY, upcomingEnd)
|
||||
}
|
||||
assertThat(dates.due).isEqualTo(allDayInstantOf(localToday.plusDays(1)))
|
||||
val unlimited = newTaskDates(DefaultDueDate.NEXT_MONTH, DefaultStartDate.NONE, preset, localToday, now) {
|
||||
it.showsIn(SmartList.UPCOMING, null, start, end)
|
||||
it.showsIn(SmartList.UPCOMING, null, start, end, DayOfWeek.MONDAY)
|
||||
}
|
||||
assertThat(unlimited.due).isEqualTo(allDayInstantOf(localToday.plusMonths(1)))
|
||||
}
|
||||
@@ -77,13 +78,13 @@ class NewTaskDefaultsTest {
|
||||
val (start, end) = DayWindow.today(now, zone)
|
||||
val upcoming = SmartList.UPCOMING.dueWindows.presetDue(localToday)
|
||||
fun inUpcoming(due: DefaultDueDate) = newTaskDates(due, DefaultStartDate.NONE, upcoming, localToday, now) {
|
||||
it.showsIn(SmartList.UPCOMING, null, start, end)
|
||||
it.showsIn(SmartList.UPCOMING, null, start, end, DayOfWeek.MONDAY)
|
||||
}.due
|
||||
assertThat(inUpcoming(DefaultDueDate.NEXT_WEEK)).isEqualTo(allDayInstantOf(localToday.plusWeeks(1)))
|
||||
assertThat(inUpcoming(DefaultDueDate.TODAY)).isEqualTo(allDayInstantOf(localToday.plusDays(1)))
|
||||
assertThat(inUpcoming(DefaultDueDate.NONE)).isEqualTo(allDayInstantOf(localToday.plusDays(1)))
|
||||
val noDate = newTaskDates(DefaultDueDate.TOMORROW, DefaultStartDate.NONE, null, localToday, now) {
|
||||
it.showsIn(SmartList.NO_DATE, null, start, end)
|
||||
it.showsIn(SmartList.NO_DATE, null, start, end, DayOfWeek.MONDAY)
|
||||
}
|
||||
assertThat(noDate.due).isNull()
|
||||
}
|
||||
@@ -93,7 +94,7 @@ class NewTaskDefaultsTest {
|
||||
val (start, end) = DayWindow.today(now, zone)
|
||||
fun inToday(startDefault: DefaultStartDate) =
|
||||
newTaskDates(DefaultDueDate.NEXT_WEEK, startDefault, localToday, localToday, now) {
|
||||
it.showsIn(SmartList.TODAY, null, start, end)
|
||||
it.showsIn(SmartList.TODAY, null, start, end, DayOfWeek.MONDAY)
|
||||
}.due
|
||||
assertThat(inToday(DefaultStartDate.TODAY)).isEqualTo(allDayInstantOf(localToday.plusWeeks(1)))
|
||||
assertThat(inToday(DefaultStartDate.NONE)).isEqualTo(allDayInstantOf(localToday))
|
||||
@@ -104,16 +105,16 @@ class NewTaskDefaultsTest {
|
||||
val (start, end) = DayWindow.today(now, zone)
|
||||
val filter = SavedFilter(id = 1L, name = "Soon", criteria = FilterCriteria(due = setOf(DueWindow.TODAY), listIds = setOf(9L)))
|
||||
val dates = newTaskDates(DefaultDueDate.TODAY, DefaultStartDate.NONE, localToday, localToday, now)
|
||||
assertThat(dates.showsIn(null, filter, start, end)).isTrue()
|
||||
assertThat(dates.showsIn(null, filter, start, end, DayOfWeek.MONDAY)).isTrue()
|
||||
val later = newTaskDates(DefaultDueDate.TOMORROW, DefaultStartDate.NONE, localToday, localToday, now)
|
||||
assertThat(later.showsIn(null, filter, start, end)).isFalse()
|
||||
assertThat(later.showsIn(null, filter, start, end, DayOfWeek.MONDAY)).isFalse()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a view no new task shows in keeps the default due`() {
|
||||
val (start, end) = DayWindow.today(now, zone)
|
||||
val dates = newTaskDates(DefaultDueDate.TOMORROW, DefaultStartDate.NONE, null, localToday, now) {
|
||||
it.showsIn(SmartList.OVERDUE, null, start, end)
|
||||
it.showsIn(SmartList.OVERDUE, null, start, end, DayOfWeek.MONDAY)
|
||||
}
|
||||
assertThat(dates.due).isEqualTo(allDayInstantOf(localToday.plusDays(1)))
|
||||
}
|
||||
|
||||
@@ -202,8 +202,8 @@ class RuleTest {
|
||||
assertThat(Rule.Not(thisWeek).dueVerdict(later, clock)).isTrue()
|
||||
assertThat(Rule.Everything.dueVerdict(later, clock)).isTrue()
|
||||
val filter = SavedFilter(1, "f", rule)
|
||||
assertThat(filter.admitsDueOf(inWeek, clock.todayStart, clock.todayEnd, DayOfWeek.MONDAY)).isTrue()
|
||||
assertThat(filter.admitsDueOf(later, clock.todayStart, clock.todayEnd, DayOfWeek.MONDAY)).isFalse()
|
||||
assertThat(filter.admitsDueOf(inWeek, clock)).isTrue()
|
||||
assertThat(filter.admitsDueOf(later, clock)).isFalse()
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -2,6 +2,7 @@ package de.jeanlucmakiola.agendula.domain
|
||||
|
||||
import com.google.common.truth.Truth.assertThat
|
||||
import org.junit.jupiter.api.Test
|
||||
import java.time.DayOfWeek
|
||||
import java.time.LocalDate
|
||||
import java.time.ZoneId
|
||||
import kotlin.time.Instant
|
||||
@@ -15,7 +16,7 @@ class SavedFilterTest {
|
||||
private val today = Instant.fromEpochMilliseconds(1000 * dayMs + 1)
|
||||
private val upcoming = Instant.fromEpochMilliseconds(1002 * dayMs)
|
||||
|
||||
private fun SavedFilter.matches(task: Task) = matches(task, todayStart, todayEnd)
|
||||
private fun SavedFilter.matches(task: Task) = matches(task, RuleClock(todayStart, todayEnd, ZoneId.systemDefault(), DayOfWeek.MONDAY))
|
||||
|
||||
private val empty = SavedFilter(id = 1, name = "f")
|
||||
|
||||
|
||||
Reference in new issue
Block a user