Keep an untimed series' zone when splitting it (#101)

This commit is contained in:
makiolaj committed 2026-11-20 12:37:25 +01:00
1 parent 22b95298e2
commit 7d7ce3d662
3 files changed
+96 -24

No files matched your search

@@ -376,32 +376,11 @@ class RoomTasksDataSource @Inject constructor(
val sameRule = form.rrule == master.rrule
val rule = form.rrule?.let { if (sameRule) TaskFormWriter.remainingCount(it, spent) else it }
val base = master.copy(
id = 0,
uid = UUID.randomUUID().toString(),
href = null,
etag = null,
rrule = null,
exdate = null,
rdate = null,
createdAt = now,
sequence = 0,
isDeleted = false,
dtstart = start,
due = due,
status = TaskStatus.NEEDS_ACTION,
percentComplete = null,
completedAt = null,
val fresh = TaskFormWriter.splitTail(
// Recurrence lines belong to the old series; everything else carries over.
unknownProperties = seriesFreeText(master.unknownProperties),
master.copy(unknownProperties = seriesFreeText(master.unknownProperties)),
occurrenceStart, start, due, form, rule, UUID.randomUUID().toString(), now, zone(),
)
val applied = TaskFormWriter.apply(base, form.copy(percentComplete = null), now, zone())
val delta = applied.dtstart?.let { newStart -> start?.let { newStart - it } }
val laterExceptions = TaskFormWriter.valuesFrom(master.exdate, occurrenceStart, master.timezone ?: zone())
val exceptions = if (rule == null) null
else if (delta == null || delta == kotlin.time.Duration.ZERO) laterExceptions
else TaskFormWriter.shiftValues(laterExceptions, delta, master.timezone ?: zone())
val fresh = TaskFormWriter.withRule(applied, rule, form.repeatFromCompletion).let { if (it.rrule != null) it.copy(exdate = exceptions) else it }
val newId = tasks.insert(fresh)
// A new UID, so a new resource: the alarms are copies, not the same ones.
alarms.copyToTask(master.id, newId, sameResource = false)
@@ -140,6 +140,43 @@ object TaskFormWriter {
)
}
/**
* The new series a "this and following" edit of [master] continues as, from
* the occurrence at [occurrenceStart] whose times are [start]/[due] under
* [rule]: edited as [seriesEdit] edits a series, with the exceptions from
* that occurrence on.
*/
fun splitTail(
master: TaskEntity,
occurrenceStart: Instant,
start: Instant?,
due: Instant?,
form: TaskForm,
rule: String?,
uid: String,
now: Instant,
tzId: String,
): TaskEntity {
val base = master.copy(
id = 0,
uid = uid,
href = null,
etag = null,
rrule = rule,
exdate = rule?.let { valuesFrom(master.exdate, occurrenceStart, master.timezone ?: tzId) },
rdate = null,
createdAt = now,
sequence = 0,
isDeleted = false,
dtstart = start,
due = due,
status = TaskStatus.NEEDS_ACTION,
percentComplete = null,
completedAt = null,
)
return seriesEdit(base, start, due, form.copy(rrule = rule), now, tzId)
}
/**
* An `EXDATE`/`RDATE` list with every value moved by [delta], each kept in the
* shape it was written in — so a series moved in time keeps its exceptions.
@@ -279,4 +279,60 @@ class TaskFormWriterSeriesTest {
assertThat(edited.timezone).isEqualTo("Europe/Berlin")
assertThat(edited.exdate).isEqualTo("20260304T230000Z")
}
@Test
fun `a title-only split from another zone keeps an untimed tail 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 = null,
untimed = TaskEnd.START,
rrule = "FREQ=DAILY",
exdate = "20260304T000000,20260312T000000",
)
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),
untimed = TaskEnd.START,
rrule = "FREQ=DAILY",
)
val tail = TaskFormWriter.splitTail(untimed, occurrence, occurrence, null, form, "FREQ=DAILY", "tail", now, "America/New_York")
assertThat(tail.title).isEqualTo("Water all plants")
assertThat(tail.timezone).isEqualTo("Europe/Berlin")
assertThat(tail.dtstart).isEqualTo(occurrence)
assertThat(tail.exdate).isEqualTo("20260312T000000")
assertThat(untimedInstantOf(TaskEnd.START, LocalDate.of(2026, 3, 12), berlin)).isEqualTo(tail.dtstart!! + 2.days)
}
@Test
fun `a split moved a day from another zone moves the tail's 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,20260311T230000Z",
)
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),
untimed = TaskEnd.START,
rrule = "FREQ=DAILY",
)
val tail = TaskFormWriter.splitTail(untimed, occurrence, occurrence, null, form, "FREQ=DAILY", "tail", now, "America/New_York")
assertThat(tail.dtstart).isEqualTo(untimedInstantOf(TaskEnd.START, LocalDate.of(2026, 3, 11), berlin))
assertThat(tail.timezone).isEqualTo("Europe/Berlin")
assertThat(tail.exdate).isEqualTo("20260312T230000Z")
}
}