fix(#517): review round 1 - keep reading after a failed pref read, drawer count and notes agree, entry row not 'selected'

This commit is contained in:
makiolaj committed 2026-11-14 12:00:29 +01:00
1 parent 1aa66ffc99
commit 8df21b965a
8 files changed
+59 -19

No files matched your search

@@ -42,8 +42,6 @@ import androidx.compose.ui.layout.onGloballyPositioned
import androidx.compose.ui.layout.positionInWindow
import androidx.compose.ui.platform.LocalDensity
import androidx.hilt.navigation.compose.hiltViewModel
import de.jeanlucmakiola.calendula.ui.filter.FilterViewModel
import de.jeanlucmakiola.calendula.ui.filter.ViewCalendarsPicker
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import de.jeanlucmakiola.calendula.data.time.currentDate
import de.jeanlucmakiola.calendula.domain.EventForm
@@ -99,6 +97,8 @@ import de.jeanlucmakiola.calendula.ui.month.MonthScreen
import de.jeanlucmakiola.calendula.ui.search.SearchScreen
import de.jeanlucmakiola.calendula.ui.settings.SettingsScreen
import de.jeanlucmakiola.calendula.ui.week.TimelineScreen
import de.jeanlucmakiola.calendula.ui.filter.FilterViewModel
import de.jeanlucmakiola.calendula.ui.filter.ViewCalendarsPicker
import kotlinx.coroutines.NonCancellable
import kotlinx.coroutines.withContext
import kotlinx.coroutines.launch
@@ -79,7 +79,6 @@ import de.jeanlucmakiola.calendula.ui.common.CalendarView
import de.jeanlucmakiola.calendula.ui.common.IMPLEMENTED_VIEWS
import de.jeanlucmakiola.calendula.ui.common.icon
import de.jeanlucmakiola.calendula.ui.common.label
import de.jeanlucmakiola.floret.components.MultiOptionPicker
import de.jeanlucmakiola.calendula.ui.common.CalendarVisibilitySwitch
import de.jeanlucmakiola.calendula.ui.common.accountGroupTitle
import de.jeanlucmakiola.calendula.ui.common.groupByAccount
@@ -100,6 +99,7 @@ import de.jeanlucmakiola.floret.identity.expandEnter
import de.jeanlucmakiola.floret.identity.predictiveBack
import de.jeanlucmakiola.floret.components.GroupedRow
import de.jeanlucmakiola.floret.components.InlineTextField
import de.jeanlucmakiola.floret.components.MultiOptionPicker
import de.jeanlucmakiola.floret.components.Position
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.heightIn
@@ -17,6 +17,7 @@ import de.jeanlucmakiola.calendula.domain.CalendarSource
import de.jeanlucmakiola.calendula.domain.FailureReason
import de.jeanlucmakiola.calendula.domain.ics.IcsWriter
import de.jeanlucmakiola.calendula.ui.common.CalendarView
import de.jeanlucmakiola.calendula.ui.common.hidingNothingOnError
import de.jeanlucmakiola.floret.android.stateWhileSubscribed
import de.jeanlucmakiola.floret.di.IoDispatcher
import kotlinx.coroutines.CoroutineDispatcher
@@ -109,7 +110,7 @@ class CalendarsViewModel @Inject constructor(
/** The views each calendar is hidden in (#270); null until first read. */
val hiddenViews: StateFlow<Map<Long, Set<CalendarView>>?> =
settingsPrefs.perCalendarHiddenViews
.catch { emit(emptyMap()) }
.hidingNothingOnError()
.stateWhileSubscribed(
scope = viewModelScope,
initialValue = null,
@@ -4,13 +4,15 @@ 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.delay
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
import kotlinx.coroutines.flow.retryWhen
import kotlinx.coroutines.flow.shareIn
import java.io.IOException
import kotlin.time.Instant
/** The calendars hidden in [view] under a per-calendar hidden-views map (#270). */
@@ -27,12 +29,26 @@ fun Flow<Map<Long, Set<CalendarView>>>.calendarsHiddenIn(
view: CalendarView,
scope: CoroutineScope,
): Flow<Set<Long>> =
catch { emit(emptyMap()) }
hidingNothingOnError()
.map { it.calendarsHiddenIn(view) }
.distinctUntilChanged()
// The grace period outlasts a flatMapLatest swap, so paging doesn't re-read it.
.shareIn(scope, SharingStarted.WhileSubscribed(5_000, replayExpirationMillis = 0), replay = 1)
/**
* A failed read of the per-view setting hides nothing rather than failing what
* shows it, and keeps reading, so the next change still lands.
*/
fun Flow<Map<Long, Set<CalendarView>>>.hidingNothingOnError(): Flow<Map<Long, Set<CalendarView>>> =
retryWhen { cause, _ ->
if (cause !is IOException) return@retryWhen false
emit(emptyMap())
delay(RETRY_DELAY_MS)
true
}
private const val RETRY_DELAY_MS = 1_000L
/**
* [CalendarRepository.instances] as one view shows them: also without the
* [hidden] calendars the user keeps out of it. Display only — reminders, search
@@ -17,6 +17,7 @@ import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.pluralStringResource
@@ -43,7 +44,8 @@ import de.jeanlucmakiola.floret.components.positionOf
* Every calendar grouped by account, each with a colour swatch and a visibility
* switch; toggling writes straight to DataStore and every calendar view
* re-filters live. Three states (Loading / Failure / Success). A calendar hidden
* in [currentView] through its per-view setting (#270) says so on its row.
* in [currentView] through its per-view setting (#270) says so on its row, and
* the entry below the list opens that setting for the view (#517).
*/
@Composable
fun CalendarFilterList(
@@ -56,15 +58,18 @@ fun CalendarFilterList(
when (val s = state) {
FilterUiState.Loading -> FilterLoading(modifier)
is FilterUiState.Failure -> FilterMessage(s.reason, modifier)
is FilterUiState.Success -> FilterList(
groups = s.groups,
hidden = s.hidden,
hiddenInView = s.hiddenViews.calendarsHiddenIn(currentView),
hiddenOnlyHere = s.hiddenOnlyIn(currentView),
onHideInView = onHideInView,
onSetVisible = viewModel::setVisible,
modifier = modifier,
)
is FilterUiState.Success -> {
val hiddenInView = remember(s, currentView) { s.hiddenViews.calendarsHiddenIn(currentView) - s.hidden }
FilterList(
groups = s.groups,
hidden = s.hidden,
hiddenInView = hiddenInView,
hiddenOnlyHere = remember(s, currentView) { s.hiddenOnlyIn(currentView) },
onHideInView = onHideInView,
onSetVisible = viewModel::setVisible,
modifier = modifier,
)
}
}
}
@@ -125,7 +130,9 @@ private fun FilterList(
},
position = Position.Alone,
minHeight = 56.dp,
selected = hiddenOnlyHere > 0,
// Tinted, not "selected": it is an entry point, not a chosen option.
container = if (hiddenOnlyHere > 0) MaterialTheme.colorScheme.secondaryContainer else null,
contentColor = if (hiddenOnlyHere > 0) MaterialTheme.colorScheme.onSecondaryContainer else null,
leading = {
Box(Modifier.size(40.dp), contentAlignment = Alignment.Center) {
Icon(Icons.Filled.FilterList, contentDescription = null)
@@ -10,6 +10,7 @@ import de.jeanlucmakiola.calendula.data.prefs.SettingsPrefs
import de.jeanlucmakiola.calendula.domain.FailureReason
import de.jeanlucmakiola.calendula.domain.calendarListFailure
import de.jeanlucmakiola.calendula.ui.common.CalendarView
import de.jeanlucmakiola.calendula.ui.common.hidingNothingOnError
import de.jeanlucmakiola.calendula.ui.common.groupByAccount
import de.jeanlucmakiola.floret.android.stateWhileSubscribed
import de.jeanlucmakiola.floret.di.IoDispatcher
@@ -36,7 +37,7 @@ class FilterViewModel @Inject constructor(
repository.calendars(),
prefs.hiddenCalendarIds,
// Only feeds a note on the rows; a failed read leaves it off rather than failing the list.
settingsPrefs.perCalendarHiddenViews.catch { emit(emptyMap()) },
settingsPrefs.perCalendarHiddenViews.hidingNothingOnError(),
) { calendars, hidden, hiddenViews ->
// Calendars switched off device-wide don't belong in the drawer's
// hide/show list; they live only in Settings → Calendars.
@@ -19,7 +19,7 @@ import de.jeanlucmakiola.floret.components.GroupedSectionHeader
import de.jeanlucmakiola.floret.components.MultiOptionRows
/**
* The calendars hidden in [view] (#517), opened from that view's top bar: the
* The calendars hidden in [view] (#517), opened from the drawer while on it: the
* per-view setting from Settings → Calendars (#270), seen from the view's side.
* Toggles apply at once.
*/
@@ -82,4 +82,19 @@ class ViewHiddenCalendarsTest {
cancelAndIgnoreRemainingEvents()
}
}
@Test
fun `a failed read hides nothing and keeps reading`() = runTest {
var reads = 0
val source = kotlinx.coroutines.flow.flow {
reads++
if (reads == 1) throw java.io.IOException("disk")
emit(mapOf(1L to setOf(CalendarView.Month)))
}
source.hidingNothingOnError().test {
assertThat(awaitItem()).isEmpty()
assertThat(awaitItem()).containsExactly(1L, setOf(CalendarView.Month))
awaitComplete()
}
}
}