fix(#429): keep the default across renames and restores, drop a duplicate's colour off its calendar
This commit is contained in:
1 parent
6a9274402a
commit
aa1ce0405a
6 files changed
+89
-37
No files matched your search
@@ -2,9 +2,8 @@ package de.jeanlucmakiola.calendula.domain
|
||||
|
||||
/**
|
||||
* The calendar chosen as default for new events (#429). Provider ids are
|
||||
* device-local and can be handed out again, so the account and name ride along
|
||||
* and the choice only holds while the calendar under [id] still matches them,
|
||||
* e.g. not after a backup restore onto another device.
|
||||
* device-local and get reassigned when an account is re-added or a backup is
|
||||
* restored onto another device, so the account and name ride along: see [find].
|
||||
*/
|
||||
data class DefaultCalendarRef(
|
||||
val id: Long,
|
||||
@@ -12,11 +11,16 @@ data class DefaultCalendarRef(
|
||||
val accountName: String,
|
||||
val displayName: String,
|
||||
) {
|
||||
fun matches(calendar: CalendarSource): Boolean =
|
||||
calendar.id == id &&
|
||||
calendar.accountType == accountType &&
|
||||
calendar.accountName == accountName &&
|
||||
calendar.displayName == displayName
|
||||
/**
|
||||
* The calendar this refers to: the exact one, else the account's calendar of
|
||||
* that name (the id changed), else the id within the account (it was renamed).
|
||||
*/
|
||||
fun find(calendars: List<CalendarSource>): CalendarSource? {
|
||||
val inAccount = calendars.filter { it.accountType == accountType && it.accountName == accountName }
|
||||
return inAccount.firstOrNull { it.id == id && it.displayName == displayName }
|
||||
?: inAccount.singleOrNull { it.displayName == displayName }
|
||||
?: inAccount.firstOrNull { it.id == id }
|
||||
}
|
||||
|
||||
fun encode(): String = listOf(id.toString(), accountType, accountName, displayName).joinToString(SEPARATOR)
|
||||
|
||||
@@ -55,23 +59,18 @@ fun resolveEventCalendar(
|
||||
picked: Long?,
|
||||
hints: NewEventCalendarHints,
|
||||
calendars: List<CalendarSource>,
|
||||
): Long? = picked
|
||||
?: hints.requested?.takeIf { id -> calendars.any { it.id == id && it.isEventTarget } }
|
||||
?: effectiveDefaultCalendar(hints.default, calendars)?.id
|
||||
?: hints.lastUsed?.takeIf { id -> calendars.any { it.id == id && it.isEventTarget } }
|
||||
?: calendars.firstOrNull { it.isEventTarget }?.id
|
||||
): Long? {
|
||||
fun Long.ifTarget() = takeIf { id -> calendars.any { it.id == id && it.isEventTarget } }
|
||||
return picked
|
||||
?: hints.requested?.ifTarget()
|
||||
?: effectiveDefaultCalendar(hints.default, calendars)?.id
|
||||
?: hints.lastUsed?.ifTarget()
|
||||
?: calendars.firstOrNull { it.isEventTarget }?.id
|
||||
}
|
||||
|
||||
/**
|
||||
* The default calendar setting as it applies now: null ("Automatic") when unset,
|
||||
* or when the stored calendar is gone, read-only or switched off.
|
||||
*/
|
||||
fun effectiveDefaultCalendar(default: DefaultCalendarRef?, calendars: List<CalendarSource>): CalendarSource? =
|
||||
default?.let { ref -> calendars.firstOrNull { ref.matches(it) && it.isEventTarget } }
|
||||
|
||||
/**
|
||||
* The stored default calendar while it still exists but can't take new events
|
||||
* (switched off, read-only, managed), so the setting can say why it reads
|
||||
* Automatic. Null when it applies, is unset or is gone.
|
||||
*/
|
||||
fun unavailableDefaultCalendar(default: DefaultCalendarRef?, calendars: List<CalendarSource>): CalendarSource? =
|
||||
default?.let { ref -> calendars.firstOrNull { ref.matches(it) && !it.isEventTarget } }
|
||||
default?.find(calendars)?.takeIf { it.isEventTarget }
|
||||
@@ -441,6 +441,7 @@ 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 ->
|
||||
@@ -455,6 +456,20 @@ 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
|
||||
|
||||
@@ -38,8 +38,6 @@ 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.unavailableDefaultCalendar
|
||||
import de.jeanlucmakiola.calendula.domain.isEventTarget
|
||||
import de.jeanlucmakiola.calendula.domain.contacts.SpecialDateType
|
||||
import de.jeanlucmakiola.calendula.ui.agenda.AgendaRange
|
||||
@@ -92,7 +90,10 @@ class SettingsViewModel @Inject constructor(
|
||||
initialValue = SettingsUiState(dynamicColorAvailable = dynamicColorAvailable),
|
||||
)
|
||||
|
||||
private fun Preferences.toUiState(calendars: List<CalendarSource>) = SettingsUiState(
|
||||
private fun Preferences.toUiState(
|
||||
calendars: List<CalendarSource>,
|
||||
defaultCalendar: CalendarSource? = this[SettingsKeys.defaultNewEventCalendar]?.find(calendars),
|
||||
) = SettingsUiState(
|
||||
themeMode = this[SettingsKeys.themeMode],
|
||||
dynamicColor = this[SettingsKeys.dynamicColor] && dynamicColorAvailable,
|
||||
dynamicColorAvailable = dynamicColorAvailable,
|
||||
@@ -130,8 +131,8 @@ class SettingsViewModel @Inject constructor(
|
||||
perCalendarAllDayReminderOverride = this[SettingsKeys.perCalendarAllDayReminderOverride],
|
||||
writableCalendars = calendars.filter { it.canModifyContents && it.isVisibleInSystem },
|
||||
eventTargetCalendars = calendars.filter { it.isEventTarget },
|
||||
defaultNewEventCalendar = effectiveDefaultCalendar(this[SettingsKeys.defaultNewEventCalendar], calendars),
|
||||
unavailableDefaultCalendar = unavailableDefaultCalendar(this[SettingsKeys.defaultNewEventCalendar], calendars),
|
||||
defaultNewEventCalendar = defaultCalendar?.takeIf { it.isEventTarget },
|
||||
unavailableDefaultCalendar = defaultCalendar?.takeUnless { it.isEventTarget },
|
||||
allowColorOnUnsupportedCalendars = this[SettingsKeys.allowColorOnUnsupportedCalendars],
|
||||
managedCalendarIds = specialDatesCalendars.values.toSet(),
|
||||
)
|
||||
|
||||
Reference in new issue
Block a user