Simplifying relay connection status

pull/519/head
Vitor Pamplona 2023-07-23 18:37:37 -04:00
rodzic 3e6938bf51
commit 95ac046a09
4 zmienionych plików z 18 dodań i 26 usunięć

Wyświetl plik

@ -7,7 +7,6 @@ import androidx.compose.runtime.Stable
import androidx.core.os.ConfigurationCompat import androidx.core.os.ConfigurationCompat
import androidx.lifecycle.LiveData import androidx.lifecycle.LiveData
import androidx.lifecycle.distinctUntilChanged import androidx.lifecycle.distinctUntilChanged
import androidx.lifecycle.map
import com.vitorpamplona.amethyst.OptOutFromFilters import com.vitorpamplona.amethyst.OptOutFromFilters
import com.vitorpamplona.amethyst.service.FileHeader import com.vitorpamplona.amethyst.service.FileHeader
import com.vitorpamplona.amethyst.service.NostrLnZapPaymentResponseDataSource import com.vitorpamplona.amethyst.service.NostrLnZapPaymentResponseDataSource

Wyświetl plik

@ -24,7 +24,7 @@ object RelayPool : Relay.Listener {
} }
fun connectedRelays(): Int { fun connectedRelays(): Int {
return relays.filter { it.isConnected() }.size return relays.count { it.isConnected() }
} }
fun getRelay(url: String): Relay? { fun getRelay(url: String): Relay? {

Wyświetl plik

@ -34,7 +34,6 @@ import androidx.compose.material.TextButton
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.MutableState import androidx.compose.runtime.MutableState
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.mutableStateOf
@ -53,6 +52,7 @@ import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp import androidx.compose.ui.unit.sp
import androidx.lifecycle.distinctUntilChanged
import androidx.lifecycle.viewmodel.compose.viewModel import androidx.lifecycle.viewmodel.compose.viewModel
import coil.compose.AsyncImage import coil.compose.AsyncImage
import com.vitorpamplona.amethyst.BuildConfig import com.vitorpamplona.amethyst.BuildConfig
@ -296,8 +296,10 @@ fun ListContent(
modifier: Modifier, modifier: Modifier,
accountViewModel: AccountViewModel accountViewModel: AccountViewModel
) { ) {
val accountState by accountViewModel.accountLiveData.observeAsState() val route = remember(accountViewModel) {
val account = remember(accountState) { accountState?.account } ?: return "User/${accountViewModel.userProfile().pubkeyHex}"
}
val coroutineScope = rememberCoroutineScope() val coroutineScope = rememberCoroutineScope()
val relayViewModel: RelayPoolViewModel = viewModel { RelayPoolViewModel() } val relayViewModel: RelayPoolViewModel = viewModel { RelayPoolViewModel() }
var wantsToEditRelays by remember { var wantsToEditRelays by remember {
@ -322,7 +324,7 @@ fun ListContent(
tint = MaterialTheme.colors.primary, tint = MaterialTheme.colors.primary,
nav = nav, nav = nav,
scaffoldState = scaffoldState, scaffoldState = scaffoldState,
route = "User/${account.userProfile().pubkeyHex}" route = route
) )
NavigationRow( NavigationRow(
@ -481,20 +483,8 @@ private fun enableTor(
private fun RelayStatus( private fun RelayStatus(
relayViewModel: RelayPoolViewModel relayViewModel: RelayPoolViewModel
) { ) {
val connectedRelaysLiveData = relayViewModel.connectedRelaysLiveData.observeAsState() val connectedRelaysText by relayViewModel.connectionStatus.observeAsState("--/--")
val availableRelaysLiveData = relayViewModel.availableRelaysLiveData.observeAsState() val isConnected by relayViewModel.isConnected.distinctUntilChanged().observeAsState(false)
val connectedRelaysText by remember(connectedRelaysLiveData, availableRelaysLiveData) {
derivedStateOf {
"${connectedRelaysLiveData.value ?: "--"}/${availableRelaysLiveData.value ?: "--"}"
}
}
val isConnected by remember(connectedRelaysLiveData) {
derivedStateOf {
(connectedRelaysLiveData.value ?: 0) > 0
}
}
RenderRelayStatus(connectedRelaysText, isConnected) RenderRelayStatus(connectedRelaysText, isConnected)
} }

Wyświetl plik

@ -1,15 +1,18 @@
package com.vitorpamplona.amethyst.ui.screen package com.vitorpamplona.amethyst.ui.screen
import androidx.lifecycle.LiveData
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import androidx.lifecycle.distinctUntilChanged
import androidx.lifecycle.map import androidx.lifecycle.map
import com.vitorpamplona.amethyst.service.relays.RelayPool import com.vitorpamplona.amethyst.service.relays.RelayPool
class RelayPoolViewModel : ViewModel() { class RelayPoolViewModel : ViewModel() {
val connectedRelaysLiveData: LiveData<Int> = RelayPool.live.map { val connectionStatus = RelayPool.live.map {
it.relays.connectedRelays() val connectedRelays = it.relays.connectedRelays()
} val availableRelays = it.relays.availableRelays()
val availableRelaysLiveData: LiveData<Int> = RelayPool.live.map { "$connectedRelays/$availableRelays"
it.relays.availableRelays() }.distinctUntilChanged()
val isConnected = RelayPool.live.map {
it.relays.connectedRelays() > 0
} }
} }