diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/ui/settings/CalendarReminderSettings.kt b/app/src/main/java/de/jeanlucmakiola/calendula/ui/settings/CalendarReminderSettings.kt new file mode 100644 index 00000000..22e79cc8 --- /dev/null +++ b/app/src/main/java/de/jeanlucmakiola/calendula/ui/settings/CalendarReminderSettings.kt @@ -0,0 +1,436 @@ +package de.jeanlucmakiola.calendula.ui.settings + +import androidx.compose.animation.AnimatedVisibility +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.Spacer +import androidx.compose.foundation.layout.height +import androidx.compose.foundation.layout.padding +import androidx.compose.material.icons.Icons +import androidx.compose.material.icons.automirrored.filled.KeyboardArrowRight +import androidx.compose.material3.Icon +import androidx.compose.material3.MaterialTheme +import androidx.compose.runtime.Composable +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.rememberCoroutineScope +import androidx.compose.runtime.saveable.rememberSaveable +import androidx.compose.runtime.setValue +import androidx.compose.ui.Modifier +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.unit.dp +import androidx.lifecycle.compose.LifecycleResumeEffect +import androidx.lifecycle.compose.collectAsStateWithLifecycle +import de.jeanlucmakiola.calendula.R +import de.jeanlucmakiola.calendula.data.reminders.RING_CHANNEL_ID +import de.jeanlucmakiola.calendula.domain.CalendarSource +import de.jeanlucmakiola.calendula.domain.reminders.ReminderAlertStyle +import de.jeanlucmakiola.calendula.domain.reminders.alertStyleFor +import de.jeanlucmakiola.calendula.ui.common.CalendarColorChip +import de.jeanlucmakiola.calendula.ui.common.REMINDER_PRESETS +import de.jeanlucmakiola.calendula.ui.common.ReminderDefaultPicker +import de.jeanlucmakiola.calendula.ui.common.accountGroupTitle +import de.jeanlucmakiola.calendula.ui.common.groupByAccount +import de.jeanlucmakiola.calendula.ui.common.orUnnamed +import de.jeanlucmakiola.floret.android.openNotificationSettings +import de.jeanlucmakiola.floret.components.CollapsingScaffold +import de.jeanlucmakiola.floret.components.ExpandIcon +import de.jeanlucmakiola.floret.components.GroupedHint +import de.jeanlucmakiola.floret.components.GroupedRow +import de.jeanlucmakiola.floret.components.GroupedSectionHeader +import de.jeanlucmakiola.floret.components.GroupedSwitchRow +import de.jeanlucmakiola.floret.components.OptionPicker +import de.jeanlucmakiola.floret.components.Position +import de.jeanlucmakiola.floret.components.positionOf +import de.jeanlucmakiola.floret.identity.collapseExit +import de.jeanlucmakiola.floret.identity.expandEnter +import de.jeanlucmakiola.floret.reminders.ReminderOverride +import de.jeanlucmakiola.floret.reminders.reminderOverrideFor +import de.jeanlucmakiola.floret.ring.canUseFullScreenIntent +import de.jeanlucmakiola.floret.ring.openFullScreenIntentSettings +import kotlinx.coroutines.launch + +/** + * Per-calendar notifications and reminder overrides (#145, #272), reached from + * Settings → Notifications. Calendars are listed by account as connected groups; + * a calendar unfolds its options in place, one tone darker, inside its group. + */ +@Composable +internal fun CalendarRemindersScreen( + state: SettingsUiState, + viewModel: SettingsViewModel, + onBack: () -> Unit, + onOpenSpecialDates: () -> Unit, + visible: Boolean = true, +) { + val context = LocalContext.current + val scope = rememberCoroutineScope() + var expandedCalendars by rememberSaveable { mutableStateOf(emptyList()) } + var overrideDialog by remember { mutableStateOf(null) } + var alertStylePicker by remember { mutableStateOf(null) } + var alertSoundPicker by remember { mutableStateOf(null) } + val canFullScreen = rememberCanUseFullScreenIntent() + val channelStatus by viewModel.channelStatus.collectAsStateWithLifecycle() + val calendarIds = state.reminderCalendars.map { it.id } + LifecycleResumeEffect(calendarIds) { + viewModel.refreshChannelStatus(calendarIds) + onPauseOrDispose { } + } + + CollapsingScaffold( + title = stringResource(R.string.settings_calendar_reminders_title), + onBack = onBack, + predictiveBack = true, + visible = visible, + ) { + GroupedHint(stringResource(R.string.settings_calendar_reminders_hint), style = MaterialTheme.typography.bodySmall) + state.reminderCalendars.groupByAccount().forEachIndexed { groupIndex, group -> + if (groupIndex > 0) Spacer(Modifier.height(8.dp)) + GroupedSectionHeader(accountGroupTitle(group)) + val calendars = group.calendars + calendars.forEachIndexed { index, calendar -> + val expanded = calendar.id in expandedCalendars + val notifying = calendar.id !in state.mutedReminderCalendarIds + val blocked = notifying && calendar.id in channelStatus.blockedCalendarIds + CalendarReminderEntry( + calendar = calendar, + state = state, + position = positionOf(index, calendars.size), + expanded = expanded, + notifying = notifying, + blocked = blocked, + heldBack = fullScreenHeldBack( + style = state.reminderAlertStyles.alertStyleFor(calendar.id), + notifying = notifying, + blocked = blocked, + ringBlocked = channelStatus.ringBlocked, + canFullScreen = canFullScreen, + ), + onToggleExpand = { + expandedCalendars = if (expanded) { + expandedCalendars - calendar.id + } else { + expandedCalendars + calendar.id + } + }, + onSetNotifying = { viewModel.setCalendarRemindersEnabled(calendar.id, it) }, + onOpenSound = { + scope.launch { + context.openNotificationSettings(viewModel.soundSettingsChannel(calendar.id)) + } + }, + onOpenRingSettings = { context.openNotificationSettings(RING_CHANNEL_ID) }, + onEditAlertStyle = { alertStylePicker = calendar.id }, + onEditAlertSound = { alertSoundPicker = calendar.id }, + onAllowFullScreen = { context.openFullScreenIntentSettings() }, + onOpenSpecialDates = onOpenSpecialDates, + onEditOverride = { overrideDialog = it }, + ) + } + } + } + + alertStylePicker?.let { calendarId -> + val current = state.reminderAlertStyles.alertStyleFor(calendarId) + OptionPicker( + title = stringResource(R.string.settings_alert_style), + options = listOf(ReminderAlertStyle.Notification, ReminderAlertStyle.FullScreen), + selected = if (current.isFullScreen) ReminderAlertStyle.FullScreen else ReminderAlertStyle.Notification, + label = { alertStyleLabel(it) }, + summary = { + stringResource( + if (it.isFullScreen) { + R.string.settings_alert_style_full_screen_summary + } else { + R.string.settings_alert_style_notification_summary + }, + ) + }, + onSelect = { picked -> + if (picked.isFullScreen != current.isFullScreen) { + viewModel.setCalendarAlertStyle(calendarId, picked) + // Guide the user straight to the switch Android 14+ may hold back. + if (picked.isFullScreen && !canFullScreen) context.openFullScreenIntentSettings() + } + }, + onDismiss = { alertStylePicker = null }, + ) + } + alertSoundPicker?.let { calendarId -> + OptionPicker( + title = stringResource(R.string.settings_alert_sound), + options = listOf(ReminderAlertStyle.FullScreen, ReminderAlertStyle.FullScreenRing), + selected = state.reminderAlertStyles.alertStyleFor(calendarId), + label = { alertSoundLabel(it) }, + summary = { + stringResource( + if (it == ReminderAlertStyle.FullScreenRing) { + R.string.settings_alert_sound_ring_summary + } else { + R.string.settings_alert_sound_once_summary + }, + ) + }, + onSelect = { viewModel.setCalendarAlertStyle(calendarId, it) }, + onDismiss = { alertSoundPicker = null }, + ) + } + overrideDialog?.let { target -> + val map = if (target.isAllDay) { + state.perCalendarAllDayReminderOverride + } else { + state.perCalendarReminderOverride + } + ReminderDefaultPicker( + title = stringResource( + if (target.isAllDay) { + R.string.settings_default_reminder_allday + } else { + R.string.settings_default_reminder + }, + ), + presets = if (target.isAllDay) ALLDAY_REMINDER_PRESETS else REMINDER_PRESETS, + selected = map.reminderOverrideFor(target.calendarId), + allowInherit = true, + onSelect = { + if (target.isAllDay) { + viewModel.setCalendarAllDayReminderOverride(target.calendarId, it) + } else { + viewModel.setCalendarReminderOverride(target.calendarId, it) + } + }, + onDismiss = { overrideDialog = null }, + leadTimeSummary = if (target.isAllDay) { + allDayFiringTimeSummary(state.allDayReminderTimeMinutes) + } else { + null + }, + ) + } +} + +/** + * One calendar in its account group, with its options unfolding beneath it: + * the notification switch and channel deep-link for every calendar, then the + * default-reminder overrides where it takes them — or, for a special-dates + * calendar, a link to the section that owns them. + */ +@Composable +private fun CalendarReminderEntry( + calendar: CalendarSource, + state: SettingsUiState, + position: Position, + expanded: Boolean, + notifying: Boolean, + blocked: Boolean, + heldBack: FullScreenHeldBack, + onToggleExpand: () -> Unit, + onSetNotifying: (Boolean) -> Unit, + onOpenSound: () -> Unit, + onOpenRingSettings: () -> Unit, + onEditAlertStyle: () -> Unit, + onEditAlertSound: () -> Unit, + onAllowFullScreen: () -> Unit, + onOpenSpecialDates: () -> Unit, + onEditOverride: (OverrideTarget) -> Unit, +) { + val opensGroup = position == Position.Top || position == Position.Alone + val closesGroup = position == Position.Bottom || position == Position.Alone + GroupedRow( + title = calendar.displayName.orUnnamed(), + summary = when { + !notifying -> stringResource(R.string.settings_calendar_notifications_off) + blocked -> stringResource(R.string.settings_notifications_blocked) + heldBack.any -> stringResource(R.string.settings_full_screen_held_back) + else -> null + }, + position = when { + !expanded -> position + opensGroup -> Position.Top + else -> Position.Middle + }, + leading = { CalendarColorChip(calendar.color) }, + trailing = { ExpandIcon(expanded) }, + onClick = onToggleExpand, + ) + AnimatedVisibility( + visible = expanded, + enter = expandEnter(), + exit = collapseExit(), + ) { + val managed = calendar.id in state.managedCalendarIds + val takesOverrides = calendar.canModifyContents && !managed + val alertStyle = state.reminderAlertStyles.alertStyleFor(calendar.id) + val nested = MaterialTheme.colorScheme.surfaceContainerHighest + // The options round off their calendar; mid-group they keep the row gap. + Column(if (closesGroup) Modifier else Modifier.padding(bottom = 2.dp)) { + GroupedSwitchRow( + title = stringResource(R.string.settings_calendar_notifications), + checked = notifying, + onCheckedChange = onSetNotifying, + summary = stringResource(R.string.settings_calendar_notifications_hint), + position = Position.Middle, + container = nested, + ) + GroupedRow( + title = stringResource(R.string.settings_notification_sound), + summary = when { + blocked -> stringResource(R.string.settings_notifications_blocked_hint) + alertStyle == ReminderAlertStyle.FullScreenRing -> + stringResource(R.string.settings_calendar_notification_sound_ring_hint) + else -> stringResource(R.string.settings_calendar_notification_sound_hint) + }, + position = Position.Middle, + container = nested, + trailing = { ExternalLinkIcon() }, + onClick = onOpenSound, + ) + val lastAlertRow = !(managed || takesOverrides) + GroupedRow( + title = stringResource(R.string.settings_alert_style), + summary = alertStyleLabel(alertStyle), + position = if (lastAlertRow && !alertStyle.isFullScreen) Position.Bottom else Position.Middle, + container = nested, + onClick = onEditAlertStyle, + ) + if (alertStyle.isFullScreen) { + GroupedRow( + title = stringResource(R.string.settings_alert_sound), + summary = alertSoundLabel(alertStyle), + position = if (lastAlertRow && !heldBack.any) Position.Bottom else Position.Middle, + container = nested, + onClick = onEditAlertSound, + ) + } + if (heldBack.ring) { + GroupedRow( + title = stringResource(R.string.reminder_ring_channel_name), + summary = stringResource(R.string.settings_ring_blocked_hint), + position = if (lastAlertRow && !heldBack.permission) Position.Bottom else Position.Middle, + container = nested, + trailing = { ExternalLinkIcon() }, + onClick = onOpenRingSettings, + ) + } + if (heldBack.permission) { + GroupedRow( + title = stringResource(R.string.settings_full_screen_permission), + summary = stringResource(R.string.settings_full_screen_permission_hint), + position = if (lastAlertRow) Position.Bottom else Position.Middle, + container = nested, + trailing = { ExternalLinkIcon() }, + onClick = onAllowFullScreen, + ) + } + if (managed) { + GroupedRow( + title = stringResource(R.string.settings_default_reminder_allday), + summary = stringResource(R.string.settings_calendar_reminders_managed_hint), + position = Position.Bottom, + container = nested, + trailing = { ChevronIcon() }, + onClick = onOpenSpecialDates, + ) + } else if (takesOverrides) { + OverrideRow( + calendarId = calendar.id, + isAllDay = false, + choice = state.perCalendarReminderOverride.reminderOverrideFor(calendar.id), + globalDefault = state.defaultReminderMinutes, + position = Position.Middle, + container = nested, + onEditOverride = onEditOverride, + ) + OverrideRow( + calendarId = calendar.id, + isAllDay = true, + choice = state.perCalendarAllDayReminderOverride.reminderOverrideFor(calendar.id), + globalDefault = state.defaultAllDayReminderMinutes, + position = Position.Bottom, + container = nested, + onEditOverride = onEditOverride, + ) + } + } + } +} + +@Composable +private fun OverrideRow( + calendarId: Long, + isAllDay: Boolean, + choice: ReminderOverride, + globalDefault: List, + position: Position, + container: Color, + onEditOverride: (OverrideTarget) -> Unit, +) { + GroupedRow( + title = stringResource( + if (isAllDay) R.string.settings_default_reminder_allday else R.string.settings_default_reminder, + ), + summary = calendarOverrideSummary(choice, globalDefault), + position = position, + container = container, + onClick = { onEditOverride(OverrideTarget(calendarId, isAllDay)) }, + ) +} + +@Composable +private fun ChevronIcon() { + Icon( + imageVector = Icons.AutoMirrored.Filled.KeyboardArrowRight, + contentDescription = null, + tint = MaterialTheme.colorScheme.onSurfaceVariant, + ) +} + +/** Which calendar + event kind a per-calendar reminder-override dialog targets. */ +private data class OverrideTarget(val calendarId: Long, val isAllDay: Boolean) + +/** Row summary for a calendar: its override, or the inherited global default. */ +@Composable +private fun calendarOverrideSummary( + choice: ReminderOverride, + globalDefault: List, +): String = when (choice) { + ReminderOverride.Inherit -> + stringResource(R.string.settings_calendar_reminder_inherits, reminderChoiceLabel(globalDefault)) + ReminderOverride.None -> stringResource(R.string.reminder_none) + is ReminderOverride.Minutes -> reminderChoiceLabel(choice.minutes) +} + +/** What keeps a full-screen calendar from alerting as chosen: the missing permission, or the blocked ring channel. */ +private data class FullScreenHeldBack(val permission: Boolean, val ring: Boolean) { + val any: Boolean get() = permission || ring +} + +private fun fullScreenHeldBack( + style: ReminderAlertStyle, + notifying: Boolean, + blocked: Boolean, + ringBlocked: Boolean, + canFullScreen: Boolean, +): FullScreenHeldBack { + val live = notifying && !blocked + return FullScreenHeldBack( + permission = live && style.isFullScreen && !canFullScreen, + ring = live && style == ReminderAlertStyle.FullScreenRing && ringBlocked, + ) +} + +@Composable +private fun alertStyleLabel(style: ReminderAlertStyle): String = stringResource( + if (style.isFullScreen) R.string.settings_alert_style_full_screen else R.string.settings_alert_style_notification, +) + +@Composable +private fun alertSoundLabel(style: ReminderAlertStyle): String = stringResource( + if (style == ReminderAlertStyle.FullScreenRing) R.string.settings_alert_sound_ring else R.string.settings_alert_sound_once, +) + +/** Whether Android lets full-screen alerts open, re-read on every `ON_RESUME`. */ +@Composable +private fun rememberCanUseFullScreenIntent(): Boolean = rememberOnResume { it.canUseFullScreenIntent() } diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/ui/settings/NotificationSettings.kt b/app/src/main/java/de/jeanlucmakiola/calendula/ui/settings/NotificationSettings.kt index 4b359dee..c3d027ff 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/ui/settings/NotificationSettings.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/ui/settings/NotificationSettings.kt @@ -8,9 +8,7 @@ import android.os.PowerManager import android.provider.Settings import androidx.activity.compose.rememberLauncherForActivityResult import androidx.activity.result.contract.ActivityResultContracts -import androidx.compose.animation.AnimatedVisibility import androidx.compose.foundation.layout.Arrangement -import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.fillMaxWidth @@ -38,34 +36,22 @@ import androidx.core.net.toUri import androidx.lifecycle.compose.LifecycleResumeEffect import androidx.lifecycle.compose.collectAsStateWithLifecycle import de.jeanlucmakiola.calendula.R -import de.jeanlucmakiola.calendula.data.reminders.RING_CHANNEL_ID -import de.jeanlucmakiola.calendula.domain.CalendarSource -import de.jeanlucmakiola.calendula.domain.reminders.ReminderAlertStyle -import de.jeanlucmakiola.calendula.domain.reminders.alertStyleFor -import de.jeanlucmakiola.calendula.ui.common.CalendarColorChip import de.jeanlucmakiola.calendula.ui.common.DurationPicker import de.jeanlucmakiola.calendula.ui.common.REMINDER_PRESETS import de.jeanlucmakiola.calendula.ui.common.ReminderDefaultPicker import de.jeanlucmakiola.calendula.ui.common.TimePickerAlert import de.jeanlucmakiola.calendula.ui.common.durationLabel -import de.jeanlucmakiola.calendula.ui.common.orUnnamed import de.jeanlucmakiola.floret.android.hasPermission import de.jeanlucmakiola.floret.android.openNotificationSettings import de.jeanlucmakiola.floret.components.CollapsingScaffold -import de.jeanlucmakiola.floret.components.ExpandIcon import de.jeanlucmakiola.floret.components.GroupedRow import de.jeanlucmakiola.floret.components.GroupedSwitchRow import de.jeanlucmakiola.floret.components.IconInfoCard import de.jeanlucmakiola.floret.components.OptionPicker import de.jeanlucmakiola.floret.components.Position import de.jeanlucmakiola.floret.components.SelectedCheck -import de.jeanlucmakiola.floret.identity.collapseExit -import de.jeanlucmakiola.floret.identity.expandEnter import de.jeanlucmakiola.floret.reminders.ReminderOverride -import de.jeanlucmakiola.floret.ring.canUseFullScreenIntent -import de.jeanlucmakiola.floret.ring.openFullScreenIntentSettings import de.jeanlucmakiola.floret.reminders.minutesOrEmpty -import de.jeanlucmakiola.floret.reminders.reminderOverrideFor import kotlinx.coroutines.launch import kotlinx.datetime.LocalTime @@ -79,7 +65,7 @@ internal fun NotificationsScreen( state: SettingsUiState, viewModel: SettingsViewModel, onBack: () -> Unit, - onOpenSpecialDates: () -> Unit, + onOpenCalendarReminders: () -> Unit, visible: Boolean = true, ) { val context = LocalContext.current @@ -100,12 +86,6 @@ internal fun NotificationsScreen( var showAllDayReminder by remember { mutableStateOf(false) } var showAllDayReminderTime by remember { mutableStateOf(false) } var showSnooze by remember { mutableStateOf(false) } - var overrideDialog by remember { mutableStateOf(null) } - var alertStylePicker by remember { mutableStateOf(null) } - var alertSoundPicker by remember { mutableStateOf(null) } - val canFullScreen = rememberCanUseFullScreenIntent() - var calendarSectionExpanded by remember { mutableStateOf(false) } - var expandedCalendars by remember { mutableStateOf(emptySet()) } val scope = rememberCoroutineScope() val channelStatus by viewModel.channelStatus.collectAsStateWithLifecycle() val calendarIds = state.reminderCalendars.map { it.id } @@ -113,9 +93,6 @@ internal fun NotificationsScreen( viewModel.refreshChannelStatus(calendarIds) onPauseOrDispose { } } - val openCalendarSound: (Long) -> Unit = { calendarId -> - scope.launch { context.openNotificationSettings(viewModel.soundSettingsChannel(calendarId)) } - } CollapsingScaffold( title = stringResource(R.string.settings_section_notifications), @@ -208,125 +185,24 @@ internal fun NotificationsScreen( onClick = { showSnooze = true }, ) - // Per-calendar notifications and reminder overrides (#145, #272). if (state.reminderCalendars.isNotEmpty()) { Spacer(Modifier.height(24.dp)) GroupedRow( title = stringResource(R.string.settings_calendar_reminders_title), - summary = stringResource(R.string.settings_calendar_reminders_hint), + summary = stringResource(R.string.settings_calendar_reminders_subtitle), position = Position.Alone, - trailing = { ExpandIcon(calendarSectionExpanded) }, - onClick = { calendarSectionExpanded = !calendarSectionExpanded }, + trailing = { + Icon( + imageVector = Icons.AutoMirrored.Filled.KeyboardArrowRight, + contentDescription = null, + tint = MaterialTheme.colorScheme.onSurfaceVariant, + ) + }, + onClick = onOpenCalendarReminders, ) - AnimatedVisibility( - visible = calendarSectionExpanded, - enter = expandEnter(), - exit = collapseExit(), - ) { - Column { - state.reminderCalendars.forEach { calendar -> - Spacer(Modifier.height(16.dp)) - val expanded = calendar.id in expandedCalendars - val notifying = calendar.id !in state.mutedReminderCalendarIds - val blocked = notifying && calendar.id in channelStatus.blockedCalendarIds - val heldBack = fullScreenHeldBack( - style = state.reminderAlertStyles.alertStyleFor(calendar.id), - notifying = notifying, - blocked = blocked, - ringBlocked = channelStatus.ringBlocked, - canFullScreen = canFullScreen, - ) - GroupedRow( - title = calendar.displayName.orUnnamed(), - summary = when { - !notifying -> stringResource(R.string.settings_calendar_notifications_off) - blocked -> stringResource(R.string.settings_notifications_blocked) - heldBack.any -> stringResource(R.string.settings_full_screen_held_back) - else -> null - }, - position = if (expanded) Position.Top else Position.Alone, - leading = { CalendarColorChip(calendar.color) }, - trailing = { ExpandIcon(expanded) }, - onClick = { - expandedCalendars = if (expanded) { - expandedCalendars - calendar.id - } else { - expandedCalendars + calendar.id - } - }, - ) - AnimatedVisibility( - visible = expanded, - enter = expandEnter(), - exit = collapseExit(), - ) { - CalendarNotificationRows( - calendar = calendar, - state = state, - notifying = notifying, - blocked = blocked, - heldBack = heldBack, - onOpenRingSettings = { context.openNotificationSettings(RING_CHANNEL_ID) }, - onSetNotifying = { viewModel.setCalendarRemindersEnabled(calendar.id, it) }, - onOpenSound = { openCalendarSound(calendar.id) }, - onEditAlertStyle = { alertStylePicker = calendar.id }, - onEditAlertSound = { alertSoundPicker = calendar.id }, - onAllowFullScreen = { context.openFullScreenIntentSettings() }, - onOpenSpecialDates = onOpenSpecialDates, - onEditOverride = { overrideDialog = it }, - ) - } - } - } - } } } - alertStylePicker?.let { calendarId -> - val current = state.reminderAlertStyles.alertStyleFor(calendarId) - OptionPicker( - title = stringResource(R.string.settings_alert_style), - options = listOf(ReminderAlertStyle.Notification, ReminderAlertStyle.FullScreen), - selected = if (current.isFullScreen) ReminderAlertStyle.FullScreen else ReminderAlertStyle.Notification, - label = { alertStyleLabel(it) }, - summary = { - stringResource( - if (it.isFullScreen) { - R.string.settings_alert_style_full_screen_summary - } else { - R.string.settings_alert_style_notification_summary - }, - ) - }, - onSelect = { picked -> - if (picked.isFullScreen != current.isFullScreen) { - viewModel.setCalendarAlertStyle(calendarId, picked) - // Guide the user straight to the switch Android 14+ may hold back. - if (picked.isFullScreen && !canFullScreen) context.openFullScreenIntentSettings() - } - }, - onDismiss = { alertStylePicker = null }, - ) - } - alertSoundPicker?.let { calendarId -> - OptionPicker( - title = stringResource(R.string.settings_alert_sound), - options = listOf(ReminderAlertStyle.FullScreen, ReminderAlertStyle.FullScreenRing), - selected = state.reminderAlertStyles.alertStyleFor(calendarId), - label = { alertSoundLabel(it) }, - summary = { - stringResource( - if (it == ReminderAlertStyle.FullScreenRing) { - R.string.settings_alert_sound_ring_summary - } else { - R.string.settings_alert_sound_once_summary - }, - ) - }, - onSelect = { viewModel.setCalendarAlertStyle(calendarId, it) }, - onDismiss = { alertSoundPicker = null }, - ) - } if (showSnooze) { DurationPicker( title = stringResource(R.string.settings_snooze_duration), @@ -372,150 +248,6 @@ internal fun NotificationsScreen( onDismiss = { showAllDayReminderTime = false }, ) } - overrideDialog?.let { target -> - val map = if (target.isAllDay) { - state.perCalendarAllDayReminderOverride - } else { - state.perCalendarReminderOverride - } - ReminderDefaultPicker( - title = stringResource( - if (target.isAllDay) { - R.string.settings_default_reminder_allday - } else { - R.string.settings_default_reminder - }, - ), - presets = if (target.isAllDay) ALLDAY_REMINDER_PRESETS else REMINDER_PRESETS, - selected = map.reminderOverrideFor(target.calendarId), - allowInherit = true, - onSelect = { - if (target.isAllDay) { - viewModel.setCalendarAllDayReminderOverride(target.calendarId, it) - } else { - viewModel.setCalendarReminderOverride(target.calendarId, it) - } - }, - onDismiss = { overrideDialog = null }, - leadTimeSummary = if (target.isAllDay) { - allDayFiringTimeSummary(state.allDayReminderTimeMinutes) - } else { - null - }, - ) - } -} - -/** - * One calendar's expanded rows: its notification switch and channel deep-link - * for every calendar, then the default-reminder overrides where it takes them — - * or, for a special-dates calendar, a link to the section that owns them. - */ -@Composable -private fun CalendarNotificationRows( - calendar: CalendarSource, - state: SettingsUiState, - notifying: Boolean, - blocked: Boolean, - heldBack: FullScreenHeldBack, - onOpenRingSettings: () -> Unit, - onSetNotifying: (Boolean) -> Unit, - onOpenSound: () -> Unit, - onEditAlertStyle: () -> Unit, - onEditAlertSound: () -> Unit, - onAllowFullScreen: () -> Unit, - onOpenSpecialDates: () -> Unit, - onEditOverride: (OverrideTarget) -> Unit, -) { - val managed = calendar.id in state.managedCalendarIds - val takesOverrides = calendar.canModifyContents && !managed - val alertStyle = state.reminderAlertStyles.alertStyleFor(calendar.id) - val needsPermission = heldBack.permission - val ringHeldBack = heldBack.ring - Column { - GroupedSwitchRow( - title = stringResource(R.string.settings_calendar_notifications), - checked = notifying, - onCheckedChange = onSetNotifying, - summary = stringResource(R.string.settings_calendar_notifications_hint), - position = Position.Middle, - ) - GroupedRow( - title = stringResource(R.string.settings_notification_sound), - summary = when { - blocked -> stringResource(R.string.settings_notifications_blocked_hint) - alertStyle == ReminderAlertStyle.FullScreenRing -> - stringResource(R.string.settings_calendar_notification_sound_ring_hint) - else -> stringResource(R.string.settings_calendar_notification_sound_hint) - }, - position = Position.Middle, - trailing = { ExternalLinkIcon() }, - onClick = onOpenSound, - ) - val lastAlertRow = !(managed || takesOverrides) - GroupedRow( - title = stringResource(R.string.settings_alert_style), - summary = alertStyleLabel(alertStyle), - position = if (lastAlertRow && !alertStyle.isFullScreen) Position.Bottom else Position.Middle, - onClick = onEditAlertStyle, - ) - if (alertStyle.isFullScreen) { - GroupedRow( - title = stringResource(R.string.settings_alert_sound), - summary = alertSoundLabel(alertStyle), - position = if (lastAlertRow && !needsPermission && !ringHeldBack) Position.Bottom else Position.Middle, - onClick = onEditAlertSound, - ) - } - if (ringHeldBack) { - GroupedRow( - title = stringResource(R.string.reminder_ring_channel_name), - summary = stringResource(R.string.settings_ring_blocked_hint), - position = if (lastAlertRow && !needsPermission) Position.Bottom else Position.Middle, - trailing = { ExternalLinkIcon() }, - onClick = onOpenRingSettings, - ) - } - if (needsPermission) { - GroupedRow( - title = stringResource(R.string.settings_full_screen_permission), - summary = stringResource(R.string.settings_full_screen_permission_hint), - position = if (lastAlertRow) Position.Bottom else Position.Middle, - trailing = { ExternalLinkIcon() }, - onClick = onAllowFullScreen, - ) - } - if (managed) { - GroupedRow( - title = stringResource(R.string.settings_default_reminder_allday), - summary = stringResource(R.string.settings_calendar_reminders_managed_hint), - position = Position.Bottom, - trailing = { - Icon( - imageVector = Icons.AutoMirrored.Filled.KeyboardArrowRight, - contentDescription = null, - tint = MaterialTheme.colorScheme.onSurfaceVariant, - ) - }, - onClick = onOpenSpecialDates, - ) - } else if (takesOverrides) { - val timed = state.perCalendarReminderOverride.reminderOverrideFor(calendar.id) - GroupedRow( - title = stringResource(R.string.settings_default_reminder), - summary = calendarOverrideSummary(timed, state.defaultReminderMinutes), - position = Position.Middle, - onClick = { onEditOverride(OverrideTarget(calendar.id, isAllDay = false)) }, - ) - val allDay = state.perCalendarAllDayReminderOverride.reminderOverrideFor(calendar.id) - GroupedRow( - title = stringResource(R.string.settings_default_reminder_allday), - summary = calendarOverrideSummary(allDay, state.defaultAllDayReminderMinutes), - position = Position.Bottom, - onClick = { onEditOverride(OverrideTarget(calendar.id, isAllDay = true)) }, - ) - } - } } /** Samsung's One UI can start a new notification category switched off (#145). */ @@ -541,7 +273,7 @@ private fun NotificationCategoryHint(onOpen: () -> Unit, onDismiss: () -> Unit) } @Composable -private fun ExternalLinkIcon() { +internal fun ExternalLinkIcon() { Icon( imageVector = Icons.AutoMirrored.Filled.OpenInNew, contentDescription = null, @@ -549,58 +281,10 @@ private fun ExternalLinkIcon() { ) } -/** Which calendar + event kind a per-calendar reminder-override dialog targets. */ -private data class OverrideTarget(val calendarId: Long, val isAllDay: Boolean) - /** A global default (empty = none) as a picker choice for selection highlighting. */ private fun List.toReminderChoice(): ReminderOverride = if (isEmpty()) ReminderOverride.None else ReminderOverride.Minutes(this) -/** Row summary for a calendar: its override, or the inherited global default. */ -@Composable -private fun calendarOverrideSummary( - choice: ReminderOverride, - globalDefault: List, -): String = when (choice) { - ReminderOverride.Inherit -> - stringResource(R.string.settings_calendar_reminder_inherits, reminderChoiceLabel(globalDefault)) - ReminderOverride.None -> stringResource(R.string.reminder_none) - is ReminderOverride.Minutes -> reminderChoiceLabel(choice.minutes) -} - -/** What keeps a full-screen calendar from alerting as chosen: the missing permission, or the blocked ring channel. */ -private data class FullScreenHeldBack(val permission: Boolean, val ring: Boolean) { - val any: Boolean get() = permission || ring -} - -private fun fullScreenHeldBack( - style: ReminderAlertStyle, - notifying: Boolean, - blocked: Boolean, - ringBlocked: Boolean, - canFullScreen: Boolean, -): FullScreenHeldBack { - val live = notifying && !blocked - return FullScreenHeldBack( - permission = live && style.isFullScreen && !canFullScreen, - ring = live && style == ReminderAlertStyle.FullScreenRing && ringBlocked, - ) -} - -@Composable -private fun alertStyleLabel(style: ReminderAlertStyle): String = stringResource( - if (style.isFullScreen) R.string.settings_alert_style_full_screen else R.string.settings_alert_style_notification, -) - -@Composable -private fun alertSoundLabel(style: ReminderAlertStyle): String = stringResource( - if (style == ReminderAlertStyle.FullScreenRing) R.string.settings_alert_sound_ring else R.string.settings_alert_sound_once, -) - -/** Whether Android lets full-screen alerts open, re-read on every `ON_RESUME` like [rememberBatteryOptimizationExempt]. */ -@Composable -private fun rememberCanUseFullScreenIntent(): Boolean = rememberOnResume { it.canUseFullScreenIntent() } - /** Snooze delays offered for the notification "Snooze" action, in minutes. */ private val SNOOZE_PRESETS = listOf(5, 10, 15, 30, 60) @@ -613,7 +297,7 @@ private fun rememberBatteryOptimizationExempt(): Boolean = rememberOnResume(::is /** [read], re-read on every `ON_RESUME` so a change made in system settings shows up at once. */ @Composable -private fun rememberOnResume(read: (Context) -> Boolean): Boolean { +internal fun rememberOnResume(read: (Context) -> Boolean): Boolean { val context = LocalContext.current var value by remember { mutableStateOf(read(context)) } LifecycleResumeEffect(context) { diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/ui/settings/SettingsScreen.kt b/app/src/main/java/de/jeanlucmakiola/calendula/ui/settings/SettingsScreen.kt index 3bab7449..e3ba551c 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/ui/settings/SettingsScreen.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/ui/settings/SettingsScreen.kt @@ -73,7 +73,7 @@ import de.jeanlucmakiola.floret.identity.predictiveBackExit /** The settings sub-screens reached from the hub's category rows. */ private enum class SettingsSection { - Appearance, Views, EventForm, Notifications, SpecialDates, Widgets, Licences + Appearance, Views, EventForm, Notifications, CalendarReminders, SpecialDates, Widgets, Licences } /** @@ -131,8 +131,9 @@ fun SettingsScreen( ) { EventFormScreen(state = state, viewModel = viewModel, onBack = { section = null }, visible = section == SettingsSection.EventForm) } + // Stays up beneath its per-calendar subpage, so backing out reveals it. AnimatedVisibility( - visible = section == SettingsSection.Notifications, + visible = section == SettingsSection.Notifications || section == SettingsSection.CalendarReminders, enter = slideInHorizontally(slideSpec) { it } + fadeIn(), exit = overlayExit, ) { @@ -140,10 +141,23 @@ fun SettingsScreen( state = state, viewModel = viewModel, onBack = { section = null }, - onOpenSpecialDates = { section = SettingsSection.SpecialDates }, + onOpenCalendarReminders = { section = SettingsSection.CalendarReminders }, visible = section == SettingsSection.Notifications, ) } + AnimatedVisibility( + visible = section == SettingsSection.CalendarReminders, + enter = slideInHorizontally(slideSpec) { it } + fadeIn(), + exit = overlayExit, + ) { + CalendarRemindersScreen( + state = state, + viewModel = viewModel, + onBack = { section = SettingsSection.Notifications }, + onOpenSpecialDates = { section = SettingsSection.SpecialDates }, + visible = section == SettingsSection.CalendarReminders, + ) + } AnimatedVisibility( visible = section == SettingsSection.SpecialDates, enter = slideInHorizontally(slideSpec) { it } + fadeIn(), diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 44e4c6ff..2371206a 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -733,6 +733,7 @@ Amount Set Per-calendar reminders + Notifications, sound and default reminders per calendar Turn notifications off or pick a sound per calendar, and override the default reminder for calendars you can edit. Notifications Notify at this calendar\'s reminder times diff --git a/floret-kit b/floret-kit index 2fc730dd..b2f9a623 160000 --- a/floret-kit +++ b/floret-kit @@ -1 +1 @@ -Subproject commit 2fc730dd44d1c8a97384ceb0146d3111aa7969d5 +Subproject commit b2f9a6230a352680ce7ced81e9773d7b4b01ba5d