fix(#502): series rows without a usable DURATION no longer lose their occurrences on a single edit or delete
This commit is contained in:
1 parent
a24420edfb
commit
0652859ecc
3 files changed
+100
-7
No files matched your search
@@ -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) {
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in new issue
Block a user