fix(#143): review round 2 — decide group toggle from the stored set
This commit is contained in:
1 parent
22173be22e
commit
4186640a63
5 files changed
+37
-11
No files matched your search
@@ -33,8 +33,12 @@ class CalendarPrefs @Inject constructor(
|
||||
suspend fun setHiddenCalendarIds(ids: Set<Long>) = prefs.set(HIDDEN_IDS, ids)
|
||||
|
||||
/** Hide or show [ids] in the drawer filter in one atomic write. */
|
||||
suspend fun setCalendarsHidden(ids: Collection<Long>, hidden: Boolean) {
|
||||
prefs.update(HIDDEN_IDS) { if (hidden) it + ids else it - ids.toSet() }
|
||||
suspend fun setCalendarsHidden(ids: Collection<Long>, hidden: Boolean) =
|
||||
updateHiddenCalendarIds { if (hidden) it + ids else it - ids.toSet() }
|
||||
|
||||
/** Rewrite the drawer's hidden set atomically from its current value. */
|
||||
suspend fun updateHiddenCalendarIds(transform: (Set<Long>) -> Set<Long>) {
|
||||
prefs.update(HIDDEN_IDS, transform)
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -57,7 +57,7 @@ fun CalendarFilterList(
|
||||
groups = s.groups,
|
||||
hidden = s.hidden,
|
||||
onSetVisible = viewModel::setVisible,
|
||||
onSetGroupVisible = viewModel::setGroupVisible,
|
||||
onToggleGroup = viewModel::toggleGroup,
|
||||
modifier = modifier,
|
||||
)
|
||||
}
|
||||
@@ -73,7 +73,7 @@ private fun FilterList(
|
||||
groups: List<CalendarAccountGroup>,
|
||||
hidden: Set<Long>,
|
||||
onSetVisible: (Long, Boolean) -> Unit,
|
||||
onSetGroupVisible: (List<Long>, Boolean) -> Unit,
|
||||
onToggleGroup: (List<Long>) -> Unit,
|
||||
modifier: Modifier = Modifier,
|
||||
) {
|
||||
Column(modifier = modifier.fillMaxWidth()) {
|
||||
@@ -82,7 +82,7 @@ private fun FilterList(
|
||||
GroupHeader(
|
||||
title = accountGroupTitle(group),
|
||||
state = groupVisibility(ids, hidden),
|
||||
onToggle = { visible -> onSetGroupVisible(ids, visible) },
|
||||
onToggle = { onToggleGroup(ids) },
|
||||
)
|
||||
group.calendars.forEachIndexed { index, cal ->
|
||||
val visible = cal.id !in hidden
|
||||
@@ -113,7 +113,7 @@ private fun FilterList(
|
||||
private fun GroupHeader(
|
||||
title: String,
|
||||
state: ToggleableState,
|
||||
onToggle: (Boolean) -> Unit,
|
||||
onToggle: () -> Unit,
|
||||
) {
|
||||
val label = stringResource(
|
||||
if (state.nextGroupVisible()) R.string.filter_group_show_all else R.string.filter_group_hide_all,
|
||||
@@ -124,7 +124,7 @@ private fun GroupHeader(
|
||||
.fillMaxWidth()
|
||||
.padding(horizontal = GroupedListInset / 2)
|
||||
.clip(MaterialTheme.shapes.large)
|
||||
.clickable(onClickLabel = label, role = Role.Checkbox) { onToggle(state.nextGroupVisible()) }
|
||||
.clickable(onClickLabel = label, role = Role.Checkbox) { onToggle() }
|
||||
.semantics { toggleableState = state }
|
||||
.padding(start = GroupedListInset / 2, end = GroupedListInset / 2 + 16.dp),
|
||||
) {
|
||||
|
||||
@@ -48,11 +48,16 @@ class FilterViewModel @Inject constructor(
|
||||
)
|
||||
|
||||
/** Show or hide a single calendar; persists the new hidden set. */
|
||||
fun setVisible(calendarId: Long, visible: Boolean) = setGroupVisible(listOf(calendarId), visible)
|
||||
fun setVisible(calendarId: Long, visible: Boolean) {
|
||||
viewModelScope.launch { prefs.setCalendarsHidden(listOf(calendarId), hidden = !visible) }
|
||||
}
|
||||
|
||||
/** Show or hide every calendar in [calendarIds] at once (#143). */
|
||||
fun setGroupVisible(calendarIds: Collection<Long>, visible: Boolean) {
|
||||
viewModelScope.launch { prefs.setCalendarsHidden(calendarIds, hidden = !visible) }
|
||||
/**
|
||||
* The group header's tap (#143), decided against the stored set rather than
|
||||
* the last drawn state so quick repeated taps still alternate.
|
||||
*/
|
||||
fun toggleGroup(calendarIds: List<Long>) {
|
||||
viewModelScope.launch { prefs.updateHiddenCalendarIds { toggledGroup(it, calendarIds) } }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,3 +14,7 @@ internal fun groupVisibility(calendarIds: Collection<Long>, hidden: Set<Long>):
|
||||
|
||||
/** What tapping the header does: a fully shown group hides, anything else shows all. */
|
||||
internal fun ToggleableState.nextGroupVisible(): Boolean = this != ToggleableState.On
|
||||
|
||||
/** [hidden] after tapping the header of the group holding [calendarIds]. */
|
||||
internal fun toggledGroup(hidden: Set<Long>, calendarIds: Collection<Long>): Set<Long> =
|
||||
if (groupVisibility(calendarIds, hidden).nextGroupVisible()) hidden - calendarIds.toSet() else hidden + calendarIds
|
||||
@@ -33,4 +33,17 @@ class GroupVisibilityTest {
|
||||
fun `empty group is off`() {
|
||||
assertThat(groupVisibility(emptyList(), hidden = emptySet())).isEqualTo(ToggleableState.Off)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `toggling twice from the stored set alternates`() {
|
||||
val hidden = setOf(9L)
|
||||
val once = toggledGroup(hidden, group)
|
||||
assertThat(once).isEqualTo(setOf(1L, 2L, 3L, 9L))
|
||||
assertThat(toggledGroup(once, group)).isEqualTo(setOf(9L))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `toggling a partly shown group shows all`() {
|
||||
assertThat(toggledGroup(setOf(2L, 9L), group)).isEqualTo(setOf(9L))
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user