From f8b920a128f17374d814a3b38196d71e44e0d0e2 Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Tue, 28 Oct 2025 13:12:36 -0400 Subject: [PATCH] Adds a popup to warn users of the lack of inbox relays --- .../nip65RelayList/Nip65RelayListState.kt | 15 ++ .../notifications/AddInboxRelayCard.kt | 130 ++++++++++++++++++ .../notifications/AddInboxRelayListDialog.kt | 118 ++++++++++++++++ .../notifications/NotificationScreen.kt | 1 + .../relays/nip65/Nip65RelayListView.kt | 20 +++ amethyst/src/main/res/values/strings.xml | 6 + 6 files changed, 290 insertions(+) create mode 100644 amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/notifications/AddInboxRelayCard.kt create mode 100644 amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/notifications/AddInboxRelayListDialog.kt diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip65RelayList/Nip65RelayListState.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip65RelayList/Nip65RelayListState.kt index 60a2fb107..9b343a9e3 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip65RelayList/Nip65RelayListState.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nip65RelayList/Nip65RelayListState.kt @@ -63,6 +63,10 @@ class Nip65RelayListState( fun normalizeNIP65ReadRelayListWithBackup(note: Note): Set = nip65Event(note)?.readRelaysNorm()?.toSet() ?: Constants.bootstrapInbox + fun normalizeNIP65WriteRelayListNoDefaults(note: Note): Set = nip65Event(note)?.writeRelaysNorm()?.toSet() ?: emptySet() + + fun normalizeNIP65ReadRelayListNoDefaults(note: Note): Set = nip65Event(note)?.readRelaysNorm()?.toSet() ?: emptySet() + fun normalizeNIP65AllRelayListWithBackup(note: Note): Set = nip65Event(note)?.relays()?.map { it.relayUrl }?.toSet() ?: Constants.eventFinderRelays fun normalizeNIP65AllRelayListWithBackupNoDefaults(note: Note): Set = nip65Event(note)?.relays()?.map { it.relayUrl }?.toSet() ?: emptySet() @@ -89,6 +93,17 @@ class Nip65RelayListState( emptySet(), ) + val inboxFlowNoDefaults = + getNIP65RelayListFlow() + .map { normalizeNIP65ReadRelayListNoDefaults(it.note) } + .onStart { emit(normalizeNIP65ReadRelayListNoDefaults(nip65ListNote)) } + .flowOn(Dispatchers.IO) + .stateIn( + scope, + SharingStarted.Eagerly, + emptySet(), + ) + val allFlowNoDefaults = getNIP65RelayListFlow() .map { normalizeNIP65AllRelayListWithBackupNoDefaults(it.note) } diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/notifications/AddInboxRelayCard.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/notifications/AddInboxRelayCard.kt new file mode 100644 index 000000000..a98d8401f --- /dev/null +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/notifications/AddInboxRelayCard.kt @@ -0,0 +1,130 @@ +/** + * Copyright (c) 2025 Vitor Pamplona + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the + * Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN + * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.vitorpamplona.amethyst.ui.screen.loggedIn.notifications + +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.Spacer +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.material3.Button +import androidx.compose.material3.Card +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.setValue +import androidx.compose.ui.Modifier +import androidx.compose.ui.text.TextStyle +import androidx.compose.ui.text.font.FontWeight +import androidx.compose.ui.tooling.preview.Preview +import androidx.compose.ui.unit.sp +import androidx.lifecycle.compose.collectAsStateWithLifecycle +import com.vitorpamplona.amethyst.R +import com.vitorpamplona.amethyst.ui.navigation.navs.EmptyNav +import com.vitorpamplona.amethyst.ui.navigation.navs.INav +import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel +import com.vitorpamplona.amethyst.ui.screen.loggedIn.mockAccountViewModel +import com.vitorpamplona.amethyst.ui.stringRes +import com.vitorpamplona.amethyst.ui.theme.BigPadding +import com.vitorpamplona.amethyst.ui.theme.StdPadding +import com.vitorpamplona.amethyst.ui.theme.StdVertSpacer +import com.vitorpamplona.amethyst.ui.theme.ThemeComparisonColumn +import com.vitorpamplona.amethyst.ui.theme.imageModifier + +@Preview +@Composable +fun AddInboxRelayCardPreview() { + ThemeComparisonColumn { + AddInboxRelayCard( + accountViewModel = mockAccountViewModel(), + nav = EmptyNav, + ) + } +} + +@Composable +fun ObserveInboxRelayListAndDisplayIfNotFound( + accountViewModel: AccountViewModel, + nav: INav, +) { + val inboxRelayList by accountViewModel.account.nip65RelayList.inboxFlowNoDefaults + .collectAsStateWithLifecycle() + + if (inboxRelayList.isEmpty()) { + AddInboxRelayCard( + accountViewModel = accountViewModel, + nav = nav, + ) + } +} + +@Composable +fun AddInboxRelayCard( + accountViewModel: AccountViewModel, + nav: INav, +) { + Column(modifier = StdPadding) { + Card( + modifier = MaterialTheme.colorScheme.imageModifier, + ) { + Column( + modifier = BigPadding, + ) { + // Title + Text( + text = stringRes(id = R.string.inbox_relays_not_found), + style = + TextStyle( + fontSize = 20.sp, + fontWeight = FontWeight.Bold, + ), + ) + + Spacer(modifier = StdVertSpacer) + + Text( + text = stringRes(id = R.string.inbox_relays_not_found_description), + ) + + Spacer(modifier = StdVertSpacer) + + var wantsToEditRelays by remember { mutableStateOf(false) } + if (wantsToEditRelays) { + AddInboxRelayListDialog( + { wantsToEditRelays = false }, + accountViewModel, + nav = nav, + ) + } + + Button( + onClick = { + wantsToEditRelays = true + }, + modifier = Modifier.fillMaxWidth(), + ) { + Text(text = stringRes(id = R.string.dm_relays_not_found_create_now)) + } + } + } + } +} diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/notifications/AddInboxRelayListDialog.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/notifications/AddInboxRelayListDialog.kt new file mode 100644 index 000000000..0045a6e0e --- /dev/null +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/notifications/AddInboxRelayListDialog.kt @@ -0,0 +1,118 @@ +/** + * Copyright (c) 2025 Vitor Pamplona + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the + * Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN + * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.vitorpamplona.amethyst.ui.screen.loggedIn.notifications + +import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.Spacer +import androidx.compose.foundation.layout.padding +import androidx.compose.material3.Card +import androidx.compose.material3.ExperimentalMaterial3Api +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.Scaffold +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect +import androidx.compose.ui.Modifier +import androidx.compose.ui.unit.dp +import androidx.compose.ui.window.Dialog +import androidx.compose.ui.window.DialogProperties +import androidx.lifecycle.viewmodel.compose.viewModel +import com.vitorpamplona.amethyst.R +import com.vitorpamplona.amethyst.ui.components.SetDialogToEdgeToEdge +import com.vitorpamplona.amethyst.ui.navigation.navs.INav +import com.vitorpamplona.amethyst.ui.navigation.topbars.SavingTopBar +import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel +import com.vitorpamplona.amethyst.ui.screen.loggedIn.relays.nip65.Nip65InboxRelayList +import com.vitorpamplona.amethyst.ui.screen.loggedIn.relays.nip65.Nip65RelayListViewModel +import com.vitorpamplona.amethyst.ui.stringRes +import com.vitorpamplona.amethyst.ui.theme.StdVertSpacer +import com.vitorpamplona.amethyst.ui.theme.imageModifier + +@OptIn(ExperimentalMaterial3Api::class) +@Composable +fun AddInboxRelayListDialog( + onClose: () -> Unit, + accountViewModel: AccountViewModel, + nav: INav, +) { + val postViewModel: Nip65RelayListViewModel = viewModel() + + postViewModel.init(accountViewModel) + + LaunchedEffect(postViewModel, accountViewModel.account) { + postViewModel.load() + } + + Dialog( + onDismissRequest = onClose, + properties = DialogProperties(usePlatformDefaultWidth = false), + ) { + SetDialogToEdgeToEdge() + Scaffold( + topBar = { + SavingTopBar( + titleRes = R.string.inbox_relays_title, + onCancel = { + postViewModel.clear() + onClose() + }, + onPost = { + postViewModel.create() + onClose() + }, + ) + }, + ) { pad -> + Column( + modifier = + Modifier.padding( + 16.dp, + pad.calculateTopPadding(), + 16.dp, + pad.calculateBottomPadding(), + ), + verticalArrangement = Arrangement.SpaceAround, + ) { + Explanation(postViewModel) + + Nip65InboxRelayList(postViewModel, accountViewModel, onClose, nav) + } + } + } +} + +@Composable +private fun Explanation(postViewModel: Nip65RelayListViewModel) { + Card(modifier = MaterialTheme.colorScheme.imageModifier) { + Column(modifier = Modifier.padding(16.dp)) { + Text( + text = stringRes(id = R.string.inbox_relays_not_found_editing), + ) + + Spacer(modifier = StdVertSpacer) + + Text( + text = stringRes(id = R.string.inbox_relays_not_found_examples), + ) + } + } +} diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/notifications/NotificationScreen.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/notifications/NotificationScreen.kt index 13c85120e..c4ad71232 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/notifications/NotificationScreen.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/notifications/NotificationScreen.kt @@ -87,6 +87,7 @@ fun NotificationScreen( Column( modifier = Modifier.padding(it).consumeWindowInsets(it), ) { + ObserveInboxRelayListAndDisplayIfNotFound(accountViewModel, nav) RefreshableCardView( feedContent = notifFeedContentState, accountViewModel = accountViewModel, diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/relays/nip65/Nip65RelayListView.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/relays/nip65/Nip65RelayListView.kt index 5007073ba..5761fdf87 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/relays/nip65/Nip65RelayListView.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/relays/nip65/Nip65RelayListView.kt @@ -61,6 +61,26 @@ fun Nip65RelayList( } } +@Composable +fun Nip65InboxRelayList( + postViewModel: Nip65RelayListViewModel, + accountViewModel: AccountViewModel, + onClose: () -> Unit, + nav: INav, +) { + val newNav = rememberExtendedNav(nav, onClose) + + val notifFeedState by postViewModel.notificationRelays.collectAsStateWithLifecycle() + + Row(verticalAlignment = Alignment.CenterVertically) { + LazyColumn( + contentPadding = FeedPadding, + ) { + renderNip65NotifItems(notifFeedState, postViewModel, accountViewModel, newNav) + } + } +} + fun LazyListScope.renderNip65HomeItems( feedState: List, postViewModel: Nip65RelayListViewModel, diff --git a/amethyst/src/main/res/values/strings.xml b/amethyst/src/main/res/values/strings.xml index 0a9b4446d..6ff72c3d3 100644 --- a/amethyst/src/main/res/values/strings.xml +++ b/amethyst/src/main/res/values/strings.xml @@ -1146,6 +1146,12 @@ Insert between 1–3 relays to use when searching for content or tagging users. Make sure your chosen relays implement NIP-50 Good options are:\n - nostr.wine\n - relay.nostr.band\n - relay.noswhere.com + Inbox Relays + Set up your Public Inbox relays to receive notifications + Creating a relay list specifically designed to receive notifications is crucial for your Nostr experience. + Insert between 1–3 relays that receive notifications for you. Make sure they don\'t require payment or web of trust from the sender otherwise you will only receive notifications from payers or long-term users + Good options are:\n - nos.lol\n - nostr.mom\n - nostr.bitcoiner.social + DM Upload Relay Settings Public Outbox/Home Relays