Scope, source and start rules for views (#59, #141)

A view's scope applied with deleted lists matching nothing, block sources
and their deleted state, pinning a list or filter, reordering, and where
a start destination opens. The view icons grow to the curated set.
This commit is contained in:
makiolaj committed 2026-11-27 12:32:36 +01:00
1 parent 080afe7aa3
commit fa5b8e1cef
3 files changed
+364 -2

No files matched your search

@@ -187,8 +187,13 @@ sealed interface BlockOptions {
/** The tonal colour roles a count tile can take. */
enum class TileColor { PRIMARY, SECONDARY, TERTIARY, ERROR }
/** The curated icons a view (or a tile) can carry. Stored by name. */
enum class ViewIcon { HOME, WORK, PERSON, STAR, CALENDAR, LIST, FLAG, BOLT, HEART, SCHOOL, SHOPPING, FITNESS }
/** The curated icons a view (or a tile) can carry, in the picker's order. Stored by name. */
enum class ViewIcon {
HOME, WORK, HEART, BOLT, CALENDAR, TODAY, INBOX, LIST, DONE, FLAG,
PERSON, GROUP, STAR, FILTER, TAG, FOLDER, ARCHIVE, ALARM, NOTIFICATIONS, LIGHTBULB,
SCHOOL, BOOK, SHOPPING, MONEY, FITNESS, PETS, RESTAURANT, FLIGHT, PLACE, GLOBE,
PALETTE, DEVICES, CODE, BUG, BUILD, NOTE, HISTORY, GRID, TREE,
}
/** Where the app opens. */
sealed interface StartDestination {
@@ -0,0 +1,187 @@
package de.jeanlucmakiola.agendula.domain
import kotlin.time.Instant
/** The lists this rule names anywhere in it, as a condition or under a [Rule.Not]. */
fun Rule.namedListIds(): Set<Long> =
flatten().filterIsInstance<Rule.InList>().flatMapTo(HashSet()) { it.listIds.asIterable() }
/**
* 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
* [Rule.Not] says nothing about where a task is.
*/
fun Rule.listUniverse(): Set<Long>? = when (this) {
is Rule.InList -> listIds
is Rule.AllOf -> children.mapNotNull { it.listUniverse() }.reduceOrNull { a, b -> a intersect b }
is Rule.AnyOf -> children.map { it.listUniverse() }.let { universes ->
if (null in universes) null else universes.flatMapTo(HashSet()) { it.orEmpty() }
}
is Rule.Not, is Rule.HasTag, is Rule.MinPriority, is Rule.Due -> null
}
/**
* A view's scope as it applies now. [rule] is the stored scope without the lists
* since deleted ([goneLists]), so a list condition left with none matches nothing
* rather than every list; `null` admits every task. [namedLists] are the lists the
* scope still names, which show even when kept out of the smart lists.
*/
data class AppliedScope(
val rule: Rule?,
val goneLists: Set<Long> = emptySet(),
val namedLists: Set<Long> = emptySet(),
) {
fun admits(task: Task, clock: RuleClock): Boolean = rule == null || rule.matches(task, clock)
/** [hidden], the lists kept out of the smart lists, less the ones this scope names. */
fun hiddenFromSmart(hidden: Set<Long>): Set<Long> = if (namedLists.isEmpty()) hidden else hidden - namedLists
/** The lists a task in this scope can be in; `null` when any. */
val lists: Set<Long>? get() = rule?.listUniverse()
companion object {
val None = AppliedScope(null)
}
}
/**
* This view's scope against the lists that [exist]. A read that found no list at
* all proves nothing, so then no list counts as gone.
*/
fun View.appliedScope(exist: Set<Long>): AppliedScope {
val scope = scope ?: return AppliedScope.None
val named = scope.namedListIds()
val gone = if (exist.isEmpty()) emptySet() else named - exist
val rule = if (gone.isEmpty()) scope else scope.withoutListIds { it in gone }
return AppliedScope(rule, gone, named - gone)
}
/** The scope [block] reads its tasks through: none when it ignores the view's. */
fun AppliedScope.forBlock(block: Block): AppliedScope = if (block.ignoreScope) AppliedScope.None else this
/**
* Whether this source no longer exists: a list or saved filter since deleted. A
* read that found no list at all proves nothing about a list.
*/
fun BlockSource.isGone(lists: Set<Long>, filters: Set<Long>): Boolean = when (this) {
is BlockSource.List -> lists.isNotEmpty() && listId !in lists
is BlockSource.Filter -> filterId !in filters
is BlockSource.Smart, is BlockSource.ListElsewhere, is BlockSource.Tag, is BlockSource.Rules -> false
}
/**
* What a block's source is read against: [clock]'s day, Upcoming ending at
* [upcomingEnd] (`null` for no end), and the saved [filters] by id.
*/
class SourceContext(
val clock: RuleClock,
val upcomingEnd: Instant?,
val filters: Map<Long, SavedFilter>,
)
/**
* Whether [task] is in this source. A smart list goes by its own rules, statuses
* included, and leaves out the lists in [hidden]; any other source by what the
* task is, whatever its status. A list of another store holds nothing here.
*/
fun BlockSource.contains(task: Task, context: SourceContext, hidden: Set<Long> = emptySet()): Boolean {
val clock = context.clock
return when (this) {
is BlockSource.Smart -> task.listId !in hidden && TaskFiltering.matches(
task,
TaskFilter.Smart(smart),
clock.todayStart,
clock.todayEnd,
upcomingEnd = context.upcomingEnd,
)
is BlockSource.List -> task.listId == listId
is BlockSource.ListElsewhere -> false
is BlockSource.Tag -> tag in task.tags
is BlockSource.Filter -> context.filters[filterId]?.rule?.matches(task, clock) == true
is BlockSource.Rules -> rule.matches(task, clock)
}
}
/**
* Whether a count or preview of this source shows [task]: an open task in it, or
* a completed one for the Completed list.
*/
fun BlockSource.counts(task: Task, context: SourceContext, hidden: Set<Long> = emptySet()): Boolean {
val completedList = this == BlockSource.Smart(SmartList.COMPLETED)
return (if (completedList) task.isCompleted else !task.isClosed) && contains(task, context, hidden)
}
/** The task screen this source opens; `null` for one that has none. */
val BlockSource.taskFilter: TaskFilter?
get() = when (this) {
is BlockSource.Smart -> TaskFilter.Smart(smart)
is BlockSource.List -> TaskFilter.OfList(listId)
is BlockSource.Tag -> TaskFilter.Tagged(tag)
is BlockSource.Filter -> TaskFilter.Saved(filterId)
is BlockSource.ListElsewhere, is BlockSource.Rules -> null
}
/**
* A new view showing [scope]'s tasks, named [name]: one task list over everything
* in it, completed ones included, with the quick-add bar.
*/
fun pinnedView(id: Long, name: String, icon: ViewIcon, scope: Rule): View = View(
id = id,
name = name,
icon = icon,
scope = scope,
blocks = listOf(Block(1, BlockType.TASK_LIST, BlockSource.Smart(SmartList.ALL))),
quickAdd = true,
)
/**
* The view pinning [list], or else [filter], makes: named after it, with its
* scope, and an id of 0 for the store to give it one. `null` with neither.
*/
fun pinnedViewOf(list: TaskList?, filter: SavedFilter?): View? = when {
list != null -> pinnedView(0, list.name, ViewIcon.LIST, Rule.InList(setOf(list.id)))
filter != null -> pinnedView(0, filter.name, ViewIcon.FILTER, filter.rule)
else -> null
}
/** A new view with no scope and Home's default blocks. */
fun newView(id: Long, name: String): View = View.home().copy(id = id, name = name, icon = ViewIcon.STAR)
/**
* These views in the order of [ids], Home first whatever [ids] says. Views [ids]
* leaves out keep their place after the ones it names.
*/
fun List<View>.reordered(ids: List<Long>): List<View> {
val rank = ids.withIndex().associate { (i, id) -> id to i }
val (home, rest) = partition { it.id == View.HOME_ID }
return home + rest.withIndex().sortedBy { (i, view) -> rank[view.id] ?: (ids.size + i) }.map { it.value }
}
/** Whether a view can be deleted: any but Home. */
val View.deletable: Boolean get() = id != View.HOME_ID
/**
* Where the app opens: the view [viewId] shows, with [push] (a list or filter)
* on top of it when the start destination is one.
*/
data class StartPlan(val viewId: Long, val push: TaskFilter? = null)
/**
* How this start destination opens, given the [views] there are and the one
* [lastUsed]. A list or filter opens over Home, so back leads there; one since
* deleted opens Home alone, as does a view since deleted. [lists] are the lists
* there are, `null` when they couldn't be read, and then a list is opened anyway.
*/
fun StartDestination.plan(views: Set<Long>, lastUsed: Long, lists: Set<Long>?, filters: Set<Long>): StartPlan {
fun known(id: Long) = id.takeIf { it in views } ?: View.HOME_ID
return when (this) {
StartDestination.LastUsed -> StartPlan(known(lastUsed))
is StartDestination.View -> StartPlan(known(id))
is StartDestination.List ->
StartPlan(View.HOME_ID, TaskFilter.OfList(id).takeIf { lists == null || id in lists })
is StartDestination.Filter -> StartPlan(View.HOME_ID, TaskFilter.Saved(id).takeIf { id in filters })
}
}
/** The start destination before one is chosen. */
val DefaultStartDestination: StartDestination = StartDestination.View(View.HOME_ID)
@@ -0,0 +1,170 @@
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 ViewScopeTest {
private val zone = ZoneId.of("Europe/Berlin")
private val today = LocalDate.of(2026, 11, 26)
private val clock = RuleClock(startOfDay(today, zone), startOfDay(today.plusDays(1), zone), zone, DayOfWeek.MONDAY)
private val context = SourceContext(clock, upcomingEnd = null, filters = emptyMap())
private fun view(scope: Rule?) = View(2, "Work", ViewIcon.WORK, scope, emptyList())
@Test
fun `a scope names its lists and the lists a task must be in`() {
val rule = Rule.AllOf(
listOf(
Rule.AnyOf(listOf(Rule.InList(setOf(1L, 2L)), Rule.InList(setOf(3L)))),
Rule.Not(Rule.InList(setOf(4L))),
Rule.HasTag("x"),
),
)
assertThat(rule.namedListIds()).containsExactly(1L, 2L, 3L, 4L)
assertThat(rule.listUniverse()).containsExactly(1L, 2L, 3L)
assertThat(Rule.AllOf(listOf(Rule.InList(setOf(1L, 2L)), Rule.InList(setOf(2L, 3L)))).listUniverse()).containsExactly(2L)
assertThat(Rule.AnyOf(listOf(Rule.InList(setOf(1L)), Rule.HasTag("x"))).listUniverse()).isNull()
assertThat(Rule.Not(Rule.InList(setOf(1L))).listUniverse()).isNull()
assertThat(Rule.Everything.listUniverse()).isNull()
assertThat(Rule.AnyOf(emptyList()).listUniverse()).isEmpty()
}
@Test
fun `no scope admits every task`() {
val applied = view(null).appliedScope(setOf(1L))
assertThat(applied).isEqualTo(AppliedScope.None)
assertThat(applied.admits(testTask(listId = 9), clock)).isTrue()
assertThat(applied.lists).isNull()
}
@Test
fun `a deleted list in the scope matches nothing, never widening`() {
val applied = view(Rule.InList(setOf(1L, 2L))).appliedScope(exist = setOf(2L, 3L))
assertThat(applied.goneLists).containsExactly(1L)
assertThat(applied.admits(testTask(listId = 2), clock)).isTrue()
assertThat(applied.admits(testTask(listId = 3), clock)).isFalse()
val allGone = view(Rule.AllOf(listOf(Rule.InList(setOf(1L)), Rule.HasTag("x")))).appliedScope(exist = setOf(3L))
assertThat(allGone.goneLists).containsExactly(1L)
assertThat(allGone.admits(testTask(listId = 3, tags = listOf("x")), clock)).isFalse()
// A list made later with the deleted one's id doesn't creep back in.
assertThat(allGone.admits(testTask(listId = 1, tags = listOf("x")), clock)).isFalse()
assertThat(allGone.lists).isEmpty()
}
@Test
fun `a read finding no lists proves none gone`() {
val applied = view(Rule.InList(setOf(1L))).appliedScope(exist = emptySet())
assertThat(applied.goneLists).isEmpty()
assertThat(applied.admits(testTask(listId = 1), clock)).isTrue()
}
@Test
fun `a list named in the scope shows in the smart lists though hidden`() {
val applied = view(Rule.InList(setOf(1L))).appliedScope(setOf(1L, 2L))
assertThat(applied.hiddenFromSmart(setOf(1L, 2L))).containsExactly(2L)
assertThat(AppliedScope.None.hiddenFromSmart(setOf(1L))).containsExactly(1L)
}
@Test
fun `a block that ignores the scope reads through none`() {
val applied = view(Rule.HasTag("x")).appliedScope(setOf(1L))
val block = Block(1, BlockType.TAGS)
assertThat(applied.forBlock(block)).isEqualTo(applied)
assertThat(applied.forBlock(block.copy(ignoreScope = true))).isEqualTo(AppliedScope.None)
}
@Test
fun `a source is gone once its list or filter is`() {
assertThat(BlockSource.List(1).isGone(lists = setOf(2L), filters = emptySet())).isTrue()
assertThat(BlockSource.List(1).isGone(lists = emptySet(), filters = emptySet())).isFalse()
assertThat(BlockSource.Filter(4).isGone(lists = setOf(1L), filters = setOf(5L))).isTrue()
assertThat(BlockSource.Filter(4).isGone(lists = setOf(1L), filters = setOf(4L))).isFalse()
assertThat(BlockSource.Smart(SmartList.ALL).isGone(emptySet(), emptySet())).isFalse()
assertThat(BlockSource.Tag("x").isGone(setOf(1L), emptySet())).isFalse()
}
@Test
fun `sources hold their tasks, counts the open ones but for Completed`() {
val open = testTask(listId = 1, tags = listOf("x"))
val done = testTask(id = 2, listId = 1, status = TaskStatus.COMPLETED)
val filter = SavedFilter(4, "f", Rule.HasTag("x"))
val withFilter = SourceContext(clock, null, mapOf(4L to filter))
assertThat(BlockSource.List(1).contains(done, context)).isTrue()
assertThat(BlockSource.List(1).counts(done, context)).isFalse()
assertThat(BlockSource.List(1).counts(open, context)).isTrue()
assertThat(BlockSource.Smart(SmartList.COMPLETED).counts(done, context)).isTrue()
assertThat(BlockSource.Smart(SmartList.COMPLETED).counts(open, context)).isFalse()
assertThat(BlockSource.Smart(SmartList.ALL).counts(open, context, hidden = setOf(1L))).isFalse()
assertThat(BlockSource.Tag("x").counts(open, context)).isTrue()
assertThat(BlockSource.Filter(4).counts(open, withFilter)).isTrue()
assertThat(BlockSource.Filter(4).counts(open, context)).isFalse()
assertThat(BlockSource.ListElsewhere("OWN:1").contains(open, context)).isFalse()
assertThat(BlockSource.Rules(Rule.InList(setOf(1L))).counts(open, context)).isTrue()
}
@Test
fun `pinning a list or filter makes a view of it`() {
val list = TaskList(7, "Work", 0, "Local", "local", isSynced = false, isVisible = true, owner = null)
val pinned = pinnedViewOf(list, null)!!
assertThat(pinned.id).isEqualTo(0L)
assertThat(pinned.name).isEqualTo("Work")
assertThat(pinned.icon).isEqualTo(ViewIcon.LIST)
assertThat(pinned.scope).isEqualTo(Rule.InList(setOf(7L)))
assertThat(pinned.quickAdd).isTrue()
assertThat(pinned.blocks.map { it.type to it.source })
.containsExactly(BlockType.TASK_LIST to BlockSource.Smart(SmartList.ALL))
assertThat(pinned.problems).isEmpty()
val rule = Rule.AnyOf(listOf(Rule.HasTag("next"), Rule.InList(setOf(3L))))
val fromFilter = pinnedViewOf(null, SavedFilter(4, "Next", rule))!!
assertThat(fromFilter.name).isEqualTo("Next")
assertThat(fromFilter.icon).isEqualTo(ViewIcon.FILTER)
assertThat(fromFilter.scope).isEqualTo(rule)
assertThat(pinnedViewOf(null, null)).isNull()
}
@Test
fun `a new view starts with Home's blocks and no scope`() {
val view = newView(0, "Mine")
assertThat(view.blocks).isEqualTo(View.home().blocks)
assertThat(view.scope).isNull()
assertThat(view.deletable).isTrue()
assertThat(View.home().deletable).isFalse()
}
@Test
fun `reordering keeps Home first and leaves views not named after the others`() {
val views = listOf(View.home(), view(null), view(null).copy(id = 3), view(null).copy(id = 4))
assertThat(views.reordered(listOf(4L, View.HOME_ID, 2L)).map { it.id }).containsExactly(1L, 4L, 2L, 3L).inOrder()
assertThat(views.reordered(listOf(3L, 2L, 4L)).map { it.id }).containsExactly(1L, 3L, 2L, 4L).inOrder()
}
@Test
fun `the start destination opens its view, or a list or filter over Home`() {
val views = setOf(1L, 2L, 3L)
assertThat(StartDestination.LastUsed.plan(views, lastUsed = 3, lists = null, filters = emptySet()))
.isEqualTo(StartPlan(3))
assertThat(StartDestination.LastUsed.plan(views, lastUsed = 9, lists = null, filters = emptySet()))
.isEqualTo(StartPlan(View.HOME_ID))
assertThat(StartDestination.View(2).plan(views, lastUsed = 3, lists = null, filters = emptySet()))
.isEqualTo(StartPlan(2))
assertThat(StartDestination.View(9).plan(views, lastUsed = 3, lists = null, filters = emptySet()))
.isEqualTo(StartPlan(View.HOME_ID))
assertThat(StartDestination.List(5).plan(views, lastUsed = 3, lists = setOf(5L), filters = emptySet()))
.isEqualTo(StartPlan(View.HOME_ID, TaskFilter.OfList(5)))
assertThat(StartDestination.List(5).plan(views, lastUsed = 3, lists = setOf(6L), filters = emptySet()))
.isEqualTo(StartPlan(View.HOME_ID))
assertThat(StartDestination.List(5).plan(views, lastUsed = 3, lists = null, filters = emptySet()))
.isEqualTo(StartPlan(View.HOME_ID, TaskFilter.OfList(5)))
assertThat(StartDestination.Filter(4).plan(views, lastUsed = 3, lists = null, filters = setOf(4L)))
.isEqualTo(StartPlan(View.HOME_ID, TaskFilter.Saved(4)))
assertThat(StartDestination.Filter(4).plan(views, lastUsed = 3, lists = null, filters = emptySet()))
.isEqualTo(StartPlan(View.HOME_ID))
assertThat(DefaultStartDestination).isEqualTo(StartDestination.View(View.HOME_ID))
}
}