Reverse-solve a view's scope into what a new task gets, and build quick-add tasks from it (#186)

This commit is contained in:
makiolaj committed 2026-11-27 17:57:35 +01:00
1 parent 5032434a7d
commit be1b270e5c
5 files changed
+391

No files matched your search

@@ -15,6 +15,16 @@ import de.jeanlucmakiola.agendula.domain.DefaultDueDate
import de.jeanlucmakiola.agendula.domain.DefaultStartDate
import de.jeanlucmakiola.agendula.domain.NewTaskDates
import de.jeanlucmakiola.agendula.domain.SavedFilter
import de.jeanlucmakiola.agendula.domain.QuickAddParse
import de.jeanlucmakiola.agendula.domain.Rule
import de.jeanlucmakiola.agendula.domain.RuleClock
import de.jeanlucmakiola.agendula.domain.TaskList
import de.jeanlucmakiola.agendula.domain.datesForDue
import de.jeanlucmakiola.agendula.domain.datesForTimedDue
import de.jeanlucmakiola.agendula.domain.defaultTaskList
import de.jeanlucmakiola.agendula.domain.dueVerdict
import de.jeanlucmakiola.agendula.domain.openTaskDated
import de.jeanlucmakiola.agendula.domain.scopePreset
import de.jeanlucmakiola.agendula.domain.localeWeekStart
import de.jeanlucmakiola.agendula.domain.newTaskDates
import de.jeanlucmakiola.agendula.domain.Task
@@ -258,6 +268,45 @@ data class Settings(
)
}
/**
* A task from the quick-add bar, in a view of [scope] (`null` for none) read
* against [clock]; `null` when there is no list to put it in. It goes to the
* list [parse] names, else the one the scope keeps tasks to, else the default
* list, and takes the tags, priority and, without a date of its own, the due
* date that keep it in the scope (see [scopePreset]). [writable] are the lists that take tasks.
*/
fun quickAddTask(
parse: QuickAddParse,
scope: Rule?,
writable: List<TaskList>,
clock: RuleClock,
now: Instant = Clock.System.now(),
): TaskForm? {
val preset = scope.scopePreset(clock)
val listId = parse.list?.id?.takeIf { id -> writable.any { it.id == id } }
?: preset.listId?.takeIf { id -> writable.any { it.id == id } }
?: defaultTaskList(writable, defaultListId)
?: return null
val due = parse.due
val dates = when {
due == null -> defaultNewTaskDates(clock.today, now, preset.dueDate) { dates ->
scope == null || scope.dueVerdict(openTaskDated(dates.start, dates.due, dates.isAllDay), clock) != false
}
due.time == null -> datesForDue(due.date, defaultStartDate, clock.today, now)
else -> datesForTimedDue(due.date, due.time, defaultStartDate, clock.today, now, clock.zone)
}
return TaskForm(
title = parse.title,
listId = listId,
start = dates.start,
due = dates.due,
isAllDay = dates.isAllDay,
startsNow = dates.start == now,
priority = preset.priority,
tags = preset.tags,
)
}
fun persistenceFor(listId: Long): ListPersistence = perListPersistentReminders[listId] ?: ListPersistence.INHERIT
/** Whether a reminder for a task in [listId] is persistent: its list's override, else the app setting. */
@@ -1,6 +1,9 @@
package de.jeanlucmakiola.agendula.domain
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.LocalTime
import java.time.ZoneId
import kotlin.time.Instant
/** The due date a new task starts with, counted from the day it is created. */
@@ -49,6 +52,28 @@ fun datesForDue(dueDate: LocalDate?, start: DefaultStartDate, today: LocalDate,
return NewTaskDates(start = startDate?.let(::allDayInstantOf), due = allDayInstantOf(dueDate), isAllDay = true)
}
/**
* The dates a new task due at [time] on [dueDate] opens with: the default [start]
* goes along at that time, a start of today being [now]; never after the due.
*/
fun datesForTimedDue(
dueDate: LocalDate,
time: LocalTime,
start: DefaultStartDate,
today: LocalDate,
now: Instant,
zone: ZoneId = ZoneId.systemDefault(),
): NewTaskDates {
fun at(date: LocalDate) = Instant.fromEpochSeconds(LocalDateTime.of(date, time).atZone(zone).toEpochSecond())
val due = at(dueDate)
val startAt = when (start) {
DefaultStartDate.NONE -> null
DefaultStartDate.TODAY -> now
else -> start.dateFor(dueDate, today)?.let(::at)
}
return NewTaskDates(start = startAt?.let { minOf(it, due) }, due = due, isAllDay = false)
}
/**
* A new task's dates from the [due] and [start] defaults, or from [presetDue]
* when there is no default due, or when the view the task is added from would
@@ -88,3 +113,14 @@ fun NewTaskDates.showsIn(
else -> true
}
}
/**
* The list a new task goes to when nothing names one: the default list when it
* takes tasks and is one of [within] (any when empty), else the first of
* [within], else the first synced list, else any. [writable] are the lists that take tasks.
*/
fun defaultTaskList(writable: List<TaskList>, defaultListId: Long?, within: Set<Long> = emptySet()): Long? =
defaultListId?.takeIf { id -> writable.any { it.id == id } && (within.isEmpty() || id in within) }
?: writable.firstOrNull { it.id in within }?.id
?: writable.firstOrNull { !it.isLocal }?.id
?: writable.firstOrNull()?.id
@@ -1,5 +1,6 @@
package de.jeanlucmakiola.agendula.domain
import java.time.LocalDate
import kotlin.time.Instant
/** The lists this rule names anywhere in it, as a condition or under a [Rule.Not]. */
@@ -21,6 +22,53 @@ fun Rule.requiredTag(): String? = when (this) {
else -> null
}
/**
* What a new task gets so that a scope admits it, read from the conditions every
* task in it meets: the [listId] when they leave one list, their [tags], the
* least [priority] and a [dueDate] (see [scopePreset]).
*/
data class ScopePreset(
val listId: Long? = null,
val tags: List<String> = emptyList(),
val priority: Priority = Priority.NONE,
val dueDate: LocalDate? = null,
)
/**
* The [ScopePreset] of this scope. Only the conditions at its top, alone or
* joined by all-of, count; one under any-of or not says nothing a task can be
* given. The due date is the first of none, today and tomorrow that the top's
* due conditions all admit; none of them, or no due condition, leaves it empty.
*/
fun Rule?.scopePreset(clock: RuleClock): ScopePreset {
val required = this?.requiredConditions().orEmpty()
if (required.isEmpty()) return ScopePreset()
val lists = required.filterIsInstance<Rule.InList>().map { it.listIds }.reduceOrNull { a, b -> a intersect b }
val dues = required.filterIsInstance<Rule.Due>()
val dueDate = if (dues.isEmpty()) {
null
} else {
listOf(clock.today, clock.today.plusDays(1))
.takeUnless { dues.all { it.spec.matches(openTaskDated(null, null, false), clock) } }
?.firstOrNull { day -> dues.all { it.spec.matches(openTaskDated(null, allDayInstantOf(day), true), clock) } }
}
return ScopePreset(
listId = lists?.singleOrNull(),
tags = required.filterIsInstance<Rule.HasTag>().map { it.tag }.distinct(),
priority = required.filterIsInstance<Rule.MinPriority>().maxOfOrNull { it.priority } ?: Priority.NONE,
dueDate = dueDate,
)
}
private fun Rule.requiredConditions(): List<Rule> = when (this) {
is Rule.AllOf -> children.flatMap { it.requiredConditions() }
is Rule.AnyOf, is Rule.Not -> emptyList()
is Rule.InList, is Rule.HasTag, is Rule.MinPriority, is Rule.Due -> listOf(this)
}
/** An open task with this form's list, tags, priority and dates, to ask a scope whether it shows it. */
fun TaskForm.asOpenTask(): Task = openTaskDated(start, due, isAllDay).copy(listId = listId, tags = tags, priority = priority)
/**
* The lists a task has to be in to pass this rule, or `null` when a task of any
* list can. Read from list conditions joined by [Rule.AllOf] and [Rule.AnyOf]; a
@@ -0,0 +1,169 @@
package de.jeanlucmakiola.agendula.data.prefs
import com.google.common.truth.Truth.assertThat
import de.jeanlucmakiola.agendula.domain.DefaultDueDate
import de.jeanlucmakiola.agendula.domain.DefaultStartDate
import de.jeanlucmakiola.agendula.domain.DueSpec
import de.jeanlucmakiola.agendula.domain.DueWindow
import de.jeanlucmakiola.agendula.domain.LocalAccount
import de.jeanlucmakiola.agendula.domain.Priority
import de.jeanlucmakiola.agendula.domain.QuickAddParse
import de.jeanlucmakiola.agendula.domain.QuickDue
import de.jeanlucmakiola.agendula.domain.Rule
import de.jeanlucmakiola.agendula.domain.RuleClock
import de.jeanlucmakiola.agendula.domain.TaskList
import de.jeanlucmakiola.agendula.domain.allDayInstantOf
import de.jeanlucmakiola.agendula.domain.asOpenTask
import de.jeanlucmakiola.agendula.domain.matches
import de.jeanlucmakiola.agendula.domain.startOfDay
import org.junit.jupiter.api.Test
import java.time.DayOfWeek
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.LocalTime
import java.time.ZoneId
import kotlin.time.Instant
class QuickAddTaskTest {
private val zone = ZoneId.of("Europe/Berlin")
private val today = LocalDate.of(2026, 11, 27)
private val clock = RuleClock(startOfDay(today, zone), startOfDay(today.plusDays(1), zone), zone, DayOfWeek.MONDAY)
private val now = at(today, LocalTime.of(10, 0))
private fun at(date: LocalDate, time: LocalTime) =
Instant.fromEpochSeconds(LocalDateTime.of(date, time).atZone(zone).toEpochSecond())
private fun list(id: Long, name: String, local: Boolean = false, readOnly: Boolean = false) = TaskList(
id, name, 0, if (local) "Local" else "me",
if (local) LocalAccount.TYPE else "bitfire.at.davdroid",
isSynced = !local, isVisible = true, owner = null, isReadOnly = readOnly,
)
private val work = list(1, "Work")
private val errands = list(2, "Errands")
private val inbox = list(3, "Inbox", local = true)
private val writable = listOf(work, errands, inbox)
private val settings = Settings(defaultListId = 2)
private fun add(
parse: QuickAddParse,
scope: Rule? = null,
settings: Settings = this.settings,
lists: List<de.jeanlucmakiola.agendula.domain.TaskList> = writable,
) = settings.quickAddTask(parse, scope, lists, clock, now)
@Test
fun `without a list or scope the task goes to the default list`() {
val form = add(QuickAddParse("Buy milk"))!!
assertThat(form.title).isEqualTo("Buy milk")
assertThat(form.listId).isEqualTo(2L)
assertThat(form.due).isNull()
assertThat(form.tags).isEmpty()
}
@Test
fun `a default list gone or read-only falls back to the first synced list`() {
assertThat(add(QuickAddParse("x"), settings = Settings(defaultListId = 9))!!.listId).isEqualTo(1L)
assertThat(add(QuickAddParse("x"), lists = listOf(inbox, work))!!.listId).isEqualTo(1L)
assertThat(add(QuickAddParse("x"), lists = listOf(inbox))!!.listId).isEqualTo(3L)
assertThat(add(QuickAddParse("x"), lists = emptyList())).isNull()
}
@Test
fun `a named list wins over the scope's and the default`() {
val form = add(QuickAddParse("x", list = inbox), scope = Rule.InList(setOf(1L)))!!
assertThat(form.listId).isEqualTo(3L)
}
@Test
fun `the scope's one list comes before the default`() {
assertThat(add(QuickAddParse("x"), scope = Rule.InList(setOf(1L)))!!.listId).isEqualTo(1L)
assertThat(add(QuickAddParse("x"), scope = Rule.InList(setOf(1L, 3L)))!!.listId).isEqualTo(2L)
// A scope list the task can't go to isn't one.
assertThat(add(QuickAddParse("x"), scope = Rule.InList(setOf(9L)))!!.listId).isEqualTo(2L)
}
@Test
fun `scopes with any-of or not fall back to the default list`() {
val gtd = Rule.AnyOf(listOf(Rule.InList(setOf(1L)), Rule.HasTag("next")))
val form = add(QuickAddParse("x"), scope = gtd)!!
assertThat(form.listId).isEqualTo(2L)
assertThat(form.tags).isEmpty()
assertThat(gtd.matches(form.asOpenTask(), clock)).isFalse()
}
@Test
fun `the scope's tags and priority come along`() {
val scope = Rule.AllOf(listOf(Rule.InList(setOf(1L)), Rule.HasTag("x"), Rule.MinPriority(Priority.MEDIUM)))
val form = add(QuickAddParse("x"), scope = scope)!!
assertThat(form.listId).isEqualTo(1L)
assertThat(form.tags).containsExactly("x")
assertThat(form.priority).isEqualTo(Priority.MEDIUM)
assertThat(scope.matches(form.asOpenTask(), clock)).isTrue()
}
@Test
fun `a scope on due dates presets one, unless the default due date already fits`() {
val upcoming = Rule.Due(DueSpec.Window(DueWindow.UPCOMING))
val form = add(QuickAddParse("x"), scope = upcoming)!!
assertThat(form.due).isEqualTo(allDayInstantOf(today.plusDays(1)))
assertThat(form.isAllDay).isTrue()
assertThat(upcoming.matches(form.asOpenTask(), clock)).isTrue()
val nextWeek = Settings(defaultListId = 2, defaultDueDate = DefaultDueDate.NEXT_WEEK)
assertThat(add(QuickAddParse("x"), scope = upcoming, settings = nextWeek)!!.due).isEqualTo(allDayInstantOf(today.plusWeeks(1)))
val todayOnly = Rule.Due(DueSpec.Window(DueWindow.TODAY))
assertThat(add(QuickAddParse("x"), scope = todayOnly, settings = nextWeek)!!.due).isEqualTo(allDayInstantOf(today))
}
@Test
fun `without a scope the default due date applies`() {
val tomorrow = Settings(defaultListId = 2, defaultDueDate = DefaultDueDate.TOMORROW)
assertThat(add(QuickAddParse("x"), settings = tomorrow)!!.due).isEqualTo(allDayInstantOf(today.plusDays(1)))
}
@Test
fun `a typed date is all day, and wins over the scope's and the default`() {
val tomorrow = Settings(defaultListId = 2, defaultDueDate = DefaultDueDate.TOMORROW)
val form = add(QuickAddParse("x", due = QuickDue(today.plusDays(5))), scope = Rule.Due(DueSpec.Window(DueWindow.TODAY)), settings = tomorrow)!!
assertThat(form.due).isEqualTo(allDayInstantOf(today.plusDays(5)))
assertThat(form.isAllDay).isTrue()
}
@Test
fun `a typed time makes the due timed, with the default start at that time`() {
val due = QuickDue(today.plusDays(2), LocalTime.of(14, 30))
val plain = add(QuickAddParse("x", due = due))!!
assertThat(plain.due).isEqualTo(at(today.plusDays(2), LocalTime.of(14, 30)))
assertThat(plain.isAllDay).isFalse()
assertThat(plain.start).isNull()
val dayBefore = Settings(defaultListId = 2, defaultStartDate = DefaultStartDate.DAY_BEFORE_DUE)
assertThat(add(QuickAddParse("x", due = due), settings = dayBefore)!!.start).isEqualTo(at(today.plusDays(1), LocalTime.of(14, 30)))
val startToday = Settings(defaultListId = 2, defaultStartDate = DefaultStartDate.TODAY)
val form = add(QuickAddParse("x", due = due), settings = startToday)!!
assertThat(form.start).isEqualTo(now)
assertThat(form.startsNow).isTrue()
}
@Test
fun `a time already past today never puts the start after the due`() {
val startToday = Settings(defaultListId = 2, defaultStartDate = DefaultStartDate.TODAY)
val form = add(QuickAddParse("x", due = QuickDue(today, LocalTime.of(8, 0))), settings = startToday)!!
assertThat(form.start).isEqualTo(form.due)
assertThat(form.validate()).isEmpty()
}
@Test
fun `a task the scope won't show is told apart`() {
val scope = Rule.AllOf(listOf(Rule.InList(setOf(1L)), Rule.Due(DueSpec.Window(DueWindow.TODAY))))
val shown = add(QuickAddParse("x"), scope = scope)!!
assertThat(scope.matches(shown.asOpenTask(), clock)).isTrue()
val elsewhere = add(QuickAddParse("x", list = errands), scope = scope)!!
assertThat(scope.matches(elsewhere.asOpenTask(), clock)).isFalse()
val later = add(QuickAddParse("x", due = QuickDue(today.plusDays(3))), scope = scope)!!
assertThat(scope.matches(later.asOpenTask(), clock)).isFalse()
}
}
@@ -0,0 +1,89 @@
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
class ScopePresetTest {
private val zone = ZoneId.of("Europe/Berlin")
// A Thursday.
private val today = LocalDate.of(2026, 11, 26)
private fun clockOn(day: LocalDate) = RuleClock(startOfDay(day, zone), startOfDay(day.plusDays(1), zone), zone, DayOfWeek.MONDAY)
private val clock = clockOn(today)
private fun all(vararg rules: Rule) = Rule.AllOf(rules.toList())
private fun any(vararg rules: Rule) = Rule.AnyOf(rules.toList())
private fun due(window: DueWindow) = Rule.Due(DueSpec.Window(window))
@Test
fun `no scope presets nothing`() {
assertThat((null as Rule?).scopePreset(clock)).isEqualTo(ScopePreset())
assertThat(Rule.Everything.scopePreset(clock)).isEqualTo(ScopePreset())
}
@Test
fun `one list the scope keeps tasks to is the list`() {
assertThat(Rule.InList(setOf(1L)).scopePreset(clock).listId).isEqualTo(1L)
assertThat(Rule.InList(setOf(1L, 2L)).scopePreset(clock).listId).isNull()
assertThat(all(Rule.InList(setOf(1L, 2L)), Rule.InList(setOf(2L, 3L))).scopePreset(clock).listId).isEqualTo(2L)
assertThat(all(Rule.InList(setOf(1L)), Rule.InList(setOf(2L))).scopePreset(clock).listId).isNull()
}
@Test
fun `the conditions at the top give list, tags and priority`() {
val scope = all(
Rule.InList(setOf(1L)),
Rule.HasTag("x"),
all(Rule.HasTag("y"), Rule.HasTag("x")),
Rule.MinPriority(Priority.LOW),
Rule.MinPriority(Priority.MEDIUM),
)
assertThat(scope.scopePreset(clock)).isEqualTo(
ScopePreset(listId = 1L, tags = listOf("x", "y"), priority = Priority.MEDIUM),
)
}
@Test
fun `nothing under any-of or not is solved for`() {
assertThat(any(Rule.InList(setOf(1L)), Rule.HasTag("x")).scopePreset(clock)).isEqualTo(ScopePreset())
assertThat(Rule.Not(Rule.InList(setOf(1L))).scopePreset(clock)).isEqualTo(ScopePreset())
assertThat(any(Rule.InList(setOf(1L)), Rule.InList(setOf(1L))).scopePreset(clock)).isEqualTo(ScopePreset())
val mixed = all(Rule.InList(setOf(1L)), any(Rule.HasTag("a"), Rule.HasTag("b")), Rule.Not(Rule.HasTag("c")))
assertThat(mixed.scopePreset(clock)).isEqualTo(ScopePreset(listId = 1L))
}
@Test
fun `the due date is the first of none, today and tomorrow the scope admits`() {
assertThat(due(DueWindow.TODAY).scopePreset(clock).dueDate).isEqualTo(today)
assertThat(due(DueWindow.UPCOMING).scopePreset(clock).dueDate).isEqualTo(today.plusDays(1))
assertThat(due(DueWindow.NO_DATE).scopePreset(clock).dueDate).isNull()
assertThat(due(DueWindow.OVERDUE).scopePreset(clock).dueDate).isNull()
assertThat(Rule.Due(DueSpec.ThisWeek).scopePreset(clock).dueDate).isEqualTo(today)
assertThat(Rule.Due(DueSpec.NextDays(3)).scopePreset(clock).dueDate).isEqualTo(today)
assertThat(Rule.HasTag("x").scopePreset(clock).dueDate).isNull()
}
@Test
fun `due conditions together all have to admit the day`() {
val upcomingThisWeek = all(due(DueWindow.UPCOMING), Rule.Due(DueSpec.ThisWeek))
assertThat(upcomingThisWeek.scopePreset(clock).dueDate).isEqualTo(today.plusDays(1))
// On the week's last day, tomorrow is next week's.
assertThat(upcomingThisWeek.scopePreset(clockOn(LocalDate.of(2026, 11, 29))).dueDate).isNull()
assertThat(any(due(DueWindow.TODAY), due(DueWindow.UPCOMING)).scopePreset(clock).dueDate).isNull()
}
@Test
fun `a form reads as the open task it makes`() {
val form = TaskForm(title = "t", listId = 3, tags = listOf("x"), priority = Priority.HIGH, due = allDayInstantOf(today), isAllDay = true)
val task = form.asOpenTask()
assertThat(task.listId).isEqualTo(3L)
assertThat(task.tags).containsExactly("x")
assertThat(task.priority).isEqualTo(Priority.HIGH)
assertThat(task.isClosed).isFalse()
assertThat(due(DueWindow.TODAY).matches(task, clock)).isTrue()
}
}