fix(#270): review round 2 - safe pref reads, diff-only editor saves, optimistic picker
This commit is contained in:
1 parent
91fdaba6a2
commit
db8d649467
9 files changed
+54
-30
No files matched your search
@@ -309,3 +309,7 @@ enum class FailureReason {
|
||||
EventNotFound,
|
||||
Unknown,
|
||||
}
|
||||
|
||||
/** [this] without the events of [hidden] calendars. */
|
||||
fun List<EventInstance>.withoutCalendars(hidden: Set<Long>): List<EventInstance> =
|
||||
if (hidden.isEmpty()) this else filterNot { it.calendarId in hidden }
|
||||
@@ -372,7 +372,7 @@ internal fun CalendarEditor(
|
||||
initialName: String,
|
||||
initialColor: Int,
|
||||
initialDescription: String,
|
||||
onSave: (name: String, color: Int, description: String?, hiddenViews: Set<CalendarView>?) -> Unit,
|
||||
onSave: (name: String, color: Int, description: String?, hiddenChanges: Map<CalendarView, Boolean>) -> Unit,
|
||||
onDelete: () -> Unit,
|
||||
onClose: () -> Unit,
|
||||
deleteLocked: Boolean = false,
|
||||
@@ -380,9 +380,8 @@ internal fun CalendarEditor(
|
||||
initialHiddenViews: Set<CalendarView> = emptySet(),
|
||||
) {
|
||||
var name by rememberSaveable(sessionKey) { mutableStateOf(initialName) }
|
||||
val seededHiddenViews = rememberSaveable(sessionKey) { initialHiddenViews }
|
||||
var hiddenViews by rememberSaveable(sessionKey) { mutableStateOf(initialHiddenViews) }
|
||||
// Only a set the user touched is written back, so a save never resets one read before it loaded.
|
||||
var hiddenViewsEdited by rememberSaveable(sessionKey) { mutableStateOf(false) }
|
||||
var pickHiddenViews by rememberSaveable(sessionKey) { mutableStateOf(false) }
|
||||
var color by rememberSaveable(sessionKey) { mutableStateOf(initialColor) }
|
||||
var description by rememberSaveable(sessionKey) { mutableStateOf(initialDescription) }
|
||||
@@ -392,7 +391,10 @@ internal fun CalendarEditor(
|
||||
val valid = name.isNotBlank()
|
||||
val commit = {
|
||||
if (valid) {
|
||||
onSave(name.trim(), color, description.trim().ifEmpty { null }, hiddenViews.takeIf { hiddenViewsEdited })
|
||||
// Only the views toggled here are written, so a save never resets ones it didn't load.
|
||||
val changes = (hiddenViews + seededHiddenViews - (hiddenViews intersect seededHiddenViews))
|
||||
.associateWith { it in hiddenViews }
|
||||
onSave(name.trim(), color, description.trim().ifEmpty { null }, changes)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -469,7 +471,6 @@ internal fun CalendarEditor(
|
||||
hidden = hiddenViews,
|
||||
onToggle = { view, hide ->
|
||||
hiddenViews = if (hide) hiddenViews + view else hiddenViews - view
|
||||
hiddenViewsEdited = true
|
||||
},
|
||||
onDismiss = { pickHiddenViews = false },
|
||||
)
|
||||
@@ -757,8 +758,9 @@ private fun CalendarManagerRow(
|
||||
}
|
||||
|
||||
/**
|
||||
* Which views one calendar stays out of (#270). Applies each toggle as it is
|
||||
* made; display only, so reminders and search keep the calendar.
|
||||
* Which views one calendar stays out of (#270); display only, so reminders and
|
||||
* search keep the calendar. [onToggle] decides when a toggle lands: at once for
|
||||
* a synced calendar, on Save inside the local editor.
|
||||
*/
|
||||
@Composable
|
||||
private fun HiddenViewsPicker(
|
||||
@@ -767,12 +769,18 @@ private fun HiddenViewsPicker(
|
||||
onToggle: (CalendarView, Boolean) -> Unit,
|
||||
onDismiss: () -> Unit,
|
||||
) {
|
||||
// Ahead of the store, so a second tap before the first write lands still flips.
|
||||
var shown by remember { mutableStateOf(hidden) }
|
||||
LaunchedEffect(hidden) { shown = hidden }
|
||||
MultiOptionPicker(
|
||||
title = stringResource(R.string.calendars_hide_in_views),
|
||||
options = IMPLEMENTED_VIEWS,
|
||||
selected = hidden,
|
||||
selected = shown,
|
||||
label = { it.label() },
|
||||
onToggle = onToggle,
|
||||
onToggle = { view, hide ->
|
||||
shown = if (hide) shown + view else shown - view
|
||||
onToggle(view, hide)
|
||||
},
|
||||
onDismiss = onDismiss,
|
||||
leading = { Icon(it.icon, contentDescription = null) },
|
||||
header = {
|
||||
|
||||
@@ -3,6 +3,7 @@ package de.jeanlucmakiola.calendula.ui.calendars
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
import android.util.Log
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
@@ -29,6 +30,7 @@ import kotlinx.coroutines.flow.flowOn
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import java.io.IOException
|
||||
import kotlin.coroutines.cancellation.CancellationException
|
||||
import kotlin.time.Clock
|
||||
import javax.inject.Inject
|
||||
@@ -157,10 +159,10 @@ class CalendarsViewModel @Inject constructor(
|
||||
displayName: String,
|
||||
color: Int,
|
||||
description: String?,
|
||||
hiddenViews: Set<CalendarView>?,
|
||||
hiddenChanges: Map<CalendarView, Boolean> = emptyMap(),
|
||||
) = write {
|
||||
val id = repository.createLocalCalendar(displayName, color, description)
|
||||
if (!hiddenViews.isNullOrEmpty()) settingsPrefs.setCalendarHiddenViews(id, hiddenViews)
|
||||
applyHiddenChanges(id, hiddenChanges)
|
||||
}
|
||||
|
||||
fun updateCalendar(
|
||||
@@ -168,15 +170,24 @@ class CalendarsViewModel @Inject constructor(
|
||||
displayName: String,
|
||||
color: Int,
|
||||
description: String?,
|
||||
hiddenViews: Set<CalendarView>?,
|
||||
hiddenChanges: Map<CalendarView, Boolean>,
|
||||
) = write {
|
||||
repository.updateCalendar(id, displayName, color, description)
|
||||
if (hiddenViews != null) settingsPrefs.setCalendarHiddenViews(id, hiddenViews)
|
||||
applyHiddenChanges(id, hiddenChanges)
|
||||
}
|
||||
|
||||
private suspend fun applyHiddenChanges(id: Long, changes: Map<CalendarView, Boolean>) {
|
||||
changes.forEach { (view, hidden) -> settingsPrefs.setCalendarHiddenIn(id, view, hidden) }
|
||||
}
|
||||
|
||||
fun deleteCalendar(id: Long) = write {
|
||||
repository.deleteCalendar(id)
|
||||
settingsPrefs.setCalendarHiddenViews(id, emptySet())
|
||||
// Tidy-up only: the delete already landed, so a failure here isn't its failure.
|
||||
try {
|
||||
settingsPrefs.setCalendarHiddenViews(id, emptySet())
|
||||
} catch (e: IOException) {
|
||||
Log.w("CalendarsViewModel", "Could not clear hidden views of $id", e)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -2,9 +2,11 @@ package de.jeanlucmakiola.calendula.ui.common
|
||||
|
||||
import de.jeanlucmakiola.calendula.data.calendar.CalendarRepository
|
||||
import de.jeanlucmakiola.calendula.domain.EventInstance
|
||||
import de.jeanlucmakiola.calendula.domain.withoutCalendars
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.SharingStarted
|
||||
import kotlinx.coroutines.flow.catch
|
||||
import kotlinx.coroutines.flow.combine
|
||||
import kotlinx.coroutines.flow.distinctUntilChanged
|
||||
import kotlinx.coroutines.flow.map
|
||||
@@ -16,20 +18,19 @@ fun Map<Long, Set<CalendarView>>.calendarsHiddenIn(view: CalendarView): Set<Long
|
||||
filterValues { view in it }.keys
|
||||
|
||||
/**
|
||||
* The calendars hidden in [view], shared across one view model's pages so the
|
||||
* setting is read once rather than per page.
|
||||
* The calendars hidden in [view], shared by everything one view model shows so
|
||||
* the setting is read once rather than per page. A failed read hides nothing
|
||||
* rather than failing the view; the replay expires with the last subscriber, so
|
||||
* a view coming back never draws a stale set.
|
||||
*/
|
||||
fun Flow<Map<Long, Set<CalendarView>>>.calendarsHiddenIn(
|
||||
view: CalendarView,
|
||||
scope: CoroutineScope,
|
||||
): Flow<Set<Long>> =
|
||||
map { it.calendarsHiddenIn(view) }
|
||||
catch { emit(emptyMap()) }
|
||||
.map { it.calendarsHiddenIn(view) }
|
||||
.distinctUntilChanged()
|
||||
.shareIn(scope, SharingStarted.WhileSubscribed(), replay = 1)
|
||||
|
||||
/** [this] without the events of [hidden] calendars. */
|
||||
fun List<EventInstance>.withoutCalendars(hidden: Set<Long>): List<EventInstance> =
|
||||
if (hidden.isEmpty()) this else filterNot { it.calendarId in hidden }
|
||||
.shareIn(scope, SharingStarted.WhileSubscribed(replayExpirationMillis = 0), replay = 1)
|
||||
|
||||
/**
|
||||
* [CalendarRepository.instances] as one view shows them: also without the
|
||||
@@ -41,4 +42,3 @@ fun CalendarRepository.instancesWithout(
|
||||
hidden: Flow<Set<Long>>,
|
||||
): Flow<List<EventInstance>> =
|
||||
combine(instances(range), hidden) { instances, ids -> instances.withoutCalendars(ids) }
|
||||
.distinctUntilChanged()
|
||||
@@ -32,7 +32,8 @@ class FilterViewModel @Inject constructor(
|
||||
combine(
|
||||
repository.calendars(),
|
||||
prefs.hiddenCalendarIds,
|
||||
settingsPrefs.perCalendarHiddenViews,
|
||||
// Only feeds a note on the rows; a failed read leaves it off rather than failing the list.
|
||||
settingsPrefs.perCalendarHiddenViews.catch { emit(emptyMap()) },
|
||||
) { calendars, hidden, hiddenViews ->
|
||||
// Calendars switched off device-wide don't belong in the drawer's
|
||||
// hide/show list; they live only in Settings → Calendars.
|
||||
|
||||
@@ -14,12 +14,12 @@ import de.jeanlucmakiola.calendula.data.prefs.WeekStartPref
|
||||
import de.jeanlucmakiola.calendula.data.prefs.is24Hour
|
||||
import de.jeanlucmakiola.calendula.data.prefs.resolveFirstDay
|
||||
import de.jeanlucmakiola.calendula.domain.EventInstance
|
||||
import de.jeanlucmakiola.calendula.domain.withoutCalendars
|
||||
import de.jeanlucmakiola.calendula.ui.agenda.AgendaDay
|
||||
import de.jeanlucmakiola.calendula.ui.agenda.AgendaRange
|
||||
import de.jeanlucmakiola.calendula.ui.agenda.agendaRange
|
||||
import de.jeanlucmakiola.calendula.ui.agenda.dayCount
|
||||
import de.jeanlucmakiola.calendula.ui.agenda.groupAgendaDays
|
||||
import de.jeanlucmakiola.calendula.ui.common.withoutCalendars
|
||||
import de.jeanlucmakiola.floret.prefs.ThemeMode
|
||||
import de.jeanlucmakiola.floret.prefs.get
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
@@ -12,6 +12,7 @@ import de.jeanlucmakiola.floret.glance.updateWithRefresh
|
||||
import kotlinx.coroutines.CancellationException
|
||||
import de.jeanlucmakiola.calendula.data.prefs.is24Hour
|
||||
import de.jeanlucmakiola.calendula.domain.EventInstance
|
||||
import de.jeanlucmakiola.calendula.domain.withoutCalendars
|
||||
import de.jeanlucmakiola.calendula.widget.Themed
|
||||
import de.jeanlucmakiola.calendula.widget.WidgetKind
|
||||
import de.jeanlucmakiola.calendula.widget.WidgetLoadContext
|
||||
@@ -22,7 +23,6 @@ import de.jeanlucmakiola.calendula.widget.today
|
||||
import de.jeanlucmakiola.calendula.widget.widgetEntryPoint
|
||||
import de.jeanlucmakiola.calendula.widget.widgetLoadContext
|
||||
import de.jeanlucmakiola.calendula.widget.widgetState
|
||||
import de.jeanlucmakiola.calendula.ui.common.withoutCalendars
|
||||
import de.jeanlucmakiola.floret.prefs.toEnum
|
||||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.datetime.LocalDate
|
||||
|
||||
@@ -45,7 +45,7 @@ class ViewHiddenCalendarsTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `instancesIn drops a calendar only in the views it is hidden in`(@TempDir tempDir: Path) = runTest {
|
||||
fun `instancesWithout drops a calendar only in the views it is hidden in`(@TempDir tempDir: Path) = runTest {
|
||||
val fake = FakeCalendarDataSource().apply {
|
||||
calendarsResult = listOf(calendar(1L), calendar(2L))
|
||||
instancesResult = { _, _ -> listOf(event(10L, 1L), event(20L, 2L)) }
|
||||
@@ -64,7 +64,7 @@ class ViewHiddenCalendarsTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `instancesIn re-filters live when the setting changes`(@TempDir tempDir: Path) = runTest {
|
||||
fun `instancesWithout re-filters live when the setting changes`(@TempDir tempDir: Path) = runTest {
|
||||
val fake = FakeCalendarDataSource().apply {
|
||||
calendarsResult = listOf(calendar(1L), calendar(2L))
|
||||
instancesResult = { _, _ -> listOf(event(10L, 1L), event(20L, 2L)) }
|
||||
|
||||
@@ -391,8 +391,8 @@ gate also covers a snooze re-shown from our own alarm after its calendar was
|
||||
switched off. The drawer's filter sheet (`CalendarPrefs.hiddenCalendarIds`) is a
|
||||
separate in-app declutter that never touches reminders. Narrower still is the per-view hide
|
||||
(`SettingsPrefs.perCalendarHiddenViews`, #270): each view model asks for its
|
||||
events through `instancesIn(view, …)`, which drops the calendars hidden in that
|
||||
one view on top of the repository's filters; reminders, search and widgets read
|
||||
events through `instancesWithout(range, calendarsHiddenIn(view, …))`, which drops
|
||||
the calendars hidden in that one view on top of the repository's filters; reminders, search and widgets read
|
||||
the repository and never see it.
|
||||
|
||||
Deliberately absent: a fallback to the provider's `EVENT_REMINDER` broadcast.
|
||||
|
||||
Reference in new issue
Block a user