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,101 +60,142 @@ 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
)
}
}
Row( @Composable
verticalAlignment = Alignment.CenterVertically, private fun OptionNote(
modifier = Modifier.padding(vertical = 3.dp) optionNumber: Int,
) { optionText: String,
if (pollViewModel.canZap()) { pollViewModel: PollNoteViewModel,
ZapVote( baseNote: Note,
baseNote, accountViewModel: AccountViewModel,
accountViewModel, canPreview: Boolean,
pollViewModel, backgroundColor: Color,
poll_op.key, navController: NavController
nonClickablePrepend = { ) {
Box( Row(
Modifier.fillMaxWidth(0.75f).clip(shape = RoundedCornerShape(15.dp)) verticalAlignment = Alignment.CenterVertically,
.border( modifier = Modifier.padding(vertical = 3.dp)
2.dp, ) {
color, if (!pollViewModel.canZap()) {
RoundedCornerShape(15.dp) val defaultColor = MaterialTheme.colors.primary.copy(alpha = 0.32f)
) var optionTally by remember { mutableStateOf(Pair(BigDecimal.ZERO, defaultColor)) }
) {
LinearProgressIndicator( LaunchedEffect(key1 = optionNumber, key2 = pollViewModel) {
modifier = Modifier.matchParentSize(), val myTally = pollViewModel.optionVoteTally(optionNumber)
color = color, val color = if (
progress = optionTally.toFloat() 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( Row(
verticalAlignment = Alignment.CenterVertically verticalAlignment = Alignment.CenterVertically
) {
Column(
horizontalAlignment = Alignment.End,
modifier = Modifier
.padding(horizontal = 10.dp)
.width(40.dp)
) { ) {
Column( Text(
horizontalAlignment = Alignment.End, text = "${(optionTally.first.toFloat() * 100).roundToInt()}%",
modifier = Modifier.padding(horizontal = 10.dp).width(40.dp) fontWeight = FontWeight.Bold
) { )
Text( }
text = "${(optionTally.toFloat() * 100).roundToInt()}%",
fontWeight = FontWeight.Bold
)
}
Column(modifier = Modifier.fillMaxWidth().padding(15.dp)) { Column(
TranslatableRichTextViewer( modifier = Modifier
poll_op.value, .fillMaxWidth()
canPreview, .padding(15.dp)
Modifier, ) {
pollViewModel.pollEvent?.tags(), TranslatableRichTextViewer(
backgroundColor, optionText,
accountViewModel, canPreview,
navController Modifier,
) pollViewModel.pollEvent?.tags(),
} backgroundColor,
accountViewModel,
navController
)
} }
} }
},
clickablePrepend = {
} }
) },
} else { clickablePrepend = {
ZapVote( }
baseNote, )
accountViewModel, } else {
pollViewModel, ZapVote(
poll_op.key, baseNote,
nonClickablePrepend = {}, accountViewModel,
clickablePrepend = { pollViewModel,
Box( optionNumber,
Modifier.fillMaxWidth(0.75f) nonClickablePrepend = {},
.clip(shape = RoundedCornerShape(15.dp)) clickablePrepend = {
.border( Box(
2.dp, Modifier
MaterialTheme.colors.primary, .fillMaxWidth(0.75f)
RoundedCornerShape(15.dp) .clip(shape = RoundedCornerShape(15.dp))
) .border(
) { 2.dp,
TranslatableRichTextViewer( MaterialTheme.colors.primary,
poll_op.value, RoundedCornerShape(15.dp)
canPreview,
Modifier.padding(15.dp),
pollViewModel.pollEvent?.tags(),
backgroundColor,
accountViewModel,
navController
) )
} ) {
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) } 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