From 915ddd0bad75b8a9c33280298163bd14b0924148 Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Thu, 22 Jun 2023 11:49:15 -0400 Subject: [PATCH] Improving relay options rendering performance. --- .../amethyst/ui/note/ReactionsRow.kt | 10 ++--- .../amethyst/ui/note/RelayCompose.kt | 39 ++++++++++++++----- .../ui/screen/loggedIn/AccountViewModel.kt | 1 + 3 files changed, 35 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/ReactionsRow.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/ReactionsRow.kt index 4ded6f804..108ec561c 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/ReactionsRow.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/ReactionsRow.kt @@ -883,13 +883,11 @@ fun ZapReaction( if (zappingProgress > 0.00001 && zappingProgress < 0.99999) { Spacer(Modifier.width(3.dp)) - val animatedProgress = animateFloatAsState( - targetValue = zappingProgress, - animationSpec = ProgressIndicatorDefaults.ProgressAnimationSpec - ).value - CircularProgressIndicator( - progress = animatedProgress, + progress = animateFloatAsState( + targetValue = zappingProgress, + animationSpec = ProgressIndicatorDefaults.ProgressAnimationSpec + ).value, modifier = remember { Modifier.size(animationSize) }, strokeWidth = 2.dp ) diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/RelayCompose.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/RelayCompose.kt index 95beaa343..077b3e6e8 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/RelayCompose.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/RelayCompose.kt @@ -11,8 +11,10 @@ import androidx.compose.material.Divider import androidx.compose.material.MaterialTheme import androidx.compose.material.Text import androidx.compose.runtime.Composable +import androidx.compose.runtime.derivedStateOf import androidx.compose.runtime.getValue import androidx.compose.runtime.livedata.observeAsState +import androidx.compose.runtime.remember import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color @@ -37,9 +39,6 @@ fun RelayCompose( onAddRelay: () -> Unit, onRemoveRelay: () -> Unit ) { - val accountState by accountViewModel.accountLiveData.observeAsState() - val account = accountState?.account ?: return - val context = LocalContext.current Column() { @@ -60,8 +59,14 @@ fun RelayCompose( overflow = TextOverflow.Ellipsis ) + val lastTime by remember(relay.lastEvent) { + derivedStateOf { + timeAgo(relay.lastEvent, context = context) + } + } + Text( - timeAgo(relay.lastEvent, context = context), + text = lastTime, maxLines = 1 ) } @@ -75,11 +80,7 @@ fun RelayCompose( } Column(modifier = Modifier.padding(start = 10.dp)) { - if (account.activeRelays()?.none { it.url == relay.url } == true) { - AddRelayButton { onAddRelay() } - } else { - RemoveRelayButton { onRemoveRelay() } - } + RelayOptions(accountViewModel, relay, onAddRelay, 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 fun AddRelayButton(onClick: () -> Unit) { Button( 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 ef793eb5c..d508e5d5a 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 @@ -34,6 +34,7 @@ class AccountViewModel(val account: Account) : ViewModel() { val accountLastReadLiveData: LiveData = account.liveLastRead.map { it } val userFollows: LiveData = account.userProfile().live().follows.map { it } + val userRelays: LiveData = account.userProfile().live().relays.map { it } fun isWriteable(): Boolean { return account.isWriteable()