Normalizes the click to open relay info dialog

This commit is contained in:
Vitor Pamplona
2025-07-02 14:52:16 -04:00
parent 67dc396588
commit 11d9af41bd
3 changed files with 103 additions and 89 deletions
@@ -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(
@@ -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,
@@ -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) {