Split off overrides on undo only after a move from completion (#79)

This commit is contained in:
makiolaj committed 2026-11-18 21:00:54 +01:00
1 parent 33466dc6d9
commit b957b20f10
3 files changed
+32 -7

No files matched your search

@@ -884,13 +884,10 @@ class RoomTasksDataSource @Inject constructor(
tasks.update(back)
// Moved from completion, the series may have taken overrides the schedule it returns to never generates.
back.recurrenceSpec()?.let { spec ->
val floating = ZoneId.of(zone())
tasks.overrides(back.id)
.filter {
it.recurrenceId != null && it.recurrenceId >= spec.anchor &&
!RepeatFromCompletion.onSchedule(spec.copy(exdate = null), it.recurrenceId, floating)
}
.forEach { detach(it, now) }
val overrides = tasks.overrides(back.id).filter { it.recurrenceId != null }
val stranded = RepeatFromCompletion.strandedByUndo(spec, record.advancedTo, overrides.map { it.recurrenceId!! }, ZoneId.of(zone()))
.toSet()
overrides.filter { it.recurrenceId in stranded }.forEach { detach(it, now) }
}
detached.forEach { (at, task) -> if (returns(task)) toOverride(task!!, back, at, now) }
advanced.delete(row.id)
@@ -154,6 +154,17 @@ object RepeatFromCompletion {
.isNotEmpty()
}
/**
* The overrides, by recurrence id, that a series moved back to [spec] from
* [advancedTo] no longer generates: only after a move off [spec]'s schedule,
* which only a tick repeating from completion makes. Others are left be.
*/
fun strandedByUndo(spec: RecurrenceSpec, advancedTo: Instant, overrides: List<Instant>, floatingZone: ZoneId): List<Instant> {
val schedule = spec.copy(exdate = null)
if (onSchedule(schedule, advancedTo, floatingZone)) return emptyList()
return overrides.filter { it >= spec.anchor && !onSchedule(schedule, it, floatingZone) }
}
/** The last instant [rule]'s `UNTIL` admits, read as [RecurrenceExpander] reads it; null without one. */
private fun untilOf(rule: String, zone: ZoneId): Instant? {
val until = runCatching { RecurrenceRule(rule).until }.getOrNull() ?: return null
@@ -101,6 +101,23 @@ class RepeatFromCompletionTest {
assertThat(RepeatFromCompletion.onSchedule(spec, at("2026-03-17T00:00:00Z"), berlin)).isFalse()
}
@Test
fun `undoing a move off the schedule lets go of the overrides it no longer generates`() {
val spec = allDay("FREQ=WEEKLY", "2026-03-02T00:00:00Z")
val overrides = listOf(at("2026-03-09T00:00:00Z"), at("2026-03-12T00:00:00Z"))
assertThat(RepeatFromCompletion.strandedByUndo(spec, at("2026-03-10T00:00:00Z"), overrides, berlin))
.containsExactly(at("2026-03-12T00:00:00Z"))
}
@Test
fun `undoing a move along the schedule leaves every override be`() {
val spec = allDay("FREQ=WEEKLY", "2026-03-02T00:00:00Z")
val overrides = listOf(at("2026-03-09T00:00:00Z"), at("2026-03-12T00:00:00Z"))
assertThat(RepeatFromCompletion.strandedByUndo(spec, at("2026-03-09T00:00:00Z"), overrides, berlin)).isEmpty()
}
@Test
fun `COUNT is left to the caller`() {
val spec = allDay("FREQ=DAILY;COUNT=1", "2026-03-02T00:00:00Z")