feat(#270): hide calendars in chosen views
This commit is contained in:
1 parent
e9cf7d66ba
commit
e3e9865d3f
20 files changed
+390
-28
No files matched your search
@@ -165,6 +165,7 @@ class SettingsPrefs @Inject constructor(
|
||||
val autofocusEventTitle: Flow<Boolean> = prefs.flow(SettingsKeys.autofocusEventTitle)
|
||||
val defaultEventDurationMinutes: Flow<Int> = prefs.flow(SettingsKeys.defaultEventDurationMinutes)
|
||||
val perCalendarEventDuration: Flow<Map<Long, Int>> = prefs.flow(SettingsKeys.perCalendarEventDuration)
|
||||
val perCalendarHiddenViews: Flow<Map<Long, Set<CalendarView>>> = prefs.flow(SettingsKeys.perCalendarHiddenViews)
|
||||
val remindersEnabled: Flow<Boolean> = prefs.flow(SettingsKeys.remindersEnabled)
|
||||
val allowColorOnUnsupportedCalendars: Flow<Boolean> =
|
||||
prefs.flow(SettingsKeys.allowColorOnUnsupportedCalendars)
|
||||
@@ -271,6 +272,13 @@ class SettingsPrefs @Inject constructor(
|
||||
}
|
||||
}
|
||||
|
||||
/** Hide [calendarId] in [views] only (#270); an empty set shows it everywhere again. */
|
||||
suspend fun setCalendarHiddenViews(calendarId: Long, views: Set<CalendarView>) {
|
||||
prefs.update(SettingsKeys.perCalendarHiddenViews) { current ->
|
||||
if (views.isEmpty()) current - calendarId else current + (calendarId to views)
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun setRemindersEnabled(enabled: Boolean) = prefs.set(SettingsKeys.remindersEnabled, enabled)
|
||||
|
||||
suspend fun setAllowColorOnUnsupportedCalendars(enabled: Boolean) =
|
||||
@@ -588,6 +596,15 @@ object SettingsKeys {
|
||||
val perCalendarEventDuration: Pref<Map<Long, Int>> =
|
||||
nullableStringPref("per_calendar_event_duration").map(::parseDurationOverrides) { it.toStoredDurations() }
|
||||
|
||||
/**
|
||||
* Views each calendar is hidden in (#270): a display-only filter on top of
|
||||
* `Calendars.VISIBLE` and the drawer's filter, so reminders and search never
|
||||
* see it. A calendar absent shows in every view. Serialised as
|
||||
* `id=Month,Agenda;id=Week`.
|
||||
*/
|
||||
val perCalendarHiddenViews: Pref<Map<Long, Set<CalendarView>>> =
|
||||
nullableStringPref("per_calendar_hidden_views").map(::parseHiddenViews) { it.toStoredHiddenViews() }
|
||||
|
||||
/**
|
||||
* Whether Calendula posts reminder notifications (v1.4). On by default — for
|
||||
* users whose only calendar app this is, reminders are essential.
|
||||
@@ -977,6 +994,28 @@ private fun parseDurationOverrides(stored: String?): Map<Long, Int> =
|
||||
calendarId to minutes
|
||||
}.toMap()
|
||||
|
||||
/**
|
||||
* Parse the per-calendar hidden-views map (`id=View,View` entries joined by `;`).
|
||||
* Unknown view names and malformed entries are dropped; an entry left with no
|
||||
* views is dropped with them.
|
||||
*/
|
||||
internal fun parseHiddenViews(stored: String?): Map<Long, Set<CalendarView>> =
|
||||
stored?.split(ENTRY_SEP).orEmpty().mapNotNull { entry ->
|
||||
val parts = entry.split(KEY_VALUE_SEP)
|
||||
if (parts.size != 2) return@mapNotNull null
|
||||
val calendarId = parts[0].trim().toLongOrNull() ?: return@mapNotNull null
|
||||
val views = parts[1].split(LIST_SEP)
|
||||
.mapNotNullTo(mutableSetOf()) { name -> CalendarView.entries.firstOrNull { it.name == name.trim() } }
|
||||
if (views.isEmpty()) null else calendarId to views
|
||||
}.toMap()
|
||||
|
||||
internal fun Map<Long, Set<CalendarView>>.toStoredHiddenViews(): String? =
|
||||
entries.filter { it.value.isNotEmpty() }.sortedBy { it.key }
|
||||
.joinToString(ENTRY_SEP) { (id, views) ->
|
||||
"$id$KEY_VALUE_SEP" + views.sortedBy { it.ordinal }.joinToString(LIST_SEP) { it.name }
|
||||
}
|
||||
.ifEmpty { null }
|
||||
|
||||
private fun Map<Long, Int>.toStoredDurations(): String =
|
||||
entries.sortedBy { it.key }.joinToString(ENTRY_SEP) { (id, minutes) ->
|
||||
"$id$KEY_VALUE_SEP$minutes"
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package de.jeanlucmakiola.calendula.ui.agenda
|
||||
|
||||
import de.jeanlucmakiola.calendula.ui.common.dayRange
|
||||
import de.jeanlucmakiola.calendula.ui.common.CalendarView
|
||||
import de.jeanlucmakiola.calendula.ui.common.instancesIn
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
@@ -138,7 +140,7 @@ class AgendaViewModel @Inject constructor(
|
||||
)
|
||||
combine(
|
||||
repository.calendars(),
|
||||
repository.instances(window),
|
||||
repository.instancesIn(CalendarView.Agenda, window, settingsPrefs.perCalendarHiddenViews),
|
||||
today,
|
||||
) { calendars, instances, today ->
|
||||
buildState(params, calendars, instances, today)
|
||||
|
||||
@@ -72,6 +72,11 @@ import de.jeanlucmakiola.calendula.domain.stateLabels
|
||||
import de.jeanlucmakiola.calendula.data.calendar.CalendarColorPalette
|
||||
import de.jeanlucmakiola.calendula.ui.common.AccountKey
|
||||
import de.jeanlucmakiola.calendula.ui.common.CalendarColorChip
|
||||
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
|
||||
@@ -130,12 +135,15 @@ fun CalendarsScreen(
|
||||
val calendars by viewModel.calendars.collectAsStateWithLifecycle()
|
||||
val deleteLockedIds by viewModel.deleteLockedCalendarIds.collectAsStateWithLifecycle()
|
||||
val error by viewModel.error.collectAsStateWithLifecycle()
|
||||
val hiddenViews by viewModel.hiddenViews.collectAsStateWithLifecycle()
|
||||
|
||||
// null = list; NEW_CALENDAR_ID = create; any other id = edit that calendar.
|
||||
// [editorSession] bumps on every open so the editor's field state resets for
|
||||
// a fresh open while still surviving configuration changes within one open.
|
||||
var editorId by rememberSaveable { mutableStateOf<Long?>(null) }
|
||||
var editorSession by rememberSaveable { mutableStateOf(0) }
|
||||
// A synced calendar has no editor; tapping it opens its hidden-views picker.
|
||||
var hidingId by rememberSaveable { mutableStateOf<Long?>(null) }
|
||||
|
||||
if (editorId != null) {
|
||||
val editing = calendars.firstOrNull { it.id == editorId }
|
||||
@@ -145,13 +153,15 @@ fun CalendarsScreen(
|
||||
initialName = editing?.displayName.orEmpty(),
|
||||
initialColor = editing?.color ?: CalendarColorPalette.all.first(),
|
||||
initialDescription = editing?.description.orEmpty(),
|
||||
showHiddenViews = true,
|
||||
initialHiddenViews = editing?.let { hiddenViews[it.id] }.orEmpty(),
|
||||
deleteLocked = editing != null && editing.id in deleteLockedIds,
|
||||
onSave = { name, color, description ->
|
||||
onSave = { name, color, description, views ->
|
||||
val id = editorId
|
||||
if (id == null || id == NEW_CALENDAR_ID) {
|
||||
viewModel.createCalendar(name, color, description)
|
||||
viewModel.createCalendar(name, color, description, views)
|
||||
} else {
|
||||
viewModel.updateCalendar(id, name, color, description)
|
||||
viewModel.updateCalendar(id, name, color, description, views)
|
||||
}
|
||||
editorId = null
|
||||
},
|
||||
@@ -165,6 +175,7 @@ fun CalendarsScreen(
|
||||
CalendarsList(
|
||||
local = calendars.filter { it.isLocal },
|
||||
synced = calendars.filterNot { it.isLocal },
|
||||
hiddenViews = hiddenViews,
|
||||
error = error,
|
||||
onConsumeError = viewModel::consumeError,
|
||||
onOpenBackup = onOpenBackup,
|
||||
@@ -172,9 +183,27 @@ fun CalendarsScreen(
|
||||
visible = visible,
|
||||
onAdd = { editorSession++; editorId = NEW_CALENDAR_ID },
|
||||
onEdit = { calendar -> editorSession++; editorId = calendar.id },
|
||||
onEditHiddenViews = { calendar -> hidingId = calendar.id },
|
||||
onSetVisible = viewModel::setCalendarVisible,
|
||||
onSetAccountVisible = viewModel::setAccountVisible,
|
||||
)
|
||||
hidingId?.let { id ->
|
||||
val calendar = calendars.firstOrNull { it.id == id }
|
||||
if (calendar == null) {
|
||||
// Gone (deleted or account removed) while the picker was up.
|
||||
LaunchedEffect(id) { hidingId = null }
|
||||
} else {
|
||||
val hidden = hiddenViews[id].orEmpty()
|
||||
HiddenViewsPicker(
|
||||
calendarName = calendar.displayName.orUnnamed(),
|
||||
hidden = hidden,
|
||||
onToggle = { view, hide ->
|
||||
viewModel.setHiddenViews(id, if (hide) hidden + view else hidden - view)
|
||||
},
|
||||
onDismiss = { hidingId = null },
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -182,6 +211,7 @@ fun CalendarsScreen(
|
||||
private fun CalendarsList(
|
||||
local: List<CalendarSource>,
|
||||
synced: List<CalendarSource>,
|
||||
hiddenViews: Map<Long, Set<CalendarView>>,
|
||||
error: Boolean,
|
||||
onConsumeError: () -> Unit,
|
||||
onOpenBackup: () -> Unit,
|
||||
@@ -189,6 +219,7 @@ private fun CalendarsList(
|
||||
visible: Boolean,
|
||||
onAdd: () -> Unit,
|
||||
onEdit: (CalendarSource) -> Unit,
|
||||
onEditHiddenViews: (CalendarSource) -> Unit,
|
||||
onSetVisible: (Long, Boolean) -> Unit,
|
||||
onSetAccountVisible: (Collection<Long>, Boolean) -> Unit,
|
||||
) {
|
||||
@@ -241,6 +272,7 @@ private fun CalendarsList(
|
||||
local.forEachIndexed { index, calendar ->
|
||||
CalendarManagerRow(
|
||||
calendar = calendar,
|
||||
hiddenViews = hiddenViews[calendar.id].orEmpty(),
|
||||
last = index == local.lastIndex,
|
||||
onSetVisible = onSetVisible,
|
||||
onClick = { onEdit(calendar) },
|
||||
@@ -315,8 +347,10 @@ private fun CalendarsList(
|
||||
ordered.forEachIndexed { index, calendar ->
|
||||
CalendarManagerRow(
|
||||
calendar = calendar,
|
||||
hiddenViews = hiddenViews[calendar.id].orEmpty(),
|
||||
last = index == ordered.lastIndex,
|
||||
onSetVisible = onSetVisible,
|
||||
onClick = { onEditHiddenViews(calendar) },
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -339,19 +373,25 @@ internal fun CalendarEditor(
|
||||
initialName: String,
|
||||
initialColor: Int,
|
||||
initialDescription: String,
|
||||
onSave: (name: String, color: Int, description: String?) -> Unit,
|
||||
onSave: (name: String, color: Int, description: String?, hiddenViews: Set<CalendarView>) -> Unit,
|
||||
onDelete: () -> Unit,
|
||||
onClose: () -> Unit,
|
||||
deleteLocked: Boolean = false,
|
||||
showHiddenViews: Boolean = false,
|
||||
initialHiddenViews: Set<CalendarView> = emptySet(),
|
||||
) {
|
||||
var name by rememberSaveable(sessionKey) { mutableStateOf(initialName) }
|
||||
var hiddenViews by rememberSaveable(sessionKey) { mutableStateOf(initialHiddenViews.toList()) }
|
||||
var pickHiddenViews by rememberSaveable(sessionKey) { mutableStateOf(false) }
|
||||
var color by rememberSaveable(sessionKey) { mutableStateOf(initialColor) }
|
||||
var description by rememberSaveable(sessionKey) { mutableStateOf(initialDescription) }
|
||||
var confirmDelete by remember { mutableStateOf(false) }
|
||||
val focusRequester = remember { FocusRequester() }
|
||||
|
||||
val valid = name.isNotBlank()
|
||||
val commit = { if (valid) onSave(name.trim(), color, description.trim().ifEmpty { null }) }
|
||||
val commit = {
|
||||
if (valid) onSave(name.trim(), color, description.trim().ifEmpty { null }, hiddenViews.toSet())
|
||||
}
|
||||
|
||||
FullScreenPicker(
|
||||
title = stringResource(
|
||||
@@ -403,6 +443,16 @@ internal fun CalendarEditor(
|
||||
}
|
||||
}
|
||||
|
||||
if (showHiddenViews) {
|
||||
Spacer(Modifier.height(20.dp))
|
||||
GroupedRow(
|
||||
title = stringResource(R.string.calendars_hide_in_views),
|
||||
summary = hiddenViewsSummary(hiddenViews.toSet()),
|
||||
position = Position.Alone,
|
||||
onClick = { pickHiddenViews = true },
|
||||
)
|
||||
}
|
||||
|
||||
if (!isNew && !deleteLocked) {
|
||||
Spacer(Modifier.height(24.dp))
|
||||
DeleteCalendarRow(onClick = { confirmDelete = true })
|
||||
@@ -410,6 +460,17 @@ internal fun CalendarEditor(
|
||||
Spacer(Modifier.height(24.dp))
|
||||
}
|
||||
|
||||
if (pickHiddenViews) {
|
||||
HiddenViewsPicker(
|
||||
calendarName = name.trim().orUnnamed(),
|
||||
hidden = hiddenViews.toSet(),
|
||||
onToggle = { view, hide ->
|
||||
hiddenViews = if (hide) (hiddenViews + view).distinct() else hiddenViews - view
|
||||
},
|
||||
onDismiss = { pickHiddenViews = false },
|
||||
)
|
||||
}
|
||||
|
||||
if (confirmDelete) {
|
||||
AlertDialog(
|
||||
onDismissRequest = { confirmDelete = false },
|
||||
@@ -636,7 +697,7 @@ private fun EditorSectionLabel(text: String) {
|
||||
* plain writable one (#76), then its own description.
|
||||
*/
|
||||
@Composable
|
||||
private fun calendarRowSummary(calendar: CalendarSource): String? {
|
||||
private fun calendarRowSummary(calendar: CalendarSource, hiddenViews: Set<CalendarView>): String? {
|
||||
val states = calendar.stateLabels().map { label ->
|
||||
stringResource(
|
||||
when (label) {
|
||||
@@ -646,7 +707,8 @@ private fun calendarRowSummary(calendar: CalendarSource): String? {
|
||||
},
|
||||
)
|
||||
}
|
||||
val parts = states + listOfNotNull(calendar.description?.takeIf { it.isNotBlank() })
|
||||
val hidden = if (hiddenViews.isEmpty()) null else hiddenViewsSummary(hiddenViews)
|
||||
val parts = states + listOfNotNull(hidden, calendar.description?.takeIf { it.isNotBlank() })
|
||||
return parts.joinToString(" · ").ifEmpty { null }
|
||||
}
|
||||
|
||||
@@ -662,6 +724,7 @@ private fun calendarRowSummary(calendar: CalendarSource): String? {
|
||||
@Composable
|
||||
private fun CalendarManagerRow(
|
||||
calendar: CalendarSource,
|
||||
hiddenViews: Set<CalendarView>,
|
||||
last: Boolean,
|
||||
onSetVisible: (Long, Boolean) -> Unit,
|
||||
onClick: (() -> Unit)? = null,
|
||||
@@ -669,7 +732,7 @@ private fun CalendarManagerRow(
|
||||
val disabled = !calendar.isVisibleInSystem || calendar.isNotSynced
|
||||
GroupedRow(
|
||||
title = calendar.displayName.orUnnamed(),
|
||||
summary = calendarRowSummary(calendar),
|
||||
summary = calendarRowSummary(calendar, hiddenViews),
|
||||
position = if (last) Position.Bottom else Position.Middle,
|
||||
container = MaterialTheme.colorScheme.surfaceContainerHighest,
|
||||
dimmed = disabled,
|
||||
@@ -689,6 +752,43 @@ 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.
|
||||
*/
|
||||
@Composable
|
||||
private fun HiddenViewsPicker(
|
||||
calendarName: String,
|
||||
hidden: Set<CalendarView>,
|
||||
onToggle: (CalendarView, Boolean) -> Unit,
|
||||
onDismiss: () -> Unit,
|
||||
) {
|
||||
MultiOptionPicker(
|
||||
title = stringResource(R.string.calendars_hide_in_views),
|
||||
options = IMPLEMENTED_VIEWS,
|
||||
selected = hidden,
|
||||
label = { it.label() },
|
||||
onToggle = onToggle,
|
||||
onDismiss = onDismiss,
|
||||
leading = { Icon(it.icon, contentDescription = null) },
|
||||
header = {
|
||||
GroupedHint(
|
||||
stringResource(R.string.calendars_hide_in_views_hint, calendarName),
|
||||
verticalPadding = 8.dp,
|
||||
)
|
||||
},
|
||||
predictiveBack = true,
|
||||
)
|
||||
}
|
||||
|
||||
/** "Shown in every view", or the views [hidden] names, in the app's view order. */
|
||||
@Composable
|
||||
private fun hiddenViewsSummary(hidden: Set<CalendarView>): String {
|
||||
if (hidden.isEmpty()) return stringResource(R.string.calendars_hide_in_views_none)
|
||||
val names = IMPLEMENTED_VIEWS.filter { it in hidden }.map { it.label() }
|
||||
return stringResource(R.string.calendars_hidden_in, names.joinToString(", "))
|
||||
}
|
||||
|
||||
/** Fade a leading element to the M3 disabled emphasis when [disabled]. */
|
||||
private fun dimIf(disabled: Boolean): Modifier =
|
||||
if (disabled) Modifier.alpha(0.38f) else Modifier
|
||||
|
||||
@@ -15,6 +15,7 @@ import de.jeanlucmakiola.calendula.data.prefs.SettingsPrefs
|
||||
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.floret.android.stateWhileSubscribed
|
||||
import de.jeanlucmakiola.floret.di.IoDispatcher
|
||||
import kotlinx.coroutines.CoroutineDispatcher
|
||||
@@ -104,6 +105,19 @@ class CalendarsViewModel @Inject constructor(
|
||||
initialValue = emptySet(),
|
||||
)
|
||||
|
||||
/** The views each calendar is hidden in (#270). */
|
||||
val hiddenViews: StateFlow<Map<Long, Set<CalendarView>>> =
|
||||
settingsPrefs.perCalendarHiddenViews
|
||||
.catch { emit(emptyMap()) }
|
||||
.stateWhileSubscribed(
|
||||
scope = viewModelScope,
|
||||
initialValue = emptyMap(),
|
||||
)
|
||||
|
||||
fun setHiddenViews(id: Long, views: Set<CalendarView>) {
|
||||
viewModelScope.launch { settingsPrefs.setCalendarHiddenViews(id, views) }
|
||||
}
|
||||
|
||||
private val _error = MutableStateFlow(false)
|
||||
val error: StateFlow<Boolean> = _error.asStateFlow()
|
||||
|
||||
@@ -139,16 +153,30 @@ class CalendarsViewModel @Inject constructor(
|
||||
}
|
||||
}
|
||||
|
||||
fun createCalendar(displayName: String, color: Int, description: String?) = write {
|
||||
repository.createLocalCalendar(displayName, color, description)
|
||||
fun createCalendar(
|
||||
displayName: String,
|
||||
color: Int,
|
||||
description: String?,
|
||||
hiddenViews: Set<CalendarView> = emptySet(),
|
||||
) = write {
|
||||
val id = repository.createLocalCalendar(displayName, color, description)
|
||||
if (hiddenViews.isNotEmpty()) settingsPrefs.setCalendarHiddenViews(id, hiddenViews)
|
||||
}
|
||||
|
||||
fun updateCalendar(id: Long, displayName: String, color: Int, description: String?) = write {
|
||||
fun updateCalendar(
|
||||
id: Long,
|
||||
displayName: String,
|
||||
color: Int,
|
||||
description: String?,
|
||||
hiddenViews: Set<CalendarView>,
|
||||
) = write {
|
||||
repository.updateCalendar(id, displayName, color, description)
|
||||
settingsPrefs.setCalendarHiddenViews(id, hiddenViews)
|
||||
}
|
||||
|
||||
fun deleteCalendar(id: Long) = write {
|
||||
repository.deleteCalendar(id)
|
||||
settingsPrefs.setCalendarHiddenViews(id, emptySet())
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -112,7 +112,7 @@ fun CalendarDrawer(
|
||||
Spacer(Modifier.height(16.dp))
|
||||
|
||||
DrawerSectionHeader(stringResource(R.string.filter_title))
|
||||
CalendarFilterList()
|
||||
CalendarFilterList(currentView = currentView)
|
||||
|
||||
Spacer(Modifier.height(16.dp))
|
||||
GroupedRow(
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
package de.jeanlucmakiola.calendula.ui.common
|
||||
|
||||
import de.jeanlucmakiola.calendula.data.calendar.CalendarRepository
|
||||
import de.jeanlucmakiola.calendula.domain.EventInstance
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.combine
|
||||
import kotlinx.coroutines.flow.distinctUntilChanged
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlin.time.Instant
|
||||
|
||||
/** The calendars hidden in [view] under a per-calendar hidden-views map (#270). */
|
||||
fun Map<Long, Set<CalendarView>>.calendarsHiddenIn(view: CalendarView): Set<Long> =
|
||||
filterValues { view in it }.keys
|
||||
|
||||
/** [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 }
|
||||
|
||||
/**
|
||||
* [CalendarRepository.instances] as [view] shows them: also without the
|
||||
* calendars the user hid in that view. Display only — reminders and search
|
||||
* read the repository directly.
|
||||
*/
|
||||
fun CalendarRepository.instancesIn(
|
||||
view: CalendarView,
|
||||
range: ClosedRange<Instant>,
|
||||
hiddenViews: Flow<Map<Long, Set<CalendarView>>>,
|
||||
): Flow<List<EventInstance>> =
|
||||
combine(
|
||||
instances(range),
|
||||
hiddenViews.map { it.calendarsHiddenIn(view) }.distinctUntilChanged(),
|
||||
) { instances, hidden -> instances.withoutCalendars(hidden) }
|
||||
.distinctUntilChanged()
|
||||
@@ -22,6 +22,8 @@ import de.jeanlucmakiola.calendula.R
|
||||
import de.jeanlucmakiola.calendula.domain.FailureReason
|
||||
import de.jeanlucmakiola.calendula.ui.common.CalendarAccountGroup
|
||||
import de.jeanlucmakiola.calendula.ui.common.CalendarColorChip
|
||||
import de.jeanlucmakiola.calendula.ui.common.CalendarView
|
||||
import de.jeanlucmakiola.calendula.ui.common.calendarsHiddenIn
|
||||
import de.jeanlucmakiola.calendula.ui.common.accountGroupTitle
|
||||
import de.jeanlucmakiola.calendula.ui.common.orUnnamed
|
||||
import de.jeanlucmakiola.floret.components.GroupedListInset
|
||||
@@ -32,10 +34,12 @@ import de.jeanlucmakiola.floret.components.positionOf
|
||||
* Calendar-visibility filter (M3), rendered inline in the navigation drawer.
|
||||
* 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).
|
||||
* re-filters live. Three states (Loading / Failure / Success). A calendar hidden
|
||||
* in [currentView] through its per-view setting (#270) says so on its row.
|
||||
*/
|
||||
@Composable
|
||||
fun CalendarFilterList(
|
||||
currentView: CalendarView,
|
||||
modifier: Modifier = Modifier,
|
||||
viewModel: FilterViewModel = hiltViewModel(),
|
||||
) {
|
||||
@@ -46,6 +50,7 @@ fun CalendarFilterList(
|
||||
is FilterUiState.Success -> FilterList(
|
||||
groups = s.groups,
|
||||
hidden = s.hidden,
|
||||
hiddenInView = s.hiddenViews.calendarsHiddenIn(currentView),
|
||||
onSetVisible = viewModel::setVisible,
|
||||
modifier = modifier,
|
||||
)
|
||||
@@ -61,6 +66,7 @@ fun CalendarFilterList(
|
||||
private fun FilterList(
|
||||
groups: List<CalendarAccountGroup>,
|
||||
hidden: Set<Long>,
|
||||
hiddenInView: Set<Long>,
|
||||
onSetVisible: (Long, Boolean) -> Unit,
|
||||
modifier: Modifier = Modifier,
|
||||
) {
|
||||
@@ -81,6 +87,7 @@ private fun FilterList(
|
||||
val visible = cal.id !in hidden
|
||||
GroupedRow(
|
||||
title = cal.displayName.orUnnamed(),
|
||||
summary = if (cal.id in hiddenInView) stringResource(R.string.filter_hidden_in_view) else null,
|
||||
position = positionOf(index, group.calendars.size),
|
||||
minHeight = 56.dp,
|
||||
leading = { CalendarColorChip(cal.color) },
|
||||
|
||||
@@ -2,6 +2,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
|
||||
|
||||
/**
|
||||
* State for the calendar-filter sheet (M3). The user toggles per-calendar
|
||||
@@ -11,9 +12,13 @@ import de.jeanlucmakiola.calendula.ui.common.CalendarAccountGroup
|
||||
sealed interface FilterUiState {
|
||||
data object Loading : FilterUiState
|
||||
data class Failure(val reason: FailureReason) : FilterUiState
|
||||
/** A calendar shows when its id is *not* in [hidden]. */
|
||||
/**
|
||||
* A calendar shows when its id is *not* in [hidden]; [hiddenViews] are the
|
||||
* views each calendar is hidden in regardless (#270).
|
||||
*/
|
||||
data class Success(
|
||||
val groups: List<CalendarAccountGroup>,
|
||||
val hidden: Set<Long>,
|
||||
val hiddenViews: Map<Long, Set<CalendarView>> = emptyMap(),
|
||||
) : FilterUiState
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import androidx.lifecycle.viewModelScope
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import de.jeanlucmakiola.calendula.data.calendar.CalendarRepository
|
||||
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.groupByAccount
|
||||
@@ -23,6 +24,7 @@ import javax.inject.Inject
|
||||
class FilterViewModel @Inject constructor(
|
||||
private val repository: CalendarRepository,
|
||||
private val prefs: CalendarPrefs,
|
||||
settingsPrefs: SettingsPrefs,
|
||||
@IoDispatcher private val io: CoroutineDispatcher,
|
||||
) : ViewModel() {
|
||||
|
||||
@@ -30,7 +32,8 @@ class FilterViewModel @Inject constructor(
|
||||
combine(
|
||||
repository.calendars(),
|
||||
prefs.hiddenCalendarIds,
|
||||
) { calendars, hidden ->
|
||||
settingsPrefs.perCalendarHiddenViews,
|
||||
) { calendars, hidden, hiddenViews ->
|
||||
// Calendars switched off device-wide don't belong in the drawer's
|
||||
// hide/show list; they live only in Settings → Calendars.
|
||||
val enabled = calendars.filter { it.isVisibleInSystem }
|
||||
@@ -38,7 +41,7 @@ class FilterViewModel @Inject constructor(
|
||||
if (failure != null) {
|
||||
FilterUiState.Failure(failure)
|
||||
} else {
|
||||
FilterUiState.Success(enabled.groupByAccount(), hidden)
|
||||
FilterUiState.Success(enabled.groupByAccount(), hidden, hiddenViews)
|
||||
}
|
||||
}
|
||||
.catch { emit(FilterUiState.Failure(FailureReason.ProviderUnavailable)) }
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package de.jeanlucmakiola.calendula.ui.month
|
||||
|
||||
import de.jeanlucmakiola.calendula.ui.common.startOfWeek
|
||||
import de.jeanlucmakiola.calendula.ui.common.CalendarView
|
||||
import de.jeanlucmakiola.calendula.ui.common.instancesIn
|
||||
import de.jeanlucmakiola.calendula.ui.common.dayRange
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
@@ -68,6 +70,8 @@ class MonthViewModel @Inject constructor(
|
||||
|
||||
private val zone = TimeZone.currentSystemDefault()
|
||||
|
||||
private val hiddenViews = settingsPrefs.perCalendarHiddenViews
|
||||
|
||||
/** First day of the week, from the Settings preference (AUTO → locale). */
|
||||
val weekStart: StateFlow<DayOfWeek> = settingsPrefs.firstDayOfWeek(viewModelScope)
|
||||
|
||||
@@ -152,7 +156,7 @@ class MonthViewModel @Inject constructor(
|
||||
) { (ym, ws) ->
|
||||
combine(
|
||||
repository.calendars(),
|
||||
repository.instances(monthGridRange(ym, ws, zone)),
|
||||
repository.instancesIn(CalendarView.Month, monthGridRange(ym, ws, zone), hiddenViews),
|
||||
today,
|
||||
) { calendars, instances, today ->
|
||||
buildState(ym, ws, calendars, instances, today)
|
||||
@@ -224,7 +228,7 @@ class MonthViewModel @Inject constructor(
|
||||
val range = dayRange(first, last, zone)
|
||||
combine(
|
||||
repository.calendars(),
|
||||
repository.instances(range),
|
||||
repository.instancesIn(CalendarView.Month, range, hiddenViews),
|
||||
today,
|
||||
) { calendars, instances, today ->
|
||||
buildContinuousState(window, ws, style, calendars, instances, today)
|
||||
|
||||
@@ -89,7 +89,7 @@ internal fun CalendarsStep(
|
||||
initialName = "",
|
||||
initialColor = CalendarColorPalette.all.first(),
|
||||
initialDescription = "",
|
||||
onSave = { name, color, description ->
|
||||
onSave = { name, color, description, _ ->
|
||||
if (!submitted) {
|
||||
submitted = true
|
||||
onCreateLocal(name, color, description)
|
||||
|
||||
@@ -19,7 +19,9 @@ import de.jeanlucmakiola.calendula.domain.displayPriority
|
||||
import de.jeanlucmakiola.calendula.domain.displayStart
|
||||
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.PageStateCache
|
||||
import de.jeanlucmakiola.calendula.ui.common.instancesIn
|
||||
import de.jeanlucmakiola.floret.android.stateWhileSubscribed
|
||||
import de.jeanlucmakiola.floret.di.IoDispatcher
|
||||
import kotlinx.coroutines.CoroutineDispatcher
|
||||
@@ -89,8 +91,11 @@ abstract class PagedTimelineViewModel(
|
||||
private val io: CoroutineDispatcher,
|
||||
gridRule: Flow<(LocalDate) -> DaysGrid>,
|
||||
initialRule: (LocalDate) -> DaysGrid,
|
||||
private val view: CalendarView,
|
||||
) : ViewModel() {
|
||||
|
||||
private val hiddenViews = settingsPrefs.perCalendarHiddenViews
|
||||
|
||||
/**
|
||||
* The zone every page is laid out in; drags and drops resolve in it too, and
|
||||
* pages lay out again when the device moves zones (#387).
|
||||
@@ -167,7 +172,7 @@ abstract class PagedTimelineViewModel(
|
||||
zone.flatMapLatest { zone ->
|
||||
combine(
|
||||
repository.calendars(),
|
||||
repository.instances(spanRange(span.start, span.count, zone)),
|
||||
repository.instancesIn(view, spanRange(span.start, span.count, zone), hiddenViews),
|
||||
today,
|
||||
) { calendars, instances, today ->
|
||||
buildState(span, calendars, instances, today, zone)
|
||||
@@ -240,6 +245,7 @@ class WeekViewModel @Inject constructor(
|
||||
rule
|
||||
},
|
||||
initialRule = { DaysGrid.weeks(DayOfWeek.MONDAY) },
|
||||
view = CalendarView.Week,
|
||||
)
|
||||
|
||||
/**
|
||||
@@ -264,6 +270,7 @@ class MultiDayViewModel @Inject constructor(
|
||||
{ anchor: LocalDate -> DaysGrid.startingAt(anchor, count) }
|
||||
},
|
||||
initialRule = { DaysGrid.startingAt(it, SettingsPrefs.DEFAULT_MULTI_DAY_COUNT) },
|
||||
view = CalendarView.MultiDay,
|
||||
)
|
||||
|
||||
/** The day view: the timeline one day at a time. */
|
||||
@@ -282,6 +289,7 @@ class DayViewModel @Inject constructor(
|
||||
io = io,
|
||||
gridRule = flowOf { _: LocalDate -> DaysGrid.days },
|
||||
initialRule = { DaysGrid.days },
|
||||
view = CalendarView.Day,
|
||||
)
|
||||
|
||||
/**
|
||||
|
||||
@@ -11,7 +11,6 @@ import androidx.glance.GlanceId
|
||||
import androidx.glance.appwidget.state.getAppWidgetState
|
||||
import androidx.glance.state.PreferencesGlanceStateDefinition
|
||||
import de.jeanlucmakiola.calendula.data.prefs.PastEventDisplay
|
||||
import de.jeanlucmakiola.calendula.domain.EventInstance
|
||||
import de.jeanlucmakiola.calendula.ui.agenda.AgendaRange
|
||||
import de.jeanlucmakiola.calendula.ui.agenda.parseAgendaRange
|
||||
import de.jeanlucmakiola.calendula.ui.agenda.storageValue
|
||||
@@ -116,8 +115,5 @@ internal fun MutablePreferences.putHiddenCalendars(ids: Set<Long>) {
|
||||
this[HiddenCalendarsKey] = ids.map(Long::toString).toSet()
|
||||
}
|
||||
|
||||
internal fun List<EventInstance>.withoutCalendars(hidden: Set<Long>): List<EventInstance> =
|
||||
if (hidden.isEmpty()) this else filterNot { it.calendarId in hidden }
|
||||
|
||||
internal suspend fun Context.widgetState(id: GlanceId): Preferences =
|
||||
getAppWidgetState(this, PreferencesGlanceStateDefinition, id)
|
||||
@@ -1,5 +1,6 @@
|
||||
package de.jeanlucmakiola.calendula.widget
|
||||
|
||||
import de.jeanlucmakiola.calendula.ui.common.withoutCalendars
|
||||
import android.Manifest
|
||||
import android.content.Context
|
||||
import android.content.pm.PackageManager
|
||||
|
||||
@@ -22,7 +22,7 @@ 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.widget.withoutCalendars
|
||||
import de.jeanlucmakiola.calendula.ui.common.withoutCalendars
|
||||
import de.jeanlucmakiola.floret.prefs.toEnum
|
||||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.datetime.LocalDate
|
||||
|
||||
@@ -863,6 +863,11 @@
|
||||
<string name="calendars_color_label">Color</string>
|
||||
<string name="calendars_description_label">Description</string>
|
||||
<string name="calendars_description_hint">Add a description</string>
|
||||
<string name="calendars_hide_in_views">Hide in views</string>
|
||||
<string name="calendars_hide_in_views_hint">Events from “%1$s” stay out of the views checked here. Reminders, search and widgets still include them.</string>
|
||||
<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="color_name_mauve">Mauve</string>
|
||||
<string name="color_name_red">Red</string>
|
||||
<string name="color_name_orange">Orange</string>
|
||||
|
||||
@@ -616,6 +616,49 @@ class SettingsPrefsTest {
|
||||
assertThat(prefs.perCalendarEventDuration.first()).containsExactly(9L, 45)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `per-calendar hidden views round-trip and clear back to every view`(
|
||||
@TempDir tempDir: Path,
|
||||
) = runTest {
|
||||
val prefs = testSettings(tempDir)
|
||||
assertThat(prefs.perCalendarHiddenViews.first()).isEmpty()
|
||||
prefs.setCalendarHiddenViews(7L, setOf(CalendarView.Month, CalendarView.Agenda))
|
||||
prefs.setCalendarHiddenViews(9L, setOf(CalendarView.MultiDay))
|
||||
assertThat(prefs.perCalendarHiddenViews.first()).containsExactly(
|
||||
7L, setOf(CalendarView.Month, CalendarView.Agenda),
|
||||
9L, setOf(CalendarView.MultiDay),
|
||||
)
|
||||
prefs.setCalendarHiddenViews(7L, emptySet())
|
||||
assertThat(prefs.perCalendarHiddenViews.first()).containsExactly(9L, setOf(CalendarView.MultiDay))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `garbage hidden-views entries are dropped, the rest survive`(
|
||||
@TempDir tempDir: Path,
|
||||
) = runTest {
|
||||
val store = testDataStore(tempDir, "test_settings")
|
||||
val prefs = SettingsPrefs(store)
|
||||
store.updateData { p ->
|
||||
val m = p.toMutablePreferences()
|
||||
// A bad id, only an unknown view, no separator, and a good entry with one unknown view.
|
||||
m[stringPreferencesKey("per_calendar_hidden_views")] = "x=Month;7=Year;8;9=Month,Year,Agenda"
|
||||
m
|
||||
}
|
||||
assertThat(prefs.perCalendarHiddenViews.first())
|
||||
.containsExactly(9L, setOf(CalendarView.Month, CalendarView.Agenda))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `hidden views serialise in view order and drop empty entries`() {
|
||||
val stored = mapOf(
|
||||
3L to setOf(CalendarView.Agenda, CalendarView.Month),
|
||||
1L to emptySet(),
|
||||
).toStoredHiddenViews()
|
||||
assertThat(stored).isEqualTo("3=Month,Agenda")
|
||||
assertThat(emptyMap<Long, Set<CalendarView>>().toStoredHiddenViews()).isNull()
|
||||
assertThat(parseHiddenViews(stored)).containsExactly(3L, setOf(CalendarView.Month, CalendarView.Agenda))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `resolveDefaultEventDuration prefers the calendar's own length`() {
|
||||
val overrides = mapOf(7L to 480)
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
package de.jeanlucmakiola.calendula.ui.common
|
||||
|
||||
import app.cash.turbine.test
|
||||
import com.google.common.truth.Truth.assertThat
|
||||
import de.jeanlucmakiola.calendula.data.calendar.FakeCalendarDataSource
|
||||
import de.jeanlucmakiola.calendula.domain.CalendarSource
|
||||
import de.jeanlucmakiola.calendula.testEvent
|
||||
import de.jeanlucmakiola.calendula.testRepository
|
||||
import de.jeanlucmakiola.calendula.testSettings
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.coroutines.test.UnconfinedTestDispatcher
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.junit.jupiter.api.io.TempDir
|
||||
import java.nio.file.Path
|
||||
import kotlin.time.Instant
|
||||
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
class ViewHiddenCalendarsTest {
|
||||
|
||||
private val range = Instant.fromEpochMilliseconds(0)..Instant.fromEpochMilliseconds(10_000_000_000L)
|
||||
|
||||
private fun event(id: Long, calendarId: Long) = testEvent(
|
||||
id = id,
|
||||
calendarId = calendarId,
|
||||
title = "E $id",
|
||||
start = Instant.fromEpochMilliseconds(1_000_000_000L),
|
||||
end = Instant.fromEpochMilliseconds(1_000_003_600L),
|
||||
color = 0,
|
||||
)
|
||||
|
||||
private fun calendar(id: Long) = CalendarSource(id, "Cal $id", "x@y", "LOCAL", 0, true)
|
||||
|
||||
@Test
|
||||
fun `calendarsHiddenIn picks only the calendars hidden in that view`() {
|
||||
val map = mapOf(
|
||||
1L to setOf(CalendarView.Month),
|
||||
2L to setOf(CalendarView.Month, CalendarView.Agenda),
|
||||
3L to setOf(CalendarView.Week),
|
||||
)
|
||||
assertThat(map.calendarsHiddenIn(CalendarView.Month)).containsExactly(1L, 2L)
|
||||
assertThat(map.calendarsHiddenIn(CalendarView.Agenda)).containsExactly(2L)
|
||||
assertThat(map.calendarsHiddenIn(CalendarView.Day)).isEmpty()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `instancesIn 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)) }
|
||||
}
|
||||
val settings = testSettings(tempDir)
|
||||
val repo = testRepository(fake, tempDir, UnconfinedTestDispatcher(testScheduler), settings = settings)
|
||||
settings.setCalendarHiddenViews(2L, setOf(CalendarView.Month))
|
||||
val hidden = settings.perCalendarHiddenViews
|
||||
|
||||
assertThat(repo.instancesIn(CalendarView.Month, range, hidden).first().map { it.eventId })
|
||||
.containsExactly(10L)
|
||||
assertThat(repo.instancesIn(CalendarView.Week, range, hidden).first().map { it.eventId })
|
||||
.containsExactly(10L, 20L)
|
||||
// The repository itself, which reminders and search build on, is untouched.
|
||||
assertThat(repo.instances(range).first().map { it.eventId }).containsExactly(10L, 20L)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `instancesIn 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)) }
|
||||
}
|
||||
val settings = testSettings(tempDir)
|
||||
val repo = testRepository(fake, tempDir, UnconfinedTestDispatcher(testScheduler), settings = settings)
|
||||
|
||||
repo.instancesIn(CalendarView.Agenda, range, settings.perCalendarHiddenViews).test {
|
||||
assertThat(awaitItem().map { it.eventId }).containsExactly(10L, 20L)
|
||||
settings.setCalendarHiddenViews(1L, setOf(CalendarView.Agenda))
|
||||
assertThat(awaitItem().map { it.eventId }).containsExactly(20L)
|
||||
settings.setCalendarHiddenViews(1L, emptySet())
|
||||
assertThat(awaitItem().map { it.eventId }).containsExactly(10L, 20L)
|
||||
cancelAndIgnoreRemainingEvents()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -389,7 +389,11 @@ pre-permission launch); `CalendarVisibilityReconciler` drains it entry by entry,
|
||||
and until it does, the repository and `ReminderNotifier.post` honour it. That
|
||||
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.
|
||||
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
|
||||
the repository and never see it.
|
||||
|
||||
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 —
|
||||
|
||||
+1
-1
Submodule floret-kit updated: a039ab5bd4...089245a4af.
Reference in new issue
Block a user