diff --git a/composeApp/src/commonMain/composeResources/values/strings.xml b/composeApp/src/commonMain/composeResources/values/strings.xml index e7914059..c3212204 100644 --- a/composeApp/src/commonMain/composeResources/values/strings.xml +++ b/composeApp/src/commonMain/composeResources/values/strings.xml @@ -881,4 +881,37 @@ Mark as read Reply Message + + Disappearing messages + How long the messages you send last, in direct messages and in quorum and confederation conversations + How long the messages you send here last + Off + Custom… + Use your default (%1$s) + Default (%1$s) + Messages you send are deleted from your devices and from the recipient's Giza after this time, and relays are asked to delete them too. Other apps, screenshots and relays that ignore the request can keep a copy. + If someone does not open Giza within this time, they may never receive the message. + Signing and key ceremony messages always expire. They are kept for at least a day, or a week for key ceremonies, and are not deleted from this device until the ceremony they belong to has finished. + A group's own room follows the group's setting, not this one. + Disappearing messages set to %1$s + Custom length + Length + Choose between 5 minutes and 365 days + Minutes + Hours + Days + Set + Disappears %1$s + Messages you send here disappear after %1$s + New messages you send here disappear after %1$s. + Change + Got it + 1 minute + %1$d minutes + 1 hour + %1$d hours + 1 day + %1$d days + 1 week + %1$d weeks diff --git a/composeApp/src/commonMain/kotlin/mobi/giza/compose/database/dao/DisappearingDao.kt b/composeApp/src/commonMain/kotlin/mobi/giza/compose/database/dao/DisappearingDao.kt index 313a63e7..20dbbc92 100644 --- a/composeApp/src/commonMain/kotlin/mobi/giza/compose/database/dao/DisappearingDao.kt +++ b/composeApp/src/commonMain/kotlin/mobi/giza/compose/database/dao/DisappearingDao.kt @@ -46,6 +46,9 @@ abstract class DisappearingDao { @Query("SELECT disappearingNoticeShownAt FROM ChatRoom WHERE id = :chatRoomId") abstract suspend fun noticeShownAt(chatRoomId: String): Instant? + @Query("SELECT disappearingNoticeShownAt IS NOT NULL FROM ChatRoom WHERE id = :chatRoomId") + abstract fun observeNoticeShown(chatRoomId: String): Flow + @Query("UPDATE ChatRoom SET disappearingNoticeShownAt = :at WHERE id = :chatRoomId AND disappearingNoticeShownAt IS NULL") abstract suspend fun markNoticeShown(chatRoomId: String, at: Instant): Int diff --git a/composeApp/src/commonMain/kotlin/mobi/giza/compose/database/repository/DisappearingSettingsStore.kt b/composeApp/src/commonMain/kotlin/mobi/giza/compose/database/repository/DisappearingSettingsStore.kt index 4b6e833f..27ef3314 100644 --- a/composeApp/src/commonMain/kotlin/mobi/giza/compose/database/repository/DisappearingSettingsStore.kt +++ b/composeApp/src/commonMain/kotlin/mobi/giza/compose/database/repository/DisappearingSettingsStore.kt @@ -3,6 +3,7 @@ package mobi.giza.compose.database.repository import com.vitorpamplona.quartz.nip01Core.core.HexKey import com.vitorpamplona.quartz.nip01Core.core.Kind import kotlin.time.Clock +import kotlin.time.Instant import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.map import mobi.giza.compose.database.GizaDatabase @@ -36,6 +37,14 @@ class DisappearingSettingsStore(private val database: GizaDatabase) { database.disappearingDao().setRoomOverride(chatRoomId, seconds) } + /** Whether the one-time notice has been shown in [chatRoomId] (D9); a room not found reads as shown. */ + fun observeNoticeShown(chatRoomId: String): Flow = + database.disappearingDao().observeNoticeShown(chatRoomId).map { it ?: true } + + suspend fun markNoticeShown(chatRoomId: String, at: Instant) { + database.disappearingDao().markNoticeShown(chatRoomId, at) + } + /** The policy a message queued now in [chatRoomId] by [userPublicKey] is stamped with. */ suspend fun policyFor(chatRoomId: String, userPublicKey: HexKey): Long = DisappearingMessages.policyFor(database.disappearingDao().getRoomOverride(chatRoomId), defaultFor(userPublicKey)) 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 d29036b5..d1caae9a 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 @@ -1,5 +1,8 @@ package mobi.giza.compose.ui.composable +import mobi.giza.compose.ui.composable.navigation.routes.DisappearingMessagesRoute +import mobi.giza.compose.resources.disappearing_messages +import androidx.compose.material.icons.filled.HourglassBottom import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Row @@ -375,6 +378,29 @@ fun ActiveProfileScreen( } } + // How long what this profile writes in NIP-17 conversations lasts. + // docs/disappearing-messages.md, Phase 6. + if (canSign) item { + TextButton( + onClick = { + onNavigateToRoute.invoke( + DisappearingMessagesRoute(activeUserPublicKey = activeUserPublicKey) + ) + } + ) { + Icon( + Icons.Default.HourglassBottom, + contentDescription = Decorative + ) + + Spacer( + modifier = Modifier.width(MaterialTheme.spacing.space125) + ) + + Text(stringResource(Res.string.disappearing_messages)) + } + } + 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 c0900421..d2c57b59 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 @@ -1,5 +1,11 @@ package mobi.giza.compose.ui.composable +import mobi.giza.compose.ui.composable.navigation.routes.DisappearingMessagesRoute +import mobi.giza.compose.resources.disappearing_messages +import mobi.giza.compose.resources.disappearing_messages_room_supporting +import androidx.compose.material.icons.filled.HourglassBottom +import androidx.compose.foundation.clickable +import androidx.compose.material3.minimumInteractiveComponentSize import mobi.giza.compose.ui.composable.widgets.LocalCanSign import mobi.giza.compose.resources.propose_event import mobi.giza.compose.ui.composable.navigation.routes.ProposeGroupEventRoute @@ -598,6 +604,24 @@ fun ChatRoomDetailScreen( } } + // How long what the user writes here lasts. Local, like muting, and only in + // a NIP-17 room: an MLS room follows the group's own retention. + // docs/disappearing-messages.md, D4. + if (chatRoomDetailUIState.localChatRoom.chatRoom.mlsGroupState == null) item(key = "disappearing") { + val room = chatRoomDetailUIState.localChatRoom.chatRoom + ListItem( + modifier = Modifier.minimumInteractiveComponentSize().clickable { + onNavigateToRoute.invoke(DisappearingMessagesRoute(activeUserPublicKey, room.id)) + }, + leadingContent = { Icon(Icons.Default.HourglassBottom, contentDescription = Decorative) }, + headlineContent = { Text(stringResource(Res.string.disappearing_messages)) }, + // The fixed description, not the current length: this screen reads the + // room once, so a length shown here would be stale after a change. + supportingContent = { Text(stringResource(Res.string.disappearing_messages_room_supporting)) }, + trailingContent = { Icon(Icons.Default.ChevronRight, contentDescription = Decorative) }, + ) + } + // 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/ChatRoomMessagingScreen.kt b/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/composable/ChatRoomMessagingScreen.kt index 58cb2cba..87ec5ca6 100644 --- a/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/composable/ChatRoomMessagingScreen.kt +++ b/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/composable/ChatRoomMessagingScreen.kt @@ -1,5 +1,9 @@ package mobi.giza.compose.ui.composable +import androidx.compose.runtime.remember +import mobi.giza.compose.GizaRuntime +import mobi.giza.compose.database.repository.DisappearingSettingsStore +import mobi.giza.compose.ui.composable.navigation.routes.DisappearingMessagesRoute import androidx.compose.foundation.background import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Box @@ -97,6 +101,10 @@ fun ChatRoomMessagingScreen( * phone the same screen is a destination, and the button is what leaves it. */ onNavigateBack: (() -> Unit)?, + /** How long what the user writes here lasts (docs/disappearing-messages.md, D12); none in a preview. */ + disappearingSettings: DisappearingSettingsStore? = remember { + GizaRuntime.current()?.let { DisappearingSettingsStore(it.auxDatabaseManager.auxDatabase) } + }, ) { val chatRoomMessagingViewModel: ChatRoomMessagingViewModel = viewModel( factory = ChatRoomMessagingViewModel.factory( @@ -156,6 +164,12 @@ fun ChatRoomMessagingScreen( if (onNavigateBack != null) NavigateBackButton(onNavigateBack) }, actions = { + // A NIP-17 conversation's timer: an MLS room follows the group's own retention. + if (disappearingSettings != null && chatRoomDetailUIState.localChatRoom.chatRoom.mlsGroupState == null) { + DisappearingTimerAction(chatRoomId, activeUserPublicKey, disappearingSettings) { + onNavigateToRoute.invoke(DisappearingMessagesRoute(activeUserPublicKey, chatRoomId)) + } + } IconButton( onClick = { // Navigate to the invite user screen... @@ -180,6 +194,11 @@ fun ChatRoomMessagingScreen( Column( modifier = Modifier.padding(innerPadding).readableContent().fillMaxSize() ) { + if (disappearingSettings != null && chatRoomDetailUIState.localChatRoom.chatRoom.mlsGroupState == null) { + DisappearingNotice(chatRoomId, activeUserPublicKey, disappearingSettings) { + onNavigateToRoute.invoke(DisappearingMessagesRoute(activeUserPublicKey, chatRoomId)) + } + } Column( modifier = Modifier.weight(1f).fillMaxWidth() ) { diff --git a/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/composable/DisappearingMessagesScreen.kt b/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/composable/DisappearingMessagesScreen.kt new file mode 100644 index 00000000..1ae11135 --- /dev/null +++ b/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/composable/DisappearingMessagesScreen.kt @@ -0,0 +1,375 @@ +package mobi.giza.compose.ui.composable + +import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.layout.size +import androidx.compose.foundation.lazy.LazyColumn +import androidx.compose.foundation.selection.selectable +import androidx.compose.foundation.selection.selectableGroup +import androidx.compose.foundation.text.KeyboardOptions +import androidx.compose.material.icons.Icons +import androidx.compose.material.icons.filled.HourglassBottom +import androidx.compose.material3.AlertDialog +import androidx.compose.material3.Card +import androidx.compose.material3.ExperimentalMaterial3Api +import androidx.compose.material3.Icon +import androidx.compose.material3.IconButton +import androidx.compose.material3.ListItem +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.OutlinedTextField +import androidx.compose.material3.RadioButton +import androidx.compose.material3.Scaffold +import androidx.compose.material3.SegmentedButton +import androidx.compose.material3.SegmentedButtonDefaults +import androidx.compose.material3.SingleChoiceSegmentedButtonRow +import androidx.compose.material3.Text +import androidx.compose.material3.TextButton +import androidx.compose.material3.TopAppBar +import androidx.compose.runtime.Composable +import androidx.compose.runtime.collectAsState +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.rememberCoroutineScope +import androidx.compose.runtime.setValue +import androidx.compose.ui.Modifier +import androidx.compose.ui.semantics.Role +import androidx.compose.ui.text.input.KeyboardType +import androidx.compose.ui.unit.dp +import kotlin.time.Clock +import kotlin.time.Instant +import kotlinx.coroutines.flow.combine +import kotlinx.coroutines.flow.map +import kotlinx.coroutines.launch +import mobi.giza.compose.database.repository.DisappearingSettingsStore +import mobi.giza.compose.extensions.toFormattedTimeAndDateString +import mobi.giza.compose.nostr.DisappearingMessages +import mobi.giza.compose.resources.Res +import mobi.giza.compose.resources.cancel +import mobi.giza.compose.resources.disappearing_custom +import mobi.giza.compose.resources.disappearing_custom_amount +import mobi.giza.compose.resources.disappearing_custom_out_of_range +import mobi.giza.compose.resources.disappearing_custom_title +import mobi.giza.compose.resources.disappearing_default_value +import mobi.giza.compose.resources.disappearing_explanation +import mobi.giza.compose.resources.disappearing_groups_note +import mobi.giza.compose.resources.disappearing_messages +import mobi.giza.compose.resources.disappearing_notice +import mobi.giza.compose.resources.disappearing_notice_change +import mobi.giza.compose.resources.disappearing_notice_dismiss +import mobi.giza.compose.resources.disappearing_off +import mobi.giza.compose.resources.disappearing_offline_warning +import mobi.giza.compose.resources.disappearing_protocol_note +import mobi.giza.compose.resources.disappearing_saved +import mobi.giza.compose.resources.disappearing_set +import mobi.giza.compose.resources.disappearing_timer_description +import mobi.giza.compose.resources.disappearing_unit_days +import mobi.giza.compose.resources.disappearing_unit_hours +import mobi.giza.compose.resources.disappearing_unit_minutes +import mobi.giza.compose.resources.disappearing_use_default +import mobi.giza.compose.resources.disappears_at +import mobi.giza.compose.resources.duration_days +import mobi.giza.compose.resources.duration_hours +import mobi.giza.compose.resources.duration_minutes +import mobi.giza.compose.resources.duration_one_day +import mobi.giza.compose.resources.duration_one_hour +import mobi.giza.compose.resources.duration_one_minute +import mobi.giza.compose.resources.duration_one_week +import mobi.giza.compose.resources.duration_weeks +import mobi.giza.compose.ui.composable.widgets.Decorative +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.composable.widgets.rememberNotifier +import mobi.giza.compose.ui.theme.readableContent +import mobi.giza.compose.ui.theme.spacing +import org.jetbrains.compose.resources.getString +import org.jetbrains.compose.resources.stringResource + +/** + * A length as a person reads it: the largest unit that divides it exactly, so a preset reads + * "1 week" and a custom ninety minutes reads "90 minutes". Off is "Off". + */ +@Composable +fun disappearingLengthText(seconds: Long): String { + val (unit, count) = DisappearingLength.split(seconds) ?: return stringResource(Res.string.disappearing_off) + return when (unit) { + DisappearingLength.Unit.WEEKS -> if (count == 1L) stringResource(Res.string.duration_one_week) else stringResource(Res.string.duration_weeks, count) + DisappearingLength.Unit.DAYS -> if (count == 1L) stringResource(Res.string.duration_one_day) else stringResource(Res.string.duration_days, count) + DisappearingLength.Unit.HOURS -> if (count == 1L) stringResource(Res.string.duration_one_hour) else stringResource(Res.string.duration_hours, count) + DisappearingLength.Unit.MINUTES -> if (count == 1L) stringResource(Res.string.duration_one_minute) else stringResource(Res.string.duration_minutes, count) + } +} + +/** The arithmetic under [disappearingLengthText] and the custom dialog, kept out of composition so it can be tested. */ +object DisappearingLength { + enum class Unit(val seconds: Long) { + MINUTES(DisappearingMessages.MINUTE), + HOURS(DisappearingMessages.HOUR), + DAYS(DisappearingMessages.DAY), + WEEKS(7 * DisappearingMessages.DAY), + } + + /** The largest unit [seconds] is a whole number of, and how many; null for Off. */ + fun split(seconds: Long): Pair? { + if (seconds <= 0) return null + val unit = Unit.entries.reversed().firstOrNull { seconds % it.seconds == 0L } ?: Unit.MINUTES + return unit to seconds / unit.seconds + } + + /** A custom length from what was typed, or null when it is not a whole number in bounds. */ + fun parse(amount: String, unit: Unit): Long? { + val count = amount.trim().toLongOrNull() ?: return null + if (count <= 0 || count > DisappearingMessages.MAX_SECONDS / unit.seconds) return null + return (count * unit.seconds).takeIf { DisappearingMessages.isValidPolicy(it) } + } +} + +private sealed interface DisappearingUIState { + data object Loading : DisappearingUIState + + /** [selected] is null when a conversation follows the default; [default] is the identity's. */ + data class Loaded(val selected: Long?, val default: Long) : DisappearingUIState +} + +/** + * How long what the user writes lasts: their default, or with [chatRoomId] one conversation's + * override, which may also go back to following the default. docs/disappearing-messages.md, D3, + * D4, D7 and D11. + * + * No error or empty state: the value is one row of the local database with a default when absent, + * and a read that fails there has failed for every screen in the app. + */ +@OptIn(ExperimentalMaterial3Api::class) +@Composable +fun DisappearingMessagesScreen( + activeUserPublicKey: String, + chatRoomId: String?, + store: DisappearingSettingsStore, + onNavigateBack: () -> Unit, +) { + val state by remember(activeUserPublicKey, chatRoomId) { + if (chatRoomId == null) { + store.observeDefault(activeUserPublicKey).map { default -> DisappearingUIState.Loaded(default, default) } + } else { + combine(store.observeOverride(chatRoomId), store.observeDefault(activeUserPublicKey)) { override, default -> + DisappearingUIState.Loaded(override, default) + } + } + }.collectAsState(initial = DisappearingUIState.Loading) + + val scope = rememberCoroutineScope() + val notify = rememberNotifier(scope) + var customOpen by remember { mutableStateOf(false) } + + fun save(seconds: Long?, label: String) { + scope.launch { + if (chatRoomId == null) store.saveDefault(activeUserPublicKey, seconds ?: DisappearingMessages.DEFAULT_SECONDS) + else store.saveOverride(chatRoomId, seconds) + notify(getString(Res.string.disappearing_saved, label)) + } + } + + Scaffold( + topBar = { + TopAppBar( + title = { Text(stringResource(Res.string.disappearing_messages)) }, + navigationIcon = { NavigateBackButton(onNavigateBack) }, + ) + }, + ) { innerPadding -> + Column(modifier = Modifier.padding(innerPadding).readableContent()) { + ScreenStateTransition(state) { uiState -> + when (uiState) { + DisappearingUIState.Loading -> LoadingDataIndicator() + is DisappearingUIState.Loaded -> { + val defaultLabel = disappearingLengthText(uiState.default) + val isCustom = uiState.selected != null && uiState.selected !in DisappearingMessages.PRESETS + LazyColumn(modifier = Modifier.selectableGroup()) { + if (chatRoomId != null) item { + val label = stringResource(Res.string.disappearing_use_default, defaultLabel) + ChoiceRow(label, selected = uiState.selected == null) { save(null, defaultLabel) } + } + DisappearingMessages.PRESETS.forEach { preset -> + item(key = preset) { + val label = disappearingLengthText(preset) + ChoiceRow(label, selected = uiState.selected == preset) { save(preset, label) } + } + } + item { + ChoiceRow( + label = stringResource(Res.string.disappearing_custom), + selected = isCustom, + supporting = if (isCustom) disappearingLengthText(uiState.selected!!) else null, + ) { customOpen = true } + } + item { + Column( + modifier = Modifier.padding(MaterialTheme.spacing.screenMargin), + verticalArrangement = Arrangement.spacedBy(MaterialTheme.spacing.itemGap), + ) { + Explanation(stringResource(Res.string.disappearing_explanation)) + Explanation(stringResource(Res.string.disappearing_offline_warning)) + Explanation(stringResource(Res.string.disappearing_protocol_note)) + Explanation(stringResource(Res.string.disappearing_groups_note)) + } + } + } + if (customOpen) { + CustomLengthDialog( + initial = uiState.selected?.takeIf { isCustom }, + onDismiss = { customOpen = false }, + onSet = { seconds, label -> + customOpen = false + save(seconds, label) + }, + ) + } + } + } + } + } + } +} + +@Composable +private fun Explanation(text: String) = Text( + text = text, + style = MaterialTheme.typography.bodyMedium, + color = MaterialTheme.colorScheme.onSurfaceVariant, +) + +/** A radio row chosen from anywhere on it: the row is the target, at the list item's height. */ +@Composable +private fun ChoiceRow(label: String, selected: Boolean, supporting: String? = null, onSelect: () -> Unit) { + ListItem( + modifier = Modifier.selectable(selected = selected, role = Role.RadioButton, onClick = onSelect), + leadingContent = { RadioButton(selected = selected, onClick = null) }, + headlineContent = { Text(label) }, + supportingContent = supporting?.let { { Text(it) } }, + ) +} + +@OptIn(ExperimentalMaterial3Api::class) +@Composable +private fun CustomLengthDialog(initial: Long?, onDismiss: () -> Unit, onSet: (Long, String) -> Unit) { + val start = initial?.let { DisappearingLength.split(it) } + ?.let { (unit, count) -> if (unit == DisappearingLength.Unit.WEEKS) DisappearingLength.Unit.DAYS to count * 7 else unit to count } + var amount by remember { mutableStateOf(start?.second?.toString() ?: "") } + var unit by remember { mutableStateOf(start?.first ?: DisappearingLength.Unit.HOURS) } + val parsed = DisappearingLength.parse(amount, unit) + val showError = amount.isNotBlank() && parsed == null + val label = parsed?.let { disappearingLengthText(it) } + val units = listOf( + DisappearingLength.Unit.MINUTES to stringResource(Res.string.disappearing_unit_minutes), + DisappearingLength.Unit.HOURS to stringResource(Res.string.disappearing_unit_hours), + DisappearingLength.Unit.DAYS to stringResource(Res.string.disappearing_unit_days), + ) + + AlertDialog( + onDismissRequest = onDismiss, + title = { Text(stringResource(Res.string.disappearing_custom_title)) }, + text = { + Column(verticalArrangement = Arrangement.spacedBy(MaterialTheme.spacing.itemGap)) { + OutlinedTextField( + value = amount, + onValueChange = { amount = it.filter(Char::isDigit).take(6) }, + label = { Text(stringResource(Res.string.disappearing_custom_amount)) }, + singleLine = true, + isError = showError, + supportingText = { if (showError) Text(stringResource(Res.string.disappearing_custom_out_of_range)) }, + keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number), + modifier = Modifier.fillMaxWidth(), + ) + SingleChoiceSegmentedButtonRow(modifier = Modifier.fillMaxWidth()) { + units.forEachIndexed { index, (option, text) -> + SegmentedButton( + selected = unit == option, + onClick = { unit = option }, + shape = SegmentedButtonDefaults.itemShape(index, units.size), + ) { Text(text) } + } + } + } + }, + confirmButton = { + TextButton(onClick = { if (parsed != null && label != null) onSet(parsed, label) }, enabled = parsed != null) { + Text(stringResource(Res.string.disappearing_set)) + } + }, + dismissButton = { + TextButton(onClick = onDismiss) { Text(stringResource(Res.string.cancel)) } + }, + ) +} + +/** + * The hourglass beside an expiring line's time. Its description carries what the icon shows and + * the text beside it does not: when the line goes (D12). + */ +@Composable +fun DisappearsIcon(expiresAt: Instant) { + Icon( + Icons.Default.HourglassBottom, + contentDescription = stringResource(Res.string.disappears_at, expiresAt.toFormattedTimeAndDateString()), + modifier = Modifier.size(MaterialTheme.typography.labelSmall.lineHeight.value.dp), + tint = MaterialTheme.colorScheme.onSurfaceVariant, + ) +} + +/** + * The conversation's timer in its app bar (D12), opening its setting; nothing while the policy in + * force is Off. + */ +@Composable +fun DisappearingTimerAction(chatRoomId: String, userPublicKey: String, store: DisappearingSettingsStore, onOpen: () -> Unit) { + val policy by remember(chatRoomId, userPublicKey) { + combine(store.observeOverride(chatRoomId), store.observeDefault(userPublicKey)) { override, default -> + DisappearingMessages.policyFor(override, default) + } + }.collectAsState(initial = DisappearingMessages.OFF) + if (policy == DisappearingMessages.OFF) return + IconButton(onClick = onOpen) { + Icon( + Icons.Default.HourglassBottom, + contentDescription = stringResource(Res.string.disappearing_timer_description, disappearingLengthText(policy)), + ) + } +} + +/** + * The one-time notice that what the user writes in this conversation now disappears (D9). Shown + * until dismissed or acted on, once per conversation, and only while a policy is in force. + */ +@Composable +fun DisappearingNotice(chatRoomId: String, userPublicKey: String, store: DisappearingSettingsStore, onChange: () -> Unit) { + val scope = rememberCoroutineScope() + val shown by remember(chatRoomId) { store.observeNoticeShown(chatRoomId) }.collectAsState(initial = true) + val policy by remember(chatRoomId, userPublicKey) { + combine(store.observeOverride(chatRoomId), store.observeDefault(userPublicKey)) { override, default -> + DisappearingMessages.policyFor(override, default) + } + }.collectAsState(initial = DisappearingMessages.OFF) + if (shown || policy == DisappearingMessages.OFF) return + + fun dismiss() = scope.launch { store.markNoticeShown(chatRoomId, Clock.System.now()) } + + Card(modifier = Modifier.fillMaxWidth().padding(MaterialTheme.spacing.compactPadding)) { + Column( + modifier = Modifier.padding(MaterialTheme.spacing.containerPadding), + verticalArrangement = Arrangement.spacedBy(MaterialTheme.spacing.relatedGap), + ) { + Row(horizontalArrangement = Arrangement.spacedBy(MaterialTheme.spacing.relatedGap)) { + Icon(Icons.Default.HourglassBottom, contentDescription = Decorative) + Text(stringResource(Res.string.disappearing_notice, disappearingLengthText(policy))) + } + Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.End) { + TextButton(onClick = { dismiss(); onChange() }) { Text(stringResource(Res.string.disappearing_notice_change)) } + TextButton(onClick = { dismiss() }) { Text(stringResource(Res.string.disappearing_notice_dismiss)) } + } + } + } +} 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 6516af3a..a324d183 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 @@ -1,5 +1,8 @@ package mobi.giza.compose.ui.composable.navigation +import mobi.giza.compose.ui.composable.navigation.routes.DisappearingMessagesRoute +import mobi.giza.compose.ui.composable.DisappearingMessagesScreen +import mobi.giza.compose.database.repository.DisappearingSettingsStore import mobi.giza.compose.ui.composable.navigation.routes.ProposeGroupEventRoute import mobi.giza.compose.ui.composable.ProposeGroupEventScreen import mobi.giza.compose.ui.composable.navigation.routes.BroadcastGroupSignedEventRoute @@ -928,6 +931,15 @@ fun GizaNavHost( onNavigateBack = { navController.popBackStack() }, ) } + composable { backStackEntry -> + val route = backStackEntry.toRoute() + DisappearingMessagesScreen( + activeUserPublicKey = route.activeUserPublicKey, + chatRoomId = route.chatRoomId, + store = remember(runtime) { DisappearingSettingsStore(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/DisappearingMessagesRoute.kt b/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/composable/navigation/routes/DisappearingMessagesRoute.kt new file mode 100644 index 00000000..2ef67a42 --- /dev/null +++ b/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/composable/navigation/routes/DisappearingMessagesRoute.kt @@ -0,0 +1,13 @@ +package mobi.giza.compose.ui.composable.navigation.routes + +import kotlinx.serialization.Serializable + +/** + * How long what the user writes lasts: their default when [chatRoomId] is null, else that + * conversation's override. docs/disappearing-messages.md, Phase 6. + */ +@Serializable +data class DisappearingMessagesRoute( + val activeUserPublicKey: String, + val chatRoomId: String? = null, +): Route() diff --git a/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/composable/widgets/chat/ChatTranscript.kt b/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/composable/widgets/chat/ChatTranscript.kt index 3bdbb693..d7bcbc8d 100644 --- a/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/composable/widgets/chat/ChatTranscript.kt +++ b/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/composable/widgets/chat/ChatTranscript.kt @@ -1,5 +1,6 @@ package mobi.giza.compose.ui.composable.widgets.chat +import mobi.giza.compose.ui.composable.DisappearsIcon import androidx.compose.material.icons.filled.Warning import androidx.compose.foundation.clickable import androidx.compose.foundation.layout.Arrangement @@ -521,6 +522,7 @@ fun ChatTranscript( text = localChatMessage.chatMessage.createdAt.toFormattedTimeAndDateString(), style = MaterialTheme.typography.labelSmall ) + localChatMessage.chatMessage.expiresAt?.let { DisappearsIcon(it) } if (localChatMessage.chatMessage.isUserMessage) { if (localChatMessage.chatMessageBroadcastNostrEventReceiptRelation != null) { Icon( diff --git a/composeApp/src/jvmTest/kotlin/mobi/giza/compose/ui/composable/DisappearingLengthJvmTest.kt b/composeApp/src/jvmTest/kotlin/mobi/giza/compose/ui/composable/DisappearingLengthJvmTest.kt new file mode 100644 index 00000000..9b431e82 --- /dev/null +++ b/composeApp/src/jvmTest/kotlin/mobi/giza/compose/ui/composable/DisappearingLengthJvmTest.kt @@ -0,0 +1,39 @@ +package mobi.giza.compose.ui.composable + +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertNull +import mobi.giza.compose.nostr.DisappearingMessages +import mobi.giza.compose.ui.composable.DisappearingLength.Unit.DAYS +import mobi.giza.compose.ui.composable.DisappearingLength.Unit.HOURS +import mobi.giza.compose.ui.composable.DisappearingLength.Unit.MINUTES +import mobi.giza.compose.ui.composable.DisappearingLength.Unit.WEEKS + +/** docs/disappearing-messages.md, Phase 6: how a length reads, and what the custom dialog accepts (D3). */ +class DisappearingLengthJvmTest { + + @Test + fun `a length reads in the largest unit it is a whole number of`() { + assertNull(DisappearingLength.split(DisappearingMessages.OFF)) + assertEquals(HOURS to 1L, DisappearingLength.split(DisappearingMessages.HOUR)) + assertEquals(HOURS to 8L, DisappearingLength.split(8 * DisappearingMessages.HOUR)) + assertEquals(DAYS to 1L, DisappearingLength.split(DisappearingMessages.DAY)) + assertEquals(WEEKS to 1L, DisappearingLength.split(7 * DisappearingMessages.DAY)) + assertEquals(WEEKS to 4L, DisappearingLength.split(28 * DisappearingMessages.DAY)) + assertEquals(MINUTES to 90L, DisappearingLength.split(90 * DisappearingMessages.MINUTE)) + assertEquals(DAYS to 365L, DisappearingLength.split(DisappearingMessages.MAX_SECONDS)) + } + + @Test + fun `the custom dialog takes whole numbers within five minutes and a year`() { + assertEquals(5 * DisappearingMessages.MINUTE, DisappearingLength.parse("5", MINUTES)) + assertEquals(36 * DisappearingMessages.HOUR, DisappearingLength.parse(" 36 ", HOURS)) + assertEquals(DisappearingMessages.MAX_SECONDS, DisappearingLength.parse("365", DAYS)) + assertNull(DisappearingLength.parse("4", MINUTES)) + assertNull(DisappearingLength.parse("366", DAYS)) + assertNull(DisappearingLength.parse("0", HOURS)) + assertNull(DisappearingLength.parse("", HOURS)) + assertNull(DisappearingLength.parse("1.5", HOURS)) + assertNull(DisappearingLength.parse("99999999999999", DAYS)) + } +} diff --git a/docs/disappearing-messages.md b/docs/disappearing-messages.md index 750b7a3b..438e579b 100644 --- a/docs/disappearing-messages.md +++ b/docs/disappearing-messages.md @@ -603,6 +603,33 @@ Every point in CLAUDE.md applies: strings in `strings.xml` written in sentence c Tests: view-model JVM tests for the picker's validation and for override versus default, plus a string-catalogue entry for every format string this adds. +**Built.** + +- `DisappearingMessagesScreen` sits behind `DisappearingMessagesRoute(activeUserPublicKey, + chatRoomId?)`. It is reached from the profile screen, next to Notifications, for the default, + and from a NIP-17 room's detail screen for that room's override. It shows D3's presets as radio + rows, a custom dialog with a number field and a minutes/hours/days segmented row checked against + the bounds, and D7's and D11's paragraphs. +- The conversation shows `DisappearingTimerAction`, an hourglass in the app bar that opens the + room's setting, and `DisappearingNotice`, D9's one-time card. The transcript shows + `DisappearsIcon` beside an expiring line's time, with the time it goes as its description. +- `ChatRoomMessagingScreen` gets the store through a defaulted parameter that reads the running + `GizaRuntime`, so neither caller changed. A preview has no runtime and shows none of it. +- Lengths read in the largest whole unit ("1 week", "90 minutes"). `DisappearingLengthJvmTest` + covers that and the custom dialog's bounds. The m3, brand and wallet audits pass at their + budgets. + +It differs from the plan in three ways: + +- The room detail row shows a fixed description, not the current length. That screen reads the + room once, so a length shown there would go stale after a change. +- Nothing yet reports a message that expired before it could be sent. The sweeper counts these + (`Swept.unsent`), but there is no path from the runtime's sweeper to a screen's snackbar. +- The transcript lines a ceremony writes ("abandoned the shared key ceremony" and the like) are + local summaries. They have no `giftWrapPayloadId` and no expiry. So they show no hourglass, as + D12 says, but they are also not deleted with their rounds, which is less than D6 says. They hold + text the app wrote, not anybody's words. + ### Phase 7: when a group sends (D10) - `GroupDmSender` and `InboxReplies`: put the rumor expiration in at proposal time, show it on