feat(#517): quick per-view calendar toggle in the top bar

This commit is contained in:
makiolaj committed 2026-11-14 03:14:47 +01:00
1 parent dad1f2cd9c
commit d943937b3a
8 files changed
+180 -3

No files matched your search

@@ -42,6 +42,9 @@ 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.FilterUiState
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
@@ -211,6 +214,9 @@ fun CalendarHost(
// top bar. Sits below the detail/edit overlays so tapping a result reveals
// the detail on top and backing out returns to the results.
var showSearch by rememberSaveable { mutableStateOf(false) }
var showViewCalendars by rememberSaveable { mutableStateOf(false) }
val filterViewModel: FilterViewModel = hiltViewModel()
val filterState by filterViewModel.state.collectAsStateWithLifecycle()
val onOpenSearch = { showSearch = true }
// Calendar manager (reached from Settings) — its own overlay so it slides
@@ -558,6 +564,14 @@ fun CalendarHost(
// (#239); nothing else in the calendar surfaces navigates here.
LocalManageCalendars provides remember { { showCalendars = true } },
) {
if (showViewCalendars) {
ViewCalendarsPicker(
view = view,
state = filterState,
onSetHidden = { id, hidden -> filterViewModel.setHiddenIn(id, view, hidden) },
onDismiss = { showViewCalendars = false },
)
}
// One drawer, top bar and FAB for every screen (#184): a switch changes
// what they say, from what the screen on show publishes, rather than
// swapping them for another screen's.
@@ -601,6 +615,8 @@ fun CalendarHost(
onOpenDrawer = { switchScope.launch { drawerState.open() } },
onOpenSearch = onOpenSearch,
showTodayButton = todayInToolbar,
hiddenInView = (filterState as? FilterUiState.Success)?.hiddenOnlyIn(view) ?: 0,
onOpenViewCalendars = { showViewCalendars = true },
)
},
floatingActionButton = {
@@ -14,8 +14,11 @@ import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.FilterList
import androidx.compose.material.icons.filled.Menu
import androidx.compose.material.icons.filled.Search
import androidx.compose.material3.Badge
import androidx.compose.material3.BadgedBox
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
@@ -34,6 +37,7 @@ import androidx.compose.runtime.Stable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.staticCompositionLocalOf
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.pluralStringResource
import androidx.compose.ui.res.stringResource
import de.jeanlucmakiola.calendula.R
import de.jeanlucmakiola.calendula.domain.EventInstance
@@ -177,6 +181,8 @@ fun CalendarTopBar(
onOpenDrawer: () -> Unit,
onOpenSearch: () -> Unit,
showTodayButton: Boolean,
hiddenInView: Int,
onOpenViewCalendars: () -> Unit,
) {
val effects = MaterialTheme.motionScheme.fastEffectsSpec<Float>()
AnimatedContent(
@@ -199,6 +205,8 @@ fun CalendarTopBar(
onOpenDrawer = onOpenDrawer,
onOpenSearch = onOpenSearch,
showTodayButton = showTodayButton,
hiddenInView = hiddenInView,
onOpenViewCalendars = onOpenViewCalendars,
)
}
}
@@ -214,6 +222,8 @@ private fun ViewTopBar(
onOpenDrawer: () -> Unit,
onOpenSearch: () -> Unit,
showTodayButton: Boolean,
hiddenInView: Int,
onOpenViewCalendars: () -> Unit,
) {
val chrome = host.chrome
TopAppBar(
@@ -243,6 +253,7 @@ private fun ViewTopBar(
contentDescription = stringResource(R.string.search_action),
)
}
ViewCalendarsAction(hiddenInView = hiddenInView, onClick = onOpenViewCalendars)
ViewSwitcherPill(
current = selectedView,
cycle = quickSwitchViews,
@@ -259,6 +270,24 @@ private fun ViewTopBar(
)
}
/**
* Opens the calendars hidden in the view on show (#517). A dot marks a view
* that hides any, so missing events have a visible reason.
*/
@Composable
private fun ViewCalendarsAction(hiddenInView: Int, onClick: () -> Unit) {
val description = if (hiddenInView > 0) {
pluralStringResource(R.plurals.view_calendars_action_hidden, hiddenInView, hiddenInView)
} else {
stringResource(R.string.view_calendars_action)
}
IconButton(onClick = onClick) {
BadgedBox(badge = { if (hiddenInView > 0) Badge() }) {
Icon(imageVector = Icons.Default.FilterList, contentDescription = description)
}
}
}
/**
* The FAB column every calendar screen shares. While an event is selected the
* FAB is [selection]'s toggle instead, and it steps aside mid-gesture.
@@ -3,6 +3,7 @@ package de.jeanlucmakiola.calendula.ui.filter
import de.jeanlucmakiola.calendula.domain.FailureReason
import de.jeanlucmakiola.calendula.ui.common.CalendarAccountGroup
import de.jeanlucmakiola.calendula.ui.common.CalendarView
import de.jeanlucmakiola.calendula.ui.common.calendarsHiddenIn
/**
* State for the calendar-filter sheet (M3). The user toggles per-calendar
@@ -20,5 +21,14 @@ sealed interface FilterUiState {
val groups: List<CalendarAccountGroup>,
val hidden: Set<Long>,
val hiddenViews: Map<Long, Set<CalendarView>> = emptyMap(),
) : FilterUiState
) : FilterUiState {
/**
* How many listed calendars [view] hides on its own (#517); ones already
* hidden everywhere from the drawer, or not listed, don't count.
*/
fun hiddenOnlyIn(view: CalendarView): Int {
val listed = groups.flatMap { group -> group.calendars.map { it.id } }
return hiddenViews.calendarsHiddenIn(view).count { it in listed && it !in hidden }
}
}
}
@@ -1,5 +1,6 @@
package de.jeanlucmakiola.calendula.ui.filter
import android.util.Log
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
@@ -8,9 +9,11 @@ import de.jeanlucmakiola.calendula.data.prefs.CalendarPrefs
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.groupByAccount
import de.jeanlucmakiola.floret.android.stateWhileSubscribed
import de.jeanlucmakiola.floret.di.IoDispatcher
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.catch
@@ -24,7 +27,7 @@ import javax.inject.Inject
class FilterViewModel @Inject constructor(
private val repository: CalendarRepository,
private val prefs: CalendarPrefs,
settingsPrefs: SettingsPrefs,
private val settingsPrefs: SettingsPrefs,
@IoDispatcher private val io: CoroutineDispatcher,
) : ViewModel() {
@@ -52,6 +55,19 @@ class FilterViewModel @Inject constructor(
initialValue = FilterUiState.Loading,
)
/** Hide or show one calendar in [view] only (#517), the same setting as Settings → Calendars. */
fun setHiddenIn(calendarId: Long, view: CalendarView, hidden: Boolean) {
viewModelScope.launch {
try {
settingsPrefs.setCalendarHiddenIn(calendarId, view, hidden)
} catch (e: CancellationException) {
throw e
} catch (e: Exception) {
Log.w("FilterViewModel", "Could not change hidden views of $calendarId", e)
}
}
}
/** Show or hide a single calendar; persists the new hidden set. */
fun setVisible(calendarId: Long, visible: Boolean) {
viewModelScope.launch {
@@ -0,0 +1,66 @@
package de.jeanlucmakiola.calendula.ui.filter
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import de.jeanlucmakiola.calendula.R
import de.jeanlucmakiola.calendula.ui.common.CalendarColorChip
import de.jeanlucmakiola.calendula.ui.common.CalendarView
import de.jeanlucmakiola.calendula.ui.common.accountGroupTitle
import de.jeanlucmakiola.calendula.ui.common.calendarsHiddenIn
import de.jeanlucmakiola.calendula.ui.common.label
import de.jeanlucmakiola.calendula.ui.common.orUnnamed
import de.jeanlucmakiola.floret.components.FullScreenPicker
import de.jeanlucmakiola.floret.components.GroupedHint
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
* per-view setting from Settings → Calendars (#270), seen from the view's side.
* Toggles apply at once.
*/
@Composable
fun ViewCalendarsPicker(
view: CalendarView,
state: FilterUiState,
onSetHidden: (calendarId: Long, hidden: Boolean) -> Unit,
onDismiss: () -> Unit,
) {
FullScreenPicker(
title = stringResource(R.string.view_calendars_title, view.label()),
onDismiss = onDismiss,
predictiveBack = true,
) {
when (state) {
FilterUiState.Loading -> Unit
is FilterUiState.Failure -> GroupedHint(stringResource(R.string.state_failure_provider), verticalPadding = 8.dp)
is FilterUiState.Success -> {
// Seeded once, then ahead of the store: the picker is the only
// writer while open, so a quick second tap still flips.
var hidden by remember { mutableStateOf(state.hiddenViews.calendarsHiddenIn(view)) }
GroupedHint(stringResource(R.string.view_calendars_hint), verticalPadding = 8.dp)
state.groups.forEach { group ->
GroupedSectionHeader(accountGroupTitle(group))
MultiOptionRows(
options = group.calendars,
selected = group.calendars.filterTo(mutableSetOf()) { it.id in hidden },
label = { it.displayName.orUnnamed() },
onToggle = { calendar, hide ->
hidden = if (hide) hidden + calendar.id else hidden - calendar.id
onSetHidden(calendar.id, hide)
},
summary = { calendar ->
if (calendar.id in state.hidden) stringResource(R.string.view_calendars_hidden_everywhere) else null
},
leading = { CalendarColorChip(it.color) },
)
}
}
}
}
}
+8
View File
@@ -868,6 +868,14 @@
<string name="calendars_hide_in_views_none">Shown in every view</string>
<string name="calendars_hidden_in">Hidden in %1$s</string>
<string name="filter_hidden_in_view">Hidden in this view</string>
<string name="view_calendars_action">Calendars in this view</string>
<plurals name="view_calendars_action_hidden">
<item quantity="one">Calendars in this view, %d hidden</item>
<item quantity="other">Calendars in this view, %d hidden</item>
</plurals>
<string name="view_calendars_title">Hide in %1$s</string>
<string name="view_calendars_hint">Checked calendars stay out of this view only. Reminders, search and widgets still include them.</string>
<string name="view_calendars_hidden_everywhere">Hidden in every view from the menu</string>
<string name="color_name_mauve">Mauve</string>
<string name="color_name_red">Red</string>
<string name="color_name_orange">Orange</string>
@@ -0,0 +1,31 @@
package de.jeanlucmakiola.calendula.ui.filter
import com.google.common.truth.Truth.assertThat
import de.jeanlucmakiola.calendula.domain.CalendarSource
import de.jeanlucmakiola.calendula.ui.common.CalendarView
import de.jeanlucmakiola.calendula.ui.common.groupByAccount
import org.junit.jupiter.api.Test
class FilterUiStateTest {
private fun calendar(id: Long) = CalendarSource(id, "Cal $id", "x@y", "LOCAL", 0, true)
@Test
fun `hiddenOnlyIn counts listed calendars the view hides on its own`() {
val state = FilterUiState.Success(
groups = listOf(calendar(1L), calendar(2L), calendar(3L)).groupByAccount(),
// 3 is hidden everywhere from the drawer already.
hidden = setOf(3L),
hiddenViews = mapOf(
1L to setOf(CalendarView.Month),
2L to setOf(CalendarView.Month, CalendarView.Week),
3L to setOf(CalendarView.Month),
// Not listed (switched off device-wide).
9L to setOf(CalendarView.Month),
),
)
assertThat(state.hiddenOnlyIn(CalendarView.Month)).isEqualTo(2)
assertThat(state.hiddenOnlyIn(CalendarView.Week)).isEqualTo(1)
assertThat(state.hiddenOnlyIn(CalendarView.Agenda)).isEqualTo(0)
}
}
+2 -1
View File
@@ -393,7 +393,8 @@ separate in-app declutter that never touches reminders. Narrower still is the pe
(`SettingsPrefs.perCalendarHiddenViews`, #270): each view model asks for its
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.
the repository and never see it. The top bar's filter action (#517) edits the same set for
the view on show, and its dot and the drawer's "Hidden in this view" read it too.
Deliberately absent: a fallback to the provider's `EVENT_REMINDER` broadcast.
Keeping both would double-post wherever the provider works, and Etar's way out —