diff --git a/app/src/main/java/de/jeanlucmakiola/agendula/domain/Rule.kt b/app/src/main/java/de/jeanlucmakiola/agendula/domain/Rule.kt new file mode 100644 index 0000000..e07116f --- /dev/null +++ b/app/src/main/java/de/jeanlucmakiola/agendula/domain/Rule.kt @@ -0,0 +1,146 @@ +package de.jeanlucmakiola.agendula.domain + +import java.time.DayOfWeek +import java.time.LocalDate +import java.time.ZoneId +import java.time.temporal.TemporalAdjusters +import java.time.temporal.WeekFields +import java.util.Locale +import kotlin.time.Instant + +/** + * A condition on a task, nestable with [All], [Any] and [Not]. An empty [All] + * passes every task, an empty [Any] none. Whether a task is open or done is not + * a rule: the view a rule is used in decides that. + */ +sealed interface Rule { + data class All(val children: List) : Rule + data class Any(val children: List) : Rule + data class Not(val child: Rule) : Rule + + /** + * In one of [listIds], this store's lists. [elsewhere] holds the lists of + * other task stores as stored, so saving here keeps them; they never match + * a task here, so with no [listIds] this matches nothing. + */ + data class InList(val listIds: Set, val elsewhere: Set = emptySet()) : Rule + + /** Carries [tag], compared exactly. */ + data class HasTag(val tag: String) : Rule + + /** At [priority] or higher. */ + data class MinPriority(val priority: Priority) : Rule + + data class Due(val window: DueSpec) : Rule + + companion object { + /** The rule every task passes. */ + val Everything: Rule = All(emptyList()) + } +} + +/** When a task is due: one of the [DueWindow]s, or a range of days around today. */ +sealed interface DueSpec { + data class Window(val window: DueWindow) : DueSpec + + /** Due on a day in a range relative to today; an undated task is in none. */ + sealed interface Range : DueSpec { + /** The first day of the range and the day after its last, when today is [today]. */ + fun days(today: LocalDate, weekStart: DayOfWeek): Pair + } + + data object Today : Range { + override fun days(today: LocalDate, weekStart: DayOfWeek) = today to today.plusDays(1) + } + + /** The calendar week holding today, starting on [weekStart]. */ + data object ThisWeek : Range { + override fun days(today: LocalDate, weekStart: DayOfWeek): Pair { + val first = today.with(TemporalAdjusters.previousOrSame(weekStart)) + return first to first.plusWeeks(1) + } + } + + /** The calendar month holding today. */ + data object ThisMonth : Range { + override fun days(today: LocalDate, weekStart: DayOfWeek): Pair { + val first = today.withDayOfMonth(1) + return first to first.plusMonths(1) + } + } + + /** Today and the [days] after it, the way [UpcomingRange] reaches ahead. */ + data class NextDays(val days: Int) : Range { + init { + require(days >= 0) { "days must not be negative: $days" } + } + + override fun days(today: LocalDate, weekStart: DayOfWeek) = today to today.plusDays(days + 1L) + } +} + +/** + * The day rules are read against: local today from [todayStart] to [todayEnd] + * (see [DayWindow]) in [zone], with weeks starting on [weekStart]. + */ +data class RuleClock( + val todayStart: Instant, + val todayEnd: Instant, + val zone: ZoneId = ZoneId.systemDefault(), + val weekStart: DayOfWeek = localeWeekStart(), +) { + val today: LocalDate get() = todayStart.calendarDate(allDay = false, zone = zone) +} + +/** The first day of the week in [locale]; what an unset week start means. */ +fun localeWeekStart(locale: Locale = Locale.getDefault()): DayOfWeek = WeekFields.of(locale).firstDayOfWeek + +/** Whether [task] passes this rule, whatever its status. */ +fun Rule.matches(task: Task, clock: RuleClock): Boolean = when (this) { + is Rule.All -> children.all { it.matches(task, clock) } + is Rule.Any -> children.any { it.matches(task, clock) } + is Rule.Not -> !child.matches(task, clock) + 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) +} + +/** Whether [task]'s due falls in this spec. */ +fun DueSpec.matches(task: Task, clock: RuleClock): Boolean = when (this) { + is DueSpec.Window -> dueWindowOf(task, clock.todayStart, clock.todayEnd, clock.zone) == window + is DueSpec.Range -> { + val (first, afterLast) = days(clock.today, clock.weekStart) + val due = task.wallClock(TaskEnd.DUE, clock.zone) + due != null && due >= startOfDay(first, clock.zone) && due < startOfDay(afterLast, clock.zone) + } +} + +/** + * This rule without the lists [gone] names, e.g. lists since deleted. A list + * condition left without lists, or that never had any, drops out, as do groups it leaves empty, so the + * rule reads as if it had never been there; `null` when nothing is left. + */ +fun Rule.withoutLists(gone: (Long) -> Boolean): Rule? = when (this) { + is Rule.InList -> { + val kept = listIds.filterNotTo(HashSet(), gone) + when { + kept.isEmpty() && elsewhere.isEmpty() -> null + kept.size == listIds.size -> this + else -> copy(listIds = kept) + } + } + is Rule.All -> prunedGroup(children, gone)?.let(Rule::All) + is Rule.Any -> prunedGroup(children, gone)?.let(Rule::Any) + is Rule.Not -> child.withoutLists(gone)?.let { if (it === child) this else Rule.Not(it) } + is Rule.HasTag, is Rule.MinPriority, is Rule.Due -> this +} + +private fun prunedGroup(children: List, gone: (Long) -> Boolean): List? { + val kept = children.mapNotNull { it.withoutLists(gone) } + return when { + kept.isEmpty() && children.isNotEmpty() -> null + kept == children -> children + else -> kept + } +} diff --git a/app/src/main/java/de/jeanlucmakiola/agendula/domain/UpcomingRange.kt b/app/src/main/java/de/jeanlucmakiola/agendula/domain/UpcomingRange.kt index 997dbed..860ca24 100644 --- a/app/src/main/java/de/jeanlucmakiola/agendula/domain/UpcomingRange.kt +++ b/app/src/main/java/de/jeanlucmakiola/agendula/domain/UpcomingRange.kt @@ -22,10 +22,14 @@ enum class UpcomingRange { fun end(now: Instant, zone: ZoneId = ZoneId.systemDefault()): Instant? { val today = now.calendarDate(allDay = false, zone = zone) val last = lastDay(today) ?: return null - return Instant.fromEpochMilliseconds(last.plusDays(1).atStartOfDay(zone).toInstant().toEpochMilli()) + return startOfDay(last.plusDays(1), zone) } } +/** Local midnight starting [date] in [zone]. */ +fun startOfDay(date: LocalDate, zone: ZoneId = ZoneId.systemDefault()): Instant = + Instant.fromEpochMilliseconds(date.atStartOfDay(zone).toInstant().toEpochMilli()) + /** Whether [task] is due before [end]; an undated task never is. */ fun dueBefore(task: Task, end: Instant, zone: ZoneId = ZoneId.systemDefault()): Boolean = task.wallClock(TaskEnd.DUE, zone)?.let { it < end } ?: false diff --git a/app/src/main/java/de/jeanlucmakiola/agendula/ui/common/TimeFormat.kt b/app/src/main/java/de/jeanlucmakiola/agendula/ui/common/TimeFormat.kt index 3dbee55..d3ffb29 100644 --- a/app/src/main/java/de/jeanlucmakiola/agendula/ui/common/TimeFormat.kt +++ b/app/src/main/java/de/jeanlucmakiola/agendula/ui/common/TimeFormat.kt @@ -1,6 +1,7 @@ package de.jeanlucmakiola.agendula.ui.common import androidx.compose.runtime.staticCompositionLocalOf +import de.jeanlucmakiola.agendula.domain.localeWeekStart import java.time.DayOfWeek import java.time.LocalTime import java.time.format.DateTimeFormatter @@ -22,4 +23,4 @@ fun formatMinuteOfDay(minuteOfDay: Int, is24Hour: Boolean, locale: Locale = Loca .format(DateTimeFormatter.ofPattern(if (is24Hour) "HH:mm" else "h:mm a", locale)) /** The locale's first day of the week, for a week start left on automatic. */ -fun localeFirstDayOfWeek(locale: Locale = Locale.getDefault()): DayOfWeek = WeekFields.of(locale).firstDayOfWeek +fun localeFirstDayOfWeek(locale: Locale = Locale.getDefault()): DayOfWeek = localeWeekStart(locale) diff --git a/app/src/test/java/de/jeanlucmakiola/agendula/domain/RuleTest.kt b/app/src/test/java/de/jeanlucmakiola/agendula/domain/RuleTest.kt new file mode 100644 index 0000000..00e374c --- /dev/null +++ b/app/src/test/java/de/jeanlucmakiola/agendula/domain/RuleTest.kt @@ -0,0 +1,166 @@ +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.LocalTime +import java.time.ZoneId +import kotlin.time.Instant + +class RuleTest { + + private val zone = ZoneId.of("Europe/Berlin") + + private fun clockOn(day: LocalDate, weekStart: DayOfWeek = DayOfWeek.MONDAY, zone: ZoneId = this.zone) = + RuleClock(startOfDay(day, zone), startOfDay(day.plusDays(1), zone), zone, weekStart) + + private fun dueOn(day: LocalDate) = testTask(due = allDayInstantOf(day), isAllDay = true) + + private fun dueAt(day: LocalDate, time: LocalTime, zone: ZoneId = this.zone) = + testTask(due = Instant.fromEpochMilliseconds(day.atTime(time).atZone(zone).toInstant().toEpochMilli())) + + private fun Rule.matches(task: Task, day: LocalDate, weekStart: DayOfWeek = DayOfWeek.MONDAY) = + matches(task, clockOn(day, weekStart)) + + private fun DueSpec.Range.daysIn(today: LocalDate, weekStart: DayOfWeek = DayOfWeek.MONDAY): List = + (-40L..40L).map { today.plusDays(it) }.filter { Rule.Due(this).matches(dueOn(it), today, weekStart) } + + @Test + fun `groups combine their children`() { + val work = Rule.HasTag("work") + val high = Rule.MinPriority(Priority.HIGH) + val task = testTask(tags = listOf("work"), priority = Priority.LOW) + val day = LocalDate.of(2026, 11, 26) + assertThat(Rule.All(listOf(work, high)).matches(task, day)).isFalse() + assertThat(Rule.Any(listOf(work, high)).matches(task, day)).isTrue() + assertThat(Rule.Not(work).matches(task, day)).isFalse() + assertThat(Rule.Not(high).matches(task, day)).isTrue() + assertThat(Rule.All(emptyList()).matches(task, day)).isTrue() + assertThat(Rule.Any(emptyList()).matches(task, day)).isFalse() + } + + @Test + fun `next actions are a list or a tag`() { + val day = LocalDate.of(2026, 11, 26) + val rule = Rule.All( + listOf( + Rule.Any(listOf(Rule.InList(setOf(7L)), Rule.HasTag("next"))), + Rule.Not(Rule.Any(listOf(Rule.HasTag("someday"), Rule.InList(setOf(9L))))), + ), + ) + assertThat(rule.matches(testTask(listId = 7), day)).isTrue() + assertThat(rule.matches(testTask(listId = 1, tags = listOf("next")), day)).isTrue() + assertThat(rule.matches(testTask(listId = 1), day)).isFalse() + assertThat(rule.matches(testTask(listId = 7, tags = listOf("someday")), day)).isFalse() + assertThat(rule.matches(testTask(listId = 9, tags = listOf("next")), day)).isFalse() + assertThat(Rule.Not(Rule.Not(Rule.InList(setOf(7L)))).matches(testTask(listId = 7), day)).isTrue() + } + + @Test + fun `tags compare exactly and other stores' lists never match here`() { + val day = LocalDate.of(2026, 11, 26) + assertThat(Rule.HasTag("work").matches(testTask(tags = listOf("Work")), day)).isFalse() + assertThat(Rule.InList(emptySet(), setOf("EXTERNAL:1")).matches(testTask(listId = 1), day)).isFalse() + } + + @Test + fun `due windows read as before`() { + val day = LocalDate.of(2026, 11, 26) + fun window(w: DueWindow) = Rule.Due(DueSpec.Window(w)) + assertThat(window(DueWindow.TODAY).matches(dueAt(day, LocalTime.of(23, 59)), day)).isTrue() + assertThat(window(DueWindow.OVERDUE).matches(dueOn(day.minusDays(1)), day)).isTrue() + assertThat(window(DueWindow.UPCOMING).matches(dueOn(day.plusDays(1)), day)).isTrue() + assertThat(window(DueWindow.NO_DATE).matches(testTask(), day)).isTrue() + assertThat(window(DueWindow.NO_DATE).matches(dueOn(day), day)).isFalse() + } + + @Test + fun `today is the day itself, timed or not`() { + val day = LocalDate.of(2026, 11, 26) + val today = Rule.Due(DueSpec.Today) + assertThat(DueSpec.Today.daysIn(day)).containsExactly(day) + assertThat(today.matches(dueAt(day, LocalTime.MIDNIGHT), day)).isTrue() + assertThat(today.matches(dueAt(day, LocalTime.of(23, 59)), day)).isTrue() + assertThat(today.matches(dueAt(day.plusDays(1), LocalTime.MIDNIGHT), day)).isFalse() + assertThat(today.matches(testTask(), day)).isFalse() + } + + @Test + fun `this week starts on the week start`() { + // A Thursday. + val thursday = LocalDate.of(2026, 11, 26) + val monday = LocalDate.of(2026, 11, 23) + val sunday = LocalDate.of(2026, 11, 22) + val saturday = LocalDate.of(2026, 11, 21) + assertThat(DueSpec.ThisWeek.daysIn(thursday, DayOfWeek.MONDAY)).isEqualTo((0L..6L).map { monday.plusDays(it) }) + assertThat(DueSpec.ThisWeek.daysIn(thursday, DayOfWeek.SUNDAY)).isEqualTo((0L..6L).map { sunday.plusDays(it) }) + assertThat(DueSpec.ThisWeek.daysIn(thursday, DayOfWeek.SATURDAY)).isEqualTo((0L..6L).map { saturday.plusDays(it) }) + // On the week start itself, and on the last day of the week. + assertThat(DueSpec.ThisWeek.daysIn(monday, DayOfWeek.MONDAY).first()).isEqualTo(monday) + assertThat(DueSpec.ThisWeek.daysIn(sunday, DayOfWeek.MONDAY).first()).isEqualTo(LocalDate.of(2026, 11, 16)) + assertThat(DueSpec.ThisWeek.daysIn(sunday, DayOfWeek.SUNDAY).first()).isEqualTo(sunday) + } + + @Test + fun `this week crosses the year`() { + val newYear = LocalDate.of(2027, 1, 1) + assertThat(DueSpec.ThisWeek.daysIn(newYear, DayOfWeek.MONDAY)) + .isEqualTo((0L..6L).map { LocalDate.of(2026, 12, 28).plusDays(it) }) + } + + @Test + fun `this month is the calendar month`() { + assertThat(DueSpec.ThisMonth.daysIn(LocalDate.of(2026, 12, 31))) + .isEqualTo((1..31).map { LocalDate.of(2026, 12, it) }) + assertThat(DueSpec.ThisMonth.daysIn(LocalDate.of(2027, 1, 1))) + .isEqualTo((1..31).map { LocalDate.of(2027, 1, it) }) + assertThat(DueSpec.ThisMonth.daysIn(LocalDate.of(2028, 2, 29))) + .isEqualTo((1..29).map { LocalDate.of(2028, 2, it) }) + assertThat(DueSpec.ThisMonth.daysIn(LocalDate.of(2027, 2, 15))) + .isEqualTo((1..28).map { LocalDate.of(2027, 2, it) }) + } + + @Test + fun `next days run from today and reach as far as upcoming does`() { + val day = LocalDate.of(2026, 12, 29) + assertThat(DueSpec.NextDays(0).daysIn(day)).containsExactly(day) + assertThat(DueSpec.NextDays(7).daysIn(day)).isEqualTo((0L..7L).map { day.plusDays(it) }) + val now = Instant.fromEpochMilliseconds(day.atTime(12, 0).atZone(zone).toInstant().toEpochMilli()) + assertThat(DueSpec.NextDays(7).days(day, DayOfWeek.MONDAY).second) + .isEqualTo(UpcomingRange.WEEK.lastDay(day)!!.plusDays(1)) + assertThat(startOfDay(DueSpec.NextDays(7).days(day, DayOfWeek.MONDAY).second, zone)) + .isEqualTo(UpcomingRange.WEEK.end(now, zone)) + } + + @Test + fun `an all-day due is read as its date west of utc`() { + val west = ZoneId.of("America/New_York") + val day = LocalDate.of(2026, 11, 30) + val clock = clockOn(day, zone = west) + assertThat(Rule.Due(DueSpec.Today).matches(dueOn(day), clock)).isTrue() + assertThat(Rule.Due(DueSpec.ThisMonth).matches(dueOn(LocalDate.of(2026, 12, 1)), clock)).isFalse() + assertThat(Rule.Due(DueSpec.ThisMonth).matches(dueAt(day, LocalTime.of(23, 30), west), clock)).isTrue() + } + + @Test + fun `a list condition losing its lists drops out`() { + val gone: (Long) -> Boolean = { it == 1L } + assertThat(Rule.InList(setOf(1L, 2L)).withoutLists(gone)).isEqualTo(Rule.InList(setOf(2L))) + assertThat(Rule.InList(setOf(1L)).withoutLists(gone)).isNull() + assertThat(Rule.InList(setOf(1L), setOf("EXTERNAL:1")).withoutLists(gone)) + .isEqualTo(Rule.InList(emptySet(), setOf("EXTERNAL:1"))) + val rule = Rule.All( + listOf( + Rule.HasTag("a"), + Rule.Not(Rule.InList(setOf(1L))), + Rule.Any(listOf(Rule.InList(setOf(1L)), Rule.HasTag("b"))), + Rule.Any(listOf(Rule.InList(setOf(1L)))), + ), + ) + assertThat(rule.withoutLists(gone)).isEqualTo(Rule.All(listOf(Rule.HasTag("a"), Rule.Any(listOf(Rule.HasTag("b")))))) + assertThat(Rule.All(listOf(Rule.InList(setOf(1L)))).withoutLists(gone)).isNull() + assertThat(Rule.Everything.withoutLists(gone)).isEqualTo(Rule.Everything) + assertThat(rule.withoutLists { false }).isEqualTo(rule) + } +}