Keep an untimed series' zone on an edit for all occurrences (#101)

This commit is contained in:
makiolaj committed 2026-11-20 12:28:37 +01:00
1 parent d6425124ee
commit fd28ffc451
2 files changed
+79 -14

No files matched your search

@@ -3,6 +3,7 @@ package de.jeanlucmakiola.agendula.data.tasks.room
import de.jeanlucmakiola.agendula.domain.TaskEnd
import de.jeanlucmakiola.agendula.domain.TaskForm
import de.jeanlucmakiola.agendula.domain.calendarDate
import de.jeanlucmakiola.agendula.domain.knownZone
import de.jeanlucmakiola.agendula.domain.untimedDate
import de.jeanlucmakiola.agendula.domain.untimedMoved
import de.jeanlucmakiola.agendula.domain.zoneOr
@@ -101,6 +102,10 @@ object TaskFormWriter {
* [master] edited through one of its occurrences for the whole series: every
* content field from [form], and the anchor moved by however far the user
* moved that occurrence, so the other occurrences move with it.
*
* A series that already had an end without a time keeps its zone, so an edit
* from another zone that leaves the dates alone leaves its instants (and its
* exceptions and overrides) alone too.
*/
fun seriesEdit(
master: TaskEntity,
@@ -110,12 +115,14 @@ object TaskFormWriter {
now: Instant,
tzId: String,
): TaskEntity {
val edited = withRule(apply(master, form, now, tzId), form.rrule, form.repeatFromCompletion)
val dtstart = shifted(TaskEnd.START, master, occurrenceStart, form, tzId)
val keepsZone = !form.isAllDay && !master.isAllDay && form.untimed != null && form.untimed == master.untimed
val seriesZone = if (keepsZone) knownZone(master.timezone) ?: tzId else tzId
val edited = withRule(apply(master, form, now, seriesZone), form.rrule, form.repeatFromCompletion)
val dtstart = shifted(TaskEnd.START, master, occurrenceStart, form, tzId, seriesZone)
val delta = if (dtstart != null && master.dtstart != null) dtstart - master.dtstart else null
return edited.copy(
dtstart = dtstart,
due = shifted(TaskEnd.DUE, master, occurrenceDue, form, tzId),
due = shifted(TaskEnd.DUE, master, occurrenceDue, form, tzId, seriesZone),
// The occurrence's completion is its own, not the series'.
status = master.status,
percentComplete = master.percentComplete,
@@ -125,8 +132,8 @@ object TaskFormWriter {
// Exceptions of a start without a time move by whole days, as the start does.
!form.isAllDay && form.untimed == TaskEnd.START -> {
val zone = master.timezone ?: tzId
val days = ChronoUnit.DAYS.between(master.dtstart!!.untimedDate(zone), dtstart!!.untimedDate(tzId))
mapValues(edited.exdate, zone) { untimedMoved(TaskEnd.START, it, days, zone, zoneOr(zone)) }
val days = ChronoUnit.DAYS.between(master.dtstart!!.untimedDate(zone), dtstart!!.untimedDate(seriesZone))
mapValues(edited.exdate, zone, seriesZone) { untimedMoved(TaskEnd.START, it, days, zone, zoneOr(seriesZone)) }
}
else -> shiftValues(edited.exdate, delta, master.timezone ?: tzId)
},
@@ -156,10 +163,10 @@ object TaskFormWriter {
fun instantsWhere(values: String?, zone: String, take: (Instant) -> Boolean): List<Instant> =
parsed(values, zone).mapNotNull { it.second }.filter(take)
private fun mapValues(values: String?, zone: String, map: (Instant) -> Instant?): String? {
private fun mapValues(values: String?, zone: String, writeZone: String = zone, map: (Instant) -> Instant?): String? {
if (values == null) return null
return parsed(values, zone).mapNotNull { (raw, at) ->
if (at == null) raw else map(at)?.let { writeValueLike(raw, it, zone) }
if (at == null) raw else map(at)?.let { writeValueLike(raw, it, writeZone) }
}.joinToString(",").ifEmpty { null }
}
@@ -189,16 +196,18 @@ object TaskFormWriter {
/**
* [master]'s [end] moved as far as the occurrence's moved from [was] to the
* form's value. An end without a time moves by whole days, so a clock
* change in between cannot push it off its date.
* change in between cannot push it off its date; the form is read in
* [tzId] and the result written in [seriesZone].
*/
private fun shifted(end: TaskEnd, master: TaskEntity, was: Instant?, form: TaskForm, tzId: String): Instant? {
private fun shifted(end: TaskEnd, master: TaskEntity, was: Instant?, form: TaskForm, tzId: String, seriesZone: String): Instant? {
val anchor = if (end == TaskEnd.START) master.dtstart else master.due
val now = (if (end == TaskEnd.START) form.start else form.due) ?: return null
if (anchor == null || was == null) return now.forAllDay(form.isAllDay)
if (form.isAllDay || form.untimed != end) return (anchor + (now - was)).forAllDay(form.isAllDay)
val zone = zoneOr(tzId)
val days = ChronoUnit.DAYS.between(was.untimedDate(master.timezone), now.calendarDate(allDay = false, zone = zone))
return untimedMoved(end, anchor, days, master.timezone, zone)
if (form.isAllDay || form.untimed != end) {
return (if (anchor == null || was == null) now else anchor + (now - was)).forAllDay(form.isAllDay)
}
if (anchor == null || was == null) return untimedMoved(end, now, 0, tzId, zoneOr(seriesZone))
val days = ChronoUnit.DAYS.between(was.untimedDate(master.timezone), now.calendarDate(allDay = false, zone = zoneOr(tzId)))
return untimedMoved(end, anchor, days, master.timezone, zoneOr(seriesZone))
}
/**
@@ -223,4 +223,60 @@ class TaskFormWriterSeriesTest {
assertThat(edited.exdate).isEqualTo("20260329T000000")
}
@Test
fun `a title-only edit from another zone keeps an untimed series where it was (#101)`() {
val berlin = java.time.ZoneId.of("Europe/Berlin")
val newYork = java.time.ZoneId.of("America/New_York")
val untimed = master.copy(
dtstart = untimedInstantOf(TaskEnd.START, LocalDate.of(2026, 3, 2), berlin),
due = Instant.parse("2026-03-02T16:00:00Z"),
untimed = TaskEnd.START,
rrule = "FREQ=DAILY",
exdate = "20260304T000000,20260305T000000",
)
val occurrence = untimedInstantOf(TaskEnd.START, LocalDate.of(2026, 3, 10), berlin)
val form = TaskForm(
title = "Water all plants",
listId = 1,
start = untimedInstantOf(TaskEnd.START, LocalDate.of(2026, 3, 10), newYork),
due = Instant.parse("2026-03-10T16:00:00Z"),
untimed = TaskEnd.START,
rrule = "FREQ=DAILY",
)
val edited = TaskFormWriter.seriesEdit(untimed, occurrence, Instant.parse("2026-03-10T16:00:00Z"), form, now, "America/New_York")
assertThat(edited.title).isEqualTo("Water all plants")
assertThat(edited.dtstart).isEqualTo(untimed.dtstart)
assertThat(edited.due).isEqualTo(untimed.due)
assertThat(edited.timezone).isEqualTo("Europe/Berlin")
assertThat(edited.exdate).isEqualTo(untimed.exdate)
}
@Test
fun `moving it a day from another zone moves start and exceptions in the series' zone (#101)`() {
val berlin = java.time.ZoneId.of("Europe/Berlin")
val newYork = java.time.ZoneId.of("America/New_York")
val untimed = master.copy(
dtstart = untimedInstantOf(TaskEnd.START, LocalDate.of(2026, 3, 2), berlin),
due = null,
untimed = TaskEnd.START,
rrule = "FREQ=DAILY",
exdate = "20260303T230000Z",
)
val occurrence = untimedInstantOf(TaskEnd.START, LocalDate.of(2026, 3, 10), berlin)
val form = TaskForm(
title = "Water plants",
listId = 1,
start = untimedInstantOf(TaskEnd.START, LocalDate.of(2026, 3, 11), newYork),
due = Instant.parse("2026-03-12T16:00:00Z"),
untimed = TaskEnd.START,
rrule = "FREQ=DAILY",
)
val edited = TaskFormWriter.seriesEdit(untimed, occurrence, null, form, now, "America/New_York")
assertThat(edited.dtstart).isEqualTo(untimedInstantOf(TaskEnd.START, LocalDate.of(2026, 3, 3), berlin))
assertThat(edited.timezone).isEqualTo("Europe/Berlin")
assertThat(edited.exdate).isEqualTo("20260304T230000Z")
}
}