Compare commits

..
5 changed files with 35 additions and 122 deletions

No files matched your search

@@ -1270,7 +1270,7 @@ class AndroidCalendarDataSource @Inject constructor(
form: EventForm,
allDayReminderTimeMinutes: Int,
): Long {
val row = querySeriesRow(eventId, beginMillis)
val row = querySeriesRow(eventId)
// 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, beginMillis)
val row = querySeriesRow(eventId)
// 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, beginMillis)
val row = querySeriesRow(eventId)
// 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,12 +1476,8 @@ 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. 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(
/** The series anchor: every time-related column of the Events row. */
private fun querySeriesRow(eventId: Long): SeriesRow = resolver.query(
eventUri(eventId),
arrayOf(
CalendarContract.Events.DTSTART,
@@ -1492,12 +1488,11 @@ 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()) {
val row = SeriesRow(
SeriesRow(
dtStartMillis = c.getLong(0),
rrule = c.getString(1),
timezone = c.getString(2),
@@ -1507,27 +1502,11 @@ class AndroidCalendarDataSource @Inject constructor(
exdate = c.getString(6),
rdate = c.getString(7),
)
if (!row.recurs || !row.duration.isNullOrBlank()) return@use row
row.copy(
dtEndMillis = if (c.isNull(8)) null else c.getLong(8),
occurrenceSpanMillis = occurrenceMillis?.let { occurrenceSpan(eventId, it) },
)
} 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? {
return resolver.query(
instancesUri(beginMillis, beginMillis + 1),
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?,
@@ -1538,9 +1517,6 @@ 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,23 +1526,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.
*/
fun timeColumns(): ContentValues = ContentValues().apply {
put(CalendarContract.Events.DTSTART, dtStartMillis)
putNull(CalendarContract.Events.DTEND)
put(CalendarContract.Events.RRULE, rrule)
put(CalendarContract.Events.DURATION, writeDuration())
put(CalendarContract.Events.DURATION, duration)
put(CalendarContract.Events.EVENT_TIMEZONE, timezone)
put(CalendarContract.Events.ALL_DAY, allDay)
}
@@ -1578,7 +1545,7 @@ class AndroidCalendarDataSource @Inject constructor(
dtStartMillis = dtStartMillis,
rrule = rrule,
rdate = rdate,
duration = writeDuration(),
duration = duration,
timezone = timezone,
allDay = allDay,
)
@@ -1765,7 +1732,7 @@ class AndroidCalendarDataSource @Inject constructor(
}
override fun deleteOccurrence(eventId: Long, beginMillis: Long) {
val row = querySeriesRow(eventId, beginMillis)
val row = querySeriesRow(eventId)
// 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) {
@@ -369,8 +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 ?: dtStartMillis, occurrenceSpanMillis = null, isAllDay = isAllDay)
?: throw IllegalStateException("series has no usable length")
?: rfc2445Duration((dtEndMillis ?: dtStartMillis) - dtStartMillis, isAllDay)
/**
* Raw provider snapshot of one exception row of a recurring series (a modified
@@ -455,26 +454,6 @@ internal fun buildOccurrenceExdateValues(
existingExdate, listOf(occurrenceMillis), dtStartMillis, rrule, rdate, duration, timezone, allDay,
)
/**
* 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 length (a DTEND before DTSTART), so the caller fails
* instead of guessing.
*/
internal fun seriesDuration(
dtStartMillis: Long,
dtEndMillis: Long?,
occurrenceSpanMillis: Long?,
isAllDay: Boolean,
): 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)
}
/** [buildOccurrenceExdateValues] excluding every one of [occurrences] in one write. */
internal fun buildOccurrencesExdateValues(
existingExdate: String?,
@@ -490,7 +469,6 @@ 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,
@@ -2,6 +2,7 @@ package de.jeanlucmakiola.calendula.ui.common
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ColumnScope
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
@@ -131,7 +132,7 @@ fun FixedTimeReminderEditor(
* Picker for a fixed-time default (#459), full-screen: off, or a reminder at a
* set time edited in place — and, on a per-calendar picker ([inheritedLabel]
* non-null), deferring to the global default, [inherited], which also seeds the
* editor. Applies live; the user leaves via back.
* editor. Applies live; the user leaves via back. [footer] follows the options.
*/
@Composable
fun FixedTimeReminderPicker(
@@ -141,6 +142,7 @@ fun FixedTimeReminderPicker(
onDismiss: () -> Unit,
inheritedLabel: String? = null,
inherited: FixedTimeReminder? = null,
footer: @Composable ColumnScope.() -> Unit = {},
) {
var current by rememberSaveable(stateSaver = FixedTimeOverrideSaver) { mutableStateOf(selected) }
var editing by rememberSaveable { mutableStateOf(false) }
@@ -192,6 +194,7 @@ fun FixedTimeReminderPicker(
},
)
}
footer()
}
}
@@ -19,6 +19,8 @@ import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.relocation.BringIntoViewRequester
import androidx.compose.foundation.relocation.bringIntoViewRequester
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.KeyboardArrowRight
import androidx.compose.material.icons.automirrored.filled.OpenInNew
@@ -108,6 +110,7 @@ internal fun NotificationsScreen(
var showFixedDefault by remember { mutableStateOf(false) }
var showDigestDay by remember { mutableStateOf(false) }
var showDigestTime by remember { mutableStateOf(false) }
val digestRequester = remember { BringIntoViewRequester() }
val scope = rememberCoroutineScope()
val channelStatus by viewModel.channelStatus.collectAsStateWithLifecycle()
val digestBlocked by viewModel.digestBlocked.collectAsStateWithLifecycle()
@@ -193,6 +196,7 @@ internal fun NotificationsScreen(
Spacer(Modifier.height(24.dp))
DailyDigestRows(
modifier = Modifier.bringIntoViewRequester(digestRequester),
digest = state.dailyDigest,
blocked = channelStatus.appBlocked || digestBlocked,
onEnable = toggleDigest,
@@ -257,6 +261,21 @@ internal fun NotificationsScreen(
selected = state.defaultFixedTimeReminder?.let { FixedTimeOverride.At(it) } ?: FixedTimeOverride.Off,
onSelect = { viewModel.setDefaultFixedTimeReminder((it as? FixedTimeOverride.At)?.reminder) },
onDismiss = { showFixedDefault = false },
footer = {
// A pointer for those after one list of the day's events (#459).
if (!state.dailyDigest.enabled) {
Spacer(Modifier.height(24.dp))
GroupedRow(
title = stringResource(R.string.settings_digest),
summary = stringResource(R.string.settings_digest_hint),
position = Position.Alone,
onClick = {
showFixedDefault = false
scope.launch { digestRequester.bringIntoView() }
},
)
}
},
)
}
if (showDigestDay) {
@@ -338,7 +357,8 @@ private fun DailyDigestRows(
onPickDay: () -> Unit,
onPickTime: () -> Unit,
onOpenSound: () -> Unit,
) {
modifier: Modifier = Modifier,
) = Column(modifier) {
GroupedSwitchRow(
title = stringResource(R.string.settings_digest),
checked = digest.enabled,
@@ -13,7 +13,6 @@ 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
@@ -501,52 +500,6 @@ class EventWriteMapperTest {
.isEqualTo(CalendarContract.Events.STATUS_CANCELED)
}
// --- seriesDuration (#502) ---
@Test
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(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(1_790_852_400_000L, 0L, 7_200_000L, isAllDay = false)).isEqualTo("P7200S")
}
@Test
fun `series duration gives up rather than guessing`() {
assertThat(seriesDuration(1_000L, 0L, null, isAllDay = false)).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
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
@@ -1241,14 +1194,6 @@ 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<IllegalStateException> {
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")