From 288aafc3a287e5efb5f8a7b1feeee01a83bb2859 Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Wed, 29 May 2024 12:06:45 -0400 Subject: [PATCH] Adds support for search relay lists --- .../vitorpamplona/amethyst/model/Account.kt | 43 ++ .../amethyst/model/LocalCache.kt | 21 +- .../service/NostrAccountDataSource.kt | 6 +- .../ui/actions/relays/AllRelayListView.kt | 29 ++ .../ui/actions/relays/BasicRelaySetupInfo.kt | 36 ++ .../relays/BasicRelaySetupInfoClickableRow.kt | 97 +++++ .../relays/BasicRelaySetupInfoDialog.kt | 111 +++++ .../ui/actions/relays/DMRelayListView.kt | 405 +---------------- .../ui/actions/relays/DMRelayListViewModel.kt | 24 +- .../ui/actions/relays/Nip65RelayListView.kt | 411 +----------------- .../actions/relays/Nip65RelayListViewModel.kt | 34 +- .../relays/RelayNameAndRemoveButton.kt | 88 ++++ .../ui/actions/relays/RelayStatusRow.kt | 180 ++++++++ .../ui/actions/relays/RelayUrlEditField.kt | 87 ++++ .../ui/actions/relays/SearchRelayListView.kt | 77 ++++ .../relays/SearchRelayListViewModel.kt | 109 +++++ .../ui/screen/loggedIn/AccountViewModel.kt | 2 + app/src/main/res/values/strings.xml | 4 +- .../quartz/events/EventFactory.kt | 1 + .../quartz/events/SearchRelayListEvent.kt | 103 +++++ 20 files changed, 1009 insertions(+), 859 deletions(-) create mode 100644 app/src/main/java/com/vitorpamplona/amethyst/ui/actions/relays/BasicRelaySetupInfo.kt create mode 100644 app/src/main/java/com/vitorpamplona/amethyst/ui/actions/relays/BasicRelaySetupInfoClickableRow.kt create mode 100644 app/src/main/java/com/vitorpamplona/amethyst/ui/actions/relays/BasicRelaySetupInfoDialog.kt create mode 100644 app/src/main/java/com/vitorpamplona/amethyst/ui/actions/relays/RelayNameAndRemoveButton.kt create mode 100644 app/src/main/java/com/vitorpamplona/amethyst/ui/actions/relays/RelayStatusRow.kt create mode 100644 app/src/main/java/com/vitorpamplona/amethyst/ui/actions/relays/RelayUrlEditField.kt create mode 100644 app/src/main/java/com/vitorpamplona/amethyst/ui/actions/relays/SearchRelayListView.kt create mode 100644 app/src/main/java/com/vitorpamplona/amethyst/ui/actions/relays/SearchRelayListViewModel.kt create mode 100644 quartz/src/main/java/com/vitorpamplona/quartz/events/SearchRelayListEvent.kt diff --git a/app/src/main/java/com/vitorpamplona/amethyst/model/Account.kt b/app/src/main/java/com/vitorpamplona/amethyst/model/Account.kt index 43a9fa437..f561aa424 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/model/Account.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/model/Account.kt @@ -95,6 +95,7 @@ import com.vitorpamplona.quartz.events.ReportEvent import com.vitorpamplona.quartz.events.RepostEvent import com.vitorpamplona.quartz.events.Response import com.vitorpamplona.quartz.events.SealedGossipEvent +import com.vitorpamplona.quartz.events.SearchRelayListEvent import com.vitorpamplona.quartz.events.StatusEvent import com.vitorpamplona.quartz.events.TextNoteEvent import com.vitorpamplona.quartz.events.TextNoteModificationEvent @@ -2651,6 +2652,48 @@ class Account( } } + fun getSearchRelayListNote(): AddressableNote { + return LocalCache.getOrCreateAddressableNote( + SearchRelayListEvent.createAddressATag(signer.pubKey), + ) + } + + fun getSearchRelayListFlow(): StateFlow { + return getSearchRelayListNote().flow().metadata.stateFlow + } + + fun getSearchRelayList(): SearchRelayListEvent? { + return getSearchRelayListNote().event as? SearchRelayListEvent + } + + fun saveSearchRelayList(searchRelays: List) { + if (!isWriteable()) return + + val relayListForSearch = + LocalCache.getOrCreateAddressableNote( + SearchRelayListEvent.createAddressATag(signer.pubKey), + ).event as? SearchRelayListEvent + + if (relayListForSearch != null && relayListForSearch.tags.isNotEmpty()) { + SearchRelayListEvent.updateRelayList( + earlierVersion = relayListForSearch, + relays = searchRelays, + signer = signer, + ) { + Client.send(it) + LocalCache.justConsume(it, null) + } + } else { + SearchRelayListEvent.createFromScratch( + relays = searchRelays, + signer = signer, + ) { + Client.send(it) + LocalCache.justConsume(it, null) + } + } + } + fun getNIP65RelayListNote(): AddressableNote { return LocalCache.getOrCreateAddressableNote( AdvertisedRelayListEvent.createAddressATag(signer.pubKey), diff --git a/app/src/main/java/com/vitorpamplona/amethyst/model/LocalCache.kt b/app/src/main/java/com/vitorpamplona/amethyst/model/LocalCache.kt index c2616010f..0e8145c6d 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/model/LocalCache.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/model/LocalCache.kt @@ -108,6 +108,7 @@ import com.vitorpamplona.quartz.events.RelaySetEvent import com.vitorpamplona.quartz.events.ReportEvent import com.vitorpamplona.quartz.events.RepostEvent import com.vitorpamplona.quartz.events.SealedGossipEvent +import com.vitorpamplona.quartz.events.SearchRelayListEvent import com.vitorpamplona.quartz.events.StatusEvent import com.vitorpamplona.quartz.events.TextNoteEvent import com.vitorpamplona.quartz.events.TextNoteModificationEvent @@ -928,6 +929,13 @@ object LocalCache { consumeBaseReplaceable(event, relay) } + private fun consume( + event: SearchRelayListEvent, + relay: Relay?, + ) { + consumeBaseReplaceable(event, relay) + } + private fun consume( event: CommunityDefinitionEvent, relay: Relay?, @@ -2506,6 +2514,7 @@ object LocalCache { is ChannelMetadataEvent -> consume(event) is ChannelMuteUserEvent -> consume(event) is ChatMessageEvent -> consume(event, relay) + is ChatMessageRelayListEvent -> consume(event, relay) is ClassifiedsEvent -> consume(event, relay) is CommunityDefinitionEvent -> consume(event, relay) is CommunityListEvent -> consume(event, relay) @@ -2515,11 +2524,13 @@ object LocalCache { } is ContactListEvent -> consume(event) is DeletionEvent -> consume(event) - is ChatMessageRelayListEvent -> consume(event, relay) is DraftEvent -> consume(event, relay) is EmojiPackEvent -> consume(event, relay) is EmojiPackSelectionEvent -> consume(event, relay) - is SealedGossipEvent -> consume(event, relay) + is GenericRepostEvent -> { + event.containedPost()?.let { verifyAndConsume(it, relay) } + consume(event) + } is FhirResourceEvent -> consume(event, relay) is FileHeaderEvent -> consume(event, relay) is FileServersEvent -> consume(event, relay) @@ -2565,10 +2576,8 @@ object LocalCache { event.containedPost()?.let { verifyAndConsume(it, relay) } consume(event) } - is GenericRepostEvent -> { - event.containedPost()?.let { verifyAndConsume(it, relay) } - consume(event) - } + is SealedGossipEvent -> consume(event, relay) + is SearchRelayListEvent -> consume(event, relay) is StatusEvent -> consume(event, relay) is TextNoteEvent -> consume(event, relay) is TextNoteModificationEvent -> consume(event, relay) diff --git a/app/src/main/java/com/vitorpamplona/amethyst/service/NostrAccountDataSource.kt b/app/src/main/java/com/vitorpamplona/amethyst/service/NostrAccountDataSource.kt index ee61046db..b63052cb2 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/service/NostrAccountDataSource.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/service/NostrAccountDataSource.kt @@ -61,6 +61,7 @@ import com.vitorpamplona.quartz.events.ReactionEvent import com.vitorpamplona.quartz.events.ReportEvent import com.vitorpamplona.quartz.events.RepostEvent import com.vitorpamplona.quartz.events.SealedGossipEvent +import com.vitorpamplona.quartz.events.SearchRelayListEvent import com.vitorpamplona.quartz.events.StatusEvent import com.vitorpamplona.quartz.events.TextNoteEvent import com.vitorpamplona.quartz.utils.TimeUtils @@ -102,9 +103,9 @@ object NostrAccountDataSource : NostrDataSource("AccountData") { types = COMMON_FEED_TYPES, filter = JsonFilter( - kinds = listOf(StatusEvent.KIND, AdvertisedRelayListEvent.KIND, ChatMessageRelayListEvent.KIND), + kinds = listOf(StatusEvent.KIND, AdvertisedRelayListEvent.KIND, ChatMessageRelayListEvent.KIND, SearchRelayListEvent.KIND), authors = listOf(account.userProfile().pubkeyHex), - limit = 5, + limit = 10, ), ) } @@ -121,6 +122,7 @@ object NostrAccountDataSource : NostrDataSource("AccountData") { ContactListEvent.KIND, AdvertisedRelayListEvent.KIND, ChatMessageRelayListEvent.KIND, + SearchRelayListEvent.KIND, MuteListEvent.KIND, PeopleListEvent.KIND, ), diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/relays/AllRelayListView.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/relays/AllRelayListView.kt index 0c06955d4..b4f563461 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/relays/AllRelayListView.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/relays/AllRelayListView.kt @@ -73,10 +73,14 @@ fun AllRelayListView( val homeFeedState by nip65ViewModel.homeRelays.collectAsStateWithLifecycle() val notifFeedState by nip65ViewModel.notificationRelays.collectAsStateWithLifecycle() + val searchViewModel: SearchRelayListViewModel = viewModel() + val searchFeedState by searchViewModel.relays.collectAsStateWithLifecycle() + LaunchedEffect(Unit) { kind3ViewModel.load(accountViewModel.account) dmViewModel.load(accountViewModel.account) nip65ViewModel.load(accountViewModel.account) + searchViewModel.load(accountViewModel.account) } Dialog( @@ -97,6 +101,7 @@ fun AllRelayListView( kind3ViewModel.create() dmViewModel.create() nip65ViewModel.create() + searchViewModel.create() onClose() }, true, @@ -157,6 +162,17 @@ fun AllRelayListView( } renderDMItems(dmFeedState, dmViewModel, accountViewModel, onClose, nav) + item { + SettingsCategoryWithButton( + stringResource(R.string.search_section), + stringResource(R.string.search_section_explainer), + action = { + ResetSearchRelays(searchViewModel) + }, + ) + } + renderSearchItems(searchFeedState, searchViewModel, accountViewModel, onClose, nav) + item { SettingsCategoryWithButton( stringResource(R.string.kind_3_section), @@ -186,6 +202,19 @@ fun ResetKind3Relays(postViewModel: Kind3RelayListViewModel) { } } +@Composable +fun ResetSearchRelays(postViewModel: SearchRelayListViewModel) { + Button( + onClick = { + postViewModel.deleteAll() + Constants.forcedRelayForSearch.forEach { postViewModel.addRelay(BasicRelaySetupInfo(it.url)) } + postViewModel.loadRelayDocuments() + }, + ) { + Text(stringResource(R.string.default_relays)) + } +} + @Composable fun SettingsCategory( title: String, diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/relays/BasicRelaySetupInfo.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/relays/BasicRelaySetupInfo.kt new file mode 100644 index 000000000..0f8fc18bc --- /dev/null +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/relays/BasicRelaySetupInfo.kt @@ -0,0 +1,36 @@ +/** + * Copyright (c) 2024 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.actions.relays + +import androidx.compose.runtime.Immutable +import com.vitorpamplona.amethyst.model.RelayBriefInfoCache + +@Immutable +data class BasicRelaySetupInfo( + val url: String, + val errorCount: Int = 0, + val downloadCountInBytes: Int = 0, + val uploadCountInBytes: Int = 0, + val spamCount: Int = 0, + val paidRelay: Boolean = false, +) { + val briefInfo: RelayBriefInfoCache.RelayBriefInfo = RelayBriefInfoCache.RelayBriefInfo(url) +} diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/relays/BasicRelaySetupInfoClickableRow.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/relays/BasicRelaySetupInfoClickableRow.kt new file mode 100644 index 000000000..045421fd6 --- /dev/null +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/relays/BasicRelaySetupInfoClickableRow.kt @@ -0,0 +1,97 @@ +/** + * Copyright (c) 2024 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.actions.relays + +import androidx.compose.foundation.clickable +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.Spacer +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.padding +import androidx.compose.material3.HorizontalDivider +import androidx.compose.material3.MaterialTheme +import androidx.compose.runtime.Composable +import androidx.compose.runtime.remember +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.unit.dp +import com.vitorpamplona.amethyst.service.Nip11CachedRetriever +import com.vitorpamplona.amethyst.ui.note.RenderRelayIcon +import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel +import com.vitorpamplona.amethyst.ui.theme.DividerThickness +import com.vitorpamplona.amethyst.ui.theme.HalfHorzPadding +import com.vitorpamplona.amethyst.ui.theme.HalfStartPadding +import com.vitorpamplona.amethyst.ui.theme.ReactionRowHeightChat +import com.vitorpamplona.amethyst.ui.theme.largeRelayIconModifier + +@Composable +fun BasicRelaySetupInfoClickableRow( + item: BasicRelaySetupInfo, + loadProfilePicture: Boolean, + onDelete: (BasicRelaySetupInfo) -> Unit, + onClick: () -> Unit, + accountViewModel: AccountViewModel, +) { + Column(Modifier.fillMaxWidth()) { + Row( + verticalAlignment = Alignment.CenterVertically, + modifier = Modifier.padding(vertical = 5.dp), + ) { + Column(Modifier.clickable(onClick = onClick)) { + val iconUrlFromRelayInfoDoc = + remember(item) { + Nip11CachedRetriever.getFromCache(item.url)?.icon + } + + RenderRelayIcon( + item.briefInfo.displayUrl, + iconUrlFromRelayInfoDoc ?: item.briefInfo.favIcon, + loadProfilePicture, + MaterialTheme.colorScheme.largeRelayIconModifier, + ) + } + + Spacer(modifier = HalfHorzPadding) + + Column(Modifier.weight(1f)) { + RelayNameAndRemoveButton( + item, + onClick, + onDelete, + ReactionRowHeightChat.fillMaxWidth(), + ) + + Row( + verticalAlignment = Alignment.CenterVertically, + modifier = ReactionRowHeightChat.fillMaxWidth(), + ) { + RelayStatusRow( + item = item, + modifier = HalfStartPadding.weight(1f), + accountViewModel = accountViewModel, + ) + } + } + } + + HorizontalDivider(thickness = DividerThickness) + } +} diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/relays/BasicRelaySetupInfoDialog.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/relays/BasicRelaySetupInfoDialog.kt new file mode 100644 index 000000000..5a4c8069e --- /dev/null +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/relays/BasicRelaySetupInfoDialog.kt @@ -0,0 +1,111 @@ +/** + * Copyright (c) 2024 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.actions.relays + +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.platform.LocalContext +import com.vitorpamplona.amethyst.R +import com.vitorpamplona.amethyst.model.RelayBriefInfoCache +import com.vitorpamplona.amethyst.service.Nip11Retriever +import com.vitorpamplona.amethyst.ui.actions.RelayInfoDialog +import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel + +@Composable +fun BasicRelaySetupInfoDialog( + item: BasicRelaySetupInfo, + onDelete: (BasicRelaySetupInfo) -> Unit, + accountViewModel: AccountViewModel, + nav: (String) -> Unit, +) { + var relayInfo: RelayInfoDialog? by remember { mutableStateOf(null) } + val context = LocalContext.current + + relayInfo?.let { + RelayInformationDialog( + onClose = { relayInfo = null }, + relayInfo = it.relayInfo, + relayBriefInfo = it.relayBriefInfo, + accountViewModel = accountViewModel, + nav = nav, + ) + } + + val automaticallyShowProfilePicture = + remember { + accountViewModel.settings.showProfilePictures.value + } + + BasicRelaySetupInfoClickableRow( + item = item, + loadProfilePicture = automaticallyShowProfilePicture, + onDelete = onDelete, + accountViewModel = accountViewModel, + onClick = { + accountViewModel.retrieveRelayDocument( + item.url, + onInfo = { + relayInfo = RelayInfoDialog(RelayBriefInfoCache.RelayBriefInfo(item.url), it) + }, + onError = { url, errorCode, exceptionMessage -> + val msg = + when (errorCode) { + Nip11Retriever.ErrorCode.FAIL_TO_ASSEMBLE_URL -> + context.getString( + R.string.relay_information_document_error_assemble_url, + url, + exceptionMessage, + ) + + Nip11Retriever.ErrorCode.FAIL_TO_REACH_SERVER -> + context.getString( + R.string.relay_information_document_error_assemble_url, + url, + exceptionMessage, + ) + + Nip11Retriever.ErrorCode.FAIL_TO_PARSE_RESULT -> + context.getString( + R.string.relay_information_document_error_assemble_url, + url, + exceptionMessage, + ) + + Nip11Retriever.ErrorCode.FAIL_WITH_HTTP_STATUS -> + context.getString( + R.string.relay_information_document_error_assemble_url, + url, + exceptionMessage, + ) + } + + accountViewModel.toast( + context.getString(R.string.unable_to_download_relay_document), + msg, + ) + }, + ) + }, + ) +} diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/relays/DMRelayListView.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/relays/DMRelayListView.kt index b3413ee6b..f0f09b413 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/relays/DMRelayListView.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/relays/DMRelayListView.kt @@ -20,72 +20,19 @@ */ package com.vitorpamplona.amethyst.ui.actions.relays -import android.widget.Toast -import androidx.compose.foundation.ExperimentalFoundationApi -import androidx.compose.foundation.clickable -import androidx.compose.foundation.combinedClickable -import androidx.compose.foundation.layout.Arrangement -import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.Spacer -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.lazy.LazyListScope import androidx.compose.foundation.lazy.itemsIndexed -import androidx.compose.material.icons.Icons -import androidx.compose.material.icons.filled.Cancel -import androidx.compose.material.icons.filled.DeleteSweep -import androidx.compose.material.icons.filled.Download -import androidx.compose.material.icons.filled.Paid -import androidx.compose.material.icons.filled.SyncProblem -import androidx.compose.material.icons.filled.Upload -import androidx.compose.material3.Button -import androidx.compose.material3.ButtonDefaults -import androidx.compose.material3.HorizontalDivider -import androidx.compose.material3.Icon -import androidx.compose.material3.IconButton -import androidx.compose.material3.MaterialTheme -import androidx.compose.material3.OutlinedTextField -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.rememberCoroutineScope import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment -import androidx.compose.ui.Modifier -import androidx.compose.ui.graphics.Color -import androidx.compose.ui.platform.LocalContext -import androidx.compose.ui.res.stringResource -import androidx.compose.ui.text.style.TextOverflow -import androidx.compose.ui.unit.dp -import androidx.compose.ui.unit.sp import androidx.lifecycle.compose.collectAsStateWithLifecycle -import com.vitorpamplona.amethyst.R -import com.vitorpamplona.amethyst.model.RelayBriefInfoCache -import com.vitorpamplona.amethyst.service.Nip11CachedRetriever -import com.vitorpamplona.amethyst.service.Nip11Retriever -import com.vitorpamplona.amethyst.ui.actions.RelayInfoDialog -import com.vitorpamplona.amethyst.ui.note.RenderRelayIcon import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel -import com.vitorpamplona.amethyst.ui.theme.ButtonBorder -import com.vitorpamplona.amethyst.ui.theme.DividerThickness import com.vitorpamplona.amethyst.ui.theme.FeedPadding -import com.vitorpamplona.amethyst.ui.theme.HalfHorzPadding -import com.vitorpamplona.amethyst.ui.theme.HalfStartPadding -import com.vitorpamplona.amethyst.ui.theme.ReactionRowHeightChat -import com.vitorpamplona.amethyst.ui.theme.Size10dp import com.vitorpamplona.amethyst.ui.theme.StdVertSpacer -import com.vitorpamplona.amethyst.ui.theme.WarningColor -import com.vitorpamplona.amethyst.ui.theme.allGoodColor -import com.vitorpamplona.amethyst.ui.theme.largeRelayIconModifier -import com.vitorpamplona.amethyst.ui.theme.placeholderText -import com.vitorpamplona.amethyst.ui.theme.warningColor -import com.vitorpamplona.quartz.encoders.RelayUrlFormatter -import kotlinx.coroutines.launch @Composable fun DMRelayList( @@ -106,14 +53,14 @@ fun DMRelayList( } fun LazyListScope.renderDMItems( - feedState: List, + feedState: List, postViewModel: DMRelayListViewModel, accountViewModel: AccountViewModel, onClose: () -> Unit, nav: (String) -> Unit, ) { itemsIndexed(feedState, key = { _, item -> "DM" + item.url }) { index, item -> - DMServerConfig( + BasicRelaySetupInfoDialog( item, onDelete = { postViewModel.deleteRelay(item) }, accountViewModel = accountViewModel, @@ -125,352 +72,6 @@ fun LazyListScope.renderDMItems( item { Spacer(modifier = StdVertSpacer) - DMEditableServerConfig { postViewModel.addRelay(it) } - } -} - -@Composable -fun DMServerConfig( - item: DMRelayListViewModel.DMRelaySetupInfo, - onDelete: (DMRelayListViewModel.DMRelaySetupInfo) -> Unit, - accountViewModel: AccountViewModel, - nav: (String) -> Unit, -) { - var relayInfo: RelayInfoDialog? by remember { mutableStateOf(null) } - val context = LocalContext.current - - relayInfo?.let { - RelayInformationDialog( - onClose = { relayInfo = null }, - relayInfo = it.relayInfo, - relayBriefInfo = it.relayBriefInfo, - accountViewModel = accountViewModel, - nav = nav, - ) - } - - val automaticallyShowProfilePicture = - remember { - accountViewModel.settings.showProfilePictures.value - } - - DMServerConfigClickableLine( - item = item, - loadProfilePicture = automaticallyShowProfilePicture, - onDelete = onDelete, - accountViewModel = accountViewModel, - onClick = { - accountViewModel.retrieveRelayDocument( - item.url, - onInfo = { relayInfo = RelayInfoDialog(RelayBriefInfoCache.RelayBriefInfo(item.url), it) }, - onError = { url, errorCode, exceptionMessage -> - val msg = - when (errorCode) { - Nip11Retriever.ErrorCode.FAIL_TO_ASSEMBLE_URL -> - context.getString( - R.string.relay_information_document_error_assemble_url, - url, - exceptionMessage, - ) - Nip11Retriever.ErrorCode.FAIL_TO_REACH_SERVER -> - context.getString( - R.string.relay_information_document_error_assemble_url, - url, - exceptionMessage, - ) - Nip11Retriever.ErrorCode.FAIL_TO_PARSE_RESULT -> - context.getString( - R.string.relay_information_document_error_assemble_url, - url, - exceptionMessage, - ) - Nip11Retriever.ErrorCode.FAIL_WITH_HTTP_STATUS -> - context.getString( - R.string.relay_information_document_error_assemble_url, - url, - exceptionMessage, - ) - } - - accountViewModel.toast( - context.getString(R.string.unable_to_download_relay_document), - msg, - ) - }, - ) - }, - ) -} - -@Composable -fun DMServerConfigClickableLine( - item: DMRelayListViewModel.DMRelaySetupInfo, - loadProfilePicture: Boolean, - onDelete: (DMRelayListViewModel.DMRelaySetupInfo) -> Unit, - onClick: () -> Unit, - accountViewModel: AccountViewModel, -) { - Column(Modifier.fillMaxWidth()) { - Row( - verticalAlignment = Alignment.CenterVertically, - modifier = Modifier.padding(vertical = 5.dp), - ) { - Column(Modifier.clickable(onClick = onClick)) { - val iconUrlFromRelayInfoDoc = - remember(item) { - Nip11CachedRetriever.getFromCache(item.url)?.icon - } - - RenderRelayIcon( - item.briefInfo.displayUrl, - iconUrlFromRelayInfoDoc ?: item.briefInfo.favIcon, - loadProfilePicture, - MaterialTheme.colorScheme.largeRelayIconModifier, - ) - } - - Spacer(modifier = HalfHorzPadding) - - Column(Modifier.weight(1f)) { - FirstLine(item, onClick, onDelete, ReactionRowHeightChat.fillMaxWidth()) - - Row( - verticalAlignment = Alignment.CenterVertically, - modifier = ReactionRowHeightChat.fillMaxWidth(), - ) { - StatusRow( - item = item, - modifier = HalfStartPadding.weight(1f), - accountViewModel = accountViewModel, - ) - } - } - } - - HorizontalDivider(thickness = DividerThickness) - } -} - -@Composable -@OptIn(ExperimentalFoundationApi::class) -private fun StatusRow( - item: DMRelayListViewModel.DMRelaySetupInfo, - modifier: Modifier, - accountViewModel: AccountViewModel, -) { - val scope = rememberCoroutineScope() - val context = LocalContext.current - - Icon( - imageVector = Icons.Default.Download, - contentDescription = stringResource(R.string.read_from_relay), - modifier = - Modifier - .size(15.dp) - .combinedClickable( - onClick = {}, - onLongClick = { - accountViewModel.toast( - R.string.read_from_relay, - R.string.read_from_relay_description, - ) - }, - ), - tint = MaterialTheme.colorScheme.allGoodColor, - ) - - Text( - text = countToHumanReadableBytes(item.downloadCountInBytes), - maxLines = 1, - fontSize = 12.sp, - modifier = modifier, - color = MaterialTheme.colorScheme.placeholderText, - ) - - Icon( - imageVector = Icons.Default.Upload, - stringResource(R.string.write_to_relay), - modifier = - Modifier - .size(15.dp) - .combinedClickable( - onClick = { }, - onLongClick = { - accountViewModel.toast( - R.string.write_to_relay, - R.string.write_to_relay_description, - ) - }, - ), - tint = MaterialTheme.colorScheme.allGoodColor, - ) - - Text( - text = countToHumanReadableBytes(item.uploadCountInBytes), - maxLines = 1, - fontSize = 12.sp, - modifier = modifier, - color = MaterialTheme.colorScheme.placeholderText, - ) - - Icon( - imageVector = Icons.Default.SyncProblem, - stringResource(R.string.errors), - modifier = - Modifier - .size(15.dp) - .combinedClickable( - onClick = {}, - onLongClick = { - accountViewModel.toast( - R.string.errors, - R.string.errors_description, - ) - }, - ), - tint = - if (item.errorCount > 0) { - MaterialTheme.colorScheme.warningColor - } else { - MaterialTheme.colorScheme.allGoodColor - }, - ) - - Text( - text = countToHumanReadable(item.errorCount, "errors"), - maxLines = 1, - fontSize = 12.sp, - modifier = modifier, - color = MaterialTheme.colorScheme.placeholderText, - ) - - Icon( - imageVector = Icons.Default.DeleteSweep, - stringResource(R.string.spam), - modifier = - Modifier - .size(15.dp) - .combinedClickable( - onClick = {}, - onLongClick = { - accountViewModel.toast( - R.string.spam, - R.string.spam_description, - ) - scope.launch { - Toast - .makeText( - context, - context.getString(R.string.spam), - Toast.LENGTH_SHORT, - ) - .show() - } - }, - ), - tint = - if (item.spamCount > 0) { - MaterialTheme.colorScheme.warningColor - } else { - MaterialTheme.colorScheme.allGoodColor - }, - ) - - Text( - text = countToHumanReadable(item.spamCount, "spam"), - maxLines = 1, - fontSize = 12.sp, - modifier = modifier, - color = MaterialTheme.colorScheme.placeholderText, - ) -} - -@Composable -private fun FirstLine( - item: DMRelayListViewModel.DMRelaySetupInfo, - onClick: () -> Unit, - onDelete: (DMRelayListViewModel.DMRelaySetupInfo) -> Unit, - modifier: Modifier, -) { - Row(verticalAlignment = Alignment.CenterVertically, modifier = modifier) { - Row(Modifier.weight(1f), verticalAlignment = Alignment.CenterVertically) { - Text( - text = item.briefInfo.displayUrl, - modifier = Modifier.clickable(onClick = onClick), - maxLines = 1, - overflow = TextOverflow.Ellipsis, - ) - - if (item.paidRelay) { - Icon( - imageVector = Icons.Default.Paid, - null, - modifier = - Modifier - .padding(start = 5.dp, top = 1.dp) - .size(14.dp), - tint = MaterialTheme.colorScheme.allGoodColor, - ) - } - } - - IconButton( - modifier = Modifier.size(30.dp), - onClick = { onDelete(item) }, - ) { - Icon( - imageVector = Icons.Default.Cancel, - contentDescription = stringResource(id = R.string.remove), - modifier = - Modifier - .padding(start = 10.dp) - .size(15.dp), - tint = WarningColor, - ) - } - } -} - -@Composable -fun DMEditableServerConfig(onNewRelay: (DMRelayListViewModel.DMRelaySetupInfo) -> Unit) { - var url by remember { mutableStateOf("") } - - Row(verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(Size10dp)) { - OutlinedTextField( - label = { Text(text = stringResource(R.string.add_a_relay)) }, - modifier = Modifier.weight(1f), - value = url, - onValueChange = { url = it }, - placeholder = { - Text( - text = "server.com", - color = MaterialTheme.colorScheme.placeholderText, - maxLines = 1, - ) - }, - singleLine = true, - ) - - Button( - onClick = { - if (url.isNotBlank() && url != "/") { - val addedWSS = RelayUrlFormatter.normalize(url) - onNewRelay(DMRelayListViewModel.DMRelaySetupInfo(addedWSS)) - url = "" - } - }, - shape = ButtonBorder, - colors = - ButtonDefaults.buttonColors( - containerColor = - if (url.isNotBlank()) { - MaterialTheme.colorScheme.primary - } else { - MaterialTheme.colorScheme.placeholderText - }, - ), - ) { - Text(text = stringResource(id = R.string.add), color = Color.White) - } + RelayUrlEditField { postViewModel.addRelay(it) } } } diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/relays/DMRelayListViewModel.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/relays/DMRelayListViewModel.kt index 090d1ffda..40b8ab83b 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/relays/DMRelayListViewModel.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/relays/DMRelayListViewModel.kt @@ -20,11 +20,9 @@ */ package com.vitorpamplona.amethyst.ui.actions.relays -import androidx.compose.runtime.Immutable import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import com.vitorpamplona.amethyst.model.Account -import com.vitorpamplona.amethyst.model.RelayBriefInfoCache import com.vitorpamplona.amethyst.service.Nip11CachedRetriever import com.vitorpamplona.amethyst.service.relays.RelayPool import kotlinx.coroutines.Dispatchers @@ -36,7 +34,7 @@ import kotlinx.coroutines.launch class DMRelayListViewModel : ViewModel() { private lateinit var account: Account - private val _relays = MutableStateFlow>(emptyList()) + private val _relays = MutableStateFlow>(emptyList()) val relays = _relays.asStateFlow() fun load(account: Account) { @@ -66,18 +64,6 @@ class DMRelayListViewModel : ViewModel() { } } - @Immutable - data class DMRelaySetupInfo( - val url: String, - val errorCount: Int = 0, - val downloadCountInBytes: Int = 0, - val uploadCountInBytes: Int = 0, - val spamCount: Int = 0, - val paidRelay: Boolean = false, - ) { - val briefInfo: RelayBriefInfoCache.RelayBriefInfo = RelayBriefInfoCache.RelayBriefInfo(url) - } - fun clear() { _relays.update { val relayList = account.getDMRelayList()?.relays() ?: emptyList() @@ -89,7 +75,7 @@ class DMRelayListViewModel : ViewModel() { val eventUploadCounter = liveRelay?.eventUploadCounterInBytes ?: 0 val spamCounter = liveRelay?.spamCounter ?: 0 - DMRelaySetupInfo( + BasicRelaySetupInfo( relayUrl, errorCounter, eventDownloadCounter, @@ -100,13 +86,13 @@ class DMRelayListViewModel : ViewModel() { } } - fun addRelay(relay: DMRelaySetupInfo) { + fun addRelay(relay: BasicRelaySetupInfo) { if (relays.value.any { it.url == relay.url }) return _relays.update { it.plus(relay) } } - fun deleteRelay(relay: DMRelaySetupInfo) { + fun deleteRelay(relay: BasicRelaySetupInfo) { _relays.update { it.minus(relay) } } @@ -115,7 +101,7 @@ class DMRelayListViewModel : ViewModel() { } fun togglePaidRelay( - relay: DMRelaySetupInfo, + relay: BasicRelaySetupInfo, paid: Boolean, ) { _relays.update { it.updated(relay, relay.copy(paidRelay = paid)) } diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/relays/Nip65RelayListView.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/relays/Nip65RelayListView.kt index 76edd82b0..ce285786a 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/relays/Nip65RelayListView.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/relays/Nip65RelayListView.kt @@ -20,72 +20,19 @@ */ package com.vitorpamplona.amethyst.ui.actions.relays -import android.widget.Toast -import androidx.compose.foundation.ExperimentalFoundationApi -import androidx.compose.foundation.clickable -import androidx.compose.foundation.combinedClickable -import androidx.compose.foundation.layout.Arrangement -import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.Spacer -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.lazy.LazyListScope import androidx.compose.foundation.lazy.itemsIndexed -import androidx.compose.material.icons.Icons -import androidx.compose.material.icons.filled.Cancel -import androidx.compose.material.icons.filled.DeleteSweep -import androidx.compose.material.icons.filled.Download -import androidx.compose.material.icons.filled.Paid -import androidx.compose.material.icons.filled.SyncProblem -import androidx.compose.material.icons.filled.Upload -import androidx.compose.material3.Button -import androidx.compose.material3.ButtonDefaults -import androidx.compose.material3.HorizontalDivider -import androidx.compose.material3.Icon -import androidx.compose.material3.IconButton -import androidx.compose.material3.MaterialTheme -import androidx.compose.material3.OutlinedTextField -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.rememberCoroutineScope import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment -import androidx.compose.ui.Modifier -import androidx.compose.ui.graphics.Color -import androidx.compose.ui.platform.LocalContext -import androidx.compose.ui.res.stringResource -import androidx.compose.ui.text.style.TextOverflow -import androidx.compose.ui.unit.dp -import androidx.compose.ui.unit.sp import androidx.lifecycle.compose.collectAsStateWithLifecycle -import com.vitorpamplona.amethyst.R -import com.vitorpamplona.amethyst.model.RelayBriefInfoCache -import com.vitorpamplona.amethyst.service.Nip11CachedRetriever -import com.vitorpamplona.amethyst.service.Nip11Retriever -import com.vitorpamplona.amethyst.ui.actions.RelayInfoDialog -import com.vitorpamplona.amethyst.ui.note.RenderRelayIcon import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel -import com.vitorpamplona.amethyst.ui.theme.ButtonBorder -import com.vitorpamplona.amethyst.ui.theme.DividerThickness import com.vitorpamplona.amethyst.ui.theme.FeedPadding -import com.vitorpamplona.amethyst.ui.theme.HalfHorzPadding -import com.vitorpamplona.amethyst.ui.theme.HalfStartPadding -import com.vitorpamplona.amethyst.ui.theme.ReactionRowHeightChat -import com.vitorpamplona.amethyst.ui.theme.Size10dp import com.vitorpamplona.amethyst.ui.theme.StdVertSpacer -import com.vitorpamplona.amethyst.ui.theme.WarningColor -import com.vitorpamplona.amethyst.ui.theme.allGoodColor -import com.vitorpamplona.amethyst.ui.theme.largeRelayIconModifier -import com.vitorpamplona.amethyst.ui.theme.placeholderText -import com.vitorpamplona.amethyst.ui.theme.warningColor -import com.vitorpamplona.quartz.encoders.RelayUrlFormatter -import kotlinx.coroutines.launch @Composable fun Nip65RelayList( @@ -108,14 +55,14 @@ fun Nip65RelayList( } fun LazyListScope.renderNip65HomeItems( - feedState: List, + feedState: List, postViewModel: Nip65RelayListViewModel, accountViewModel: AccountViewModel, onClose: () -> Unit, nav: (String) -> Unit, ) { itemsIndexed(feedState, key = { _, item -> "Nip65Home" + item.url }) { index, item -> - Nip65ServerConfig( + BasicRelaySetupInfoDialog( item, onDelete = { postViewModel.deleteHomeRelay(item) }, accountViewModel = accountViewModel, @@ -127,19 +74,19 @@ fun LazyListScope.renderNip65HomeItems( item { Spacer(modifier = StdVertSpacer) - Nip65EditableServerConfig { postViewModel.addHomeRelay(it) } + RelayUrlEditField { postViewModel.addHomeRelay(it) } } } fun LazyListScope.renderNip65NotifItems( - feedState: List, + feedState: List, postViewModel: Nip65RelayListViewModel, accountViewModel: AccountViewModel, onClose: () -> Unit, nav: (String) -> Unit, ) { itemsIndexed(feedState, key = { _, item -> "Nip65Notif" + item.url }) { index, item -> - Nip65ServerConfig( + BasicRelaySetupInfoDialog( item, onDelete = { postViewModel.deleteNotifRelay(item) }, accountViewModel = accountViewModel, @@ -151,352 +98,6 @@ fun LazyListScope.renderNip65NotifItems( item { Spacer(modifier = StdVertSpacer) - Nip65EditableServerConfig { postViewModel.addNotifRelay(it) } - } -} - -@Composable -fun Nip65ServerConfig( - item: Nip65RelayListViewModel.Nip65RelaySetupInfo, - onDelete: (Nip65RelayListViewModel.Nip65RelaySetupInfo) -> Unit, - accountViewModel: AccountViewModel, - nav: (String) -> Unit, -) { - var relayInfo: RelayInfoDialog? by remember { mutableStateOf(null) } - val context = LocalContext.current - - relayInfo?.let { - RelayInformationDialog( - onClose = { relayInfo = null }, - relayInfo = it.relayInfo, - relayBriefInfo = it.relayBriefInfo, - accountViewModel = accountViewModel, - nav = nav, - ) - } - - val automaticallyShowProfilePicture = - remember { - accountViewModel.settings.showProfilePictures.value - } - - Nip65ServerConfigClickableLine( - item = item, - loadProfilePicture = automaticallyShowProfilePicture, - onDelete = onDelete, - accountViewModel = accountViewModel, - onClick = { - accountViewModel.retrieveRelayDocument( - item.url, - onInfo = { relayInfo = RelayInfoDialog(RelayBriefInfoCache.RelayBriefInfo(item.url), it) }, - onError = { url, errorCode, exceptionMessage -> - val msg = - when (errorCode) { - Nip11Retriever.ErrorCode.FAIL_TO_ASSEMBLE_URL -> - context.getString( - R.string.relay_information_document_error_assemble_url, - url, - exceptionMessage, - ) - Nip11Retriever.ErrorCode.FAIL_TO_REACH_SERVER -> - context.getString( - R.string.relay_information_document_error_assemble_url, - url, - exceptionMessage, - ) - Nip11Retriever.ErrorCode.FAIL_TO_PARSE_RESULT -> - context.getString( - R.string.relay_information_document_error_assemble_url, - url, - exceptionMessage, - ) - Nip11Retriever.ErrorCode.FAIL_WITH_HTTP_STATUS -> - context.getString( - R.string.relay_information_document_error_assemble_url, - url, - exceptionMessage, - ) - } - - accountViewModel.toast( - context.getString(R.string.unable_to_download_relay_document), - msg, - ) - }, - ) - }, - ) -} - -@Composable -fun Nip65ServerConfigClickableLine( - item: Nip65RelayListViewModel.Nip65RelaySetupInfo, - loadProfilePicture: Boolean, - onDelete: (Nip65RelayListViewModel.Nip65RelaySetupInfo) -> Unit, - onClick: () -> Unit, - accountViewModel: AccountViewModel, -) { - Column(Modifier.fillMaxWidth()) { - Row( - verticalAlignment = Alignment.CenterVertically, - modifier = Modifier.padding(vertical = 5.dp), - ) { - Column(Modifier.clickable(onClick = onClick)) { - val iconUrlFromRelayInfoDoc = - remember(item) { - Nip11CachedRetriever.getFromCache(item.url)?.icon - } - - RenderRelayIcon( - item.briefInfo.displayUrl, - iconUrlFromRelayInfoDoc ?: item.briefInfo.favIcon, - loadProfilePicture, - MaterialTheme.colorScheme.largeRelayIconModifier, - ) - } - - Spacer(modifier = HalfHorzPadding) - - Column(Modifier.weight(1f)) { - FirstLine(item, onClick, onDelete, ReactionRowHeightChat.fillMaxWidth()) - - Row( - verticalAlignment = Alignment.CenterVertically, - modifier = ReactionRowHeightChat.fillMaxWidth(), - ) { - StatusRow( - item = item, - modifier = HalfStartPadding.weight(1f), - accountViewModel = accountViewModel, - ) - } - } - } - - HorizontalDivider(thickness = DividerThickness) - } -} - -@Composable -@OptIn(ExperimentalFoundationApi::class) -private fun StatusRow( - item: Nip65RelayListViewModel.Nip65RelaySetupInfo, - modifier: Modifier, - accountViewModel: AccountViewModel, -) { - val scope = rememberCoroutineScope() - val context = LocalContext.current - - Icon( - imageVector = Icons.Default.Download, - contentDescription = stringResource(R.string.read_from_relay), - modifier = - Modifier - .size(15.dp) - .combinedClickable( - onClick = {}, - onLongClick = { - accountViewModel.toast( - R.string.read_from_relay, - R.string.read_from_relay_description, - ) - }, - ), - tint = MaterialTheme.colorScheme.allGoodColor, - ) - - Text( - text = countToHumanReadableBytes(item.downloadCountInBytes), - maxLines = 1, - fontSize = 12.sp, - modifier = modifier, - color = MaterialTheme.colorScheme.placeholderText, - ) - - Icon( - imageVector = Icons.Default.Upload, - stringResource(R.string.write_to_relay), - modifier = - Modifier - .size(15.dp) - .combinedClickable( - onClick = { }, - onLongClick = { - accountViewModel.toast( - R.string.write_to_relay, - R.string.write_to_relay_description, - ) - }, - ), - tint = MaterialTheme.colorScheme.allGoodColor, - ) - - Text( - text = countToHumanReadableBytes(item.uploadCountInBytes), - maxLines = 1, - fontSize = 12.sp, - modifier = modifier, - color = MaterialTheme.colorScheme.placeholderText, - ) - - Icon( - imageVector = Icons.Default.SyncProblem, - stringResource(R.string.errors), - modifier = - Modifier - .size(15.dp) - .combinedClickable( - onClick = {}, - onLongClick = { - accountViewModel.toast( - R.string.errors, - R.string.errors_description, - ) - }, - ), - tint = - if (item.errorCount > 0) { - MaterialTheme.colorScheme.warningColor - } else { - MaterialTheme.colorScheme.allGoodColor - }, - ) - - Text( - text = countToHumanReadable(item.errorCount, "errors"), - maxLines = 1, - fontSize = 12.sp, - modifier = modifier, - color = MaterialTheme.colorScheme.placeholderText, - ) - - Icon( - imageVector = Icons.Default.DeleteSweep, - stringResource(R.string.spam), - modifier = - Modifier - .size(15.dp) - .combinedClickable( - onClick = {}, - onLongClick = { - accountViewModel.toast( - R.string.spam, - R.string.spam_description, - ) - scope.launch { - Toast - .makeText( - context, - context.getString(R.string.spam), - Toast.LENGTH_SHORT, - ) - .show() - } - }, - ), - tint = - if (item.spamCount > 0) { - MaterialTheme.colorScheme.warningColor - } else { - MaterialTheme.colorScheme.allGoodColor - }, - ) - - Text( - text = countToHumanReadable(item.spamCount, "spam"), - maxLines = 1, - fontSize = 12.sp, - modifier = modifier, - color = MaterialTheme.colorScheme.placeholderText, - ) -} - -@Composable -private fun FirstLine( - item: Nip65RelayListViewModel.Nip65RelaySetupInfo, - onClick: () -> Unit, - onDelete: (Nip65RelayListViewModel.Nip65RelaySetupInfo) -> Unit, - modifier: Modifier, -) { - Row(verticalAlignment = Alignment.CenterVertically, modifier = modifier) { - Row(Modifier.weight(1f), verticalAlignment = Alignment.CenterVertically) { - Text( - text = item.briefInfo.displayUrl, - modifier = Modifier.clickable(onClick = onClick), - maxLines = 1, - overflow = TextOverflow.Ellipsis, - ) - - if (item.paidRelay) { - Icon( - imageVector = Icons.Default.Paid, - null, - modifier = - Modifier - .padding(start = 5.dp, top = 1.dp) - .size(14.dp), - tint = MaterialTheme.colorScheme.allGoodColor, - ) - } - } - - IconButton( - modifier = Modifier.size(30.dp), - onClick = { onDelete(item) }, - ) { - Icon( - imageVector = Icons.Default.Cancel, - contentDescription = stringResource(id = R.string.remove), - modifier = - Modifier - .padding(start = 10.dp) - .size(15.dp), - tint = WarningColor, - ) - } - } -} - -@Composable -fun Nip65EditableServerConfig(onNewRelay: (Nip65RelayListViewModel.Nip65RelaySetupInfo) -> Unit) { - var url by remember { mutableStateOf("") } - - Row(verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(Size10dp)) { - OutlinedTextField( - label = { Text(text = stringResource(R.string.add_a_relay)) }, - modifier = Modifier.weight(1f), - value = url, - onValueChange = { url = it }, - placeholder = { - Text( - text = "server.com", - color = MaterialTheme.colorScheme.placeholderText, - maxLines = 1, - ) - }, - singleLine = true, - ) - - Button( - onClick = { - if (url.isNotBlank() && url != "/") { - val addedWSS = RelayUrlFormatter.normalize(url) - onNewRelay(Nip65RelayListViewModel.Nip65RelaySetupInfo(addedWSS)) - url = "" - } - }, - shape = ButtonBorder, - colors = - ButtonDefaults.buttonColors( - containerColor = - if (url.isNotBlank()) { - MaterialTheme.colorScheme.primary - } else { - MaterialTheme.colorScheme.placeholderText - }, - ), - ) { - Text(text = stringResource(id = R.string.add), color = Color.White) - } + RelayUrlEditField { postViewModel.addNotifRelay(it) } } } diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/relays/Nip65RelayListViewModel.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/relays/Nip65RelayListViewModel.kt index 07f7a66cd..b71f18b0a 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/relays/Nip65RelayListViewModel.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/relays/Nip65RelayListViewModel.kt @@ -20,11 +20,9 @@ */ package com.vitorpamplona.amethyst.ui.actions.relays -import androidx.compose.runtime.Immutable import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import com.vitorpamplona.amethyst.model.Account -import com.vitorpamplona.amethyst.model.RelayBriefInfoCache import com.vitorpamplona.amethyst.service.Nip11CachedRetriever import com.vitorpamplona.amethyst.service.relays.RelayPool import com.vitorpamplona.quartz.events.AdvertisedRelayListEvent @@ -37,10 +35,10 @@ import kotlinx.coroutines.launch class Nip65RelayListViewModel : ViewModel() { private lateinit var account: Account - private val _homeRelays = MutableStateFlow>(emptyList()) + private val _homeRelays = MutableStateFlow>(emptyList()) val homeRelays = _homeRelays.asStateFlow() - private val _notificationRelays = MutableStateFlow>(emptyList()) + private val _notificationRelays = MutableStateFlow>(emptyList()) val notificationRelays = _notificationRelays.asStateFlow() fun load(account: Account) { @@ -97,18 +95,6 @@ class Nip65RelayListViewModel : ViewModel() { } } - @Immutable - data class Nip65RelaySetupInfo( - val url: String, - val errorCount: Int = 0, - val downloadCountInBytes: Int = 0, - val uploadCountInBytes: Int = 0, - val spamCount: Int = 0, - val paidRelay: Boolean = false, - ) { - val briefInfo: RelayBriefInfoCache.RelayBriefInfo = RelayBriefInfoCache.RelayBriefInfo(url) - } - fun clear() { _homeRelays.update { val relayList = account.getNIP65RelayList()?.relays() ?: emptyList() @@ -120,7 +106,7 @@ class Nip65RelayListViewModel : ViewModel() { val eventUploadCounter = liveRelay?.eventUploadCounterInBytes ?: 0 val spamCounter = liveRelay?.spamCounter ?: 0 - Nip65RelaySetupInfo( + BasicRelaySetupInfo( relayUrl.relayUrl, errorCounter, eventDownloadCounter, @@ -140,7 +126,7 @@ class Nip65RelayListViewModel : ViewModel() { val eventUploadCounter = liveRelay?.eventUploadCounterInBytes ?: 0 val spamCounter = liveRelay?.spamCounter ?: 0 - Nip65RelaySetupInfo( + BasicRelaySetupInfo( relayUrl.relayUrl, errorCounter, eventDownloadCounter, @@ -151,13 +137,13 @@ class Nip65RelayListViewModel : ViewModel() { } } - fun addHomeRelay(relay: Nip65RelaySetupInfo) { + fun addHomeRelay(relay: BasicRelaySetupInfo) { if (_homeRelays.value.any { it.url == relay.url }) return _homeRelays.update { it.plus(relay) } } - fun deleteHomeRelay(relay: Nip65RelaySetupInfo) { + fun deleteHomeRelay(relay: BasicRelaySetupInfo) { _homeRelays.update { it.minus(relay) } } @@ -166,19 +152,19 @@ class Nip65RelayListViewModel : ViewModel() { } fun toggleHomePaidRelay( - relay: Nip65RelaySetupInfo, + relay: BasicRelaySetupInfo, paid: Boolean, ) { _homeRelays.update { it.updated(relay, relay.copy(paidRelay = paid)) } } - fun addNotifRelay(relay: Nip65RelaySetupInfo) { + fun addNotifRelay(relay: BasicRelaySetupInfo) { if (_notificationRelays.value.any { it.url == relay.url }) return _notificationRelays.update { it.plus(relay) } } - fun deleteNotifRelay(relay: Nip65RelaySetupInfo) { + fun deleteNotifRelay(relay: BasicRelaySetupInfo) { _notificationRelays.update { it.minus(relay) } } @@ -187,7 +173,7 @@ class Nip65RelayListViewModel : ViewModel() { } fun toggleNotifPaidRelay( - relay: Nip65RelaySetupInfo, + relay: BasicRelaySetupInfo, paid: Boolean, ) { _notificationRelays.update { it.updated(relay, relay.copy(paidRelay = paid)) } diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/relays/RelayNameAndRemoveButton.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/relays/RelayNameAndRemoveButton.kt new file mode 100644 index 000000000..18e0d36fb --- /dev/null +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/relays/RelayNameAndRemoveButton.kt @@ -0,0 +1,88 @@ +/** + * Copyright (c) 2024 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.actions.relays + +import androidx.compose.foundation.clickable +import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.layout.size +import androidx.compose.material.icons.Icons +import androidx.compose.material.icons.filled.Cancel +import androidx.compose.material.icons.filled.Paid +import androidx.compose.material3.Icon +import androidx.compose.material3.IconButton +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.text.style.TextOverflow +import androidx.compose.ui.unit.dp +import com.vitorpamplona.amethyst.R +import com.vitorpamplona.amethyst.ui.theme.WarningColor +import com.vitorpamplona.amethyst.ui.theme.allGoodColor + +@Composable +fun RelayNameAndRemoveButton( + item: BasicRelaySetupInfo, + onClick: () -> Unit, + onDelete: (BasicRelaySetupInfo) -> Unit, + modifier: Modifier, +) { + Row(verticalAlignment = Alignment.CenterVertically, modifier = modifier) { + Row(Modifier.weight(1f), verticalAlignment = Alignment.CenterVertically) { + Text( + text = item.briefInfo.displayUrl, + modifier = Modifier.clickable(onClick = onClick), + maxLines = 1, + overflow = TextOverflow.Ellipsis, + ) + + if (item.paidRelay) { + Icon( + imageVector = Icons.Default.Paid, + null, + modifier = + Modifier + .padding(start = 5.dp, top = 1.dp) + .size(14.dp), + tint = MaterialTheme.colorScheme.allGoodColor, + ) + } + } + + IconButton( + modifier = Modifier.size(30.dp), + onClick = { onDelete(item) }, + ) { + Icon( + imageVector = Icons.Default.Cancel, + contentDescription = stringResource(id = R.string.remove), + modifier = + Modifier + .padding(start = 10.dp) + .size(15.dp), + tint = WarningColor, + ) + } + } +} diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/relays/RelayStatusRow.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/relays/RelayStatusRow.kt new file mode 100644 index 000000000..99cc7ac71 --- /dev/null +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/relays/RelayStatusRow.kt @@ -0,0 +1,180 @@ +/** + * Copyright (c) 2024 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.actions.relays + +import android.widget.Toast +import androidx.compose.foundation.ExperimentalFoundationApi +import androidx.compose.foundation.combinedClickable +import androidx.compose.foundation.layout.size +import androidx.compose.material.icons.Icons +import androidx.compose.material.icons.filled.DeleteSweep +import androidx.compose.material.icons.filled.Download +import androidx.compose.material.icons.filled.SyncProblem +import androidx.compose.material.icons.filled.Upload +import androidx.compose.material3.Icon +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.runtime.rememberCoroutineScope +import androidx.compose.ui.Modifier +import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.unit.dp +import androidx.compose.ui.unit.sp +import com.vitorpamplona.amethyst.R +import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel +import com.vitorpamplona.amethyst.ui.theme.allGoodColor +import com.vitorpamplona.amethyst.ui.theme.placeholderText +import com.vitorpamplona.amethyst.ui.theme.warningColor +import kotlinx.coroutines.launch + +@Composable +@OptIn(ExperimentalFoundationApi::class) +fun RelayStatusRow( + item: BasicRelaySetupInfo, + modifier: Modifier, + accountViewModel: AccountViewModel, +) { + val scope = rememberCoroutineScope() + val context = LocalContext.current + + Icon( + imageVector = Icons.Default.Download, + contentDescription = stringResource(R.string.read_from_relay), + modifier = + Modifier + .size(15.dp) + .combinedClickable( + onClick = {}, + onLongClick = { + accountViewModel.toast( + R.string.read_from_relay, + R.string.read_from_relay_description, + ) + }, + ), + tint = MaterialTheme.colorScheme.allGoodColor, + ) + + Text( + text = countToHumanReadableBytes(item.downloadCountInBytes), + maxLines = 1, + fontSize = 12.sp, + modifier = modifier, + color = MaterialTheme.colorScheme.placeholderText, + ) + + Icon( + imageVector = Icons.Default.Upload, + stringResource(R.string.write_to_relay), + modifier = + Modifier + .size(15.dp) + .combinedClickable( + onClick = { }, + onLongClick = { + accountViewModel.toast( + R.string.write_to_relay, + R.string.write_to_relay_description, + ) + }, + ), + tint = MaterialTheme.colorScheme.allGoodColor, + ) + + Text( + text = countToHumanReadableBytes(item.uploadCountInBytes), + maxLines = 1, + fontSize = 12.sp, + modifier = modifier, + color = MaterialTheme.colorScheme.placeholderText, + ) + + Icon( + imageVector = Icons.Default.SyncProblem, + stringResource(R.string.errors), + modifier = + Modifier + .size(15.dp) + .combinedClickable( + onClick = {}, + onLongClick = { + accountViewModel.toast( + R.string.errors, + R.string.errors_description, + ) + }, + ), + tint = + if (item.errorCount > 0) { + MaterialTheme.colorScheme.warningColor + } else { + MaterialTheme.colorScheme.allGoodColor + }, + ) + + Text( + text = countToHumanReadable(item.errorCount, "errors"), + maxLines = 1, + fontSize = 12.sp, + modifier = modifier, + color = MaterialTheme.colorScheme.placeholderText, + ) + + Icon( + imageVector = Icons.Default.DeleteSweep, + stringResource(R.string.spam), + modifier = + Modifier + .size(15.dp) + .combinedClickable( + onClick = {}, + onLongClick = { + accountViewModel.toast( + R.string.spam, + R.string.spam_description, + ) + scope.launch { + Toast.makeText( + context, + context.getString(R.string.spam), + Toast.LENGTH_SHORT, + ) + .show() + } + }, + ), + tint = + if (item.spamCount > 0) { + MaterialTheme.colorScheme.warningColor + } else { + MaterialTheme.colorScheme.allGoodColor + }, + ) + + Text( + text = countToHumanReadable(item.spamCount, "spam"), + maxLines = 1, + fontSize = 12.sp, + modifier = modifier, + color = MaterialTheme.colorScheme.placeholderText, + ) +} diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/relays/RelayUrlEditField.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/relays/RelayUrlEditField.kt new file mode 100644 index 000000000..1b5910f31 --- /dev/null +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/relays/RelayUrlEditField.kt @@ -0,0 +1,87 @@ +/** + * Copyright (c) 2024 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.actions.relays + +import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Row +import androidx.compose.material3.Button +import androidx.compose.material3.ButtonDefaults +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.OutlinedTextField +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.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.res.stringResource +import com.vitorpamplona.amethyst.R +import com.vitorpamplona.amethyst.ui.theme.ButtonBorder +import com.vitorpamplona.amethyst.ui.theme.Size10dp +import com.vitorpamplona.amethyst.ui.theme.placeholderText +import com.vitorpamplona.quartz.encoders.RelayUrlFormatter + +@Composable +fun RelayUrlEditField(onNewRelay: (BasicRelaySetupInfo) -> Unit) { + var url by remember { mutableStateOf("") } + + Row(verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(Size10dp)) { + OutlinedTextField( + label = { Text(text = stringResource(R.string.add_a_relay)) }, + modifier = Modifier.weight(1f), + value = url, + onValueChange = { url = it }, + placeholder = { + Text( + text = "server.com", + color = MaterialTheme.colorScheme.placeholderText, + maxLines = 1, + ) + }, + singleLine = true, + ) + + Button( + onClick = { + if (url.isNotBlank() && url != "/") { + val addedWSS = RelayUrlFormatter.normalize(url) + onNewRelay(BasicRelaySetupInfo(addedWSS)) + url = "" + } + }, + shape = ButtonBorder, + colors = + ButtonDefaults.buttonColors( + containerColor = + if (url.isNotBlank()) { + MaterialTheme.colorScheme.primary + } else { + MaterialTheme.colorScheme.placeholderText + }, + ), + ) { + Text(text = stringResource(id = R.string.add), color = Color.White) + } + } +} diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/relays/SearchRelayListView.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/relays/SearchRelayListView.kt new file mode 100644 index 000000000..bb9f1489c --- /dev/null +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/relays/SearchRelayListView.kt @@ -0,0 +1,77 @@ +/** + * Copyright (c) 2024 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.actions.relays + +import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.Spacer +import androidx.compose.foundation.lazy.LazyColumn +import androidx.compose.foundation.lazy.LazyListScope +import androidx.compose.foundation.lazy.itemsIndexed +import androidx.compose.runtime.Composable +import androidx.compose.runtime.getValue +import androidx.compose.runtime.setValue +import androidx.compose.ui.Alignment +import androidx.lifecycle.compose.collectAsStateWithLifecycle +import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel +import com.vitorpamplona.amethyst.ui.theme.FeedPadding +import com.vitorpamplona.amethyst.ui.theme.StdVertSpacer + +@Composable +fun SearchRelayList( + postViewModel: SearchRelayListViewModel, + accountViewModel: AccountViewModel, + onClose: () -> Unit, + nav: (String) -> Unit, +) { + val feedState by postViewModel.relays.collectAsStateWithLifecycle() + + Row(verticalAlignment = Alignment.CenterVertically) { + LazyColumn( + contentPadding = FeedPadding, + ) { + renderSearchItems(feedState, postViewModel, accountViewModel, onClose, nav) + } + } +} + +fun LazyListScope.renderSearchItems( + feedState: List, + postViewModel: SearchRelayListViewModel, + accountViewModel: AccountViewModel, + onClose: () -> Unit, + nav: (String) -> Unit, +) { + itemsIndexed(feedState, key = { _, item -> "Search" + item.url }) { index, item -> + BasicRelaySetupInfoDialog( + item, + onDelete = { postViewModel.deleteRelay(item) }, + accountViewModel = accountViewModel, + ) { + onClose() + nav(it) + } + } + + item { + Spacer(modifier = StdVertSpacer) + RelayUrlEditField { postViewModel.addRelay(it) } + } +} diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/relays/SearchRelayListViewModel.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/relays/SearchRelayListViewModel.kt new file mode 100644 index 000000000..ee0944365 --- /dev/null +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/relays/SearchRelayListViewModel.kt @@ -0,0 +1,109 @@ +/** + * Copyright (c) 2024 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.actions.relays + +import androidx.lifecycle.ViewModel +import androidx.lifecycle.viewModelScope +import com.vitorpamplona.amethyst.model.Account +import com.vitorpamplona.amethyst.service.Nip11CachedRetriever +import com.vitorpamplona.amethyst.service.relays.RelayPool +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.asStateFlow +import kotlinx.coroutines.flow.update +import kotlinx.coroutines.launch + +class SearchRelayListViewModel : ViewModel() { + private lateinit var account: Account + + private val _relays = MutableStateFlow>(emptyList()) + val relays = _relays.asStateFlow() + + fun load(account: Account) { + this.account = account + clear() + loadRelayDocuments() + } + + fun create() { + viewModelScope.launch(Dispatchers.IO) { + account.saveSearchRelayList(_relays.value.map { it.url }) + clear() + } + } + + fun loadRelayDocuments() { + viewModelScope.launch(Dispatchers.IO) { + _relays.value.forEach { item -> + Nip11CachedRetriever.loadRelayInfo( + dirtyUrl = item.url, + onInfo = { + togglePaidRelay(item, it.limitation?.payment_required ?: false) + }, + onError = { url, errorCode, exceptionMessage -> }, + ) + } + } + } + + fun clear() { + _relays.update { + val relayList = account.getSearchRelayList()?.relays() ?: emptyList() + + relayList.map { relayUrl -> + val liveRelay = RelayPool.getRelay(relayUrl) + val errorCounter = liveRelay?.errorCounter ?: 0 + val eventDownloadCounter = liveRelay?.eventDownloadCounterInBytes ?: 0 + val eventUploadCounter = liveRelay?.eventUploadCounterInBytes ?: 0 + val spamCounter = liveRelay?.spamCounter ?: 0 + + BasicRelaySetupInfo( + relayUrl, + errorCounter, + eventDownloadCounter, + eventUploadCounter, + spamCounter, + ) + }.distinctBy { it.url }.sortedBy { it.downloadCountInBytes }.reversed() + } + } + + fun addRelay(relay: BasicRelaySetupInfo) { + if (relays.value.any { it.url == relay.url }) return + + _relays.update { it.plus(relay) } + } + + fun deleteRelay(relay: BasicRelaySetupInfo) { + _relays.update { it.minus(relay) } + } + + fun deleteAll() { + _relays.update { relays -> emptyList() } + } + + fun togglePaidRelay( + relay: BasicRelaySetupInfo, + paid: Boolean, + ) { + _relays.update { it.updated(relay, relay.copy(paidRelay = paid)) } + } +} diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/AccountViewModel.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/AccountViewModel.kt index 6ad0ac818..c5e4edee8 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/AccountViewModel.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/AccountViewModel.kt @@ -88,6 +88,7 @@ import com.vitorpamplona.quartz.events.Participant import com.vitorpamplona.quartz.events.ReportEvent import com.vitorpamplona.quartz.events.Response import com.vitorpamplona.quartz.events.SealedGossipEvent +import com.vitorpamplona.quartz.events.SearchRelayListEvent import com.vitorpamplona.quartz.events.UserMetadata import com.vitorpamplona.quartz.utils.TimeUtils import kotlinx.collections.immutable.ImmutableList @@ -134,6 +135,7 @@ class AccountViewModel(val account: Account, val settings: SettingsState) : View val kind3Relays: StateFlow = observeByAuthor(ContactListEvent.KIND, account.signer.pubKey) val dmRelays: StateFlow = observeByAuthor(ChatMessageRelayListEvent.KIND, account.signer.pubKey) + val searchRelays: StateFlow = observeByAuthor(SearchRelayListEvent.KIND, account.signer.pubKey) val toasts = MutableSharedFlow(0, 3, onBufferOverflow = BufferOverflow.DROP_OLDEST) diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index bfdcb51a9..5f818ba93 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -815,11 +815,13 @@ Public Home Relays This relay type stores all your content. Amethyst will send your posts here and others will use these relays to find your content. Insert between 1-3 relays. They can be personal relays or paid relays. Public Inbox Relays - This relay type receives events that others are sending to you, like replies, comments, likes, zaps, etc. Insert between 1-3 relays that accept posts from anyone. + This relay type receives events others are sending to you, like replies, comments, likes, zaps, etc. Insert between 1-3 relays that accept posts from anyone. DM Inbox Relays Insert between 1-3 relays to serve as your private inbox. Others will use these relays to send DMs to you. DM Inbox relays should accept any message from anyone, but only allow you to download them. Good options are:\n - inbox.nostr.wine (paid)\n - you.nostr1.com (personal relays - paid) General Relays Amethyst uses these relays to download posts for you. + Search Relays + List of relays to use for search and tagging users. Tagging and search will not work if no options are available. Zap the Devs! Your donation helps us make a difference. Every sat counts! diff --git a/quartz/src/main/java/com/vitorpamplona/quartz/events/EventFactory.kt b/quartz/src/main/java/com/vitorpamplona/quartz/events/EventFactory.kt index d639cbc37..9051a960e 100644 --- a/quartz/src/main/java/com/vitorpamplona/quartz/events/EventFactory.kt +++ b/quartz/src/main/java/com/vitorpamplona/quartz/events/EventFactory.kt @@ -131,6 +131,7 @@ class EventFactory { ReportEvent.KIND -> ReportEvent(id, pubKey, createdAt, tags, content, sig) RepostEvent.KIND -> RepostEvent(id, pubKey, createdAt, tags, content, sig) SealedGossipEvent.KIND -> SealedGossipEvent(id, pubKey, createdAt, tags, content, sig) + SearchRelayListEvent.KIND -> SearchRelayListEvent(id, pubKey, createdAt, tags, content, sig) StatusEvent.KIND -> StatusEvent(id, pubKey, createdAt, tags, content, sig) TextNoteEvent.KIND -> TextNoteEvent(id, pubKey, createdAt, tags, content, sig) TextNoteModificationEvent.KIND -> TextNoteModificationEvent(id, pubKey, createdAt, tags, content, sig) diff --git a/quartz/src/main/java/com/vitorpamplona/quartz/events/SearchRelayListEvent.kt b/quartz/src/main/java/com/vitorpamplona/quartz/events/SearchRelayListEvent.kt new file mode 100644 index 000000000..1514c2571 --- /dev/null +++ b/quartz/src/main/java/com/vitorpamplona/quartz/events/SearchRelayListEvent.kt @@ -0,0 +1,103 @@ +/** + * Copyright (c) 2024 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.quartz.events + +import androidx.compose.runtime.Immutable +import com.vitorpamplona.quartz.encoders.ATag +import com.vitorpamplona.quartz.encoders.HexKey +import com.vitorpamplona.quartz.signers.NostrSigner +import com.vitorpamplona.quartz.utils.TimeUtils + +@Immutable +class SearchRelayListEvent( + id: HexKey, + pubKey: HexKey, + createdAt: Long, + tags: Array>, + content: String, + sig: HexKey, +) : BaseAddressableEvent(id, pubKey, createdAt, KIND, tags, content, sig) { + override fun dTag() = FIXED_D_TAG + + fun relays(): List { + return tags.mapNotNull { + if (it.size > 1 && it[0] == "relay") { + it[1] + } else { + null + } + } + } + + companion object { + const val KIND = 10007 + const val FIXED_D_TAG = "" + + fun createAddressATag(pubKey: HexKey): ATag { + return ATag(KIND, pubKey, FIXED_D_TAG, null) + } + + fun createAddressTag(pubKey: HexKey): String { + return ATag.assembleATag(KIND, pubKey, FIXED_D_TAG) + } + + fun createTagArray(relays: List): Array> { + return relays.map { + arrayOf("relay", it) + }.plusElement(arrayOf("alt", "Relay list to use for Search")).toTypedArray() + } + + fun updateRelayList( + earlierVersion: SearchRelayListEvent, + relays: List, + signer: NostrSigner, + createdAt: Long = TimeUtils.now(), + onReady: (SearchRelayListEvent) -> Unit, + ) { + val tags = + earlierVersion.tags.filter { it[0] != "relay" }.plus( + relays.map { + arrayOf("relay", it) + }, + ).toTypedArray() + + signer.sign(createdAt, KIND, tags, earlierVersion.content, onReady) + } + + fun createFromScratch( + relays: List, + signer: NostrSigner, + createdAt: Long = TimeUtils.now(), + onReady: (SearchRelayListEvent) -> Unit, + ) { + create(relays, signer, createdAt, onReady) + } + + fun create( + relays: List, + signer: NostrSigner, + createdAt: Long = TimeUtils.now(), + onReady: (SearchRelayListEvent) -> Unit, + ) { + signer.sign(createdAt, KIND, createTagArray(relays), "", onReady) + } + } +}