From a51104fd82f9258b5c0f72ac8720a8333709e0b1 Mon Sep 17 00:00:00 2001 From: greenart7c3 <115044884+greenart7c3@users.noreply.github.com> Date: Fri, 23 Jun 2023 06:21:16 -0300 Subject: [PATCH] show icon for paid relays in the relay list --- .../amethyst/model/RelaySetupInfo.kt | 3 +- .../amethyst/ui/actions/NewRelayListView.kt | 35 ++++++++++++++++--- .../ui/actions/NewRelayListViewModel.kt | 6 ++++ 3 files changed, 39 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/com/vitorpamplona/amethyst/model/RelaySetupInfo.kt b/app/src/main/java/com/vitorpamplona/amethyst/model/RelaySetupInfo.kt index 48350eade..c9f099ed6 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/model/RelaySetupInfo.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/model/RelaySetupInfo.kt @@ -12,5 +12,6 @@ data class RelaySetupInfo( val downloadCountInBytes: Int = 0, val uploadCountInBytes: Int = 0, val spamCount: Int = 0, - val feedTypes: Set + val feedTypes: Set, + val paidRelay: Boolean = false ) diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewRelayListView.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewRelayListView.kt index 142b502c8..98f959e72 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewRelayListView.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewRelayListView.kt @@ -1,5 +1,6 @@ package com.vitorpamplona.amethyst.ui.actions +import android.content.Context import android.widget.Toast import androidx.compose.foundation.ExperimentalFoundationApi import androidx.compose.foundation.clickable @@ -29,6 +30,7 @@ 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.Groups +import androidx.compose.material.icons.filled.Paid import androidx.compose.material.icons.filled.Public import androidx.compose.material.icons.filled.Search import androidx.compose.material.icons.filled.SyncProblem @@ -61,6 +63,7 @@ import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel import com.vitorpamplona.amethyst.ui.theme.ButtonBorder import com.vitorpamplona.amethyst.ui.theme.Size35dp import com.vitorpamplona.amethyst.ui.theme.placeholderText +import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.launch import java.lang.Math.round @@ -68,9 +71,16 @@ import java.lang.Math.round fun NewRelayListView(onClose: () -> Unit, accountViewModel: AccountViewModel, relayToAdd: String = "", nav: (String) -> Unit) { val postViewModel: NewRelayListViewModel = viewModel() val feedState by postViewModel.relays.collectAsState() + val context = LocalContext.current + val scope = rememberCoroutineScope() LaunchedEffect(Unit) { postViewModel.load(accountViewModel.account) + postViewModel.relays.value.forEach { item -> + loadRelayInfo(item.url, context, scope) { + postViewModel.togglePaidRelay(item, it.limitation?.payment_required ?: false) + } + } } Dialog( @@ -116,6 +126,7 @@ fun NewRelayListView(onClose: () -> Unit, accountViewModel: AccountViewModel, re if (index == 0) { ServerConfigHeader() } + ServerConfig( item, onToggleDownload = { postViewModel.toggleDownload(it) }, @@ -129,7 +140,9 @@ fun NewRelayListView(onClose: () -> Unit, accountViewModel: AccountViewModel, re onDelete = { postViewModel.deleteRelay(it) }, accountViewModel = accountViewModel, - nav = nav + nav = nav, + scope = scope, + context = context ) } } @@ -228,10 +241,10 @@ fun ServerConfig( onDelete: (RelaySetupInfo) -> Unit, accountViewModel: AccountViewModel, - nav: (String) -> Unit + nav: (String) -> Unit, + context: Context, + scope: CoroutineScope ) { - val context = LocalContext.current - val scope = rememberCoroutineScope() var relayInfo: RelayInformation? by remember { mutableStateOf(null) } if (relayInfo != null) { @@ -268,6 +281,20 @@ fun ServerConfig( Column(Modifier.weight(1f)) { Row(verticalAlignment = Alignment.CenterVertically) { + Icon( + imageVector = Icons.Default.Paid, + null, + modifier = Modifier + .padding(end = 5.dp) + .size(15.dp), + tint = if (item.paidRelay) { + Color.Green + } else { + MaterialTheme.colors.onSurface.copy( + alpha = 0.32f + ) + } + ) Text( text = item.url.removePrefix("wss://"), modifier = Modifier diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewRelayListViewModel.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewRelayListViewModel.kt index 687a71cb7..17847d0b8 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewRelayListViewModel.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewRelayListViewModel.kt @@ -136,6 +136,12 @@ class NewRelayListViewModel : ViewModel() { it.updated(relay, relay.copy(feedTypes = newTypes)) } } + + fun togglePaidRelay(relay: RelaySetupInfo, paid: Boolean) { + _relays.update { + it.updated(relay, relay.copy(paidRelay = paid)) + } + } } fun Iterable.updated(old: T, new: T): List = map { if (it == old) new else it }