fix(#429): duplicates keep only target calendars, re-picking the shown calendar is no change

This commit is contained in:
makiolaj committed 2026-11-13 19:41:11 +01:00
1 parent aa1ce0405a
commit 38e7910d3c
9 files changed
+65 -54

No files matched your search

@@ -320,17 +320,22 @@ fun EventForm.problems(deviceZone: TimeZone): Set<EventFormProblem> = buildSet {
/**
* A create form seeded from this occurrence, to save a copy as a new,
* independent event (#52). The recurrence is dropped — a duplicate is a single
* event. The source calendar is kept only when [canModify], otherwise the id is
* cleared so the create resolves to a writable calendar, which lets a read-only
* event be copied into an editable one. [instance]'s own times carry over.
* event. The source calendar is kept only when [keepCalendar] (it can take new
* events), otherwise the id is cleared so the create resolves like a new event,
* which lets a read-only event be copied into an editable one; a palette colour
* is account-scoped, so it goes with the calendar. [instance]'s own times carry over.
*/
fun EventDetail.toDuplicateForm(canModify: Boolean, zone: TimeZone): EventForm =
fun EventDetail.toDuplicateForm(keepCalendar: Boolean, zone: TimeZone): EventForm =
toEditForm(
beginMillis = instance.start.toEpochMilliseconds(),
endMillis = instance.end.toEpochMilliseconds(),
zone = zone,
).copy(
rrule = null,
calendarId = instance.calendarId.takeIf { canModify },
status = status.forNewEvent(),
)
).let { form ->
form.copy(
rrule = null,
calendarId = instance.calendarId.takeIf { keepCalendar },
colorKey = form.colorKey.takeIf { keepCalendar },
color = form.color.takeIf { keepCalendar || form.colorKey == null },
status = status.forNewEvent(),
)
}
@@ -12,6 +12,7 @@ import de.jeanlucmakiola.calendula.domain.CalendarSource
import de.jeanlucmakiola.calendula.domain.EventDetail
import de.jeanlucmakiola.calendula.domain.EventForm
import de.jeanlucmakiola.calendula.domain.EventInstance
import de.jeanlucmakiola.calendula.domain.isEventTarget
import de.jeanlucmakiola.calendula.domain.RecurringWriteScope
import de.jeanlucmakiola.calendula.domain.atOccurrence
import de.jeanlucmakiola.calendula.domain.toDuplicateForm
@@ -64,8 +65,8 @@ class EventActionsViewModel @Inject constructor(
/** A create form copying [event], or null when it can't be read. */
suspend fun duplicateForm(event: EventInstance): EventForm? {
val detail = load(event) ?: return null
val canModify = calendars.value[event.calendarId]?.canModifyContents == true
return detail.toDuplicateForm(canModify, zoneSource.current())
val isTarget = calendars.value[event.calendarId]?.isEventTarget == true
return detail.toDuplicateForm(isTarget, zoneSource.current())
}
/** A staged `.ics` for [event], or null — reported — when it can't be built. */
@@ -16,6 +16,8 @@ sealed interface EventDetailUiState {
val calendarName: String?,
/** Whether the owning calendar allows modifying events (shows edit/delete). */
val canModify: Boolean = false,
/** Whether the owning calendar takes new events, so a duplicate can stay in it. */
val isEventTarget: Boolean = false,
/**
* The event belongs to a managed special-dates calendar: its editable
* fields (reminders, notes) can still be edited, but delete is hidden —
@@ -13,6 +13,7 @@ import de.jeanlucmakiola.calendula.domain.FailureReason
import de.jeanlucmakiola.calendula.domain.RecurringWriteScope
import de.jeanlucmakiola.calendula.domain.atOccurrence
import de.jeanlucmakiola.calendula.domain.descriptionRichText
import de.jeanlucmakiola.calendula.domain.isEventTarget
import de.jeanlucmakiola.calendula.domain.toDuplicateForm
import de.jeanlucmakiola.calendula.ui.common.DeleteOutcome
import de.jeanlucmakiola.calendula.ui.common.tryDeleteEvent
@@ -142,7 +143,7 @@ class EventDetailViewModel @Inject constructor(
*/
fun duplicateForm(): EventForm? {
val loaded = state.value as? EventDetailUiState.Success ?: return null
return loaded.detail.toDuplicateForm(loaded.canModify, zoneSource.current())
return loaded.detail.toDuplicateForm(loaded.isEventTarget, zoneSource.current())
}
private suspend fun loadDetail(target: Target): EventDetailUiState = try {
@@ -164,6 +165,7 @@ class EventDetailViewModel @Inject constructor(
detail = corrected,
calendarName = calendar?.displayName,
canModify = calendar?.canModifyContents == true,
isEventTarget = calendar?.isEventTarget == true,
isManaged = calendar?.isManaged == true,
// Found here, off the main thread and once per load: the platform
// matcher loads its metadata on first use.
@@ -441,7 +441,6 @@ class EventEditViewModel @Inject constructor(
_revealed.value = form.populatedFields()
_requestedCalendarId.value = form.calendarId
open(form.copy(calendarId = null))
dropColorOffRequestedCalendar(form)
if (source.ownsDuration) _durationTouched.value = true else applyDefaultDuration()
when (source) {
ImportSource.Insert, ImportSource.InsertOpenEnded ->
@@ -456,20 +455,6 @@ class EventEditViewModel @Inject constructor(
}
}
/**
* A duplicate carries its source's colour, which is scoped to that calendar's
* account. When the form lands elsewhere, the colour goes as on [setCalendar].
*/
private fun dropColorOffRequestedCalendar(form: EventForm) {
if (form.colorKey == null && form.color == null) return
val opened = session
viewModelScope.launch {
val resolved = resolvedCalendarId.first()
if (opened != session || _form.value?.calendarId != null) return@launch
if (resolved != form.calendarId) applyDefault { it.copy(colorKey = null, color = null) }
}
}
/**
* Prefill a new event's reminders from the settings default — the all-day
* default for all-day events, otherwise the resolved calendar's per-calendar
@@ -692,6 +677,9 @@ class EventEditViewModel @Inject constructor(
* The event falls back to the new calendar's colour until re-picked.
*/
fun setCalendar(id: Long) {
// Re-picking the calendar the form already shows changes nothing, so it
// keeps a duplicate's colour and raises no unsaved change.
if (id == state.value?.form?.calendarId) return
edit { it.copy(calendarId = id, colorKey = null, color = null) }
// A fresh event re-inherits the new calendar's default reminder and
// length unless the user has already hand-edited them (guarded inside).
@@ -38,6 +38,7 @@ import de.jeanlucmakiola.calendula.domain.CalendarSource
import de.jeanlucmakiola.calendula.domain.EventFormField
import de.jeanlucmakiola.calendula.domain.FontRole
import de.jeanlucmakiola.calendula.domain.DefaultCalendarRef
import de.jeanlucmakiola.calendula.domain.effectiveDefaultCalendar
import de.jeanlucmakiola.calendula.domain.isEventTarget
import de.jeanlucmakiola.calendula.domain.contacts.SpecialDateType
import de.jeanlucmakiola.calendula.ui.agenda.AgendaRange
@@ -92,7 +93,7 @@ class SettingsViewModel @Inject constructor(
private fun Preferences.toUiState(
calendars: List<CalendarSource>,
defaultCalendar: CalendarSource? = this[SettingsKeys.defaultNewEventCalendar]?.find(calendars),
storedDefault: DefaultCalendarRef? = this[SettingsKeys.defaultNewEventCalendar],
) = SettingsUiState(
themeMode = this[SettingsKeys.themeMode],
dynamicColor = this[SettingsKeys.dynamicColor] && dynamicColorAvailable,
@@ -131,8 +132,8 @@ class SettingsViewModel @Inject constructor(
perCalendarAllDayReminderOverride = this[SettingsKeys.perCalendarAllDayReminderOverride],
writableCalendars = calendars.filter { it.canModifyContents && it.isVisibleInSystem },
eventTargetCalendars = calendars.filter { it.isEventTarget },
defaultNewEventCalendar = defaultCalendar?.takeIf { it.isEventTarget },
unavailableDefaultCalendar = defaultCalendar?.takeUnless { it.isEventTarget },
defaultNewEventCalendar = effectiveDefaultCalendar(storedDefault, calendars),
unavailableDefaultCalendar = storedDefault?.find(calendars)?.takeUnless { it.isEventTarget },
allowColorOnUnsupportedCalendars = this[SettingsKeys.allowColorOnUnsupportedCalendars],
managedCalendarIds = specialDatesCalendars.values.toSet(),
)
+1 -1
View File
@@ -708,7 +708,7 @@
<string name="settings_default_calendar">Default calendar</string>
<string name="settings_default_calendar_automatic">Automatic</string>
<string name="settings_default_calendar_unavailable">Automatic while %1$s can’t take new events</string>
<string name="settings_default_calendar_automatic_hint">The calendar you last saved an event to</string>
<string name="settings_default_calendar_automatic_hint">The calendar you last added or moved an event to</string>
<string name="settings_autofocus_title">Focus title on new event</string>
<string name="settings_autofocus_title_hint">When you start a new event, place the cursor in the title field and open the keyboard right away.</string>
<string name="settings_event_duration">Default duration</string>
@@ -583,9 +583,21 @@ class EventFormTest {
@Test
fun `a copy of a cancelled event starts confirmed, a tentative one stays tentative`() {
val zone = TimeZone.of("Europe/Berlin")
assertThat(testDetail(status = EventStatus.Cancelled).toDuplicateForm(canModify = true, zone).status)
assertThat(testDetail(status = EventStatus.Cancelled).toDuplicateForm(keepCalendar = true, zone).status)
.isEqualTo(EventStatus.Confirmed)
assertThat(testDetail(status = EventStatus.Tentative).toDuplicateForm(canModify = true, zone).status)
assertThat(testDetail(status = EventStatus.Tentative).toDuplicateForm(keepCalendar = true, zone).status)
.isEqualTo(EventStatus.Tentative)
}
@Test
fun `a copy leaving its calendar drops a palette colour but keeps a raw one`() {
val zone = TimeZone.of("Europe/Berlin")
val keyed = testDetail(eventColor = 7, eventColorKey = "3")
assertThat(keyed.toDuplicateForm(keepCalendar = true, zone).colorKey).isEqualTo("3")
val moved = keyed.toDuplicateForm(keepCalendar = false, zone)
assertThat(moved.calendarId).isNull()
assertThat(moved.colorKey).isNull()
assertThat(moved.color).isNull()
assertThat(testDetail(eventColor = 7).toDuplicateForm(keepCalendar = false, zone).color).isEqualTo(7)
}
}
@@ -802,27 +802,6 @@ class EventEditViewModelTest {
job.cancel()
}
@Test
fun `a duplicate that falls back to another calendar drops its colour`(
@TempDir tempDir: Path,
) = runTest(dispatcher) {
val fake = FakeCalendarDataSource().apply { calendarsResult = listOf(cal(1L), cal(2L, visible = false)) }
val vm = viewModel(tempDir, fake)
val job = activate(vm)
vm.openImported(
importedForm(end = LocalDateTime(2030, 1, 15, 10, 0)).copy(calendarId = 2L, colorKey = "3", color = 7),
ImportSource.Insert,
)
advanceUntilIdle()
assertThat(vm.state.value?.form?.calendarId).isEqualTo(1L)
assertThat(vm.state.value?.form?.colorKey).isNull()
assertThat(vm.state.value?.form?.color).isNull()
assertThat(vm.hasUnsavedChanges.value).isFalse()
job.cancel()
}
@Test
fun `a duplicate in its own calendar keeps its colour`(
@TempDir tempDir: Path,
@@ -841,4 +820,25 @@ class EventEditViewModelTest {
assertThat(vm.state.value?.form?.colorKey).isEqualTo("3")
job.cancel()
}
@Test
fun `re-picking the shown calendar is no change`(
@TempDir tempDir: Path,
) = runTest(dispatcher) {
val fake = FakeCalendarDataSource().apply { calendarsResult = listOf(cal(1L), cal(2L)) }
val vm = viewModel(tempDir, fake)
val job = activate(vm)
vm.openImported(
importedForm(end = LocalDateTime(2030, 1, 15, 10, 0)).copy(calendarId = 2L, colorKey = "3", color = 7),
ImportSource.Insert,
)
advanceUntilIdle()
vm.setCalendar(2L)
advanceUntilIdle()
assertThat(vm.state.value?.form?.colorKey).isEqualTo("3")
assertThat(vm.hasUnsavedChanges.value).isFalse()
job.cancel()
}
}