Correctly updating the screen after zaps have been accounted for

This commit is contained in:
Vitor Pamplona 2023-04-25 17:02:44 -04:00
parent 91f3f036ae
commit b9f58b3701
2 changed files with 130 additions and 88 deletions

View File

@ -37,6 +37,7 @@ import com.vitorpamplona.amethyst.ui.theme.BitcoinOrange
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import java.math.BigDecimal
import java.util.* import java.util.*
import kotlin.math.roundToInt import kotlin.math.roundToInt
@ -59,39 +60,74 @@ fun PollNote(
pollViewModel.load(zappedNote) pollViewModel.load(zappedNote)
pollViewModel.pollEvent?.pollOptions()?.forEach { poll_op -> pollViewModel.pollEvent?.pollOptions()?.forEach { poll_op ->
val optionTally = pollViewModel.optionVoteTally(poll_op.key) OptionNote(
val color = if ( poll_op.key,
pollViewModel.consensusThreshold != null && poll_op.value,
optionTally >= pollViewModel.consensusThreshold!! pollViewModel,
) { baseNote,
Color.Green.copy(alpha = 0.32f) accountViewModel,
} else { canPreview,
MaterialTheme.colors.primary.copy(alpha = 0.32f) backgroundColor,
navController
)
}
} }
@Composable
private fun OptionNote(
optionNumber: Int,
optionText: String,
pollViewModel: PollNoteViewModel,
baseNote: Note,
accountViewModel: AccountViewModel,
canPreview: Boolean,
backgroundColor: Color,
navController: NavController
) {
Row( Row(
verticalAlignment = Alignment.CenterVertically, verticalAlignment = Alignment.CenterVertically,
modifier = Modifier.padding(vertical = 3.dp) modifier = Modifier.padding(vertical = 3.dp)
) { ) {
if (pollViewModel.canZap()) { 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( ZapVote(
baseNote, baseNote,
accountViewModel, accountViewModel,
pollViewModel, pollViewModel,
poll_op.key, optionNumber,
nonClickablePrepend = { nonClickablePrepend = {
Box( Box(
Modifier.fillMaxWidth(0.75f).clip(shape = RoundedCornerShape(15.dp)) Modifier
.fillMaxWidth(0.75f)
.clip(shape = RoundedCornerShape(15.dp))
.border( .border(
2.dp, 2.dp,
color, optionTally.second,
RoundedCornerShape(15.dp) RoundedCornerShape(15.dp)
) )
) { ) {
LinearProgressIndicator( LinearProgressIndicator(
modifier = Modifier.matchParentSize(), modifier = Modifier.matchParentSize(),
color = color, color = optionTally.second,
progress = optionTally.toFloat() progress = optionTally.first.toFloat()
) )
Row( Row(
@ -99,17 +135,23 @@ fun PollNote(
) { ) {
Column( Column(
horizontalAlignment = Alignment.End, horizontalAlignment = Alignment.End,
modifier = Modifier.padding(horizontal = 10.dp).width(40.dp) modifier = Modifier
.padding(horizontal = 10.dp)
.width(40.dp)
) { ) {
Text( Text(
text = "${(optionTally.toFloat() * 100).roundToInt()}%", text = "${(optionTally.first.toFloat() * 100).roundToInt()}%",
fontWeight = FontWeight.Bold fontWeight = FontWeight.Bold
) )
} }
Column(modifier = Modifier.fillMaxWidth().padding(15.dp)) { Column(
modifier = Modifier
.fillMaxWidth()
.padding(15.dp)
) {
TranslatableRichTextViewer( TranslatableRichTextViewer(
poll_op.value, optionText,
canPreview, canPreview,
Modifier, Modifier,
pollViewModel.pollEvent?.tags(), pollViewModel.pollEvent?.tags(),
@ -129,11 +171,12 @@ fun PollNote(
baseNote, baseNote,
accountViewModel, accountViewModel,
pollViewModel, pollViewModel,
poll_op.key, optionNumber,
nonClickablePrepend = {}, nonClickablePrepend = {},
clickablePrepend = { clickablePrepend = {
Box( Box(
Modifier.fillMaxWidth(0.75f) Modifier
.fillMaxWidth(0.75f)
.clip(shape = RoundedCornerShape(15.dp)) .clip(shape = RoundedCornerShape(15.dp))
.border( .border(
2.dp, 2.dp,
@ -142,7 +185,7 @@ fun PollNote(
) )
) { ) {
TranslatableRichTextViewer( TranslatableRichTextViewer(
poll_op.value, optionText,
canPreview, canPreview,
Modifier.padding(15.dp), Modifier.padding(15.dp),
pollViewModel.pollEvent?.tags(), pollViewModel.pollEvent?.tags(),
@ -156,7 +199,6 @@ fun PollNote(
} }
} }
} }
}
@Composable @Composable
@OptIn(ExperimentalFoundationApi::class) @OptIn(ExperimentalFoundationApi::class)
@ -294,7 +336,7 @@ fun ZapVote(
var optionWasZappedByLoggedInUser by remember { mutableStateOf(false) } var optionWasZappedByLoggedInUser by remember { mutableStateOf(false) }
LaunchedEffect(key1 = zappedNote) { LaunchedEffect(key1 = zapsState) {
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
if (!optionWasZappedByLoggedInUser) { if (!optionWasZappedByLoggedInUser) {
optionWasZappedByLoggedInUser = pollViewModel.isPollOptionZappedBy(pollOption, accountViewModel.userProfile()) optionWasZappedByLoggedInUser = pollViewModel.isPollOptionZappedBy(pollOption, accountViewModel.userProfile())
@ -331,7 +373,7 @@ fun ZapVote(
var wasZappedByLoggedInUser by remember { mutableStateOf(false) } var wasZappedByLoggedInUser by remember { mutableStateOf(false) }
LaunchedEffect(key1 = zappedNote) { LaunchedEffect(key1 = zapsState) {
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
if (!wasZappedByLoggedInUser) { if (!wasZappedByLoggedInUser) {
wasZappedByLoggedInUser = zappedNote?.isZappedBy(accountViewModel.userProfile(), account) == true wasZappedByLoggedInUser = zappedNote?.isZappedBy(accountViewModel.userProfile(), account) == true

View File

@ -316,7 +316,7 @@ fun ZapReaction(
var wasZappedByLoggedInUser by remember { mutableStateOf(false) } var wasZappedByLoggedInUser by remember { mutableStateOf(false) }
LaunchedEffect(key1 = zappedNote) { LaunchedEffect(key1 = zapsState) {
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
if (!wasZappedByLoggedInUser) { if (!wasZappedByLoggedInUser) {
wasZappedByLoggedInUser = zappedNote?.isZappedBy(account.userProfile(), account) == true wasZappedByLoggedInUser = zappedNote?.isZappedBy(account.userProfile(), account) == true