color option progress bar green when consensus achieved,

add padding to option text
This commit is contained in:
toadlyBroodle
2023-03-26 13:52:27 +09:00
parent 584c2860e4
commit 62ff9ac94b
2 changed files with 27 additions and 15 deletions
@@ -47,6 +47,8 @@ fun PollNote(
pollViewModel.load(note) pollViewModel.load(note)
pollViewModel.pollEvent?.pollOptions()?.forEach { poll_op -> pollViewModel.pollEvent?.pollOptions()?.forEach { poll_op ->
val optionTally = pollViewModel.optionVoteTally(poll_op.key)
Row( Row(
verticalAlignment = Alignment.CenterVertically verticalAlignment = Alignment.CenterVertically
) { ) {
@@ -55,7 +57,9 @@ fun PollNote(
canPreview, canPreview,
modifier = Modifier modifier = Modifier
.width(250.dp) .width(250.dp)
.border(BorderStroke(1.dp, MaterialTheme.colors.onSurface.copy(alpha = 0.32f))), .padding(0.dp) // padding outside border
.border(BorderStroke(1.dp, MaterialTheme.colors.onSurface.copy(alpha = 0.32f)))
.padding(4.dp), // padding between border and text
pollViewModel.pollEvent?.tags(), pollViewModel.pollEvent?.tags(),
backgroundColor, backgroundColor,
accountViewModel, accountViewModel,
@@ -72,7 +76,13 @@ fun PollNote(
if (note.isZappedBy(accountViewModel.userProfile())) { if (note.isZappedBy(accountViewModel.userProfile())) {
LinearProgressIndicator( LinearProgressIndicator(
modifier = Modifier.width(250.dp), modifier = Modifier.width(250.dp),
progress = pollViewModel.optionVoteTally(poll_op.key) color = if (
pollViewModel.consensusThreshold != null &&
optionTally >= pollViewModel.consensusThreshold!!
) {
Color.Green
} else { MaterialTheme.colors.primary },
progress = optionTally
) )
} }
} }
@@ -128,7 +138,7 @@ fun ZapVote(
) )
.show() .show()
} }
} else if (pollViewModel.isVoteAmountAtomic()) { } else if (pollViewModel.isVoteAmountAtomic) {
accountViewModel.zap( accountViewModel.zap(
baseNote, baseNote,
pollViewModel.valueMaximum!!.toLong() * 1000, pollViewModel.valueMaximum!!.toLong() * 1000,
@@ -231,14 +241,14 @@ fun ZapVoteAmountChoicePopup(
modifier = Modifier modifier = Modifier
.padding(10.dp) .padding(10.dp)
) { ) {
val amount = pollViewModel.inputAmountLong(inputAmountText) val amount = pollViewModel.inputVoteAmountLong(inputAmountText)
OutlinedTextField( OutlinedTextField(
value = inputAmountText, value = inputAmountText,
onValueChange = { inputAmountText = it }, onValueChange = { inputAmountText = it },
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number), keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
modifier = Modifier.width(150.dp), modifier = Modifier.width(150.dp),
colors = if (pollViewModel.isValidInputAmount(amount)) colorValid else colorInValid, colors = if (pollViewModel.isValidInputVoteAmount(amount)) colorValid else colorInValid,
label = { label = {
Text( Text(
text = stringResource(R.string.poll_zap_amount), text = stringResource(R.string.poll_zap_amount),
@@ -255,7 +265,7 @@ fun ZapVoteAmountChoicePopup(
Button( Button(
modifier = Modifier.padding(horizontal = 3.dp), modifier = Modifier.padding(horizontal = 3.dp),
enabled = pollViewModel.isValidInputAmount(amount), enabled = pollViewModel.isValidInputVoteAmount(amount),
onClick = { onClick = {
if (amount != null) { if (amount != null) {
accountViewModel.zap( accountViewModel.zap(
@@ -16,7 +16,7 @@ class PollNoteViewModel {
var valueMaximum: Int? = null var valueMaximum: Int? = null
private var valueMinimum: Int? = null private var valueMinimum: Int? = null
private var closedAt: Int? = null private var closedAt: Int? = null
private var consensusThreshold: Int? = null var consensusThreshold: Float? = null
fun load(note: Note?) { fun load(note: Note?) {
pollNote = note pollNote = note
@@ -24,7 +24,7 @@ class PollNoteViewModel {
pollOptions = pollEvent?.pollOptions() pollOptions = pollEvent?.pollOptions()
valueMaximum = pollEvent?.getTagInt(VALUE_MAXIMUM) valueMaximum = pollEvent?.getTagInt(VALUE_MAXIMUM)
valueMinimum = pollEvent?.getTagInt(VALUE_MINIMUM) valueMinimum = pollEvent?.getTagInt(VALUE_MINIMUM)
consensusThreshold = pollEvent?.getTagInt(CONSENSUS_THRESHOLD) consensusThreshold = pollEvent?.getTagInt(CONSENSUS_THRESHOLD)?.toFloat()?.div(100)
closedAt = pollEvent?.getTagInt(CLOSED_AT) closedAt = pollEvent?.getTagInt(CLOSED_AT)
} }
@@ -32,7 +32,7 @@ class PollNoteViewModel {
pollNote?.createdAt()?.plus(it * (86400 + 120))!! > Date().time / 1000 pollNote?.createdAt()?.plus(it * (86400 + 120))!! > Date().time / 1000
} == true } == true
fun isVoteAmountAtomic(): Boolean = valueMaximum != null && valueMinimum != null && valueMinimum == valueMaximum val isVoteAmountAtomic = valueMaximum != null && valueMinimum != null && valueMinimum == valueMaximum
fun voteAmountPlaceHolderText(sats: String): String = if (valueMinimum == null && valueMaximum == null) { fun voteAmountPlaceHolderText(sats: String): String = if (valueMinimum == null && valueMaximum == null) {
sats sats
@@ -44,13 +44,13 @@ class PollNoteViewModel {
"$valueMinimum$valueMaximum $sats" "$valueMinimum$valueMaximum $sats"
} }
fun inputAmountLong(textAmount: String) = if (textAmount.isEmpty()) { null } else { fun inputVoteAmountLong(textAmount: String) = if (textAmount.isEmpty()) { null } else {
try { try {
textAmount.toLong() textAmount.toLong()
} catch (e: Exception) { null } } catch (e: Exception) { null }
} }
fun isValidInputAmount(amount: Long?): Boolean { fun isValidInputVoteAmount(amount: Long?): Boolean {
if (amount == null) { if (amount == null) {
return false return false
} else if (valueMinimum == null && valueMaximum == null) { } else if (valueMinimum == null && valueMaximum == null) {
@@ -83,7 +83,7 @@ class PollNoteViewModel {
private fun zappedVoteTotal(): Float { private fun zappedVoteTotal(): Float {
var total = 0f var total = 0f
pollOptions?.keys?.forEach { pollOptions?.keys?.forEach {
total += zappedPollOptionAmount(it).toFloat() ?: 0f total += zappedPollOptionAmount(it).toFloat()
} }
return total return total
} }
@@ -103,8 +103,8 @@ class PollNoteViewModel {
} }
fun zappedPollOptionAmount(option: Int): BigDecimal { fun zappedPollOptionAmount(option: Int): BigDecimal {
if (pollNote != null) { return if (pollNote != null) {
return pollNote!!.zaps.mapNotNull { it.value?.event } pollNote!!.zaps.mapNotNull { it.value?.event }
.filterIsInstance<LnZapEvent>() .filterIsInstance<LnZapEvent>()
.mapNotNull { .mapNotNull {
val zappedOption = it.zappedPollOption() val zappedOption = it.zappedPollOption()
@@ -112,6 +112,8 @@ class PollNoteViewModel {
it.amount it.amount
} else { null } } else { null }
}.sumOf { it } }.sumOf { it }
} else { return BigDecimal(0) } } else {
BigDecimal(0)
}
} }
} }