diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/PollNote.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/PollNote.kt index ee2f86dc2..2d7f76f8c 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/PollNote.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/PollNote.kt @@ -37,6 +37,7 @@ import com.vitorpamplona.amethyst.ui.theme.BitcoinOrange import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import kotlinx.coroutines.withContext +import java.math.BigDecimal import java.util.* import kotlin.math.roundToInt @@ -59,101 +60,142 @@ fun PollNote( pollViewModel.load(zappedNote) pollViewModel.pollEvent?.pollOptions()?.forEach { poll_op -> - val optionTally = pollViewModel.optionVoteTally(poll_op.key) - val color = if ( - pollViewModel.consensusThreshold != null && - optionTally >= pollViewModel.consensusThreshold!! - ) { - Color.Green.copy(alpha = 0.32f) - } else { - MaterialTheme.colors.primary.copy(alpha = 0.32f) - } + OptionNote( + poll_op.key, + poll_op.value, + pollViewModel, + baseNote, + accountViewModel, + canPreview, + backgroundColor, + navController + ) + } +} - Row( - verticalAlignment = Alignment.CenterVertically, - modifier = Modifier.padding(vertical = 3.dp) - ) { - if (pollViewModel.canZap()) { - ZapVote( - baseNote, - accountViewModel, - pollViewModel, - poll_op.key, - nonClickablePrepend = { - Box( - Modifier.fillMaxWidth(0.75f).clip(shape = RoundedCornerShape(15.dp)) - .border( - 2.dp, - color, - RoundedCornerShape(15.dp) - ) - ) { - LinearProgressIndicator( - modifier = Modifier.matchParentSize(), - color = color, - progress = optionTally.toFloat() +@Composable +private fun OptionNote( + optionNumber: Int, + optionText: String, + pollViewModel: PollNoteViewModel, + baseNote: Note, + accountViewModel: AccountViewModel, + canPreview: Boolean, + backgroundColor: Color, + navController: NavController +) { + Row( + verticalAlignment = Alignment.CenterVertically, + modifier = Modifier.padding(vertical = 3.dp) + ) { + if (!pollViewModel.canZap()) { + val defaultColor = MaterialTheme.colors.primary.copy(alpha = 0.32f) + var optionTally by remember { mutableStateOf(Pair(BigDecimal.ZERO, defaultColor)) } + + LaunchedEffect(key1 = optionNumber, key2 = pollViewModel) { + val myTally = pollViewModel.optionVoteTally(optionNumber) + val color = if ( + pollViewModel.consensusThreshold != null && + myTally >= pollViewModel.consensusThreshold!! + ) { + Color.Green.copy(alpha = 0.32f) + } else { + defaultColor + } + + if (myTally > optionTally.first || color != optionTally.second) { + optionTally = Pair(myTally, color) + } + } + + ZapVote( + baseNote, + accountViewModel, + pollViewModel, + optionNumber, + nonClickablePrepend = { + Box( + Modifier + .fillMaxWidth(0.75f) + .clip(shape = RoundedCornerShape(15.dp)) + .border( + 2.dp, + optionTally.second, + RoundedCornerShape(15.dp) ) + ) { + LinearProgressIndicator( + modifier = Modifier.matchParentSize(), + color = optionTally.second, + progress = optionTally.first.toFloat() + ) - Row( - verticalAlignment = Alignment.CenterVertically + Row( + verticalAlignment = Alignment.CenterVertically + ) { + Column( + horizontalAlignment = Alignment.End, + modifier = Modifier + .padding(horizontal = 10.dp) + .width(40.dp) ) { - Column( - horizontalAlignment = Alignment.End, - modifier = Modifier.padding(horizontal = 10.dp).width(40.dp) - ) { - Text( - text = "${(optionTally.toFloat() * 100).roundToInt()}%", - fontWeight = FontWeight.Bold - ) - } + Text( + text = "${(optionTally.first.toFloat() * 100).roundToInt()}%", + fontWeight = FontWeight.Bold + ) + } - Column(modifier = Modifier.fillMaxWidth().padding(15.dp)) { - TranslatableRichTextViewer( - poll_op.value, - canPreview, - Modifier, - pollViewModel.pollEvent?.tags(), - backgroundColor, - accountViewModel, - navController - ) - } + Column( + modifier = Modifier + .fillMaxWidth() + .padding(15.dp) + ) { + TranslatableRichTextViewer( + optionText, + canPreview, + Modifier, + pollViewModel.pollEvent?.tags(), + backgroundColor, + accountViewModel, + navController + ) } } - }, - clickablePrepend = { } - ) - } else { - ZapVote( - baseNote, - accountViewModel, - pollViewModel, - poll_op.key, - nonClickablePrepend = {}, - clickablePrepend = { - Box( - Modifier.fillMaxWidth(0.75f) - .clip(shape = RoundedCornerShape(15.dp)) - .border( - 2.dp, - MaterialTheme.colors.primary, - RoundedCornerShape(15.dp) - ) - ) { - TranslatableRichTextViewer( - poll_op.value, - canPreview, - Modifier.padding(15.dp), - pollViewModel.pollEvent?.tags(), - backgroundColor, - accountViewModel, - navController + }, + clickablePrepend = { + } + ) + } else { + ZapVote( + baseNote, + accountViewModel, + pollViewModel, + optionNumber, + nonClickablePrepend = {}, + clickablePrepend = { + Box( + Modifier + .fillMaxWidth(0.75f) + .clip(shape = RoundedCornerShape(15.dp)) + .border( + 2.dp, + MaterialTheme.colors.primary, + RoundedCornerShape(15.dp) ) - } + ) { + TranslatableRichTextViewer( + optionText, + canPreview, + Modifier.padding(15.dp), + pollViewModel.pollEvent?.tags(), + backgroundColor, + accountViewModel, + navController + ) } - ) - } + } + ) } } } @@ -294,7 +336,7 @@ fun ZapVote( var optionWasZappedByLoggedInUser by remember { mutableStateOf(false) } - LaunchedEffect(key1 = zappedNote) { + LaunchedEffect(key1 = zapsState) { withContext(Dispatchers.IO) { if (!optionWasZappedByLoggedInUser) { optionWasZappedByLoggedInUser = pollViewModel.isPollOptionZappedBy(pollOption, accountViewModel.userProfile()) @@ -331,7 +373,7 @@ fun ZapVote( var wasZappedByLoggedInUser by remember { mutableStateOf(false) } - LaunchedEffect(key1 = zappedNote) { + LaunchedEffect(key1 = zapsState) { withContext(Dispatchers.IO) { if (!wasZappedByLoggedInUser) { wasZappedByLoggedInUser = zappedNote?.isZappedBy(accountViewModel.userProfile(), account) == true 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 c019d3acc..4b704985d 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 @@ -316,7 +316,7 @@ fun ZapReaction( var wasZappedByLoggedInUser by remember { mutableStateOf(false) } - LaunchedEffect(key1 = zappedNote) { + LaunchedEffect(key1 = zapsState) { withContext(Dispatchers.IO) { if (!wasZappedByLoggedInUser) { wasZappedByLoggedInUser = zappedNote?.isZappedBy(account.userProfile(), account) == true