show icon for paid relays in the relay list

This commit is contained in:
greenart7c3 2023-06-23 06:21:16 -03:00
parent 69481a046b
commit a51104fd82
3 changed files with 39 additions and 5 deletions

View File

@ -12,5 +12,6 @@ data class RelaySetupInfo(
val downloadCountInBytes: Int = 0,
val uploadCountInBytes: Int = 0,
val spamCount: Int = 0,
val feedTypes: Set<FeedType>
val feedTypes: Set<FeedType>,
val paidRelay: Boolean = false
)

View File

@ -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

View File

@ -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 <T> Iterable<T>.updated(old: T, new: T): List<T> = map { if (it == old) new else it }