From 11d9af41bd89b93f9b7d6d92ad6d60b1d7dd9ef9 Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Wed, 2 Jul 2025 14:52:16 -0400 Subject: [PATCH] Normalizes the click to open relay info dialog --- .../ui/actions/RelaySelectionDialog.kt | 151 ++++++++++-------- .../feed/types/RenderCreateChannelNote.kt | 21 ++- .../common/BasicRelaySetupInfoDialog.kt | 20 +-- 3 files changed, 103 insertions(+), 89 deletions(-) diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/actions/RelaySelectionDialog.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/actions/RelaySelectionDialog.kt index 4b27cf41f..89639c711 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/actions/RelaySelectionDialog.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/actions/RelaySelectionDialog.kt @@ -47,13 +47,16 @@ import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.unit.dp import androidx.compose.ui.window.Dialog import androidx.compose.ui.window.DialogProperties +import com.google.common.collect.Multimaps.index import com.vitorpamplona.amethyst.R import com.vitorpamplona.amethyst.service.Nip11Retriever import com.vitorpamplona.amethyst.ui.components.SetDialogToEdgeToEdge +import com.vitorpamplona.amethyst.ui.navigation.EmptyNav.nav import com.vitorpamplona.amethyst.ui.navigation.INav import com.vitorpamplona.amethyst.ui.note.buttons.CloseButton import com.vitorpamplona.amethyst.ui.note.buttons.SaveButton import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel +import com.vitorpamplona.amethyst.ui.screen.loggedIn.chats.publicChannels.ephemChat.header.loadRelayInfo import com.vitorpamplona.amethyst.ui.screen.loggedIn.relays.RelayInformationDialog import com.vitorpamplona.amethyst.ui.stringRes import com.vitorpamplona.amethyst.ui.theme.FeedPadding @@ -84,8 +87,6 @@ fun RelaySelectionDialog( accountViewModel: AccountViewModel, nav: INav, ) { - val context = LocalContext.current - var relays by remember { mutableStateOf( accountViewModel.account.client.connectedRelayList().map { @@ -99,18 +100,6 @@ fun RelaySelectionDialog( val hasSelectedRelay by remember { derivedStateOf { relays.any { it.isSelected } } } - var relayInfo: RelayInfoDialog? by remember { mutableStateOf(null) } - - relayInfo?.let { - RelayInformationDialog( - onClose = { relayInfo = null }, - relayInfo = it.relayInfo, - relay = it.relay, - accountViewModel = accountViewModel, - nav = nav, - ) - } - var selected by remember { mutableStateOf(true) } Dialog( @@ -156,7 +145,7 @@ fun RelaySelectionDialog( Modifier.padding(pad).padding(start = 10.dp, end = 10.dp, top = 10.dp), ) { RelaySwitch( - text = stringRes(context, R.string.select_deselect_all), + text = stringRes(R.string.select_deselect_all), checked = selected, onClick = { selected = !selected @@ -171,9 +160,8 @@ fun RelaySelectionDialog( relays, key = { _, item -> item.relay }, ) { index, item -> - RelaySwitch( - text = item.relay.displayUrl(), - checked = item.isSelected, + RenderRelaySwitch( + item, onClick = { relays = relays.mapIndexed { j, item -> @@ -184,56 +172,8 @@ fun RelaySelectionDialog( } } }, - onLongPress = { - accountViewModel.retrieveRelayDocument( - relay = item.relay, - onInfo = { - relayInfo = - RelayInfoDialog( - item.relay, - it, - ) - }, - onError = { relay, errorCode, exceptionMessage -> - val msg = - when (errorCode) { - Nip11Retriever.ErrorCode.FAIL_TO_ASSEMBLE_URL -> - stringRes( - context, - R.string.relay_information_document_error_assemble_url, - relay.url, - exceptionMessage, - ) - Nip11Retriever.ErrorCode.FAIL_TO_REACH_SERVER -> - stringRes( - context, - R.string.relay_information_document_error_assemble_url, - relay.url, - exceptionMessage, - ) - Nip11Retriever.ErrorCode.FAIL_TO_PARSE_RESULT -> - stringRes( - context, - R.string.relay_information_document_error_assemble_url, - relay.url, - exceptionMessage, - ) - Nip11Retriever.ErrorCode.FAIL_WITH_HTTP_STATUS -> - stringRes( - context, - R.string.relay_information_document_error_assemble_url, - relay.url, - exceptionMessage, - ) - } - - accountViewModel.toastManager.toast( - stringRes(context, R.string.unable_to_download_relay_document), - msg, - ) - }, - ) - }, + accountViewModel, + nav, ) } } @@ -242,6 +182,81 @@ fun RelaySelectionDialog( } } +@Composable +fun RenderRelaySwitch( + item: RelayList, + onClick: () -> Unit, + accountViewModel: AccountViewModel, + nav: INav, +) { + val context = LocalContext.current + + val relayInfo by loadRelayInfo(item.relay, accountViewModel) + + var openRelayDialog by remember { mutableStateOf(false) } + + if (openRelayDialog) { + RelayInformationDialog( + onClose = { openRelayDialog = false }, + relayInfo = relayInfo, + relay = item.relay, + accountViewModel = accountViewModel, + nav = nav, + ) + } + + RelaySwitch( + text = item.relay.displayUrl(), + checked = item.isSelected, + onClick = onClick, + onLongPress = { + openRelayDialog = true + accountViewModel.retrieveRelayDocument( + relay = item.relay, + onInfo = { }, + onError = { relay, errorCode, exceptionMessage -> + val msg = + when (errorCode) { + Nip11Retriever.ErrorCode.FAIL_TO_ASSEMBLE_URL -> + stringRes( + context, + R.string.relay_information_document_error_assemble_url, + relay.url, + exceptionMessage, + ) + Nip11Retriever.ErrorCode.FAIL_TO_REACH_SERVER -> + stringRes( + context, + R.string.relay_information_document_error_assemble_url, + relay.url, + exceptionMessage, + ) + Nip11Retriever.ErrorCode.FAIL_TO_PARSE_RESULT -> + stringRes( + context, + R.string.relay_information_document_error_assemble_url, + relay.url, + exceptionMessage, + ) + Nip11Retriever.ErrorCode.FAIL_WITH_HTTP_STATUS -> + stringRes( + context, + R.string.relay_information_document_error_assemble_url, + relay.url, + exceptionMessage, + ) + } + + accountViewModel.toastManager.toast( + stringRes(context, R.string.unable_to_download_relay_document), + msg, + ) + }, + ) + }, + ) +} + @OptIn(ExperimentalFoundationApi::class) @Composable fun RelaySwitch( diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/chats/feed/types/RenderCreateChannelNote.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/chats/feed/types/RenderCreateChannelNote.kt index aef777bed..289021747 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/chats/feed/types/RenderCreateChannelNote.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/chats/feed/types/RenderCreateChannelNote.kt @@ -244,15 +244,13 @@ fun RenderRelayLinePublicChat( var openRelayDialog by remember { mutableStateOf(false) } if (openRelayDialog) { - relayInfo?.let { - RelayInformationDialog( - onClose = { openRelayDialog = false }, - relayInfo = it, - relay = relay, - accountViewModel = accountViewModel, - nav = nav, - ) - } + RelayInformationDialog( + onClose = { openRelayDialog = false }, + relayInfo = relayInfo, + relay = relay, + accountViewModel = accountViewModel, + nav = nav, + ) } val clipboardManager = LocalClipboardManager.current @@ -263,11 +261,10 @@ fun RenderRelayLinePublicChat( clipboardManager.setText(AnnotatedString(relay.url)) }, onClick = { + openRelayDialog = true accountViewModel.retrieveRelayDocument( relay = relay, - onInfo = { - openRelayDialog = true - }, + onInfo = {}, onError = { relay, errorCode, exceptionMessage -> accountViewModel.toastManager.toast( R.string.unable_to_download_relay_document, diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/relays/common/BasicRelaySetupInfoDialog.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/relays/common/BasicRelaySetupInfoDialog.kt index 465dcf500..fe247aac0 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/relays/common/BasicRelaySetupInfoDialog.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/relays/common/BasicRelaySetupInfoDialog.kt @@ -29,9 +29,9 @@ import androidx.compose.ui.platform.LocalContext import com.vitorpamplona.amethyst.R import com.vitorpamplona.amethyst.model.FeatureSetType import com.vitorpamplona.amethyst.service.Nip11Retriever -import com.vitorpamplona.amethyst.ui.actions.RelayInfoDialog import com.vitorpamplona.amethyst.ui.navigation.INav import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel +import com.vitorpamplona.amethyst.ui.screen.loggedIn.chats.publicChannels.ephemChat.header.loadRelayInfo import com.vitorpamplona.amethyst.ui.screen.loggedIn.relays.RelayInformationDialog import com.vitorpamplona.amethyst.ui.stringRes @@ -42,14 +42,17 @@ fun BasicRelaySetupInfoDialog( accountViewModel: AccountViewModel, nav: INav, ) { - var relayInfo: RelayInfoDialog? by remember { mutableStateOf(null) } val context = LocalContext.current - relayInfo?.let { + val relayInfo by loadRelayInfo(item.relay, accountViewModel) + + var openRelayDialog by remember { mutableStateOf(false) } + + if (openRelayDialog) { RelayInformationDialog( - onClose = { relayInfo = null }, - relayInfo = it.relayInfo, - relay = it.relay, + onClose = { openRelayDialog = false }, + relayInfo = relayInfo, + relay = item.relay, accountViewModel = accountViewModel, nav = nav, ) @@ -62,11 +65,10 @@ fun BasicRelaySetupInfoDialog( onDelete = onDelete, accountViewModel = accountViewModel, onClick = { + openRelayDialog = true accountViewModel.retrieveRelayDocument( relay = item.relay, - onInfo = { - relayInfo = RelayInfoDialog(item.relay, it) - }, + onInfo = { }, onError = { relay, errorCode, exceptionMessage -> val msg = when (errorCode) {