diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/data/prefs/CalendarPrefs.kt b/app/src/main/java/de/jeanlucmakiola/calendula/data/prefs/CalendarPrefs.kt index ca0e15f5..2cab30a7 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/data/prefs/CalendarPrefs.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/data/prefs/CalendarPrefs.kt @@ -34,6 +34,11 @@ class CalendarPrefs @Inject constructor( suspend fun setHiddenCalendarIds(ids: Set) = prefs.set(HIDDEN_IDS, ids) + /** Rewrite the drawer's hidden set atomically from its current value. */ + suspend fun updateHiddenCalendarIds(transform: (Set) -> Set) { + prefs.update(HIDDEN_IDS, transform) + } + /** * Switch-offs the provider does not know about yet, because writing * `Calendars.VISIBLE` needs `WRITE_CALENDAR` (#75). Also inherits the diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/ui/filter/CalendarFilterList.kt b/app/src/main/java/de/jeanlucmakiola/calendula/ui/filter/CalendarFilterList.kt index 4a3688d3..634a09db 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/ui/filter/CalendarFilterList.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/ui/filter/CalendarFilterList.kt @@ -1,6 +1,9 @@ package de.jeanlucmakiola.calendula.ui.filter import androidx.compose.foundation.background +import androidx.compose.foundation.clickable +import androidx.compose.foundation.layout.wrapContentHeight +import androidx.compose.foundation.layout.heightIn import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Column @@ -20,8 +23,11 @@ import androidx.compose.runtime.getValue import androidx.compose.runtime.remember import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier +import androidx.compose.ui.draw.clip import androidx.compose.ui.res.pluralStringResource import androidx.compose.ui.res.stringResource +import androidx.compose.ui.semantics.Role +import androidx.compose.ui.state.ToggleableState import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.unit.dp import androidx.hilt.navigation.compose.hiltViewModel @@ -65,7 +71,8 @@ fun CalendarFilterList( hiddenInView = hiddenInView, hiddenOnlyHere = hiddenInView.size, onHideInView = onHideInView, - onSetVisible = viewModel::setVisible, + onToggleCalendar = viewModel::toggleCalendar, + onToggleGroup = viewModel::toggleGroup, modifier = modifier, ) } @@ -84,21 +91,17 @@ private fun FilterList( hiddenInView: Set, hiddenOnlyHere: Int, onHideInView: () -> Unit, - onSetVisible: (Long, Boolean) -> Unit, + onToggleCalendar: (Long) -> Unit, + onToggleGroup: (List) -> Unit, modifier: Modifier = Modifier, ) { Column(modifier = modifier.fillMaxWidth()) { groups.forEach { group -> - Text( - text = accountGroupTitle(group), - style = MaterialTheme.typography.labelMedium, - color = MaterialTheme.colorScheme.onSurfaceVariant, - modifier = Modifier.padding( - start = GroupedListInset, - end = GroupedListInset, - top = 12.dp, - bottom = 4.dp, - ), + val ids = group.calendars.map { it.id } + GroupHeader( + title = accountGroupTitle(group), + state = groupVisibility(ids, hidden), + onToggle = { onToggleGroup(ids) }, ) group.calendars.forEachIndexed { index, cal -> val visible = cal.id !in hidden @@ -111,10 +114,10 @@ private fun FilterList( trailing = { Checkbox( checked = visible, - onCheckedChange = { onSetVisible(cal.id, it) }, + onCheckedChange = { onToggleCalendar(cal.id) }, ) }, - onClick = { onSetVisible(cal.id, !visible) }, + onClick = { onToggleCalendar(cal.id) }, ) } } @@ -142,6 +145,36 @@ private fun FilterList( } } +/** + * An account's header doubling as the group's parent checkbox (#143): checked + * when every calendar shows, indeterminate when some do. Tapping it shows all, + * or hides all when the group is fully shown. + */ +@Composable +private fun GroupHeader( + title: String, + state: ToggleableState, + onToggle: () -> Unit, +) { + val label = stringResource( + if (state.nextGroupVisible()) R.string.filter_group_show_all else R.string.filter_group_hide_all, + ) + // The header itself is the toggle; the rows' checkboxes show the result. + Text( + text = title, + style = MaterialTheme.typography.labelMedium, + color = MaterialTheme.colorScheme.onSurfaceVariant, + modifier = Modifier + .fillMaxWidth() + .padding(horizontal = GroupedListInset / 2) + .clip(MaterialTheme.shapes.large) + .clickable(onClickLabel = label, role = Role.Button) { onToggle() } + .heightIn(min = 48.dp) + .wrapContentHeight(Alignment.CenterVertically) + .padding(horizontal = GroupedListInset / 2), + ) +} + @Composable internal fun FilterLoading(modifier: Modifier = Modifier) { Column( diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/ui/filter/FilterViewModel.kt b/app/src/main/java/de/jeanlucmakiola/calendula/ui/filter/FilterViewModel.kt index a1f89a9a..6528da11 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/ui/filter/FilterViewModel.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/ui/filter/FilterViewModel.kt @@ -19,7 +19,6 @@ import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.catch import kotlinx.coroutines.flow.combine -import kotlinx.coroutines.flow.first import kotlinx.coroutines.flow.flowOn import kotlinx.coroutines.launch import javax.inject.Inject @@ -69,13 +68,19 @@ class FilterViewModel @Inject constructor( } } - /** Show or hide a single calendar; persists the new hidden set. */ - fun setVisible(calendarId: Long, visible: Boolean) { + /** + * Flip one calendar, or a whole group from its header (#143). Both decide + * against the stored set rather than the last drawn state, so quick repeated + * taps still alternate. + */ + fun toggleCalendar(calendarId: Long) { viewModelScope.launch { - val current = prefs.hiddenCalendarIds.first() - val next = if (visible) current - calendarId else current + calendarId - if (next != current) prefs.setHiddenCalendarIds(next) + prefs.updateHiddenCalendarIds { if (calendarId in it) it - calendarId else it + calendarId } } } + + fun toggleGroup(calendarIds: List) { + viewModelScope.launch { prefs.updateHiddenCalendarIds { toggledGroup(it, calendarIds) } } + } } diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/ui/filter/GroupVisibility.kt b/app/src/main/java/de/jeanlucmakiola/calendula/ui/filter/GroupVisibility.kt new file mode 100644 index 00000000..3994bc9d --- /dev/null +++ b/app/src/main/java/de/jeanlucmakiola/calendula/ui/filter/GroupVisibility.kt @@ -0,0 +1,20 @@ +package de.jeanlucmakiola.calendula.ui.filter + +import androidx.compose.ui.state.ToggleableState + +/** The drawer group header's checkbox state: on, off, or some of [calendarIds] shown. */ +internal fun groupVisibility(calendarIds: Collection, hidden: Set): ToggleableState { + val shown = calendarIds.count { it !in hidden } + return when (shown) { + 0 -> ToggleableState.Off + calendarIds.size -> ToggleableState.On + else -> ToggleableState.Indeterminate + } +} + +/** 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, calendarIds: Collection): Set = + if (groupVisibility(calendarIds, hidden).nextGroupVisible()) hidden - calendarIds.toSet() else hidden + calendarIds diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 59a98ae1..9e30f19f 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -569,6 +569,9 @@ Calendars + + Show all calendars in this group + Hide all calendars in this group Settings diff --git a/app/src/test/java/de/jeanlucmakiola/calendula/data/prefs/CalendarPrefsTest.kt b/app/src/test/java/de/jeanlucmakiola/calendula/data/prefs/CalendarPrefsTest.kt index 8b0010b0..5a95a37d 100644 --- a/app/src/test/java/de/jeanlucmakiola/calendula/data/prefs/CalendarPrefsTest.kt +++ b/app/src/test/java/de/jeanlucmakiola/calendula/data/prefs/CalendarPrefsTest.kt @@ -35,6 +35,16 @@ class CalendarPrefsTest { assertThat(prefs.hiddenCalendarIds.first()).isEmpty() } + @Test + fun `updateHiddenCalendarIds transforms the stored set`(@TempDir tempDir: Path) = runTest { + val prefs = testPrefs(tempDir) + prefs.setHiddenCalendarIds(setOf(9L)) + prefs.updateHiddenCalendarIds { it + listOf(1L, 2L) } + assertThat(prefs.hiddenCalendarIds.first()).isEqualTo(setOf(1L, 2L, 9L)) + prefs.updateHiddenCalendarIds { emptySet() } + assertThat(prefs.hiddenCalendarIds.first()).isEmpty() + } + @Test fun `garbage stored string is parsed defensively`(@TempDir tempDir: Path) = runTest { val store = testDataStore(tempDir, "test_prefs") diff --git a/app/src/test/java/de/jeanlucmakiola/calendula/ui/filter/GroupVisibilityTest.kt b/app/src/test/java/de/jeanlucmakiola/calendula/ui/filter/GroupVisibilityTest.kt new file mode 100644 index 00000000..6e0cd4f6 --- /dev/null +++ b/app/src/test/java/de/jeanlucmakiola/calendula/ui/filter/GroupVisibilityTest.kt @@ -0,0 +1,49 @@ +package de.jeanlucmakiola.calendula.ui.filter + +import androidx.compose.ui.state.ToggleableState +import com.google.common.truth.Truth.assertThat +import org.junit.jupiter.api.Test + +class GroupVisibilityTest { + + private val group = listOf(1L, 2L, 3L) + + @Test + fun `all shown is on and hides on tap`() { + val state = groupVisibility(group, hidden = setOf(9L)) + assertThat(state).isEqualTo(ToggleableState.On) + assertThat(state.nextGroupVisible()).isFalse() + } + + @Test + fun `none shown is off and shows on tap`() { + val state = groupVisibility(group, hidden = setOf(1L, 2L, 3L)) + assertThat(state).isEqualTo(ToggleableState.Off) + assertThat(state.nextGroupVisible()).isTrue() + } + + @Test + fun `some shown is indeterminate and shows all on tap`() { + val state = groupVisibility(group, hidden = setOf(2L)) + assertThat(state).isEqualTo(ToggleableState.Indeterminate) + assertThat(state.nextGroupVisible()).isTrue() + } + + @Test + 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)) + } +}