diff --git a/composeApp/src/commonMain/composeResources/values/strings.xml b/composeApp/src/commonMain/composeResources/values/strings.xml index 4be696a1..b0bb1928 100644 --- a/composeApp/src/commonMain/composeResources/values/strings.xml +++ b/composeApp/src/commonMain/composeResources/values/strings.xml @@ -808,4 +808,16 @@ A key ceremony or a signing session waiting for your approval Group activity Members joining and leaving, and ceremonies finishing + Notifications + Notifications start once this profile has finished its first sync with the relays. + This device is not letting Giza show notifications. + Allow notifications + For this profile, on this device + What people say to you + A ceremony or signing session waiting for your approval + Members joining and leaving, ceremonies finishing + Show message text + Off, a notification says only that a message arrived + Mute notifications + Unread messages are still counted diff --git a/composeApp/src/commonMain/kotlin/mobi/giza/compose/notifications/NotificationSettingsStore.kt b/composeApp/src/commonMain/kotlin/mobi/giza/compose/notifications/NotificationSettingsStore.kt new file mode 100644 index 00000000..934c0e26 --- /dev/null +++ b/composeApp/src/commonMain/kotlin/mobi/giza/compose/notifications/NotificationSettingsStore.kt @@ -0,0 +1,25 @@ +package mobi.giza.compose.notifications + +import com.vitorpamplona.quartz.nip01Core.core.HexKey +import kotlinx.coroutines.flow.Flow +import mobi.giza.compose.database.GizaDatabase +import mobi.giza.compose.database.model.NotificationLedger + +/** The settings screen's view of an identity's ledger. Phase 7. */ +class NotificationSettingsStore(private val database: GizaDatabase) { + + /** The ledger, or null until the identity is armed (D3). */ + fun observe(userPublicKey: HexKey): Flow = + database.notificationDao().observeLedger(userPublicKey) + + suspend fun save(userPublicKey: HexKey, settings: NotificationSettings) { + database.notificationDao().setSettings( + userPublicKey = userPublicKey, + enabled = settings.enabled, + messages = settings.messages, + needsYou = settings.needsYou, + groupActivity = settings.groupActivity, + showMessageText = settings.showMessageText, + ) + } +} diff --git a/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/composable/ActiveProfileScreen.kt b/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/composable/ActiveProfileScreen.kt index affa6a83..d29036b5 100644 --- a/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/composable/ActiveProfileScreen.kt +++ b/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/composable/ActiveProfileScreen.kt @@ -52,6 +52,9 @@ import com.vitorpamplona.quartz.nip01Core.metadata.MetadataEvent import mobi.giza.compose.ui.composable.widgets.LocalCanSign import mobi.giza.compose.ui.theme.spacing import mobi.giza.compose.resources.Res +import mobi.giza.compose.resources.notifications +import androidx.compose.material.icons.filled.Notifications +import mobi.giza.compose.ui.composable.navigation.routes.NotificationSettingsRoute import org.jetbrains.compose.resources.stringResource import mobi.giza.compose.resources.edit_profile import mobi.giza.compose.resources.key_package_management @@ -349,6 +352,29 @@ fun ActiveProfileScreen( } } + // A profile that cannot sign can open nothing, so is told about + // nothing. docs/notifications-and-background-sync.md, Phase 7. + if (canSign) item { + TextButton( + onClick = { + onNavigateToRoute.invoke( + NotificationSettingsRoute(activeUserPublicKey = activeUserPublicKey) + ) + } + ) { + Icon( + Icons.Default.Notifications, + contentDescription = Decorative + ) + + Spacer( + modifier = Modifier.width(MaterialTheme.spacing.space125) + ) + + Text(stringResource(Res.string.notifications)) + } + } + item { TextButton( onClick = { diff --git a/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/composable/ChatRoomDetailScreen.kt b/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/composable/ChatRoomDetailScreen.kt index b0b1e387..fa049e3d 100644 --- a/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/composable/ChatRoomDetailScreen.kt +++ b/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/composable/ChatRoomDetailScreen.kt @@ -98,6 +98,7 @@ import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.unit.dp import androidx.lifecycle.viewmodel.compose.viewModel import mobi.giza.compose.database.model.ChatRoom +import kotlinx.coroutines.launch import mobi.giza.compose.database.model.GroupKeyState import mobi.giza.compose.database.model.GroupSignedEvent import mobi.giza.compose.database.model.Participant @@ -161,6 +162,8 @@ import mobi.giza.compose.ui.composable.navigation.routes.NostrEventDetailRoute import mobi.giza.compose.ui.composable.navigation.routes.ChatRoomDetailRoute import mobi.giza.compose.managers.SubgroupManager import mobi.giza.compose.resources.Res +import mobi.giza.compose.resources.mute_notifications +import mobi.giza.compose.resources.mute_notifications_supporting import org.jetbrains.compose.resources.stringResource import mobi.giza.compose.resources.delete_group import mobi.giza.compose.resources.direct_message_detail @@ -558,6 +561,33 @@ fun ChatRoomDetailScreen( } } + // Muting is about this device and this person, not about the + // group, so it sits apart from everything the group signed. + // docs/notifications-and-background-sync.md, Phase 7. + item(key = "mute") { + val room = chatRoomDetailUIState.localChatRoom.chatRoom + val muteScope = rememberCoroutineScope() + // Held here as well as stored: this screen reads the room once, + // so the stored value would not come back to the switch. + var isMuted by rememberSaveable(room.id) { + mutableStateOf(room.notifications == ChatRoom.NOTIFICATIONS_MUTED) + } + SwitchRow( + headline = stringResource(Res.string.mute_notifications), + supporting = stringResource(Res.string.mute_notifications_supporting), + checked = isMuted, + enabled = true, + ) { muted -> + isMuted = muted + muteScope.launch { + chatRepository.setChatRoomNotifications( + room.id, + if (muted) ChatRoom.NOTIFICATIONS_MUTED else ChatRoom.NOTIFICATIONS_ALL, + ) + } + } + } + // Directly under the signing key, because between them // they are what the room *is*: the identity it signs as // and whose child it is. Absent on a group that is diff --git a/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/composable/NotificationSettingsScreen.kt b/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/composable/NotificationSettingsScreen.kt new file mode 100644 index 00000000..00a0650b --- /dev/null +++ b/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/composable/NotificationSettingsScreen.kt @@ -0,0 +1,205 @@ +package mobi.giza.compose.ui.composable + +import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.lazy.LazyColumn +import androidx.compose.foundation.selection.toggleable +import androidx.compose.material.icons.Icons +import androidx.compose.material.icons.filled.NotificationsOff +import androidx.compose.material3.Button +import androidx.compose.material3.Card +import androidx.compose.material3.CardDefaults +import androidx.compose.material3.ExperimentalMaterial3Api +import androidx.compose.material3.ListItem +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.Scaffold +import androidx.compose.material3.Switch +import androidx.compose.material3.Text +import androidx.compose.material3.TopAppBar +import androidx.compose.runtime.Composable +import androidx.compose.runtime.collectAsState +import androidx.compose.runtime.getValue +import androidx.compose.runtime.remember +import androidx.compose.runtime.rememberCoroutineScope +import androidx.compose.ui.Modifier +import androidx.compose.ui.semantics.Role +import kotlinx.coroutines.launch +import mobi.giza.compose.database.model.NotificationLedger +import mobi.giza.compose.notifications.NotificationSettings +import mobi.giza.compose.notifications.NotificationSettingsStore +import mobi.giza.compose.notifications.rememberNotificationPermission +import mobi.giza.compose.resources.Res +import mobi.giza.compose.resources.allow_notifications +import mobi.giza.compose.resources.notification_channel_group_activity +import mobi.giza.compose.resources.notification_channel_messages +import mobi.giza.compose.resources.notification_channel_needs_you +import mobi.giza.compose.resources.notifications +import mobi.giza.compose.resources.notifications_group_activity_supporting +import mobi.giza.compose.resources.notifications_master_supporting +import mobi.giza.compose.resources.notifications_messages_supporting +import mobi.giza.compose.resources.notifications_needs_you_supporting +import mobi.giza.compose.resources.notifications_not_allowed +import mobi.giza.compose.resources.notifications_start_after_first_sync +import mobi.giza.compose.resources.show_message_text +import mobi.giza.compose.resources.show_message_text_supporting +import mobi.giza.compose.ui.composable.widgets.EmptyState +import mobi.giza.compose.ui.composable.widgets.LoadingDataIndicator +import mobi.giza.compose.ui.composable.widgets.NavigateBackButton +import mobi.giza.compose.ui.composable.widgets.ScreenStateTransition +import mobi.giza.compose.ui.theme.readableContent +import mobi.giza.compose.ui.theme.spacing +import org.jetbrains.compose.resources.stringResource + +/** What the screen is showing: loading, not yet armed, or the switches. */ +private sealed interface NotificationSettingsUIState { + data object Loading : NotificationSettingsUIState + data object NotArmed : NotificationSettingsUIState + data class Loaded(val ledger: NotificationLedger) : NotificationSettingsUIState +} + +/** + * An identity's notification switches, and whether the platform lets them do anything. + * docs/notifications-and-background-sync.md, Phase 7. + * + * No error state of its own: the switches are one row in the local database, and a read that + * fails there has failed for every screen in the app. + */ +@OptIn(ExperimentalMaterial3Api::class) +@Composable +fun NotificationSettingsScreen( + activeUserPublicKey: String, + store: NotificationSettingsStore, + onNavigateBack: () -> Unit, + /** Extra rows from the platform's background sync (Part B); nothing until it exists. */ + backgroundSection: @Composable () -> Unit = {}, +) { + val ledgerState by remember(activeUserPublicKey) { store.observe(activeUserPublicKey) } + .collectAsState(initial = Unit) + val uiState = when (val ledger = ledgerState) { + Unit -> NotificationSettingsUIState.Loading + is NotificationLedger -> NotificationSettingsUIState.Loaded(ledger) + else -> NotificationSettingsUIState.NotArmed + } + val scope = rememberCoroutineScope() + val permission = rememberNotificationPermission() + + fun save(settings: NotificationSettings) { + scope.launch { store.save(activeUserPublicKey, settings) } + } + + Scaffold( + topBar = { + TopAppBar( + title = { Text(stringResource(Res.string.notifications)) }, + navigationIcon = { NavigateBackButton(onNavigateBack) }, + ) + }, + ) { innerPadding -> + Column(modifier = Modifier.padding(innerPadding).readableContent()) { + ScreenStateTransition(uiState) { state -> + when (state) { + NotificationSettingsUIState.Loading -> LoadingDataIndicator() + NotificationSettingsUIState.NotArmed -> EmptyState( + message = stringResource(Res.string.notifications_start_after_first_sync), + icon = Icons.Default.NotificationsOff, + ) + is NotificationSettingsUIState.Loaded -> { + val settings = state.ledger.settings + LazyColumn(verticalArrangement = Arrangement.spacedBy(MaterialTheme.spacing.relatedGap)) { + if (settings.enabled && !permission.granted) item { + Card( + modifier = Modifier.fillMaxWidth().padding(horizontal = MaterialTheme.spacing.screenMargin), + colors = CardDefaults.cardColors( + containerColor = MaterialTheme.colorScheme.errorContainer, + contentColor = MaterialTheme.colorScheme.onErrorContainer, + ), + ) { + Column( + modifier = Modifier.padding(MaterialTheme.spacing.containerPadding), + verticalArrangement = Arrangement.spacedBy(MaterialTheme.spacing.itemGap), + ) { + Text(stringResource(Res.string.notifications_not_allowed)) + Button(onClick = { permission.request() }) { + Text(stringResource(Res.string.allow_notifications)) + } + } + } + } + item { + SwitchRow( + headline = stringResource(Res.string.notifications), + supporting = stringResource(Res.string.notifications_master_supporting), + checked = settings.enabled, + enabled = true, + ) { on -> + if (on && !permission.granted) permission.request() + save(settings.copy(enabled = on)) + } + } + item { + SwitchRow( + headline = stringResource(Res.string.notification_channel_messages), + supporting = stringResource(Res.string.notifications_messages_supporting), + checked = settings.messages, + enabled = settings.enabled, + ) { save(settings.copy(messages = it)) } + } + item { + SwitchRow( + headline = stringResource(Res.string.notification_channel_needs_you), + supporting = stringResource(Res.string.notifications_needs_you_supporting), + checked = settings.needsYou, + enabled = settings.enabled, + ) { save(settings.copy(needsYou = it)) } + } + item { + SwitchRow( + headline = stringResource(Res.string.notification_channel_group_activity), + supporting = stringResource(Res.string.notifications_group_activity_supporting), + checked = settings.groupActivity, + enabled = settings.enabled, + ) { save(settings.copy(groupActivity = it)) } + } + item { + SwitchRow( + headline = stringResource(Res.string.show_message_text), + supporting = stringResource(Res.string.show_message_text_supporting), + checked = settings.showMessageText, + enabled = settings.enabled && settings.messages, + ) { save(settings.copy(showMessageText = it)) } + } + item { backgroundSection() } + } + } + } + } + } + } +} + +/** + * A list item with a switch, toggled from anywhere on the row: the row is the target, at the + * list item's own height, and the switch only shows the state. + */ +@Composable +fun SwitchRow( + headline: String, + supporting: String?, + checked: Boolean, + enabled: Boolean, + onCheckedChange: (Boolean) -> Unit, +) { + ListItem( + modifier = Modifier.toggleable( + value = checked, + enabled = enabled, + role = Role.Switch, + onValueChange = onCheckedChange, + ), + headlineContent = { Text(headline) }, + supportingContent = supporting?.let { { Text(it) } }, + trailingContent = { Switch(checked = checked, onCheckedChange = null, enabled = enabled) }, + ) +} diff --git a/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/composable/navigation/GizaNavHost.kt b/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/composable/navigation/GizaNavHost.kt index ec310335..df886700 100644 --- a/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/composable/navigation/GizaNavHost.kt +++ b/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/composable/navigation/GizaNavHost.kt @@ -61,6 +61,9 @@ import mobi.giza.compose.database.repository.DatabaseNostrRepository import mobi.giza.compose.database.repository.DatabaseSearchRepository import mobi.giza.compose.GizaRuntime import mobi.giza.compose.notifications.NotificationTaps +import mobi.giza.compose.notifications.NotificationSettingsStore +import mobi.giza.compose.ui.composable.NotificationSettingsScreen +import mobi.giza.compose.ui.composable.navigation.routes.NotificationSettingsRoute import mobi.giza.compose.ui.composable.ActiveProfileScreen import mobi.giza.compose.ui.composable.AddMemberToChatRoomConfirmationScreen import mobi.giza.compose.ui.composable.ChatRoomCreationScreen @@ -915,6 +918,14 @@ fun GizaNavHost( }, ) } + composable { backStackEntry -> + val route = backStackEntry.toRoute() + NotificationSettingsScreen( + activeUserPublicKey = route.activeUserPublicKey, + store = remember(runtime) { NotificationSettingsStore(runtime.auxDatabaseManager.auxDatabase) }, + onNavigateBack = { navController.popBackStack() }, + ) + } composable { backStackEntry -> val route = backStackEntry.toRoute() diff --git a/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/composable/navigation/routes/NotificationSettingsRoute.kt b/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/composable/navigation/routes/NotificationSettingsRoute.kt new file mode 100644 index 00000000..34bb5ef2 --- /dev/null +++ b/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/composable/navigation/routes/NotificationSettingsRoute.kt @@ -0,0 +1,9 @@ +package mobi.giza.compose.ui.composable.navigation.routes + +import kotlinx.serialization.Serializable + +/** An identity's notification switches. docs/notifications-and-background-sync.md, Phase 7. */ +@Serializable +data class NotificationSettingsRoute( + val activeUserPublicKey: String, +): Route() diff --git a/composeApp/src/jvmTest/kotlin/mobi/giza/compose/notifications/NotificationSettingsJvmTest.kt b/composeApp/src/jvmTest/kotlin/mobi/giza/compose/notifications/NotificationSettingsJvmTest.kt new file mode 100644 index 00000000..3a6dad2f --- /dev/null +++ b/composeApp/src/jvmTest/kotlin/mobi/giza/compose/notifications/NotificationSettingsJvmTest.kt @@ -0,0 +1,66 @@ +package mobi.giza.compose.notifications + +import androidx.compose.ui.test.ExperimentalTestApi +import androidx.compose.ui.test.onNodeWithText +import androidx.compose.ui.test.performClick +import androidx.compose.ui.test.runDesktopComposeUiTest +import androidx.room3.Room +import kotlin.test.AfterTest +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertFalse +import kotlin.time.Instant +import kotlinx.coroutines.runBlocking +import mobi.giza.compose.database.GizaDatabase +import mobi.giza.compose.database.builder.getRoomDatabase +import mobi.giza.compose.ui.composable.NotificationSettingsScreen + +/** The switches are stored per identity, and the screen writes them. Phase 7. */ +class NotificationSettingsJvmTest { + + private val db: GizaDatabase = getRoomDatabase(Room.inMemoryDatabaseBuilder()) + private val store = NotificationSettingsStore(db) + private val user = "a".repeat(64) + + @AfterTest + fun closeDb() = db.close() + + private fun ledger() = runBlocking { db.notificationDao().getLedger(user)!! } + + @Test + fun `the defaults are messages and approvals on, group activity off, text shown`() = runBlocking { + db.notificationDao().arm(user, Instant.fromEpochSeconds(1_800_000_000)) + assertEquals(NotificationSettings(), ledger().settings) + assertFalse(ledger().settings.groupActivity) + } + + @Test + fun `saving leaves the cursor alone`() = runBlocking { + db.notificationDao().arm(user, Instant.fromEpochSeconds(1_800_000_000)) + db.notificationDao().advanceCursor(user, 42) + store.save(user, NotificationSettings(messages = false, showMessageText = false)) + assertEquals(NotificationSettings(messages = false, showMessageText = false), ledger().settings) + assertEquals(42, ledger().cursorMessageId) + } + + @OptIn(ExperimentalTestApi::class) + @Test + fun `before arming the screen says why there is nothing to set, and after it a row is a switch`() { + runDesktopComposeUiTest { + setContent { NotificationSettingsScreen(activeUserPublicKey = user, store = store, onNavigateBack = {}) } + waitUntil(timeoutMillis = 5_000) { + onAllNodesWithTextPresent("first sync") + } + + runBlocking { db.notificationDao().arm(user, Instant.fromEpochSeconds(1_800_000_000)) } + waitUntil(timeoutMillis = 5_000) { onAllNodesWithTextPresent("Show message text") } + + onNodeWithText("Show message text").performClick() + waitUntil(timeoutMillis = 5_000) { !ledger().showMessageText } + } + } + + @OptIn(ExperimentalTestApi::class) + private fun androidx.compose.ui.test.ComposeUiTest.onAllNodesWithTextPresent(text: String): Boolean = + onAllNodes(androidx.compose.ui.test.hasText(text, substring = true)).fetchSemanticsNodes().isNotEmpty() +}