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)