diff --git a/CHANGELOG.md b/CHANGELOG.md index 89c3dee..2802aa5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,8 +18,8 @@ All notable changes to this project are documented here. The format follows without a time is an all-day task as before. A date without a time beside a timed one syncs as the start or the end of its day, marked so Agendula reads it back as a date; other apps show that time. Reminders for it ring at the - all-day reminder time. With a tasks app as the store, both ends drop their - time together (#101). + all-day reminder time. With a tasks app as the store, both ends gain or + drop their time together (#101). ## [1.3.2] - 2026-11-13 diff --git a/app/src/main/java/de/jeanlucmakiola/agendula/data/reminders/ReminderPlanner.kt b/app/src/main/java/de/jeanlucmakiola/agendula/data/reminders/ReminderPlanner.kt index 1ab41f6..5f5eb50 100644 --- a/app/src/main/java/de/jeanlucmakiola/agendula/data/reminders/ReminderPlanner.kt +++ b/app/src/main/java/de/jeanlucmakiola/agendula/data/reminders/ReminderPlanner.kt @@ -4,8 +4,11 @@ import de.jeanlucmakiola.agendula.data.prefs.Settings import de.jeanlucmakiola.agendula.domain.AllDayReminder import de.jeanlucmakiola.agendula.domain.Task import de.jeanlucmakiola.agendula.domain.TaskEnd +import de.jeanlucmakiola.agendula.domain.reminderEnd +import de.jeanlucmakiola.agendula.domain.untimedDate import de.jeanlucmakiola.agendula.domain.TaskReminder import de.jeanlucmakiola.agendula.domain.calendarDate +import java.time.LocalDate import java.time.LocalTime import java.time.ZoneId import kotlin.time.Instant @@ -55,15 +58,14 @@ internal object ReminderPlanner { .filter { (if (it.fromStart) task.start else task.due) != null } val created = task.created?.toEpochMilliseconds() reminders.mapNotNull { reminder -> - val end = when { - reminder.fromStart -> if (task.start != null) TaskEnd.START else TaskEnd.DUE - else -> if (task.due != null) TaskEnd.DUE else TaskEnd.START - } - val anchor = untimedAnchor(task, end, settings.allDayReminderMinuteOfDay, zone) - ?: if (end == TaskEnd.START) task.start!! else task.due!! - val trigger = triggerAt( - anchor, + val anchor = task.valueOf(reminderEnd(reminder.fromStart, task.start != null, task.due != null))!! + val trigger = ringsAt( + task.start, + task.due, task.isAllDay, + task.untimed, + task.timeZone, + reminder.fromStart, reminder.minutesBefore, settings.allDayReminderMinuteOfDay, zone, @@ -105,15 +107,26 @@ internal object ReminderPlanner { .toSet() /** - * A date without a time on a timed task rings at the all-day reminder time - * of its day, not at the midnight or last second it is stored as (#101). + * When a reminder [leadMinutes] before the end [fromStart] picks (see + * [reminderEnd]) fires. An end without a time beside a timed one counts + * back from the all-day reminder time of its date, read in the task's + * [timeZone], not from the first or last second it is stored as (#101). */ - fun untimedAnchor(task: Task, end: TaskEnd, allDayMinuteOfDay: Int, zone: ZoneId): Instant? { - if (task.untimed != end || task.isAllDay) return null - val value = (if (end == TaskEnd.START) task.start else task.due) ?: return null - val time = LocalTime.of(allDayMinuteOfDay / 60 % 24, allDayMinuteOfDay % 60) - val millis = value.calendarDate(allDay = false, zone = zone).atTime(time).atZone(zone).toInstant().toEpochMilli() - return Instant.fromEpochMilliseconds(millis) + fun ringsAt( + start: Instant?, + due: Instant?, + isAllDay: Boolean, + untimed: TaskEnd?, + timeZone: String?, + fromStart: Boolean, + leadMinutes: Int, + allDayMinuteOfDay: Int, + zone: ZoneId, + ): Long { + val end = reminderEnd(fromStart, start != null, due != null) + val anchor = checkNotNull(if (end == TaskEnd.START) start else due) { "A reminder needs a date" } + if (isAllDay || untimed != end) return triggerAt(anchor, isAllDay, leadMinutes, allDayMinuteOfDay, zone) + return atMinuteOfDay(anchor.untimedDate(timeZone, zone), allDayMinuteOfDay, zone) - leadMinutes.coerceAtLeast(0) * 60_000L } /** @@ -134,9 +147,9 @@ internal object ReminderPlanner { ): Long { if (!allDay) return anchor.toEpochMilliseconds() - leadMinutes.coerceAtLeast(0) * 60_000L val (days, minuteOfDay) = AllDayReminder.of(leadMinutes) - val minute = minuteOfDay ?: allDayMinuteOfDay - val time = LocalTime.of(minute / 60 % 24, minute % 60) - return anchor.calendarDate(allDay = true).minusDays(days.toLong()) - .atTime(time).atZone(zone).toInstant().toEpochMilli() + return atMinuteOfDay(anchor.calendarDate(allDay = true).minusDays(days.toLong()), minuteOfDay ?: allDayMinuteOfDay, zone) } + + private fun atMinuteOfDay(date: LocalDate, minuteOfDay: Int, zone: ZoneId): Long = + date.atTime(LocalTime.of(minuteOfDay / 60 % 24, minuteOfDay % 60)).atZone(zone).toInstant().toEpochMilli() } diff --git a/app/src/main/java/de/jeanlucmakiola/agendula/data/reminders/TaskNotifier.kt b/app/src/main/java/de/jeanlucmakiola/agendula/data/reminders/TaskNotifier.kt index bf5cada..c06131d 100644 --- a/app/src/main/java/de/jeanlucmakiola/agendula/data/reminders/TaskNotifier.kt +++ b/app/src/main/java/de/jeanlucmakiola/agendula/data/reminders/TaskNotifier.kt @@ -21,7 +21,7 @@ import de.jeanlucmakiola.agendula.MainActivity import de.jeanlucmakiola.agendula.R import de.jeanlucmakiola.agendula.data.prefs.ReminderDescription import de.jeanlucmakiola.agendula.data.prefs.Settings -import de.jeanlucmakiola.agendula.domain.dueAsShown +import de.jeanlucmakiola.agendula.domain.TaskEnd import de.jeanlucmakiola.agendula.domain.readableDescription import de.jeanlucmakiola.agendula.data.prefs.is24Hour import de.jeanlucmakiola.agendula.data.di.ApplicationScope @@ -75,12 +75,12 @@ class TaskNotifier @Inject constructor( if (!canPost()) return val channelId = ensureListChannel(task.listId, task.listName) val title = task.title.ifBlank { context.getString(R.string.task_untitled) } - val text = task.dueAsShown?.let { due -> + val text = task.shownValue(TaskEnd.DUE)?.let { due -> context.getString( R.string.reminder_due_at, formatReminderDue( due.toEpochMilliseconds(), - !task.dueHasTime, + !task.hasTime(TaskEnd.DUE), is24Hour = settings.timeFormat.is24Hour(DateFormat.is24HourFormat(context)), relative = RelativeDays( today = context.getString(R.string.reminder_day_today), diff --git a/app/src/main/java/de/jeanlucmakiola/agendula/data/tasks/StoredReminders.kt b/app/src/main/java/de/jeanlucmakiola/agendula/data/tasks/StoredReminders.kt index 3e4d29d..0cf2b19 100644 --- a/app/src/main/java/de/jeanlucmakiola/agendula/data/tasks/StoredReminders.kt +++ b/app/src/main/java/de/jeanlucmakiola/agendula/data/tasks/StoredReminders.kt @@ -3,6 +3,7 @@ package de.jeanlucmakiola.agendula.data.tasks import de.jeanlucmakiola.agendula.data.prefs.Settings import de.jeanlucmakiola.agendula.data.reminders.ReminderPlanner import de.jeanlucmakiola.agendula.domain.Task +import de.jeanlucmakiola.agendula.domain.TaskEnd import de.jeanlucmakiola.agendula.domain.TaskForm import de.jeanlucmakiola.agendula.domain.TaskReminder import de.jeanlucmakiola.agendula.domain.calendarDate @@ -35,7 +36,9 @@ internal object StoredReminders { fun of(form: TaskForm, current: Task?, settings: Settings, now: Long, zone: ZoneId): List { val wanted = form.reminders.map { it.lead }.distinct() if (form.remindersChosen) return wanted - return withDefaults(form, current?.start, current?.due, current?.isAllDay ?: form.isAllDay, settings, now, zone) + return withDefaults( + form, current?.start, current?.due, current?.isAllDay ?: form.isAllDay, settings, now, zone, current?.untimed, + ) } /** [of] for a task whose dates were [start] and [due], whatever the form chose. */ @@ -47,9 +50,10 @@ internal object StoredReminders { settings: Settings, now: Long, zone: ZoneId, + wasUntimed: TaskEnd? = null, ): List { val wanted = form.reminders.map { it.lead }.distinct() - val fresh = freshAnchors(form, start, due, wasAllDay, settings, now, zone) + val fresh = freshAnchors(form, start, due, wasAllDay, settings, now, zone, wasUntimed) if (fresh.isEmpty()) return wanted if (anchorsOf(start, due).isEmpty() || wanted.isEmpty()) return wanted.ifEmpty { defaultsFor(form, settings, now, zone) } val named = wanted.map { it.fromStart }.toSet() @@ -68,10 +72,11 @@ internal object StoredReminders { settings: Settings, now: Long, zone: ZoneId, + wasUntimed: TaskEnd? = null, ): Set { val wanted = form.reminders.map { it.lead }.distinct() val gained = anchorsOf(form.start, form.due) - anchorsOf(start, due) - return gained + revived(form, start, due, wasAllDay, wanted, settings, now, zone) + return gained + revived(form, start, due, wasAllDay, wasUntimed, wanted, settings, now, zone) } /** @@ -114,6 +119,7 @@ internal object StoredReminders { start: Instant?, due: Instant?, wasAllDay: Boolean, + wasUntimed: TaskEnd?, wanted: List, settings: Settings, now: Long, @@ -131,7 +137,9 @@ internal object StoredReminders { } val leads = defaults.filter { it.fromStart == fromStart } return moved && leads.isNotEmpty() && leads.all { - ReminderPlanner.triggerAt(was, wasAllDay, it.minutesBefore, settings.allDayReminderMinuteOfDay, zone) < now + ReminderPlanner.ringsAt( + start, due, wasAllDay, wasUntimed, null, fromStart, it.minutesBefore, settings.allDayReminderMinuteOfDay, zone, + ) < now } } return setOfNotNull(true.takeIf { revived(true, start, form.start) }, false.takeIf { revived(false, due, form.due) }) @@ -147,10 +155,14 @@ internal object StoredReminders { */ private fun defaultsFor(form: TaskForm, settings: Settings, now: Long, zone: ZoneId): List = settings.defaultRemindersFor(form.listId, form.isAllDay).filter { reminder -> - val anchor = (if (reminder.fromStart) form.start else form.due) ?: return@filter false - form.rrule != null || ReminderPlanner.triggerAt( - anchor, + if ((if (reminder.fromStart) form.start else form.due) == null) return@filter false + form.rrule != null || ReminderPlanner.ringsAt( + form.start, + form.due, form.isAllDay, + form.untimed, + null, + reminder.fromStart, reminder.minutesBefore, settings.allDayReminderMinuteOfDay, zone, diff --git a/app/src/main/java/de/jeanlucmakiola/agendula/data/tasks/ical/VTodoMapper.kt b/app/src/main/java/de/jeanlucmakiola/agendula/data/tasks/ical/VTodoMapper.kt index eaa8172..ee07c48 100644 --- a/app/src/main/java/de/jeanlucmakiola/agendula/data/tasks/ical/VTodoMapper.kt +++ b/app/src/main/java/de/jeanlucmakiola/agendula/data/tasks/ical/VTodoMapper.kt @@ -539,12 +539,11 @@ object VTodoMapper { val tzid: String?, val isDate: Boolean, val representable: Boolean, + /** A date-time marked as standing for a date alone, still on its day boundary. */ + val noTime: Boolean = false, ) { val present get() = property != null - /** A date-time marked as standing for a date alone. */ - val noTime get() = property != null && !isDate && ICalValues.isMarkedNoTime(property) - /** True when one `is_all_day` flag, one `timezone` column and the untimed end reproduce it. */ fun reproducible(allDay: Boolean, timezone: String?, untimed: Boolean = false) = present && representable && isDate == allDay && tzid == timezone && noTime == untimed @@ -557,7 +556,10 @@ object VTodoMapper { val property = vtodo.property(name) ?: return TimeRead(null, null, null, false, false) return when (val value = ICalValues.parseTime(property)) { is ICalValues.TimeValue.Date -> TimeRead(property, value.instant, null, true, true) - is ICalValues.TimeValue.Timed -> TimeRead(property, value.instant, value.tzid, false, true) + is ICalValues.TimeValue.Timed -> TimeRead( + property, value.instant, value.tzid, false, true, + noTime = ICalValues.isMarkedNoTime(property) && onDayBoundary(name, property.value, value.tzid), + ) // Readable but not reproducible: the column gets the best-effort // instant, the property round-trips from the residue verbatim. is ICalValues.TimeValue.Unrepresentable -> @@ -584,6 +586,17 @@ object VTodoMapper { return ICalProperty(name, params, ICalValues.formatDateTime(instant, tzid)) } + /** + * Whether a zoned [value] still sits where an untimed [name] is written: a + * client that moved it but kept the mark has given it a time. A UTC value + * (an export) is taken at its word. + */ + private fun onDayBoundary(name: String, value: String, tzid: String?): Boolean { + if (tzid == null) return true + val time = value.trim().substringAfter('T', "") + return time == if (name.equals("DTSTART", ignoreCase = true)) "000000" else "235959" + } + private val TaskEnd.propertyName: String get() = when (this) { TaskEnd.START -> "DTSTART" diff --git a/app/src/main/java/de/jeanlucmakiola/agendula/data/tasks/room/RoomTasksDataSource.kt b/app/src/main/java/de/jeanlucmakiola/agendula/data/tasks/room/RoomTasksDataSource.kt index 2a1b6b9..de50ef4 100644 --- a/app/src/main/java/de/jeanlucmakiola/agendula/data/tasks/room/RoomTasksDataSource.kt +++ b/app/src/main/java/de/jeanlucmakiola/agendula/data/tasks/room/RoomTasksDataSource.kt @@ -1,5 +1,10 @@ package de.jeanlucmakiola.agendula.data.tasks.room +import de.jeanlucmakiola.agendula.domain.TaskEnd +import de.jeanlucmakiola.agendula.domain.untimedDate +import de.jeanlucmakiola.agendula.domain.untimedInstantOf +import de.jeanlucmakiola.agendula.domain.zoneOr +import java.time.temporal.ChronoUnit import android.util.Log import androidx.room.InvalidationTracker import de.jeanlucmakiola.agendula.data.tasks.ResetSubtask @@ -60,12 +65,17 @@ internal fun expansionWindow(now: Instant) = ExpansionWindow( /** * One occurrence's resolved start and due. A timed series keeps each * occurrence's duration; a due-anchored one has no start to offset from, so - * the anchor *is* the due date. + * the anchor *is* the due date. A due without a time (#101) keeps its distance + * in days instead, so a clock change in between cannot push it onto the next day. */ internal fun occurrenceTimes(master: TaskEntity, anchor: Instant): Pair { if (master.dtstart == null) return null to anchor - val length = master.due?.let { it - master.dtstart } - return anchor to length?.let { anchor + it } + val due = master.due ?: return anchor to null + if (master.untimed == TaskEnd.DUE && !master.isAllDay) { + val days = ChronoUnit.DAYS.between(master.dtstart.untimedDate(master.timezone), due.untimedDate(master.timezone)) + return anchor to untimedInstantOf(TaskEnd.DUE, anchor.untimedDate(master.timezone).plusDays(days), zoneOr(master.timezone)) + } + return anchor to anchor + (due - master.dtstart) } /** One occurrence of a series: its anchor, the override replacing it if any, and its resolved times. */ diff --git a/app/src/main/java/de/jeanlucmakiola/agendula/domain/AllDayTime.kt b/app/src/main/java/de/jeanlucmakiola/agendula/domain/AllDayTime.kt index 7a455d9..6cc4ca6 100644 --- a/app/src/main/java/de/jeanlucmakiola/agendula/domain/AllDayTime.kt +++ b/app/src/main/java/de/jeanlucmakiola/agendula/domain/AllDayTime.kt @@ -47,10 +47,30 @@ fun Instant.rebasedForAllDay(allDay: Boolean, zone: ZoneId = ZoneId.systemDefaul * * RFC 5545 makes `DTSTART` and `DUE` share a value type, so such a task is * timed on the wire and in storage: the untimed end is the start of its day - * ([START]) or its last second ([DUE]) in the device zone, and only this flag - * (`X-AGENDULA-NO-TIME` on the property) says it carries no time. + * ([START]) or its last second ([DUE]) in the task's zone, and only this flag + * (`X-AGENDULA-NO-TIME` on the property) says it carries no time. Its date is + * read back in that same zone ([untimedDate]), never in the device's. */ -enum class TaskEnd { START, DUE } +enum class TaskEnd { + START, + DUE, + ; + + val other: TaskEnd get() = if (this == START) DUE else START +} + +/** Whether [end] of a task carries a time of day. */ +fun hasTimeAt(end: TaskEnd, isAllDay: Boolean, untimed: TaskEnd?): Boolean = !isAllDay && untimed != end + +/** The end a reminder counts from: its own when the task has it, else the other. */ +fun reminderEnd(fromStart: Boolean, hasStart: Boolean, hasDue: Boolean): TaskEnd = when { + fromStart -> if (hasStart) TaskEnd.START else TaskEnd.DUE + else -> if (hasDue) TaskEnd.DUE else TaskEnd.START +} + +/** [timeZone] when it names a zone this device knows, else [fallback]. */ +fun zoneOr(timeZone: String?, fallback: ZoneId = ZoneId.systemDefault()): ZoneId = + timeZone?.let { runCatching { ZoneId.of(it) }.getOrNull() } ?: fallback /** The stored instant for [date] on an untimed [end]: the first or the last second of the day. */ fun untimedInstantOf(end: TaskEnd, date: java.time.LocalDate, zone: ZoneId = ZoneId.systemDefault()): Instant { @@ -60,3 +80,7 @@ fun untimedInstantOf(end: TaskEnd, date: java.time.LocalDate, zone: ZoneId = Zon } return Instant.fromEpochMilliseconds(local.atZone(zone).toInstant().toEpochMilli()) } + +/** The date an untimed end stored at this instant stands for, read in the task's [timeZone]. */ +fun Instant.untimedDate(timeZone: String?, fallback: ZoneId = ZoneId.systemDefault()): java.time.LocalDate = + calendarDate(allDay = false, zone = zoneOr(timeZone, fallback)) diff --git a/app/src/main/java/de/jeanlucmakiola/agendula/domain/Models.kt b/app/src/main/java/de/jeanlucmakiola/agendula/domain/Models.kt index 6195365..7d40aa9 100644 --- a/app/src/main/java/de/jeanlucmakiola/agendula/domain/Models.kt +++ b/app/src/main/java/de/jeanlucmakiola/agendula/domain/Models.kt @@ -1,5 +1,7 @@ package de.jeanlucmakiola.agendula.domain +import java.time.LocalDate +import java.time.ZoneId import kotlin.time.Instant /** A task list (the `tasklists` table). Lists group under their account. */ @@ -112,11 +114,27 @@ data class Task( /** The end that has a date but no time while the other has one; see [TaskEnd]. */ val untimed: TaskEnd? = null, ) { - /** Whether [start] carries a time of day. */ - val startHasTime: Boolean get() = !isAllDay && untimed != TaskEnd.START + fun valueOf(end: TaskEnd): Instant? = if (end == TaskEnd.START) start else due - /** Whether [due] carries a time of day. */ - val dueHasTime: Boolean get() = !isAllDay && untimed != TaskEnd.DUE + /** Whether [end] carries a time of day. */ + fun hasTime(end: TaskEnd): Boolean = hasTimeAt(end, isAllDay, untimed) + + /** The calendar date of [end]: all-day in UTC, untimed in the task's zone, timed in [zone]. */ + fun dateOf(end: TaskEnd, zone: ZoneId = ZoneId.systemDefault()): LocalDate? = valueOf(end)?.let { + when { + isAllDay -> it.calendarDate(allDay = true) + untimed == end -> it.untimedDate(timeZone) + else -> it.calendarDate(allDay = false, zone = zone) + } + } + + /** + * [end] for code that shows or buckets a date without a time by the + * all-day convention: without a time it is the all-day value of its date. + * Read it with `allDay = !hasTime(end)`. + */ + fun shownValue(end: TaskEnd): Instant? = + if (hasTime(end)) valueOf(end) else dateOf(end)?.let(::allDayInstantOf) /** One occurrence of a series, whether generated or overridden. */ val isOccurrence: Boolean get() = seriesId != null && occurrenceStart != null @@ -137,14 +155,6 @@ data class Task( get() = if (occurrenceStart == null) "$taskId" else "$taskId@${occurrenceStart.toEpochMilliseconds()}" } -/** - * [Task.due] for code that shows a date without a time by the all-day - * convention: an untimed due becomes the all-day value of its date. Read it - * with `allDay = !dueHasTime`. - */ -val Task.dueAsShown: Instant? - get() = if (dueHasTime || isAllDay) due else due?.let { allDayInstantOf(it.calendarDate(allDay = false)) } - /** Detail bundle: a task, its parent (if it's a subtask), and its direct children. */ data class TaskDetail( val task: Task, diff --git a/app/src/main/java/de/jeanlucmakiola/agendula/domain/SavedFilter.kt b/app/src/main/java/de/jeanlucmakiola/agendula/domain/SavedFilter.kt index 294e651..d363e25 100644 --- a/app/src/main/java/de/jeanlucmakiola/agendula/domain/SavedFilter.kt +++ b/app/src/main/java/de/jeanlucmakiola/agendula/domain/SavedFilter.kt @@ -59,7 +59,7 @@ fun dueWindowOf( todayEnd: Instant, zone: ZoneId = ZoneId.systemDefault(), ): DueWindow { - val due = task.wallClock(task.due, zone) + val due = task.wallClock(TaskEnd.DUE, zone) return when { due == null -> DueWindow.NO_DATE due < todayStart -> DueWindow.OVERDUE @@ -68,9 +68,12 @@ fun dueWindowOf( } } -/** [at], one of this task's dates, as a local instant: an all-day date is read as its calendar date. */ -fun Task.wallClock(at: Instant?, zone: ZoneId = ZoneId.systemDefault()): Instant? = - at?.let { if (isAllDay) it.rebasedForAllDay(allDay = false, zone = zone) else it } +/** [end] as a local instant: a date without a time is read as the start of its day. */ +fun Task.wallClock(end: TaskEnd, zone: ZoneId = ZoneId.systemDefault()): Instant? { + if (hasTime(end)) return valueOf(end) + val date = dateOf(end, zone) ?: return null + return Instant.fromEpochMilliseconds(date.atStartOfDay(zone).toInstant().toEpochMilli()) +} /** What a task created from a [SavedFilter] starts with, so it lands in that filter. */ data class NewTaskPreset( diff --git a/app/src/main/java/de/jeanlucmakiola/agendula/domain/TaskFilter.kt b/app/src/main/java/de/jeanlucmakiola/agendula/domain/TaskFilter.kt index e898062..274ebfb 100644 --- a/app/src/main/java/de/jeanlucmakiola/agendula/domain/TaskFilter.kt +++ b/app/src/main/java/de/jeanlucmakiola/agendula/domain/TaskFilter.kt @@ -55,7 +55,7 @@ object TaskFiltering { return when (dueWindowOf(task, todayStart, todayEnd, zone)) { DueWindow.TODAY -> true DueWindow.OVERDUE -> false - DueWindow.UPCOMING, DueWindow.NO_DATE -> inToday(task.wallClock(task.start, zone)) + DueWindow.UPCOMING, DueWindow.NO_DATE -> inToday(task.wallClock(TaskEnd.START, zone)) } } diff --git a/app/src/main/java/de/jeanlucmakiola/agendula/ui/common/ReminderFormatting.kt b/app/src/main/java/de/jeanlucmakiola/agendula/ui/common/ReminderFormatting.kt index 2781143..165a0e8 100644 --- a/app/src/main/java/de/jeanlucmakiola/agendula/ui/common/ReminderFormatting.kt +++ b/app/src/main/java/de/jeanlucmakiola/agendula/ui/common/ReminderFormatting.kt @@ -73,10 +73,25 @@ fun reminderLabel(minutes: Int, allDay: Boolean, fromStart: Boolean, anchored: B /** * One of a task's own reminders in one line, naming its date when [anchored]. * An all-day one names its day and the time it rings at — its own, or - * [allDayMinuteOfDay] when it has none. + * [allDayMinuteOfDay] when it has none. One counting from a date without a + * time ([untimedAnchor], #101) names the time it rings at, counted back from + * [allDayMinuteOfDay]. */ @Composable -fun taskReminderLabel(reminder: TaskReminder, allDay: Boolean, allDayMinuteOfDay: Int, anchored: Boolean): String { +fun taskReminderLabel( + reminder: TaskReminder, + allDay: Boolean, + allDayMinuteOfDay: Int, + anchored: Boolean, + untimedAnchor: Boolean = false, +): String { + if (!allDay && untimedAnchor) { + return stringResource( + R.string.reminder_at_time, + reminderLabel(reminder.minutesBefore, false, reminder.fromStart, anchored), + formatMinuteOfDay(Math.floorMod(allDayMinuteOfDay - reminder.minutesBefore, DAY_MINUTES), LocalUse24HourFormat.current), + ) + } if (!allDay) return reminderLabel(reminder.minutesBefore, false, reminder.fromStart, anchored) val (days, minuteOfDay) = reminder.allDay return stringResource( diff --git a/app/src/main/java/de/jeanlucmakiola/agendula/ui/detail/TaskDetailScreen.kt b/app/src/main/java/de/jeanlucmakiola/agendula/ui/detail/TaskDetailScreen.kt index 557b7a5..7d90b0f 100644 --- a/app/src/main/java/de/jeanlucmakiola/agendula/ui/detail/TaskDetailScreen.kt +++ b/app/src/main/java/de/jeanlucmakiola/agendula/ui/detail/TaskDetailScreen.kt @@ -101,6 +101,8 @@ import androidx.lifecycle.compose.collectAsStateWithLifecycle import de.jeanlucmakiola.agendula.R import de.jeanlucmakiola.agendula.domain.readableDescription import de.jeanlucmakiola.agendula.domain.Task +import de.jeanlucmakiola.agendula.domain.TaskEnd +import de.jeanlucmakiola.agendula.domain.reminderEnd import de.jeanlucmakiola.agendula.domain.TaskDetail import de.jeanlucmakiola.floret.components.revealOnFocus import de.jeanlucmakiola.floret.components.GroupedSurface @@ -363,6 +365,8 @@ private fun DetailBody( task.isAllDay, allDayMinuteOfDay, anchored = task.start != null && task.due != null, + untimedAnchor = !task.isAllDay && + task.untimed == reminderEnd(reminder.fromStart, task.start != null, task.due != null), ), style = MaterialTheme.typography.titleMedium, ) @@ -711,18 +715,17 @@ private fun DetailCard( @Composable private fun taskWhenLines(task: Task): Pair? { val use24 = LocalUse24HourFormat.current - val start = task.start - val due = task.due + val start = task.shownValue(TaskEnd.START) + val due = task.shownValue(TaskEnd.DUE) + // Without a time, an end is read as its date by the all-day convention (#101). + val startTime = start?.takeIf { task.hasTime(TaskEnd.START) }?.formatTime(use24) + val dueTime = due?.takeIf { task.hasTime(TaskEnd.DUE) }?.formatTime(use24) return when { start != null && due != null -> { - val allDay = task.isAllDay - val sameDay = start.formatDate(allDay) == due.formatDate(allDay) - val primary = - if (sameDay) due.formatDate(allDay) - else "${start.formatDate(allDay)} – ${due.formatDate(allDay)}" - // One end without a time (#101) keeps its side of the range open. - val startTime = start.takeIf { task.startHasTime }?.formatTime(use24) - val dueTime = due.takeIf { task.dueHasTime }?.formatTime(use24) + val startDay = start.formatDate(startTime == null) + val dueDay = due.formatDate(dueTime == null) + val primary = if (startDay == dueDay) dueDay else "$startDay – $dueDay" + // One end without a time keeps its side of the range open. val secondary = when { startTime != null && dueTime != null -> "$startTime – $dueTime" startTime != null -> "$startTime –" @@ -731,8 +734,8 @@ private fun taskWhenLines(task: Task): Pair? { } primary to secondary } - due != null -> due.formatDate(task.isAllDay) to due.takeIf { task.dueHasTime }?.formatTime(use24) - start != null -> start.formatDate(task.isAllDay) to start.takeIf { task.startHasTime }?.formatTime(use24) + due != null -> due.formatDate(dueTime == null) to dueTime + start != null -> start.formatDate(startTime == null) to startTime else -> null } } diff --git a/app/src/main/java/de/jeanlucmakiola/agendula/ui/edit/DateTimePickerSheet.kt b/app/src/main/java/de/jeanlucmakiola/agendula/ui/edit/DateTimePickerSheet.kt index 1bd0eaf..091affc 100644 --- a/app/src/main/java/de/jeanlucmakiola/agendula/ui/edit/DateTimePickerSheet.kt +++ b/app/src/main/java/de/jeanlucmakiola/agendula/ui/edit/DateTimePickerSheet.kt @@ -2,6 +2,8 @@ package de.jeanlucmakiola.agendula.ui.edit import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.height +import androidx.compose.foundation.layout.padding +import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Text import androidx.compose.material3.TextButton import androidx.compose.runtime.Composable @@ -39,13 +41,14 @@ import kotlin.time.Instant * other, and "No time" for a date alone. A tap that completes the value applies * it and closes: any time, "No time", or a date for a value that is already * set. A date for an empty value is staged with a default time and committed by - * Done. Back discards anything staged. + * Done, unless [dateOnly] (the other end has no time either), where it applies + * as a date alone. Back discards anything staged. * * [initial] is read as all-day when [allDay], and its time only when - * [hasTime]. [noTimeNote] explains a "No time" that also takes the other end's - * time. [notBefore] / [notAfter] keep a date or time filled in for an empty - * value on the right side of the other end (the start for a due, the due for a - * start). [onChange] gets the date and the time, null for none. + * [hasTime]. [linkedNote] explains a store where the other end's time comes + * and goes with this one's. [notBefore] / [notAfter] keep a date or time filled + * in for an empty value on the right side of the other end (the start for a + * due, the due for a start). [onChange] gets the date and the time, null for none. */ @Composable internal fun DateTimePickerSheet( @@ -53,13 +56,14 @@ internal fun DateTimePickerSheet( initial: Instant?, allDay: Boolean, hasTime: Boolean, - noTimeNote: String?, - notBefore: Instant?, - notAfter: Instant?, + dateOnly: Boolean, + linkedNote: String?, + notBefore: LocalDateTime?, + notAfter: LocalDateTime?, onChange: (LocalDate, LocalTime?) -> Unit, onDismiss: () -> Unit, ) { - val staging = initial == null + val staging = initial == null && !dateOnly var date by rememberSaveable { mutableStateOf(initial?.calendarDate(allDay)) } var time by rememberSaveable { mutableStateOf(initial?.takeIf { hasTime }?.toLocalTime()) } var showCalendar by rememberSaveable { mutableStateOf(false) } @@ -72,9 +76,7 @@ internal fun DateTimePickerSheet( fun dayLabel(d: LocalDate) = d.format(if (d.year == today.year) dayFormats.thisYear else dayFormats.otherYear) fun timeLabel(t: LocalTime) = formatMinuteOfDay(t.hour * 60 + t.minute, is24Hour, locale) - val floor = notBefore?.toLocalDateTime() - val ceiling = notAfter?.toLocalDateTime() - fun stagedTime(d: LocalDate) = defaultPickerTime(d, LocalDateTime.now(), floor, ceiling) + fun stagedTime(d: LocalDate) = defaultPickerTime(d, LocalDateTime.now(), notBefore, notAfter) fun commit(d: LocalDate, t: LocalTime?) { onChange(d, t) @@ -89,7 +91,7 @@ internal fun DateTimePickerSheet( } } fun pickTime(t: LocalTime) { - commit(date ?: defaultPickerDate(t, LocalDateTime.now(), floor, ceiling), t) + commit(date ?: defaultPickerDate(t, LocalDateTime.now(), notBefore, notAfter), t) } FullScreenPicker( @@ -98,7 +100,7 @@ internal fun DateTimePickerSheet( actions = { if (staging) { TextButton( - onClick = { date?.let { commit(it, stagedTime(it)) } }, + onClick = { date?.let { commit(it, time ?: stagedTime(it)) } }, enabled = date != null, ) { Text(stringResource(R.string.picker_done)) } } @@ -148,11 +150,18 @@ internal fun DateTimePickerSheet( ) GroupedRow( title = stringResource(R.string.picker_time_none), - summary = noTimeNote, position = positionOf(QuickTime.entries.size + 1, timeRows), checked = date != null && time == null, - onClick = { commit(date ?: defaultNoTimeDate(today, floor, ceiling), null) }, + onClick = { commit(date ?: defaultNoTimeDate(today, notBefore, notAfter), null) }, ) + linkedNote?.let { + Text( + text = it, + style = MaterialTheme.typography.bodySmall, + color = MaterialTheme.colorScheme.onSurfaceVariant, + modifier = Modifier.padding(horizontal = 16.dp, vertical = 8.dp), + ) + } } if (showCalendar) { @@ -184,8 +193,6 @@ private class DayFormats(locale: Locale) { val otherYear: DateTimeFormatter = localizedDateFormatter(locale, "EEEdMMMyyyy") } -private fun Instant.toLocalDateTime(): LocalDateTime = LocalDateTime.of(calendarDate(false), toLocalTime()) - private val QuickDate.labelRes: Int get() = when (this) { QuickDate.Today -> R.string.picker_date_today diff --git a/app/src/main/java/de/jeanlucmakiola/agendula/ui/edit/TaskEditScreen.kt b/app/src/main/java/de/jeanlucmakiola/agendula/ui/edit/TaskEditScreen.kt index 5bea5fc..b8908bb 100644 --- a/app/src/main/java/de/jeanlucmakiola/agendula/ui/edit/TaskEditScreen.kt +++ b/app/src/main/java/de/jeanlucmakiola/agendula/ui/edit/TaskEditScreen.kt @@ -120,6 +120,7 @@ import de.jeanlucmakiola.floret.time.DayWindow import de.jeanlucmakiola.agendula.domain.Priority import de.jeanlucmakiola.agendula.domain.Task import de.jeanlucmakiola.agendula.domain.TaskEnd +import de.jeanlucmakiola.agendula.domain.reminderEnd import de.jeanlucmakiola.agendula.domain.TaskFormError import de.jeanlucmakiola.agendula.domain.TaskFormField import de.jeanlucmakiola.agendula.domain.TaskList @@ -558,7 +559,14 @@ private fun EditContent( state.reminders.forEachIndexed { index, reminder -> ReminderItemCard( position = positionOf(index, rows), - title = taskReminderLabel(reminder, state.isAllDay, state.allDayMinuteOfDay, anchored = bothDates), + title = taskReminderLabel( + reminder, + state.isAllDay, + state.allDayMinuteOfDay, + anchored = bothDates, + untimedAnchor = !state.isAllDay && + state.untimed == reminderEnd(reminder.fromStart, state.start != null, state.due != null), + ), onRemove = { viewModel.removeReminder(reminder) }, ) } @@ -650,10 +658,10 @@ private fun EditContent( pickerTarget?.let { target -> val end = if (target == PickerTarget.Start) TaskEnd.START else TaskEnd.DUE - val other = if (end == TaskEnd.START) TaskEnd.DUE else TaskEnd.START - // A store without per-end times drops both together; say so where it would bite. - val noTimeNote = if (!state.untimedEnds && state.valueOf(other) != null && state.hasTime(other)) { - stringResource(if (other == TaskEnd.DUE) R.string.picker_time_none_also_due else R.string.picker_time_none_also_start) + val otherSet = state.valueOf(end.other) != null + // A store without per-end times moves both together; say so where it would bite. + val linkedNote = if (!state.untimedEnds && otherSet) { + stringResource(if (end.other == TaskEnd.DUE) R.string.picker_time_linked_due else R.string.picker_time_linked_start) } else { null } @@ -662,9 +670,10 @@ private fun EditContent( initial = state.valueOf(end), allDay = state.isAllDay, hasTime = state.hasTime(end), - noTimeNote = noTimeNote, - notBefore = state.start.takeIf { end == TaskEnd.DUE }, - notAfter = state.due.takeIf { end == TaskEnd.START }, + dateOnly = otherSet && !state.hasTime(end.other), + linkedNote = linkedNote, + notBefore = state.localValueOf(TaskEnd.START).takeIf { end == TaskEnd.DUE }, + notAfter = state.localValueOf(TaskEnd.DUE).takeIf { end == TaskEnd.START }, onChange = { date, time -> viewModel.onWhenChange(end, date, time) }, onDismiss = { pickerTarget = null }, ) diff --git a/app/src/main/java/de/jeanlucmakiola/agendula/ui/edit/TaskEditViewModel.kt b/app/src/main/java/de/jeanlucmakiola/agendula/ui/edit/TaskEditViewModel.kt index 2e0f638..f6cbb3b 100644 --- a/app/src/main/java/de/jeanlucmakiola/agendula/ui/edit/TaskEditViewModel.kt +++ b/app/src/main/java/de/jeanlucmakiola/agendula/ui/edit/TaskEditViewModel.kt @@ -17,6 +17,8 @@ import de.jeanlucmakiola.agendula.domain.TaskFormError import de.jeanlucmakiola.agendula.domain.TaskFormField import de.jeanlucmakiola.agendula.domain.allDayInstantOf import de.jeanlucmakiola.agendula.domain.TaskEnd +import de.jeanlucmakiola.agendula.ui.common.toLocalTime +import de.jeanlucmakiola.agendula.domain.hasTimeAt import de.jeanlucmakiola.agendula.domain.calendarDate import de.jeanlucmakiola.agendula.domain.untimedInstantOf import de.jeanlucmakiola.agendula.ui.common.localToInstant @@ -31,7 +33,6 @@ import de.jeanlucmakiola.agendula.domain.looksLikeHtml import de.jeanlucmakiola.agendula.domain.newTaskPreset import de.jeanlucmakiola.agendula.domain.normalizeTags import de.jeanlucmakiola.agendula.domain.tagsInUse -import de.jeanlucmakiola.agendula.domain.rebasedForAllDay import de.jeanlucmakiola.agendula.domain.readableDescription import de.jeanlucmakiola.agendula.domain.recurrence.RecurringScope import kotlinx.coroutines.flow.MutableStateFlow @@ -40,6 +41,7 @@ import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.first import kotlinx.coroutines.launch import java.time.LocalDate +import java.time.LocalDateTime import java.time.LocalTime import javax.inject.Inject import kotlin.time.Clock @@ -117,7 +119,19 @@ data class TaskEditUiState( fun valueOf(end: TaskEnd): Instant? = if (end == TaskEnd.START) start else due /** Whether [end] carries a time of day, as opposed to a date alone. */ - fun hasTime(end: TaskEnd): Boolean = !isAllDay && untimed != end + fun hasTime(end: TaskEnd): Boolean = hasTimeAt(end, isAllDay, untimed) + + /** + * [end] as a local date-time, for the picker to keep the other end on its + * side of: a date without a time is the start of its day for a start, the + * end of it for a due. + */ + fun localValueOf(end: TaskEnd): LocalDateTime? { + val value = valueOf(end) ?: return null + if (hasTime(end)) return LocalDateTime.of(value.calendarDate(allDay = false), value.toLocalTime()) + val date = value.calendarDate(isAllDay) + return if (end == TaskEnd.START) date.atStartOfDay() else date.atTime(23, 59) + } } @HiltViewModel @@ -164,6 +178,10 @@ class TaskEditViewModel @Inject constructor( private var loadedStart: Instant? = null private var loadedDue: Instant? = null private var loadedAllDay = false + private var loadedUntimed: TaskEnd? = null + + /** A new task's start is still the "now" it was given; a due without a time then takes it along. */ + private var startIsDefault = false /** Whether [loadedReminders] are all-day ones; a task without dates converts them once it gets one. */ private var loadedKindAllDay = false @@ -228,7 +246,10 @@ class TaskEditViewModel @Inject constructor( title = initialTitle.orEmpty(), tags = (listOfNotNull(presetTag) + preset?.tags.orEmpty()).distinct(), ).withDefaultReminders(), - ).also { baseline = it.toForm().copy(title = "") } + ).also { + baseline = it.toForm().copy(title = "") + startIsDefault = it.start != null && !it.isAllDay + } } } @@ -255,6 +276,7 @@ class TaskEditViewModel @Inject constructor( loadedStart = task.start loadedDue = task.due loadedAllDay = task.isAllDay + loadedUntimed = task.untimed loadedKindAllDay = task.isAllDay _state.value = withFields( TaskEditUiState( @@ -264,8 +286,11 @@ class TaskEditViewModel @Inject constructor( description = task.description?.readableDescription().orEmpty(), descriptionHtml = task.description?.takeIf { it.looksLikeHtml() }, listId = task.listId, - start = task.start, - due = task.due, + // An untimed end keeps its date across zones; the form holds it in this one. + start = task.start.takeIf { task.hasTime(TaskEnd.START) || task.isAllDay } + ?: task.dateOf(TaskEnd.START)?.let { untimedInstantOf(TaskEnd.START, it) }, + due = task.due.takeIf { task.hasTime(TaskEnd.DUE) || task.isAllDay } + ?: task.dateOf(TaskEnd.DUE)?.let { untimedInstantOf(TaskEnd.DUE, it) }, isAllDay = task.isAllDay, untimed = task.untimed, untimedEnds = repository.supportsUntimedEnds(), @@ -350,14 +375,27 @@ class TaskEditViewModel @Inject constructor( } } - fun onStartChange(value: Instant?) = update { - val next = it.copy(start = value, untimed = it.untimed.takeUnless { end -> end == TaskEnd.START && value == null }) - next.withDatedReminders(lostDates = it.hasDates && !next.hasDates) - } + fun onStartChange(value: Instant?) = onValueChange(TaskEnd.START, value) - fun onDueChange(value: Instant?) = update { - val next = it.copy(due = value, untimed = it.untimed.takeUnless { end -> end == TaskEnd.DUE && value == null }) - next.withDatedReminders(lostDates = it.hasDates && !next.hasDates) + fun onDueChange(value: Instant?) = onValueChange(TaskEnd.DUE, value) + + /** + * [end] set to [value] in the form's current kind, or cleared. Clearing the + * timed end leaves the other one alone without a time, which is all-day. + */ + private fun onValueChange(end: TaskEnd, value: Instant?) = update { s -> + if (end == TaskEnd.START) startIsDefault = false + val withValue = if (end == TaskEnd.START) s.copy(start = value) else s.copy(due = value) + val loneUntimed = value == null && s.untimed == end.other + if (loneUntimed) { + val date = checkNotNull(s.valueOf(end.other)).calendarDate(allDay = false) + val allDay = withValue.copy(isAllDay = true, untimed = null).let { + if (end.other == TaskEnd.START) it.copy(start = allDayInstantOf(date)) else it.copy(due = allDayInstantOf(date)) + } + return@update switchedKind(s, allDay) + } + val next = withValue.copy(untimed = s.untimed.takeUnless { it == end && value == null }) + next.withDatedReminders(lostDates = s.hasDates && !next.hasDates) } /** @@ -367,9 +405,12 @@ class TaskEditViewModel @Inject constructor( * of an all-day task leaves the other end's date without one. */ fun onWhenChange(end: TaskEnd, date: LocalDate, time: LocalTime?) = update { s -> - val other = if (end == TaskEnd.START) TaskEnd.DUE else TaskEnd.START + val other = end.other val otherValue = s.valueOf(other) - val allDay = time == null && (otherValue == null || !s.hasTime(other) || !s.untimedEnds) + // A new task's untouched "now" start goes along with a due picked without a time. + val otherFollows = end == TaskEnd.DUE && startIsDefault + if (end == TaskEnd.START) startIsDefault = false + val allDay = time == null && (otherValue == null || !s.hasTime(other) || !s.untimedEnds || otherFollows) val value = when { time != null -> localToInstant(date, time) allDay -> allDayInstantOf(date) @@ -396,27 +437,6 @@ class TaskEditViewModel @Inject constructor( ) if (allDay == s.isAllDay) next.withDatedReminders() else switchedKind(s, next) } - /** - * All-day and timed values use different conventions (UTC midnight vs. a real - * instant in the device zone), so the switch has to move the timestamps too — - * flipping the flag alone makes an all-day task read back as "02:00", which - * looks to the user like the time reset itself. The reminders are made - * the new kind's here, so the editor shows what will be stored; switching - * straight back gives the old ones back. - */ - fun onAllDayChange(value: Boolean) = update { - if (it.isAllDay == value) return@update it - switchedKind( - it, - it.copy( - isAllDay = value, - untimed = null, - start = it.start?.rebasedForAllDay(value), - due = it.due?.rebasedForAllDay(value), - ), - ) - } - /** [switched], [it] made all-day or timed, with its reminders made the new kind. */ private fun switchedKind(it: TaskEditUiState, switched: TaskEditUiState): TaskEditUiState { if (!it.remindersTouched) { @@ -490,6 +510,7 @@ class TaskEditViewModel @Inject constructor( settings, nowMillis(), ZoneId.systemDefault(), + loadedUntimed, ).sortedForDisplay(this) val visible = if (reminders.isEmpty()) visibleFields else visibleFields + TaskFormField.Reminder val applied = StoredReminders.freshAnchors( @@ -500,6 +521,7 @@ class TaskEditViewModel @Inject constructor( settings, nowMillis(), ZoneId.systemDefault(), + loadedUntimed, ).isNotEmpty() return copy( reminders = reminders, @@ -639,7 +661,7 @@ private fun TaskEditUiState.toForm(): TaskForm = TaskForm( start = start, due = due, isAllDay = isAllDay, - untimed = untimed.takeUnless { isAllDay }, + untimed = untimed, priority = priority, parentId = parentId, percentComplete = percentComplete, diff --git a/app/src/main/java/de/jeanlucmakiola/agendula/ui/lists/ListsScreen.kt b/app/src/main/java/de/jeanlucmakiola/agendula/ui/lists/ListsScreen.kt index 30da744..dd0c277 100644 --- a/app/src/main/java/de/jeanlucmakiola/agendula/ui/lists/ListsScreen.kt +++ b/app/src/main/java/de/jeanlucmakiola/agendula/ui/lists/ListsScreen.kt @@ -89,7 +89,7 @@ import androidx.compose.ui.unit.dp import androidx.hilt.lifecycle.viewmodel.compose.hiltViewModel import androidx.lifecycle.compose.collectAsStateWithLifecycle import de.jeanlucmakiola.agendula.R -import de.jeanlucmakiola.agendula.domain.dueAsShown +import de.jeanlucmakiola.agendula.domain.TaskEnd import de.jeanlucmakiola.agendula.domain.Priority import de.jeanlucmakiola.agendula.domain.SmartList import de.jeanlucmakiola.agendula.domain.Task @@ -861,16 +861,14 @@ private fun UpcomingRow(task: Task, onClick: () -> Unit) { /** "Today" / "Tomorrow" for the near dates, else the compact date. */ @Composable private fun upcomingDueLabel(task: Task): String? { - val due = task.due ?: return null + val due = task.shownValue(TaskEnd.DUE) ?: return null val zone = remember { ZoneId.systemDefault() } val today = remember { LocalDate.now(zone) } - val dueDate = remember(due) { - java.time.Instant.ofEpochMilli(due.toEpochMilliseconds()).atZone(zone).toLocalDate() - } + val dueDate = remember(task) { task.dateOf(TaskEnd.DUE, zone) } return when (dueDate) { today -> stringResource(R.string.home_due_today) today.plusDays(1) -> stringResource(R.string.home_due_tomorrow) - else -> task.dueAsShown?.formatDateTimeCompact(!task.dueHasTime, LocalUse24HourFormat.current) + else -> due.formatDateTimeCompact(!task.hasTime(TaskEnd.DUE), LocalUse24HourFormat.current) } } diff --git a/app/src/main/java/de/jeanlucmakiola/agendula/ui/tasklist/TaskListScreen.kt b/app/src/main/java/de/jeanlucmakiola/agendula/ui/tasklist/TaskListScreen.kt index b2f8a0f..43d8c2a 100644 --- a/app/src/main/java/de/jeanlucmakiola/agendula/ui/tasklist/TaskListScreen.kt +++ b/app/src/main/java/de/jeanlucmakiola/agendula/ui/tasklist/TaskListScreen.kt @@ -69,7 +69,7 @@ import androidx.compose.material.icons.rounded.Tune import androidx.compose.material3.AlertDialog import androidx.compose.material3.SwipeToDismissBox import androidx.compose.ui.res.pluralStringResource -import de.jeanlucmakiola.agendula.domain.dueAsShown +import de.jeanlucmakiola.agendula.domain.TaskEnd import de.jeanlucmakiola.agendula.domain.TaskSortOrder import de.jeanlucmakiola.floret.components.revealOnFocus import de.jeanlucmakiola.floret.components.InlineTextField @@ -703,7 +703,7 @@ internal fun TaskRowContent( val full by animateDpAsState(if (pressed) 36.dp else 22.dp, label = "fullCorner") val small by animateDpAsState(if (pressed) 36.dp else 6.dp, label = "smallCorner") val dark = isSystemInDarkTheme() - val due = task.dueAsShown?.formatDateTimeCompact(!task.dueHasTime, LocalUse24HourFormat.current) + val due = task.shownValue(TaskEnd.DUE)?.formatDateTimeCompact(!task.hasTime(TaskEnd.DUE), LocalUse24HourFormat.current) val listName = task.listName?.takeIf { showListName && it.isNotBlank() } // Everything secondary collapses into one quiet supporting line below the title. val hasMeta = task.priority != Priority.NONE || due != null || subtaskTotal > 0 || diff --git a/app/src/main/java/de/jeanlucmakiola/agendula/widget/WidgetData.kt b/app/src/main/java/de/jeanlucmakiola/agendula/widget/WidgetData.kt index d177d5d..93fc500 100644 --- a/app/src/main/java/de/jeanlucmakiola/agendula/widget/WidgetData.kt +++ b/app/src/main/java/de/jeanlucmakiola/agendula/widget/WidgetData.kt @@ -6,7 +6,7 @@ import de.jeanlucmakiola.agendula.data.prefs.ThemeMode import de.jeanlucmakiola.agendula.data.prefs.is24Hour import androidx.datastore.preferences.core.booleanPreferencesKey import androidx.datastore.preferences.core.stringPreferencesKey -import de.jeanlucmakiola.agendula.domain.dueAsShown +import de.jeanlucmakiola.agendula.domain.TaskEnd import de.jeanlucmakiola.agendula.domain.SmartList import de.jeanlucmakiola.agendula.domain.Task import de.jeanlucmakiola.agendula.domain.TaskFilter @@ -264,10 +264,10 @@ internal fun Task.toWidgetRow(now: Instant, today: LocalDate, zone: ZoneId): Wid listId = listId, occurrenceStart = occurrenceStart, title = title, - due = dueAsShown, - isAllDay = !dueHasTime, - dueKind = dueAsShown?.let { dueKind(it, !dueHasTime, today, zone) }, - isOverdue = dueAsShown?.let { isOverdue(it, !dueHasTime, now, today, zone) } ?: false, + due = shownValue(TaskEnd.DUE), + isAllDay = !hasTime(TaskEnd.DUE), + dueKind = shownValue(TaskEnd.DUE)?.let { dueKind(it, !hasTime(TaskEnd.DUE), today, zone) }, + isOverdue = shownValue(TaskEnd.DUE)?.let { isOverdue(it, !hasTime(TaskEnd.DUE), now, today, zone) } ?: false, color = effectiveColor, ) diff --git a/app/src/main/java/de/jeanlucmakiola/agendula/widget/WidgetStats.kt b/app/src/main/java/de/jeanlucmakiola/agendula/widget/WidgetStats.kt index 14b85b1..6ef934f 100644 --- a/app/src/main/java/de/jeanlucmakiola/agendula/widget/WidgetStats.kt +++ b/app/src/main/java/de/jeanlucmakiola/agendula/widget/WidgetStats.kt @@ -6,10 +6,10 @@ import de.jeanlucmakiola.agendula.data.prefs.ThemeMode import de.jeanlucmakiola.agendula.data.prefs.is24Hour import de.jeanlucmakiola.agendula.domain.SmartList import de.jeanlucmakiola.agendula.domain.Task +import de.jeanlucmakiola.agendula.domain.TaskEnd import de.jeanlucmakiola.agendula.domain.TaskFilter import de.jeanlucmakiola.agendula.domain.TaskFiltering import de.jeanlucmakiola.agendula.domain.TaskList -import de.jeanlucmakiola.agendula.domain.calendarDate import de.jeanlucmakiola.agendula.domain.comparator import de.jeanlucmakiola.floret.time.DayWindow import kotlinx.coroutines.flow.Flow @@ -50,7 +50,7 @@ private const val MAX_TODAY_ROWS = 20 /** Whether this open task is on the Today widget's plate: in the app's Today, or overdue. */ private fun Task.isDueToday(now: Instant, today: LocalDate, todayStart: Instant, todayEnd: Instant, zone: ZoneId): Boolean = TaskFiltering.isToday(this, todayStart, todayEnd, zone) || - due?.let { isOverdue(it, isAllDay, now, today, zone) } == true + shownValue(TaskEnd.DUE)?.let { isOverdue(it, !hasTime(TaskEnd.DUE), now, today, zone) } == true /** * How far through today a set of [open] and [completed] tasks says you are: @@ -90,10 +90,12 @@ internal fun weekDays(open: List, comparator: Comparator, now: Insta val dayTasks = if (offset == 0L) { todays } else { - later.filter { task -> task.due?.calendarDate(task.isAllDay, zone) == date } + later.filter { task -> task.dateOf(TaskEnd.DUE, zone) == date } } val overdueCount = if (offset == 0L) { - dayTasks.count { task -> task.due?.let { isOverdue(it, task.isAllDay, now, today, zone) } == true } + dayTasks.count { task -> + task.shownValue(TaskEnd.DUE)?.let { isOverdue(it, !task.hasTime(TaskEnd.DUE), now, today, zone) } == true + } } else { 0 } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index a3f2aec..eae9015 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -110,8 +110,8 @@ Evening Custom time No time - The due date loses its time too - The start date loses its time too + Your tasks app keeps one time setting for both dates, so the due date gets or loses a time along with this one + Your tasks app keeps one time setting for both dates, so the start date gets or loses a time along with this one Done Close Cancel diff --git a/app/src/test/java/de/jeanlucmakiola/agendula/data/tasks/StoredRemindersTest.kt b/app/src/test/java/de/jeanlucmakiola/agendula/data/tasks/StoredRemindersTest.kt index 78ea5ce..d626e2a 100644 --- a/app/src/test/java/de/jeanlucmakiola/agendula/data/tasks/StoredRemindersTest.kt +++ b/app/src/test/java/de/jeanlucmakiola/agendula/data/tasks/StoredRemindersTest.kt @@ -2,10 +2,12 @@ package de.jeanlucmakiola.agendula.data.tasks import com.google.common.truth.Truth.assertThat import de.jeanlucmakiola.agendula.data.prefs.Settings +import de.jeanlucmakiola.agendula.domain.TaskEnd import de.jeanlucmakiola.agendula.domain.TaskForm import de.jeanlucmakiola.agendula.domain.TaskReminder import de.jeanlucmakiola.agendula.domain.allDayInstantOf import de.jeanlucmakiola.agendula.domain.testTask +import de.jeanlucmakiola.agendula.domain.untimedInstantOf import de.jeanlucmakiola.agendula.domain.asReminderLead import de.jeanlucmakiola.floret.reminders.asReminderLeads import org.junit.jupiter.api.Test @@ -29,6 +31,25 @@ class StoredRemindersTest { private fun stored(form: TaskForm, current: de.jeanlucmakiola.agendula.domain.Task? = null, with: Settings = settings) = StoredReminders.of(form, current, with, now.toEpochMilliseconds(), zone) + @Test + fun `an end without a time is past or not by the time it rings at, not the one it is stored at`() { + val today = LocalDate.of(2026, 10, 1) + val withTimes = Settings( + defaultReminders = listOf(TaskReminder(60), TaskReminder(0, fromStart = true)).map { it.asReminderLead() }, + allDayReminderMinuteOfDay = 13 * 60, + ) + val untimedDue = TaskForm( + title = "t", listId = 1, start = now - 1.hours, + due = untimedInstantOf(TaskEnd.DUE, today, zone), untimed = TaskEnd.DUE, + ) + val untimedStart = TaskForm( + title = "t", listId = 1, start = untimedInstantOf(TaskEnd.START, today, zone), + due = now + 5.days, untimed = TaskEnd.START, + ) + assertThat(stored(untimedDue, with = withTimes.copy(allDayReminderMinuteOfDay = 9 * 60))).isEmpty() + assertThat(stored(untimedStart, with = withTimes)).contains(TaskReminder(0, fromStart = true)) + } + @Test fun `a new task takes the defaults for the dates it has`() { val form = TaskForm(title = "t", listId = 1, start = now + 2.days, due = now + 5.days) diff --git a/app/src/test/java/de/jeanlucmakiola/agendula/data/tasks/ical/VTodoMapperTest.kt b/app/src/test/java/de/jeanlucmakiola/agendula/data/tasks/ical/VTodoMapperTest.kt index 389aba0..89e9e93 100644 --- a/app/src/test/java/de/jeanlucmakiola/agendula/data/tasks/ical/VTodoMapperTest.kt +++ b/app/src/test/java/de/jeanlucmakiola/agendula/data/tasks/ical/VTodoMapperTest.kt @@ -475,7 +475,11 @@ class VTodoMapperTest { @Test fun `an untimed due round-trips too`() { - val dueOnly = mixed.copy(untimed = TaskEnd.DUE) + val dueOnly = mixed.copy( + dtstart = Instant.parse("2026-09-01T07:00:00Z"), + due = Instant.parse("2026-09-01T21:59:59Z"), + untimed = TaskEnd.DUE, + ) val written = VTodoMapper.write(dueOnly) assertThat(written.property("DUE")?.param(ICalValues.NO_TIME_PARAM)).isEqualTo("TRUE") assertThat(written.property("DTSTART")?.param(ICalValues.NO_TIME_PARAM)).isNull() @@ -503,6 +507,18 @@ class VTodoMapperTest { assertThat(mapped.entity.unknownProperties).contains("DUE;X-AGENDULA-NO-TIME=TRUE") } + @Test + fun `a marked value another client moved off its day boundary has a time, and loses the mark`() { + val mapped = VTodoMapper.read( + vtodo( + "DTSTART;TZID=Europe/Berlin:20260901T090000", + "DUE;X-AGENDULA-NO-TIME=TRUE;TZID=Europe/Berlin:20260901T150000", + ), + ) + assertThat(mapped.entity.untimed).isNull() + assertThat(VTodoMapper.write(mapped.entity).property("DUE")?.param(ICalValues.NO_TIME_PARAM)).isNull() + } + @Test fun `a mark on a DATE is ignored, the task is all-day`() { val read = VTodoMapper.read( diff --git a/app/src/test/java/de/jeanlucmakiola/agendula/data/tasks/room/OccurrenceTimesTest.kt b/app/src/test/java/de/jeanlucmakiola/agendula/data/tasks/room/OccurrenceTimesTest.kt new file mode 100644 index 0000000..16baf26 --- /dev/null +++ b/app/src/test/java/de/jeanlucmakiola/agendula/data/tasks/room/OccurrenceTimesTest.kt @@ -0,0 +1,45 @@ +package de.jeanlucmakiola.agendula.data.tasks.room + +import com.google.common.truth.Truth.assertThat +import de.jeanlucmakiola.agendula.domain.TaskEnd +import de.jeanlucmakiola.agendula.domain.untimedInstantOf +import org.junit.jupiter.api.Test +import java.time.LocalDate +import java.time.ZoneId +import kotlin.time.Instant + +class OccurrenceTimesTest { + + private val berlin = ZoneId.of("Europe/Berlin") + + private fun at(date: LocalDate, hour: Int) = + Instant.fromEpochMilliseconds(date.atTime(hour, 0).atZone(berlin).toInstant().toEpochMilli()) + + @Test + fun `a due without a time stays on its day across a clock change (#101)`() { + val master = TaskEntity( + listId = 1, + uid = "u", + dtstart = at(LocalDate.of(2026, 10, 24), 10), + due = untimedInstantOf(TaskEnd.DUE, LocalDate.of(2026, 10, 26), berlin), + timezone = "Europe/Berlin", + untimed = TaskEnd.DUE, + rrule = "FREQ=WEEKLY", + ) + val (_, due) = occurrenceTimes(master, at(LocalDate.of(2026, 10, 31), 10)) + assertThat(due).isEqualTo(untimedInstantOf(TaskEnd.DUE, LocalDate.of(2026, 11, 2), berlin)) + } + + @Test + fun `a timed due keeps the master's length`() { + val master = TaskEntity( + listId = 1, + uid = "u", + dtstart = at(LocalDate.of(2026, 9, 1), 10), + due = at(LocalDate.of(2026, 9, 1), 12), + timezone = "Europe/Berlin", + ) + val (_, due) = occurrenceTimes(master, at(LocalDate.of(2026, 9, 8), 10)) + assertThat(due).isEqualTo(at(LocalDate.of(2026, 9, 8), 12)) + } +} diff --git a/app/src/test/java/de/jeanlucmakiola/agendula/domain/AllDayTimeTest.kt b/app/src/test/java/de/jeanlucmakiola/agendula/domain/AllDayTimeTest.kt index b6820e9..135949f 100644 --- a/app/src/test/java/de/jeanlucmakiola/agendula/domain/AllDayTimeTest.kt +++ b/app/src/test/java/de/jeanlucmakiola/agendula/domain/AllDayTimeTest.kt @@ -67,4 +67,15 @@ class AllDayTimeTest { assertThat(start.calendarDate(allDay = false, zone = newYork)).isEqualTo(julyTwentieth) assertThat(due.calendarDate(allDay = false, zone = newYork)).isEqualTo(julyTwentieth) } + + @Test + fun `an untimed end reads as its date in the task's zone, wherever the device is`() { + val due = untimedInstantOf(TaskEnd.DUE, julyTwentieth, berlin) + val task = testTask(start = due - kotlin.time.Duration.parse("2h"), due = due) + .copy(untimed = TaskEnd.DUE, timeZone = "Europe/Berlin") + assertThat(task.dateOf(TaskEnd.DUE, ZoneId.of("Asia/Tokyo"))).isEqualTo(julyTwentieth) + assertThat(task.shownValue(TaskEnd.DUE)).isEqualTo(allDayInstantOf(julyTwentieth)) + assertThat(task.hasTime(TaskEnd.DUE)).isFalse() + assertThat(task.hasTime(TaskEnd.START)).isTrue() + } } diff --git a/app/src/test/java/de/jeanlucmakiola/agendula/ui/edit/TaskEditViewModelTest.kt b/app/src/test/java/de/jeanlucmakiola/agendula/ui/edit/TaskEditViewModelTest.kt index 17d393a..cb268dd 100644 --- a/app/src/test/java/de/jeanlucmakiola/agendula/ui/edit/TaskEditViewModelTest.kt +++ b/app/src/test/java/de/jeanlucmakiola/agendula/ui/edit/TaskEditViewModelTest.kt @@ -25,6 +25,7 @@ import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.flowOf +import kotlinx.coroutines.runBlocking import kotlinx.coroutines.test.UnconfinedTestDispatcher import kotlinx.coroutines.test.resetMain import kotlinx.coroutines.test.setMain @@ -73,11 +74,10 @@ class TaskEditViewModelTest { } @Test - fun `untouched reminders survive an all-day switch while the task has no date`() { + fun `untouched reminders survive the task coming back all-day`() { val vm = editing(reminders = listOf(TaskReminder(0))) vm.onDueChange(null) - vm.onAllDayChange(true) - vm.onDueChange(due) + vm.onWhenChange(TaskEnd.DUE, LocalDate.now().plusDays(3), null) assertThat(vm.state.value.reminders).isNotEmpty() assertThat(vm.state.value.reminders.all { it.allDay.minuteOfDay != null || it.minutesBefore % (24 * 60) == 0 }) @@ -109,10 +109,9 @@ class TaskEditViewModelTest { } @Test - fun `an all-day switch or another list does not bring them back`() { + fun `another list does not bring them back`() { val vm = editing() vm.onDueChange(null) - vm.onAllDayChange(true) vm.onListChange(LIST_ID + 1) assertThat(vm.state.value.reminders).isEmpty() @@ -214,6 +213,51 @@ class TaskEditViewModelTest { assertThat(vm.state.value.untimed).isNull() } + @Test + fun `clearing the timed end leaves the other one all-day`() { + val vm = editing(start = due - 1.days) + val day = LocalDate.now().plusDays(3) + vm.onWhenChange(TaskEnd.DUE, day, null) + vm.onStartChange(null) + + val state = vm.state.value + assertThat(state.isAllDay).isTrue() + assertThat(state.untimed).isNull() + assertThat(state.due).isEqualTo(allDayInstantOf(day)) + } + + @Test + fun `a new task's untouched start goes all-day with a due picked without a time`() { + val prefs = settingsPrefs().apply { runBlocking { setDefaultStartToday(true) } } + val vm = TaskEditViewModel(repository, prefs).apply { bindNew() } + val day = LocalDate.now().plusDays(2) + vm.onWhenChange(TaskEnd.DUE, day, null) + + assertThat(vm.state.value.isAllDay).isTrue() + assertThat(vm.state.value.start).isEqualTo(allDayInstantOf(LocalDate.now())) + } + + @Test + fun `a start the user picked keeps its time beside a due without one`() { + val prefs = settingsPrefs().apply { runBlocking { setDefaultStartToday(true) } } + val vm = TaskEditViewModel(repository, prefs).apply { bindNew() } + vm.onWhenChange(TaskEnd.START, LocalDate.now(), LocalTime.of(9, 0)) + vm.onWhenChange(TaskEnd.DUE, LocalDate.now().plusDays(2), null) + + assertThat(vm.state.value.untimed).isEqualTo(TaskEnd.DUE) + } + + @Test + fun `an untimed end from another zone opens on its own date`() { + val berlinDue = untimedInstantOf(TaskEnd.DUE, LocalDate.of(2026, 11, 20), java.time.ZoneId.of("Pacific/Kiritimati")) + repository.task = task(due - 1.days, berlinDue).copy(untimed = TaskEnd.DUE, timeZone = "Pacific/Kiritimati") + repository.reminders = emptyList() + val vm = TaskEditViewModel(repository, settingsPrefs()).apply { bindEdit(TASK_ID) } + + assertThat(vm.state.value.due).isEqualTo(untimedInstantOf(TaskEnd.DUE, LocalDate.of(2026, 11, 20))) + assertThat(vm.state.value.isDirty).isFalse() + } + @Test fun `clearing the untimed end drops its mark`() { val vm = editing(start = due - 1.days)