Read a task's dates without building them per call, drop the sort's due cache, reuse dateOf and calendarDate (#71, #101, #128)

This commit is contained in:
makiolaj committed 2026-11-20 12:59:45 +01:00
1 parent 5e204a43f4
commit 05c0199647
4 files changed
+20 -33

No files matched your search

@@ -120,10 +120,10 @@ data class Task(
) {
val dates: TaskDates get() = TaskDates(start, due, isAllDay, untimed, timeZone)
fun valueOf(end: TaskEnd): Instant? = dates.valueOf(end)
fun valueOf(end: TaskEnd): Instant? = if (end == TaskEnd.START) start else due
/** Whether [end] carries a time of day. */
fun hasTime(end: TaskEnd): Boolean = dates.hasTime(end)
/** Whether [end] carries a time of day; as [TaskDates.hasTime], without building one. */
fun hasTime(end: TaskEnd): Boolean = !isAllDay && untimed != end
/** [end] as an editor holds it: an untimed end re-written in this device's zone, on its own date. */
fun formValueOf(end: TaskEnd): Instant? {
@@ -115,7 +115,7 @@ object TaskGroups {
): TaskGroupKey {
val section = TaskSections.sectionOf(task, todayStart, todayEnd, todayList, zone)
return if (perDay && section == TaskSection.UPCOMING) {
TaskGroupKey.Day(localDueDate(task, zone))
TaskGroupKey.Day(requireNotNull(task.dateOf(TaskEnd.DUE, zone)))
} else {
TaskGroupKey.Section(section)
}
@@ -127,11 +127,6 @@ object TaskGroups {
is TaskGroupKey.Section -> key.section.ordinal
is TaskGroupKey.OfPriority, is TaskGroupKey.OfList -> error("not a due-date group: $key")
}
private fun localDueDate(task: Task, zone: ZoneId): LocalDate {
val due = requireNotNull(task.wallClock(TaskEnd.DUE, zone))
return java.time.Instant.ofEpochMilli(due.toEpochMilliseconds()).atZone(zone).toLocalDate()
}
}
/** The key a view's grouping is remembered under; `null` for views that have no task-list screen. */
@@ -1,45 +1,37 @@
package de.jeanlucmakiola.agendula.domain
import java.util.IdentityHashMap
import java.time.ZoneId
/** Default task ordering, extracted so it's unit-testable without the repository. */
object TaskSorting {
/**
* Open before completed; within each: due-soonest (none last), priority,
* title. A due without a time, all-day or not, counts from the start of its day.
* A fresh comparator per sort, as it keeps each task's due key while it lives.
* title. A due without a time, all-day or not, counts from the start of its
* day in [zone].
*/
fun default(): Comparator<Task> {
val due = DueKeys()
return compareBy(
{ it.isCompleted },
{ it.due == null },
{ due[it] },
{ -it.priority.ordinal },
{ it.title.lowercase() },
)
}
fun default(zone: ZoneId = ZoneId.systemDefault()): Comparator<Task> = compareBy(
{ it.isCompleted },
{ it.due == null },
{ dueKey(it, zone) },
{ -it.priority.ordinal },
{ it.title.lowercase() },
)
}
/** Each task's due as sorting reads it, worked out once per task; for one sort on one thread. */
private class DueKeys {
private val keys = IdentityHashMap<Task, Long>()
operator fun get(task: Task): Long =
keys.getOrPut(task) { task.wallClock(TaskEnd.DUE)?.toEpochMilliseconds() ?: Long.MAX_VALUE }
}
private fun dueKey(task: Task, zone: ZoneId): Long =
task.wallClock(TaskEnd.DUE, zone)?.toEpochMilliseconds() ?: Long.MAX_VALUE
/** The orders a user can pick for a task list. Open tasks always come before closed ones. */
enum class TaskSortOrder { DUE, PRIORITY, TITLE, CREATED }
/** A fresh comparator for this order, to use for one sort or one pass of sorts. */
/** A comparator for this order, reading dates in this device's zone as it is now. */
fun TaskSortOrder.comparator(): Comparator<Task> = when (this) {
TaskSortOrder.DUE -> TaskSorting.default()
TaskSortOrder.PRIORITY -> DueKeys().let { due ->
TaskSortOrder.PRIORITY -> ZoneId.systemDefault().let { zone ->
compareBy<Task>(
{ it.isCompleted },
{ -it.priority.ordinal },
{ due[it] },
{ dueKey(it, zone) },
{ it.title.lowercase() },
)
}
@@ -20,7 +20,7 @@ enum class UpcomingRange {
/** Local midnight after [lastDay], the bound a due must fall before; `null` for no limit. */
fun end(now: Instant, zone: ZoneId = ZoneId.systemDefault()): Instant? {
val today = java.time.Instant.ofEpochMilli(now.toEpochMilliseconds()).atZone(zone).toLocalDate()
val today = now.calendarDate(allDay = false, zone = zone)
val last = lastDay(today) ?: return null
return Instant.fromEpochMilliseconds(last.plusDays(1).atStartOfDay(zone).toInstant().toEpochMilli())
}