From 0652859eccd259624ff541fe3b2fb34c474b60e6 Mon Sep 17 00:00:00 2001 From: Jean-Luc Makiola Date: Wed, 25 Nov 2026 12:16:05 +0100 Subject: [PATCH 1/4] fix(#502): series rows without a usable DURATION no longer lose their occurrences on a single edit or delete --- .../data/calendar/CalendarDataSource.kt | 46 ++++++++++++++++--- .../data/calendar/EventWriteMapper.kt | 21 +++++++++ .../data/calendar/EventWriteMapperTest.kt | 40 ++++++++++++++++ 3 files changed, 100 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/data/calendar/CalendarDataSource.kt b/app/src/main/java/de/jeanlucmakiola/calendula/data/calendar/CalendarDataSource.kt index 8cfd3f78..f4f0b004 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/data/calendar/CalendarDataSource.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/data/calendar/CalendarDataSource.kt @@ -1270,7 +1270,7 @@ class AndroidCalendarDataSource @Inject constructor( form: EventForm, allDayReminderTimeMinutes: Int, ): Long { - val row = querySeriesRow(eventId) + val row = querySeriesRow(eventId, beginMillis) // Not a series, so there is no occurrence to single out: the row *is* the // event, and an exception with nothing to be an exception to would leave // an orphan holding the edit (the same test deleteOccurrence makes). @@ -1384,7 +1384,7 @@ class AndroidCalendarDataSource @Inject constructor( updated: EventForm, allDayReminderTimeMinutes: Int, ): Long { - val row = querySeriesRow(eventId) + val row = querySeriesRow(eventId, beginMillis) // From the first occurrence on (or with no rule to split) this is // just a series update. if (row.rrule.isNullOrBlank() || beginMillis <= row.dtStartMillis) { @@ -1447,7 +1447,7 @@ class AndroidCalendarDataSource @Inject constructor( } override fun deleteEventFromOccurrence(eventId: Long, beginMillis: Long) { - val row = querySeriesRow(eventId) + val row = querySeriesRow(eventId, beginMillis) // From the first occurrence on = the whole series; also the fallback // when there is no RRULE to truncate. if (row.rrule.isNullOrBlank() || beginMillis <= row.dtStartMillis) { @@ -1476,8 +1476,12 @@ class AndroidCalendarDataSource @Inject constructor( updateEventRow(eventId, values, "truncate series event id=$eventId begin=$beginMillis") } - /** The series anchor: every time-related column of the Events row. */ - private fun querySeriesRow(eventId: Long): SeriesRow = resolver.query( + /** + * The series anchor: every time-related column of the Events row. A recurring + * row with no usable DURATION gets one derived ([seriesDuration]), from the + * expanded occurrence at [occurrenceMillis] when DTEND can't supply it. + */ + private fun querySeriesRow(eventId: Long, occurrenceMillis: Long? = null): SeriesRow = resolver.query( eventUri(eventId), arrayOf( CalendarContract.Events.DTSTART, @@ -1488,11 +1492,12 @@ class AndroidCalendarDataSource @Inject constructor( CalendarContract.Events._SYNC_ID, CalendarContract.Events.EXDATE, CalendarContract.Events.RDATE, + CalendarContract.Events.DTEND, ), null, null, null, )?.use { c -> if (c.moveToFirst()) { - SeriesRow( + val row = SeriesRow( dtStartMillis = c.getLong(0), rrule = c.getString(1), timezone = c.getString(2), @@ -1502,11 +1507,37 @@ class AndroidCalendarDataSource @Inject constructor( exdate = c.getString(6), rdate = c.getString(7), ) + if (!row.recurs || !row.duration.isNullOrBlank()) return@use row + val dtEnd = if (c.isNull(8)) null else c.getLong(8) + row.copy( + duration = seriesDuration( + duration = null, + dtStartMillis = row.dtStartMillis, + dtEndMillis = dtEnd, + occurrenceSpanMillis = occurrenceMillis?.let { occurrenceSpan(eventId, it) }, + isAllDay = row.allDay != 0, + ), + ) } else { null } } ?: throw WriteFailedException("read series row of event id=$eventId") + /** Length of the already-expanded occurrence of [eventId] starting at [beginMillis]. */ + private fun occurrenceSpan(eventId: Long, beginMillis: Long): Long? { + val uri = CalendarContract.Instances.CONTENT_URI.buildUpon().also { + ContentUris.appendId(it, beginMillis) + ContentUris.appendId(it, beginMillis + 1) + }.build() + return resolver.query( + uri, + arrayOf(CalendarContract.Instances.BEGIN, CalendarContract.Instances.END), + "${CalendarContract.Instances.EVENT_ID} = ? AND ${CalendarContract.Instances.BEGIN} = ?", + arrayOf(eventId.toString(), beginMillis.toString()), + null, + )?.use { c -> if (c.moveToFirst()) c.getLong(1) - c.getLong(0) else null } + } + private data class SeriesRow( val dtStartMillis: Long, val rrule: String?, @@ -1532,6 +1563,7 @@ class AndroidCalendarDataSource @Inject constructor( */ fun timeColumns(): ContentValues = ContentValues().apply { put(CalendarContract.Events.DTSTART, dtStartMillis) + if (recurs) putNull(CalendarContract.Events.DTEND) put(CalendarContract.Events.RRULE, rrule) put(CalendarContract.Events.DURATION, duration) put(CalendarContract.Events.EVENT_TIMEZONE, timezone) @@ -1732,7 +1764,7 @@ class AndroidCalendarDataSource @Inject constructor( } override fun deleteOccurrence(eventId: Long, beginMillis: Long) { - val row = querySeriesRow(eventId) + val row = querySeriesRow(eventId, beginMillis) // Not a series, so the row *is* the event: an exclusion would put a // recurrence set on a one-off, an exception would leave an orphan. if (!row.recurs) { diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/data/calendar/EventWriteMapper.kt b/app/src/main/java/de/jeanlucmakiola/calendula/data/calendar/EventWriteMapper.kt index 328b1215..8f82af6d 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/data/calendar/EventWriteMapper.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/data/calendar/EventWriteMapper.kt @@ -454,6 +454,26 @@ internal fun buildOccurrenceExdateValues( existingExdate, listOf(occurrenceMillis), dtStartMillis, rrule, rdate, duration, timezone, allDay, ) +/** + * The DURATION a series row is written back with. Rows from other tools can + * carry a DTEND (sometimes 0) instead of a DURATION; rewriting them without one + * leaves the provider nothing to expand, and every occurrence vanishes (#502). + * Falls back to [dtEndMillis], then to an expanded occurrence's span. + */ +internal fun seriesDuration( + duration: String?, + dtStartMillis: Long, + dtEndMillis: Long?, + occurrenceSpanMillis: Long?, + isAllDay: Boolean, +): String { + if (!duration.isNullOrBlank()) return duration + val span = dtEndMillis?.minus(dtStartMillis)?.takeIf { it > 0 } + ?: occurrenceSpanMillis?.takeIf { it > 0 } + ?: 0L + return rfc2445Duration(if (isAllDay) span.coerceAtLeast(MILLIS_PER_DAY) else span, isAllDay) +} + /** [buildOccurrenceExdateValues] excluding every one of [occurrences] in one write. */ internal fun buildOccurrencesExdateValues( existingExdate: String?, @@ -469,6 +489,7 @@ internal fun buildOccurrencesExdateValues( mergedExdate(exdate, millis, allDay != 0, timezone) }, CalendarContract.Events.DTSTART to dtStartMillis, + CalendarContract.Events.DTEND to null, CalendarContract.Events.RRULE to rrule, CalendarContract.Events.RDATE to rdate, CalendarContract.Events.DURATION to duration, diff --git a/app/src/test/java/de/jeanlucmakiola/calendula/data/calendar/EventWriteMapperTest.kt b/app/src/test/java/de/jeanlucmakiola/calendula/data/calendar/EventWriteMapperTest.kt index 9cd03414..7d8a3bd4 100644 --- a/app/src/test/java/de/jeanlucmakiola/calendula/data/calendar/EventWriteMapperTest.kt +++ b/app/src/test/java/de/jeanlucmakiola/calendula/data/calendar/EventWriteMapperTest.kt @@ -500,6 +500,46 @@ class EventWriteMapperTest { .isEqualTo(CalendarContract.Events.STATUS_CANCELED) } + // --- seriesDuration (#502) --- + + @Test + fun `series duration keeps a stored duration`() { + assertThat(seriesDuration("PT1H", 1_000L, 0L, 5_000L, isAllDay = false)).isEqualTo("PT1H") + } + + @Test + fun `series duration falls back to dtend when it lies after dtstart`() { + assertThat(seriesDuration(null, 1_000_000L, 8_200_000L, null, isAllDay = false)).isEqualTo("P7200S") + } + + @Test + fun `series duration ignores a zero dtend and uses the occurrence span`() { + assertThat(seriesDuration(null, 1_790_852_400_000L, 0L, 7_200_000L, isAllDay = false)) + .isEqualTo("P7200S") + } + + @Test + fun `series duration never comes back empty`() { + assertThat(seriesDuration(" ", 1_000L, 0L, null, isAllDay = false)).isEqualTo("P0S") + assertThat(seriesDuration(null, 1_000L, null, null, isAllDay = true)).isEqualTo("P1D") + } + + @Test + fun `exdate drop clears a stray dtend on the series`() { + val values = buildOccurrenceExdateValues( + existingExdate = null, + occurrenceMillis = 1_784_102_400_000L, + dtStartMillis = 1_783_929_600_000L, + rrule = "FREQ=DAILY;COUNT=5", + rdate = null, + duration = "PT1H", + timezone = "Europe/Berlin", + allDay = 0, + ) + assertThat(values).containsKey(CalendarContract.Events.DTEND) + assertThat(values[CalendarContract.Events.DTEND]).isNull() + } + // --- buildOccurrenceExdateValues ("delete only this event", no _sync_id) --- @Test From 85cf59ad82baf843acd891a48654c0587a2112e7 Mon Sep 17 00:00:00 2001 From: Jean-Luc Makiola Date: Wed, 25 Nov 2026 12:23:38 +0100 Subject: [PATCH 2/4] fix(#502): review round 1 - occurrence span first, no guessed length, move path covered --- .../data/calendar/CalendarDataSource.kt | 27 ++++++++----------- .../data/calendar/EventWriteMapper.kt | 22 +++++++-------- .../data/calendar/EventWriteMapperTest.kt | 25 +++++++++++------ 3 files changed, 39 insertions(+), 35 deletions(-) diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/data/calendar/CalendarDataSource.kt b/app/src/main/java/de/jeanlucmakiola/calendula/data/calendar/CalendarDataSource.kt index f4f0b004..a3cf7bda 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/data/calendar/CalendarDataSource.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/data/calendar/CalendarDataSource.kt @@ -1508,16 +1508,13 @@ class AndroidCalendarDataSource @Inject constructor( rdate = c.getString(7), ) if (!row.recurs || !row.duration.isNullOrBlank()) return@use row - val dtEnd = if (c.isNull(8)) null else c.getLong(8) - row.copy( - duration = seriesDuration( - duration = null, - dtStartMillis = row.dtStartMillis, - dtEndMillis = dtEnd, - occurrenceSpanMillis = occurrenceMillis?.let { occurrenceSpan(eventId, it) }, - isAllDay = row.allDay != 0, - ), - ) + val duration = seriesDuration( + dtStartMillis = row.dtStartMillis, + dtEndMillis = CursorColumnReader(c).getLongOrNull(SERIES_IDX_DTEND), + occurrenceSpanMillis = occurrenceMillis?.let { occurrenceSpan(eventId, it) }, + isAllDay = row.allDay != 0, + ) ?: throw WriteFailedException("series event id=$eventId has no usable length") + row.copy(duration = duration) } else { null } @@ -1525,12 +1522,8 @@ class AndroidCalendarDataSource @Inject constructor( /** Length of the already-expanded occurrence of [eventId] starting at [beginMillis]. */ private fun occurrenceSpan(eventId: Long, beginMillis: Long): Long? { - val uri = CalendarContract.Instances.CONTENT_URI.buildUpon().also { - ContentUris.appendId(it, beginMillis) - ContentUris.appendId(it, beginMillis + 1) - }.build() return resolver.query( - uri, + instancesUri(beginMillis, beginMillis + 1), arrayOf(CalendarContract.Instances.BEGIN, CalendarContract.Instances.END), "${CalendarContract.Instances.EVENT_ID} = ? AND ${CalendarContract.Instances.BEGIN} = ?", arrayOf(eventId.toString(), beginMillis.toString()), @@ -1563,7 +1556,7 @@ class AndroidCalendarDataSource @Inject constructor( */ fun timeColumns(): ContentValues = ContentValues().apply { put(CalendarContract.Events.DTSTART, dtStartMillis) - if (recurs) putNull(CalendarContract.Events.DTEND) + putNull(CalendarContract.Events.DTEND) put(CalendarContract.Events.RRULE, rrule) put(CalendarContract.Events.DURATION, duration) put(CalendarContract.Events.EVENT_TIMEZONE, timezone) @@ -1858,6 +1851,8 @@ class AndroidCalendarDataSource @Inject constructor( SpecialDateType.entries.firstOrNull { markerFor(it) == marker } private companion object { + /** DTEND's index in [querySeriesRow]'s projection. */ + const val SERIES_IDX_DTEND = 8 const val TAG = "CalendarDataSource" /** Normalised key an attendee is matched/deduped on (case-insensitive email). */ diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/data/calendar/EventWriteMapper.kt b/app/src/main/java/de/jeanlucmakiola/calendula/data/calendar/EventWriteMapper.kt index 8f82af6d..beba38c7 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/data/calendar/EventWriteMapper.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/data/calendar/EventWriteMapper.kt @@ -369,7 +369,8 @@ internal fun buildMovedMasterValues( /** The recurring copy's DURATION: its own if present, else derived from DTEND. */ private fun MasterEventSnapshot.movedDuration(): String = duration?.takeIf { it.isNotBlank() } - ?: rfc2445Duration((dtEndMillis ?: dtStartMillis) - dtStartMillis, isAllDay) + ?: seriesDuration(dtStartMillis, dtEndMillis, occurrenceSpanMillis = null, isAllDay = isAllDay) + ?: throw IllegalStateException("series has no usable length") /** * Raw provider snapshot of one exception row of a recurring series (a modified @@ -455,22 +456,21 @@ internal fun buildOccurrenceExdateValues( ) /** - * The DURATION a series row is written back with. Rows from other tools can - * carry a DTEND (sometimes 0) instead of a DURATION; rewriting them without one - * leaves the provider nothing to expand, and every occurrence vanishes (#502). - * Falls back to [dtEndMillis], then to an expanded occurrence's span. + * DURATION for a series row that has none. Rows from other tools can carry a + * DTEND (sometimes 0) instead; rewriting them without a DURATION leaves the + * provider nothing to expand, and every occurrence vanishes (#502). The span of + * an occurrence the provider actually expanded wins over DTEND; null when + * neither gives a positive length, so the caller fails instead of guessing. */ internal fun seriesDuration( - duration: String?, dtStartMillis: Long, dtEndMillis: Long?, occurrenceSpanMillis: Long?, isAllDay: Boolean, -): String { - if (!duration.isNullOrBlank()) return duration - val span = dtEndMillis?.minus(dtStartMillis)?.takeIf { it > 0 } - ?: occurrenceSpanMillis?.takeIf { it > 0 } - ?: 0L +): String? { + val span = occurrenceSpanMillis?.takeIf { it > 0 } + ?: dtEndMillis?.minus(dtStartMillis)?.takeIf { it > 0 } + ?: return null return rfc2445Duration(if (isAllDay) span.coerceAtLeast(MILLIS_PER_DAY) else span, isAllDay) } diff --git a/app/src/test/java/de/jeanlucmakiola/calendula/data/calendar/EventWriteMapperTest.kt b/app/src/test/java/de/jeanlucmakiola/calendula/data/calendar/EventWriteMapperTest.kt index 7d8a3bd4..8f5e4e13 100644 --- a/app/src/test/java/de/jeanlucmakiola/calendula/data/calendar/EventWriteMapperTest.kt +++ b/app/src/test/java/de/jeanlucmakiola/calendula/data/calendar/EventWriteMapperTest.kt @@ -13,6 +13,7 @@ import kotlinx.datetime.LocalDateTime import kotlinx.datetime.LocalTime import kotlinx.datetime.UtcOffset import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertThrows import java.time.ZoneId import kotlin.time.Instant @@ -503,25 +504,25 @@ class EventWriteMapperTest { // --- seriesDuration (#502) --- @Test - fun `series duration keeps a stored duration`() { - assertThat(seriesDuration("PT1H", 1_000L, 0L, 5_000L, isAllDay = false)).isEqualTo("PT1H") + fun `series duration prefers the expanded occurrence over dtend`() { + // DTEND as the end of the whole series must not stretch every occurrence. + assertThat(seriesDuration(1_000_000L, 9_000_000_000L, 3_600_000L, isAllDay = false)).isEqualTo("P3600S") } @Test fun `series duration falls back to dtend when it lies after dtstart`() { - assertThat(seriesDuration(null, 1_000_000L, 8_200_000L, null, isAllDay = false)).isEqualTo("P7200S") + assertThat(seriesDuration(1_000_000L, 8_200_000L, null, isAllDay = false)).isEqualTo("P7200S") } @Test fun `series duration ignores a zero dtend and uses the occurrence span`() { - assertThat(seriesDuration(null, 1_790_852_400_000L, 0L, 7_200_000L, isAllDay = false)) - .isEqualTo("P7200S") + assertThat(seriesDuration(1_790_852_400_000L, 0L, 7_200_000L, isAllDay = false)).isEqualTo("P7200S") } @Test - fun `series duration never comes back empty`() { - assertThat(seriesDuration(" ", 1_000L, 0L, null, isAllDay = false)).isEqualTo("P0S") - assertThat(seriesDuration(null, 1_000L, null, null, isAllDay = true)).isEqualTo("P1D") + fun `series duration gives up rather than guessing`() { + assertThat(seriesDuration(1_000L, 0L, null, isAllDay = false)).isNull() + assertThat(seriesDuration(1_000L, null, 0L, isAllDay = true)).isNull() } @Test @@ -1234,6 +1235,14 @@ class EventWriteMapperTest { description = "", ) + @Test + fun `moved series with a zero dtend and no duration is refused, not copied broken`() { + val snapshot = masterSnapshot(dtEndMillis = 0L, rrule = "FREQ=WEEKLY;BYDAY=TH") + assertThrows { + buildMovedMasterValues(snapshot, targetCalendarId = 9L, uid = "u@calendula") + } + } + @Test fun `moved one-off carries target calendar, uid and DTEND but no recurrence`() { val values = buildMovedMasterValues(masterSnapshot(), targetCalendarId = 9L, uid = "u@calendula") From 2f51260f9db3faf448b75a1aff6ddb70aae3c1fd Mon Sep 17 00:00:00 2001 From: Jean-Luc Makiola Date: Wed, 25 Nov 2026 12:30:34 +0100 Subject: [PATCH 3/4] fix(#502): review round 2 - length only checked where the series is re-expanded, zero spans kept --- .../data/calendar/CalendarDataSource.kt | 26 ++++++++++++------- .../data/calendar/EventWriteMapper.kt | 9 ++++--- .../data/calendar/EventWriteMapperTest.kt | 8 +++++- 3 files changed, 28 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/data/calendar/CalendarDataSource.kt b/app/src/main/java/de/jeanlucmakiola/calendula/data/calendar/CalendarDataSource.kt index a3cf7bda..e879e690 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/data/calendar/CalendarDataSource.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/data/calendar/CalendarDataSource.kt @@ -1508,13 +1508,10 @@ class AndroidCalendarDataSource @Inject constructor( rdate = c.getString(7), ) if (!row.recurs || !row.duration.isNullOrBlank()) return@use row - val duration = seriesDuration( - dtStartMillis = row.dtStartMillis, - dtEndMillis = CursorColumnReader(c).getLongOrNull(SERIES_IDX_DTEND), + row.copy( + dtEndMillis = if (c.isNull(8)) null else c.getLong(8), occurrenceSpanMillis = occurrenceMillis?.let { occurrenceSpan(eventId, it) }, - isAllDay = row.allDay != 0, - ) ?: throw WriteFailedException("series event id=$eventId has no usable length") - row.copy(duration = duration) + ) } else { null } @@ -1541,6 +1538,9 @@ class AndroidCalendarDataSource @Inject constructor( val syncId: String? = null, val exdate: String? = null, val rdate: String? = null, + /** Only read for a series with no DURATION, to derive one ([writeDuration]). */ + val dtEndMillis: Long? = null, + val occurrenceSpanMillis: Long? = null, ) { /** * Whether this row expands into more than itself. RDATE counts as much as @@ -1550,6 +1550,14 @@ class AndroidCalendarDataSource @Inject constructor( */ val recurs: Boolean get() = !rrule.isNullOrBlank() || !rdate.isNullOrBlank() + /** + * DURATION for a write that re-expands the series. Refuses rather than + * writing none, which would leave the provider nothing to expand (#502). + */ + fun writeDuration(): String = duration?.takeIf { it.isNotBlank() } + ?: seriesDuration(dtStartMillis, dtEndMillis, occurrenceSpanMillis, allDay != 0) + ?: throw WriteFailedException("series has no usable length") + /** * The time/recurrence set a series write carries together: the provider * re-expands instances only from what the update itself holds. @@ -1558,7 +1566,7 @@ class AndroidCalendarDataSource @Inject constructor( put(CalendarContract.Events.DTSTART, dtStartMillis) putNull(CalendarContract.Events.DTEND) put(CalendarContract.Events.RRULE, rrule) - put(CalendarContract.Events.DURATION, duration) + put(CalendarContract.Events.DURATION, writeDuration()) put(CalendarContract.Events.EVENT_TIMEZONE, timezone) put(CalendarContract.Events.ALL_DAY, allDay) } @@ -1570,7 +1578,7 @@ class AndroidCalendarDataSource @Inject constructor( dtStartMillis = dtStartMillis, rrule = rrule, rdate = rdate, - duration = duration, + duration = writeDuration(), timezone = timezone, allDay = allDay, ) @@ -1851,8 +1859,6 @@ class AndroidCalendarDataSource @Inject constructor( SpecialDateType.entries.firstOrNull { markerFor(it) == marker } private companion object { - /** DTEND's index in [querySeriesRow]'s projection. */ - const val SERIES_IDX_DTEND = 8 const val TAG = "CalendarDataSource" /** Normalised key an attendee is matched/deduped on (case-insensitive email). */ diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/data/calendar/EventWriteMapper.kt b/app/src/main/java/de/jeanlucmakiola/calendula/data/calendar/EventWriteMapper.kt index beba38c7..c4bf21f7 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/data/calendar/EventWriteMapper.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/data/calendar/EventWriteMapper.kt @@ -369,7 +369,7 @@ internal fun buildMovedMasterValues( /** The recurring copy's DURATION: its own if present, else derived from DTEND. */ private fun MasterEventSnapshot.movedDuration(): String = duration?.takeIf { it.isNotBlank() } - ?: seriesDuration(dtStartMillis, dtEndMillis, occurrenceSpanMillis = null, isAllDay = isAllDay) + ?: seriesDuration(dtStartMillis, dtEndMillis ?: dtStartMillis, occurrenceSpanMillis = null, isAllDay = isAllDay) ?: throw IllegalStateException("series has no usable length") /** @@ -460,7 +460,8 @@ internal fun buildOccurrenceExdateValues( * DTEND (sometimes 0) instead; rewriting them without a DURATION leaves the * provider nothing to expand, and every occurrence vanishes (#502). The span of * an occurrence the provider actually expanded wins over DTEND; null when - * neither gives a positive length, so the caller fails instead of guessing. + * neither gives a length (a DTEND before DTSTART), so the caller fails + * instead of guessing. */ internal fun seriesDuration( dtStartMillis: Long, @@ -468,8 +469,8 @@ internal fun seriesDuration( occurrenceSpanMillis: Long?, isAllDay: Boolean, ): String? { - val span = occurrenceSpanMillis?.takeIf { it > 0 } - ?: dtEndMillis?.minus(dtStartMillis)?.takeIf { it > 0 } + val span = occurrenceSpanMillis?.takeIf { it >= 0 } + ?: dtEndMillis?.minus(dtStartMillis)?.takeIf { it >= 0 } ?: return null return rfc2445Duration(if (isAllDay) span.coerceAtLeast(MILLIS_PER_DAY) else span, isAllDay) } diff --git a/app/src/test/java/de/jeanlucmakiola/calendula/data/calendar/EventWriteMapperTest.kt b/app/src/test/java/de/jeanlucmakiola/calendula/data/calendar/EventWriteMapperTest.kt index 8f5e4e13..845b3588 100644 --- a/app/src/test/java/de/jeanlucmakiola/calendula/data/calendar/EventWriteMapperTest.kt +++ b/app/src/test/java/de/jeanlucmakiola/calendula/data/calendar/EventWriteMapperTest.kt @@ -522,7 +522,13 @@ class EventWriteMapperTest { @Test fun `series duration gives up rather than guessing`() { assertThat(seriesDuration(1_000L, 0L, null, isAllDay = false)).isNull() - assertThat(seriesDuration(1_000L, null, 0L, isAllDay = true)).isNull() + assertThat(seriesDuration(1_000L, null, null, isAllDay = true)).isNull() + } + + @Test + fun `series duration keeps a zero length the provider already shows`() { + assertThat(seriesDuration(1_000L, 0L, 0L, isAllDay = false)).isEqualTo("P0S") + assertThat(seriesDuration(1_000L, 1_000L, null, isAllDay = true)).isEqualTo("P1D") } @Test From fc1a76f27405b860531dd2fbf12054adcea2946c Mon Sep 17 00:00:00 2001 From: Jean-Luc Makiola Date: Wed, 25 Nov 2026 15:17:15 +0100 Subject: [PATCH 4/4] fix(#502): review round 3 - length checked before inserting, sync cancel and all-day spans covered --- .../calendula/data/calendar/CalendarDataSource.kt | 6 +++++- .../calendula/data/calendar/EventWriteMapper.kt | 7 +++++-- .../calendula/data/calendar/EventWriteMapperTest.kt | 8 +++++++- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/data/calendar/CalendarDataSource.kt b/app/src/main/java/de/jeanlucmakiola/calendula/data/calendar/CalendarDataSource.kt index e879e690..3790ba81 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/data/calendar/CalendarDataSource.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/data/calendar/CalendarDataSource.kt @@ -1349,6 +1349,8 @@ class AndroidCalendarDataSource @Inject constructor( // Reminders, guests and colour come along like any new event, and so does // a fresh UID — the detached row is a separate event now, and sharing the // parent's would collide with it in .ics restore dedup. + // Fail before inserting: the EXDATE write below needs a usable length. + row.writeDuration() val detachedId = insertEvent(form.toDetachedOccurrence(), allDayReminderTimeMinutes) // Rows touched, not occurrences excluded — 1 whenever the series row still // exists. It catches the row disappearing under us, not an EXDATE the @@ -1392,6 +1394,8 @@ class AndroidCalendarDataSource @Inject constructor( return eventId } // Insert the new series first: if it fails, the original is untouched. + // Fail before inserting: truncating the original needs a usable length. + row.writeDuration() val newEventId = insertEvent(updated, allDayReminderTimeMinutes) carrySplitExdate(newEventId, row, beginMillis, original, updated) truncateSeries(eventId, row, beginMillis) @@ -1795,7 +1799,7 @@ class AndroidCalendarDataSource @Inject constructor( val values = buildOccurrenceCancelValues( originalInstanceMillis = beginMillis, dtStartMillis = beginMillis, - duration = row.duration, + duration = row.writeDuration(), timezone = row.timezone, allDay = row.allDay, ) diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/data/calendar/EventWriteMapper.kt b/app/src/main/java/de/jeanlucmakiola/calendula/data/calendar/EventWriteMapper.kt index c4bf21f7..980a38ad 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/data/calendar/EventWriteMapper.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/data/calendar/EventWriteMapper.kt @@ -370,7 +370,7 @@ internal fun buildMovedMasterValues( /** The recurring copy's DURATION: its own if present, else derived from DTEND. */ private fun MasterEventSnapshot.movedDuration(): String = duration?.takeIf { it.isNotBlank() } ?: seriesDuration(dtStartMillis, dtEndMillis ?: dtStartMillis, occurrenceSpanMillis = null, isAllDay = isAllDay) - ?: throw IllegalStateException("series has no usable length") + ?: throw WriteFailedException("move series with no usable length") /** * Raw provider snapshot of one exception row of a recurring series (a modified @@ -472,7 +472,10 @@ internal fun seriesDuration( val span = occurrenceSpanMillis?.takeIf { it >= 0 } ?: dtEndMillis?.minus(dtStartMillis)?.takeIf { it >= 0 } ?: return null - return rfc2445Duration(if (isAllDay) span.coerceAtLeast(MILLIS_PER_DAY) else span, isAllDay) + if (!isAllDay) return rfc2445Duration(span, isAllDay = false) + // Whole days, rounded: a span read across a DST change or zone offset is a few hours off. + val days = ((span + MILLIS_PER_DAY / 2) / MILLIS_PER_DAY).coerceAtLeast(1) + return rfc2445Duration(days * MILLIS_PER_DAY, isAllDay = true) } /** [buildOccurrenceExdateValues] excluding every one of [occurrences] in one write. */ diff --git a/app/src/test/java/de/jeanlucmakiola/calendula/data/calendar/EventWriteMapperTest.kt b/app/src/test/java/de/jeanlucmakiola/calendula/data/calendar/EventWriteMapperTest.kt index 845b3588..bbfe7355 100644 --- a/app/src/test/java/de/jeanlucmakiola/calendula/data/calendar/EventWriteMapperTest.kt +++ b/app/src/test/java/de/jeanlucmakiola/calendula/data/calendar/EventWriteMapperTest.kt @@ -525,6 +525,12 @@ class EventWriteMapperTest { assertThat(seriesDuration(1_000L, null, null, isAllDay = true)).isNull() } + @Test + fun `all-day series duration rounds to whole days`() { + // 47 h: a two-day occurrence read across a DST change. + assertThat(seriesDuration(0L, null, 47L * 3_600_000L, isAllDay = true)).isEqualTo("P2D") + } + @Test fun `series duration keeps a zero length the provider already shows`() { assertThat(seriesDuration(1_000L, 0L, 0L, isAllDay = false)).isEqualTo("P0S") @@ -1244,7 +1250,7 @@ class EventWriteMapperTest { @Test fun `moved series with a zero dtend and no duration is refused, not copied broken`() { val snapshot = masterSnapshot(dtEndMillis = 0L, rrule = "FREQ=WEEKLY;BYDAY=TH") - assertThrows { + assertThrows { buildMovedMasterValues(snapshot, targetCalendarId = 9L, uid = "u@calendula") } }