feat(#143): toggle a whole calendar group from its drawer header

This commit is contained in:
makiolaj committed 2026-11-13 19:07:41 +01:00
1 parent e9cf7d66ba
commit 60166b349d
6 files changed
+112 -17

No files matched your search

@@ -32,6 +32,11 @@ 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() }
}
/**
* Switch-offs the provider does not know about yet, because writing
* `Calendars.VISIBLE` needs `WRITE_CALENDAR` (#75). Also inherits the
@@ -1,19 +1,28 @@
package de.jeanlucmakiola.calendula.ui.filter
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material3.Checkbox
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.TriStateCheckbox
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.semantics.Role
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.semantics.toggleableState
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
@@ -47,6 +56,7 @@ fun CalendarFilterList(
groups = s.groups,
hidden = s.hidden,
onSetVisible = viewModel::setVisible,
onSetGroupVisible = viewModel::setGroupVisible,
modifier = modifier,
)
}
@@ -62,20 +72,15 @@ private fun FilterList(
groups: List<CalendarAccountGroup>,
hidden: Set<Long>,
onSetVisible: (Long, Boolean) -> Unit,
onSetGroupVisible: (List<Long>, Boolean) -> 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,
),
GroupHeader(
title = accountGroupTitle(group),
state = groupVisibility(group.calendars.map { it.id }, hidden),
onToggle = { visible -> onSetGroupVisible(group.calendars.map { it.id }, visible) },
)
group.calendars.forEachIndexed { index, cal ->
val visible = cal.id !in hidden
@@ -97,6 +102,43 @@ 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: (Boolean) -> Unit,
) {
val label = stringResource(
if (state.nextGroupVisible()) R.string.filter_group_show_all else R.string.filter_group_hide_all,
)
Row(
verticalAlignment = Alignment.CenterVertically,
modifier = Modifier
.fillMaxWidth()
.padding(top = 4.dp)
.clickable(onClickLabel = label, role = Role.Checkbox) { onToggle(state.nextGroupVisible()) }
.semantics { toggleableState = state }
.padding(start = GroupedListInset, end = GroupedListInset + 16.dp),
) {
Text(
text = title,
style = MaterialTheme.typography.labelMedium,
color = MaterialTheme.colorScheme.onSurfaceVariant,
modifier = Modifier
.weight(1f)
.padding(end = 8.dp),
)
Box(Modifier.size(48.dp), contentAlignment = Alignment.Center) {
TriStateCheckbox(state = state, onClick = null)
}
}
}
@Composable
private fun FilterLoading(modifier: Modifier = Modifier) {
Column(
@@ -14,7 +14,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
@@ -49,12 +48,11 @@ class FilterViewModel @Inject constructor(
)
/** Show or hide a single calendar; persists the new hidden set. */
fun setVisible(calendarId: Long, visible: Boolean) {
viewModelScope.launch {
val current = prefs.hiddenCalendarIds.first()
val next = if (visible) current - calendarId else current + calendarId
if (next != current) prefs.setHiddenCalendarIds(next)
}
fun setVisible(calendarId: Long, visible: Boolean) = setGroupVisible(listOf(calendarId), 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) }
}
}
@@ -0,0 +1,16 @@
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. */
fun groupVisibility(calendarIds: Collection<Long>, hidden: Set<Long>): ToggleableState {
val shown = calendarIds.count { it !in hidden }
return when (shown) {
calendarIds.size -> ToggleableState.On
0 -> ToggleableState.Off
else -> ToggleableState.Indeterminate
}
}
/** What tapping the header does: a fully shown group hides, anything else shows all. */
fun ToggleableState.nextGroupVisible(): Boolean = this != ToggleableState.On
+3
View File
@@ -550,6 +550,9 @@
<!-- Calendar filter (M3) -->
<string name="filter_title">Calendars</string>
<!-- TalkBack action on a drawer account header that toggles all its calendars -->
<string name="filter_group_show_all">Show all calendars in this group</string>
<string name="filter_group_hide_all">Hide all calendars in this group</string>
<!-- Settings (M4) -->
<string name="settings_title">Settings</string>
@@ -0,0 +1,31 @@
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()
}
}