Improving relay options rendering performance.

This commit is contained in:
Vitor Pamplona 2023-06-22 11:49:15 -04:00
parent 52ef356c06
commit 915ddd0bad
3 changed files with 35 additions and 15 deletions

View File

@ -883,13 +883,11 @@ fun ZapReaction(
if (zappingProgress > 0.00001 && zappingProgress < 0.99999) { if (zappingProgress > 0.00001 && zappingProgress < 0.99999) {
Spacer(Modifier.width(3.dp)) Spacer(Modifier.width(3.dp))
val animatedProgress = animateFloatAsState(
targetValue = zappingProgress,
animationSpec = ProgressIndicatorDefaults.ProgressAnimationSpec
).value
CircularProgressIndicator( CircularProgressIndicator(
progress = animatedProgress, progress = animateFloatAsState(
targetValue = zappingProgress,
animationSpec = ProgressIndicatorDefaults.ProgressAnimationSpec
).value,
modifier = remember { Modifier.size(animationSize) }, modifier = remember { Modifier.size(animationSize) },
strokeWidth = 2.dp strokeWidth = 2.dp
) )

View File

@ -11,8 +11,10 @@ import androidx.compose.material.Divider
import androidx.compose.material.MaterialTheme import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text import androidx.compose.material.Text
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
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.remember
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
@ -37,9 +39,6 @@ fun RelayCompose(
onAddRelay: () -> Unit, onAddRelay: () -> Unit,
onRemoveRelay: () -> Unit onRemoveRelay: () -> Unit
) { ) {
val accountState by accountViewModel.accountLiveData.observeAsState()
val account = accountState?.account ?: return
val context = LocalContext.current val context = LocalContext.current
Column() { Column() {
@ -60,8 +59,14 @@ fun RelayCompose(
overflow = TextOverflow.Ellipsis overflow = TextOverflow.Ellipsis
) )
val lastTime by remember(relay.lastEvent) {
derivedStateOf {
timeAgo(relay.lastEvent, context = context)
}
}
Text( Text(
timeAgo(relay.lastEvent, context = context), text = lastTime,
maxLines = 1 maxLines = 1
) )
} }
@ -75,11 +80,7 @@ fun RelayCompose(
} }
Column(modifier = Modifier.padding(start = 10.dp)) { Column(modifier = Modifier.padding(start = 10.dp)) {
if (account.activeRelays()?.none { it.url == relay.url } == true) { RelayOptions(accountViewModel, relay, onAddRelay, onRemoveRelay)
AddRelayButton { onAddRelay() }
} else {
RemoveRelayButton { onRemoveRelay() }
}
} }
} }
@ -90,6 +91,26 @@ fun RelayCompose(
} }
} }
@Composable
private fun RelayOptions(
accountViewModel: AccountViewModel,
relay: RelayInfo,
onAddRelay: () -> Unit,
onRemoveRelay: () -> Unit
) {
val userState by accountViewModel.userRelays.observeAsState()
val isNotUsingRelay = remember(userState) {
accountViewModel.account.activeRelays()?.none { it.url == relay.url } == true
}
if (isNotUsingRelay) {
AddRelayButton(onAddRelay)
} else {
RemoveRelayButton(onRemoveRelay)
}
}
@Composable @Composable
fun AddRelayButton(onClick: () -> Unit) { fun AddRelayButton(onClick: () -> Unit) {
Button( Button(

View File

@ -34,6 +34,7 @@ class AccountViewModel(val account: Account) : ViewModel() {
val accountLastReadLiveData: LiveData<AccountState> = account.liveLastRead.map { it } val accountLastReadLiveData: LiveData<AccountState> = account.liveLastRead.map { it }
val userFollows: LiveData<UserState> = account.userProfile().live().follows.map { it } val userFollows: LiveData<UserState> = account.userProfile().live().follows.map { it }
val userRelays: LiveData<UserState> = account.userProfile().live().relays.map { it }
fun isWriteable(): Boolean { fun isWriteable(): Boolean {
return account.isWriteable() return account.isWriteable()