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