diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/data/reminders/ReminderChannelPlan.kt b/app/src/main/java/de/jeanlucmakiola/calendula/data/reminders/ReminderChannelPlan.kt index f75eb262..e67a7727 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/data/reminders/ReminderChannelPlan.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/data/reminders/ReminderChannelPlan.kt @@ -132,21 +132,30 @@ private fun accountGroupId(type: String, name: String): String = "$GROUP_PREFIX$ /** A channel as far as its blocked state goes: switched off itself, and the group it sits in. */ data class ReminderChannelState(val blocked: Boolean, val groupId: String?) +/** Reminder notifications the system blocks, while Calendula would post them. */ +data class ReminderChannelStatus( + val appBlocked: Boolean = false, + val blockedCalendarIds: Set = emptySet(), + /** The blocked ones whose whole account group is off, which only the app's own screen can undo. */ + val groupBlockedCalendarIds: Set = emptySet(), +) + /** - * The calendars among [calendarIds] whose reminders the system blocks: all of - * them while the app's notifications are off, otherwise those whose channel, - * or the group it sits in, is switched off. A calendar without a channel yet - * isn't blocked. + * What the system blocks of [calendarIds]' reminders: all of them while the + * app's notifications are off, otherwise those whose channel, or the group it + * sits in, is switched off. A calendar without a channel yet isn't blocked. */ -fun blockedReminderCalendars( +fun reminderChannelStatus( calendarIds: Collection, appEnabled: Boolean, channels: Map, blockedGroupIds: Set, -): Set { - if (!appEnabled) return calendarIds.toSet() - return calendarIds.filterTo(HashSet()) { id -> - val channel = channels[reminderChannelId(id)] ?: return@filterTo false - channel.blocked || channel.groupId in blockedGroupIds +): ReminderChannelStatus { + val groupBlocked = calendarIds.filterTo(HashSet()) { channels[reminderChannelId(it)]?.groupId in blockedGroupIds } + val blocked = if (!appEnabled) { + calendarIds.toSet() + } else { + calendarIds.filterTo(HashSet()) { channels[reminderChannelId(it)]?.blocked == true } + groupBlocked } + return ReminderChannelStatus(appBlocked = !appEnabled, blockedCalendarIds = blocked, groupBlockedCalendarIds = groupBlocked) } diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/data/reminders/ReminderChannels.kt b/app/src/main/java/de/jeanlucmakiola/calendula/data/reminders/ReminderChannels.kt index 0a82fe55..717292b8 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/data/reminders/ReminderChannels.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/data/reminders/ReminderChannels.kt @@ -110,13 +110,7 @@ class ReminderChannels @Inject constructor( it.id to ReminderChannelState(it.importance == NotificationManager.IMPORTANCE_NONE, it.group) } val blockedGroups = manager.notificationChannelGroups.filter { it.isBlocked }.mapTo(HashSet()) { it.id } - return ReminderChannelStatus( - appBlocked = !appEnabled, - blockedCalendarIds = blockedReminderCalendars(calendarIds, appEnabled, channels, blockedGroups), - groupBlockedCalendarIds = calendarIds.filterTo(HashSet()) { - channels[reminderChannelId(it)]?.groupId in blockedGroups - }, - ) + return reminderChannelStatus(calendarIds, appEnabled, channels, blockedGroups) } private fun plan( @@ -142,6 +136,7 @@ class ReminderChannels @Inject constructor( } if (channels.isEmpty()) return val byId = existing.associateBy { it.id } + val description = context.getString(R.string.reminder_channel_description) val template = byId[LEGACY_REMINDER_CHANNEL_ID] // Importance only applies on creation; an existing channel keeps its own. manager.createNotificationChannels( @@ -152,7 +147,7 @@ class ReminderChannels @Inject constructor( spec.name, current?.importance ?: template?.importance ?: NotificationManager.IMPORTANCE_HIGH, ).apply { - description = context.getString(R.string.reminder_channel_description) + this.description = description spec.groupId?.let { group = it } if (current == null && template != null) inheritFrom(template) } @@ -207,10 +202,3 @@ class ReminderChannels @Inject constructor( } } -/** Reminder notifications the system blocks, while Calendula would post them. */ -data class ReminderChannelStatus( - val appBlocked: Boolean = false, - val blockedCalendarIds: Set = emptySet(), - /** The blocked ones whose whole account group is off, which only the app's own screen can undo. */ - val groupBlockedCalendarIds: Set = emptySet(), -) diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/data/reminders/ReminderNotifier.kt b/app/src/main/java/de/jeanlucmakiola/calendula/data/reminders/ReminderNotifier.kt index 3631482e..7f03769c 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/data/reminders/ReminderNotifier.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/data/reminders/ReminderNotifier.kt @@ -70,7 +70,10 @@ class ReminderNotifier @Inject constructor( } catch (e: Exception) { Log.w(TAG, "No channel for the reminder of event ${alert.eventId}", e) null - } ?: return + } ?: run { + Log.i(TAG, "Dropped the reminder of event ${alert.eventId}: calendar ${alert.calendarId} is gone") + return + } val title = displayTitle(alert) val is24Hour = settingsPrefs.timeFormat.first() .is24Hour(android.text.format.DateFormat.is24HourFormat(context)) 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 46b90ee7..efa61104 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 @@ -10,23 +10,19 @@ 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.Row -import androidx.compose.foundation.layout.fillMaxWidth -import androidx.compose.material.icons.outlined.Info -import androidx.compose.material3.Text -import androidx.compose.material3.TextButton -import androidx.compose.ui.Alignment -import androidx.lifecycle.compose.LifecycleResumeEffect -import androidx.lifecycle.compose.collectAsStateWithLifecycle -import de.jeanlucmakiola.floret.components.IconInfoCard import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.Spacer +import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.height import androidx.compose.material.icons.Icons import androidx.compose.material.icons.automirrored.filled.KeyboardArrowRight import androidx.compose.material.icons.automirrored.filled.OpenInNew +import androidx.compose.material.icons.outlined.Info import androidx.compose.material3.Icon import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.Text +import androidx.compose.material3.TextButton import androidx.compose.runtime.Composable import androidx.compose.runtime.DisposableEffect import androidx.compose.runtime.getValue @@ -34,6 +30,7 @@ import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.runtime.setValue +import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.res.stringResource @@ -41,7 +38,9 @@ import androidx.compose.ui.unit.dp import androidx.core.net.toUri import androidx.lifecycle.Lifecycle import androidx.lifecycle.LifecycleEventObserver +import androidx.lifecycle.compose.LifecycleResumeEffect import androidx.lifecycle.compose.LocalLifecycleOwner +import androidx.lifecycle.compose.collectAsStateWithLifecycle import de.jeanlucmakiola.calendula.R import de.jeanlucmakiola.calendula.domain.CalendarSource import de.jeanlucmakiola.calendula.ui.common.CalendarColorChip @@ -57,6 +56,7 @@ 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.Position import de.jeanlucmakiola.floret.components.SelectedCheck import de.jeanlucmakiola.floret.identity.collapseExit @@ -109,11 +109,7 @@ internal fun NotificationsScreen( onPauseOrDispose { } } val openCalendarSound: (Long) -> Unit = { calendarId -> - scope.launch { - val channelId = viewModel.reminderChannelFor(calendarId) - val appScreen = channelStatus.appBlocked || calendarId in channelStatus.groupBlockedCalendarIds - context.openNotificationSettings(channelId.takeUnless { appScreen }) - } + scope.launch { context.openNotificationSettings(viewModel.soundSettingsChannel(calendarId)) } } CollapsingScaffold( diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/ui/settings/SettingsUiState.kt b/app/src/main/java/de/jeanlucmakiola/calendula/ui/settings/SettingsUiState.kt index 1a0cc2fc..34e603b3 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/ui/settings/SettingsUiState.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/ui/settings/SettingsUiState.kt @@ -96,14 +96,14 @@ data class SettingsUiState( val perCalendarReminderOverride: Map> = emptyMap(), /** Per-calendar overrides of [defaultAllDayReminderMinutes] for all-day events. */ val perCalendarAllDayReminderOverride: Map> = emptyMap(), - /** Switched-on calendars you can edit: the per-calendar duration and reminder-override rows. */ + /** Switched-on calendars you can edit: the per-calendar event-duration rows. */ val writableCalendars: List = emptyList(), /** Switched-on calendars, each with its own notification switch and channel (#145, #272). */ val reminderCalendars: List = emptyList(), /** Calendars whose reminders Calendula doesn't post (#272). */ val mutedReminderCalendarIds: Set = emptySet(), - /** Whether the Samsung notification-category hint has been dismissed (#145). */ - val notificationCategoryHintDismissed: Boolean = false, + /** Whether the Samsung notification-category hint has been dismissed (#145); hidden until prefs load. */ + val notificationCategoryHintDismissed: Boolean = true, /** * Whether the event-colour picker is offered on calendars that publish no * colour palette (the colour may then not survive their next sync). diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/ui/settings/SettingsViewModel.kt b/app/src/main/java/de/jeanlucmakiola/calendula/ui/settings/SettingsViewModel.kt index 8b287aff..18d7ed6b 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/ui/settings/SettingsViewModel.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/ui/settings/SettingsViewModel.kt @@ -389,8 +389,16 @@ class SettingsViewModel @Inject constructor( fun dismissNotificationCategoryHint() = save { dismissNotificationCategoryHint() } - /** [calendarId]'s reminder channel, created if needed so its system settings can open (#340). */ - suspend fun reminderChannelFor(calendarId: Long): String? = onChannels { channelFor(calendarId) } + /** + * The channel whose system screen to open for [calendarId] (#340), created + * if needed; null when only the app's own screen can help — the app or the + * calendar's account group is blocked there, or the channel couldn't be made. + */ + suspend fun soundSettingsChannel(calendarId: Long): String? = onChannels { + val channelId = channelFor(calendarId) ?: return@onChannels null + val status = status(listOf(calendarId)) + channelId.takeUnless { status.appBlocked || calendarId in status.groupBlockedCalendarIds } + } /** Create the per-calendar channels before showing the app's channel list, even with reminders off. */ suspend fun syncReminderChannels() { diff --git a/app/src/test/java/de/jeanlucmakiola/calendula/data/reminders/ReminderChannelPlanTest.kt b/app/src/test/java/de/jeanlucmakiola/calendula/data/reminders/ReminderChannelPlanTest.kt index 84d0c21d..4634644b 100644 --- a/app/src/test/java/de/jeanlucmakiola/calendula/data/reminders/ReminderChannelPlanTest.kt +++ b/app/src/test/java/de/jeanlucmakiola/calendula/data/reminders/ReminderChannelPlanTest.kt @@ -135,12 +135,14 @@ class ReminderChannelPlanTest { reminderChannelId(3) to ReminderChannelState(blocked = false, groupId = "g1"), ) val ids = listOf(1L, 2L, 3L, 4L) - assertThat(blockedReminderCalendars(ids, appEnabled = true, channels, blockedGroupIds = setOf("g2"))) - .containsExactly(1L, 2L) - assertThat(blockedReminderCalendars(ids, appEnabled = true, channels, blockedGroupIds = emptySet())) + val groupOff = reminderChannelStatus(ids, appEnabled = true, channels, blockedGroupIds = setOf("g2")) + assertThat(groupOff.blockedCalendarIds).containsExactly(1L, 2L) + assertThat(groupOff.groupBlockedCalendarIds).containsExactly(2L) + assertThat(reminderChannelStatus(ids, appEnabled = true, channels, blockedGroupIds = emptySet()).blockedCalendarIds) .containsExactly(1L) - assertThat(blockedReminderCalendars(ids, appEnabled = false, channels, blockedGroupIds = emptySet())) - .containsExactly(1L, 2L, 3L, 4L) + val appOff = reminderChannelStatus(ids, appEnabled = false, channels, blockedGroupIds = emptySet()) + assertThat(appOff.appBlocked).isTrue() + assertThat(appOff.blockedCalendarIds).containsExactly(1L, 2L, 3L, 4L) } private companion object {