From 755d89dc79dcc0a69c2364ffc3bf0e67fb4afbab Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Thu, 25 May 2023 21:09:14 -0400 Subject: [PATCH] Only changing stats for the Reaction Row if the data actually changed. --- .../amethyst/ui/note/ReactionsRow.kt | 69 ++++++++++++++++--- 1 file changed, 58 insertions(+), 11 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 3c0fb6278..8d671a9f2 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 @@ -28,6 +28,7 @@ import androidx.compose.material.icons.outlined.Bolt import androidx.compose.material.ripple.rememberRipple import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.derivedStateOf import androidx.compose.runtime.getValue import androidx.compose.runtime.livedata.observeAsState import androidx.compose.runtime.mutableStateOf @@ -62,6 +63,7 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import java.math.BigDecimal import java.math.RoundingMode +import kotlin.math.abs import kotlin.math.roundToInt @Composable @@ -124,6 +126,14 @@ fun ReplyReaction( val repliesState by baseNote.live().replies.observeAsState() val replies = remember(repliesState) { repliesState?.note?.replies } ?: emptySet() + val isWriteable = remember { accountViewModel.isWriteable() } + + val replyCount by remember(repliesState) { + derivedStateOf { + showCount(replies.size) + } + } + val context = LocalContext.current val scope = rememberCoroutineScope() @@ -138,7 +148,7 @@ fun ReplyReaction( IconButton( modifier = iconButtonModifier, onClick = { - if (accountViewModel.isWriteable()) { + if (isWriteable) { onPress() } else { scope.launch { @@ -161,7 +171,7 @@ fun ReplyReaction( if (showCounter) { Text( - " ${showCount(replies.size)}", + " $replyCount", fontSize = 14.sp, color = grayTint ) @@ -179,11 +189,25 @@ fun BoostReaction( val boostsState by baseNote.live().boosts.observeAsState() val boostedNote = remember(boostsState) { boostsState?.note } ?: return - val hasBoosted = remember(boostsState) { accountViewModel.hasBoosted(baseNote) } - val wasBoostedByLoggedIn = remember(boostsState) { boostedNote.isBoostedBy(accountViewModel.userProfile()) } + val hasBoosted by remember(boostsState) { + derivedStateOf { + accountViewModel.hasBoosted(baseNote) + } + } + + val wasBoostedByLoggedIn by remember(boostsState) { + derivedStateOf { + boostedNote.isBoostedBy(accountViewModel.userProfile()) + } + } + val isWriteable = remember { accountViewModel.isWriteable() } - val boostCount = remember(boostsState) { showCount(boostedNote.boosts.size) } + val boostCount by remember(boostsState) { + derivedStateOf { + showCount(boostedNote.boosts.size) + } + } val context = LocalContext.current val scope = rememberCoroutineScope() @@ -258,11 +282,25 @@ fun LikeReaction( val reactionsState by baseNote.live().reactions.observeAsState() val reactedNote = remember(reactionsState) { reactionsState?.note } ?: return - val hasReacted = remember(reactionsState) { accountViewModel.hasReactedTo(baseNote) } - val wasReactedByLoggedIn = remember(reactionsState) { reactedNote.isReactedBy(accountViewModel.userProfile()) } + val hasReacted by remember(reactionsState) { + derivedStateOf { + accountViewModel.hasReactedTo(baseNote) + } + } + + val wasReactedByLoggedIn by remember(reactionsState) { + derivedStateOf { + reactedNote.isReactedBy(accountViewModel.userProfile()) + } + } + val isWriteable = remember { accountViewModel.isWriteable() } - val reactionCount = remember(reactionsState) { showCount(reactedNote.reactions.size) } + val reactionCount by remember(reactionsState) { + derivedStateOf { + showCount(reactedNote.reactions.size) + } + } val context = LocalContext.current val scope = rememberCoroutineScope() @@ -350,13 +388,22 @@ fun ZapReaction( LaunchedEffect(key1 = zapsState) { scope.launch(Dispatchers.IO) { if (!wasZappedByLoggedInUser) { - wasZappedByLoggedInUser = accountViewModel.calculateIfNoteWasZappedByAccount(zappedNote) + val newWasZapped = accountViewModel.calculateIfNoteWasZappedByAccount(zappedNote) + + if (wasZappedByLoggedInUser != newWasZapped) { + wasZappedByLoggedInUser = newWasZapped + } } - zapAmountTxt = showAmount(account.calculateZappedAmount(zappedNote)) + val newZapAmount = showAmount(account.calculateZappedAmount(zappedNote)) + if (newZapAmount != zapAmountTxt) { + zapAmountTxt = newZapAmount + } if (wasZappedByLoggedInUser) { - zappingProgress = 1f + if (abs(zappingProgress - 1) < 0.001) { + zappingProgress = 1f + } } } }