only allow one vote per option on atomic (main==max) polls

fix buggy click event on disabled zap button
This commit is contained in:
toadlyBroodle
2023-03-26 14:34:32 +09:00
parent 62ff9ac94b
commit b65139f520
3 changed files with 19 additions and 8 deletions
@@ -138,7 +138,16 @@ fun ZapVote(
) )
.show() .show()
} }
} else if (pollViewModel.isVoteAmountAtomic) { } else if (pollViewModel.isVoteAmountAtomic()) {
// only allow one vote per option when min==max, i.e. atomic vote amount specified
if (pollViewModel.isPollOptionZappedBy(pollOption, account.userProfile())) {
scope.launch {
Toast
.makeText(context, R.string.one_vote_per_user_on_atomic_votes, Toast.LENGTH_SHORT)
.show()
}
return@combinedClickable
}
accountViewModel.zap( accountViewModel.zap(
baseNote, baseNote,
pollViewModel.valueMaximum!!.toLong() * 1000, pollViewModel.valueMaximum!!.toLong() * 1000,
@@ -263,11 +272,12 @@ fun ZapVoteAmountChoicePopup(
} }
) )
val isValidInputAmount = pollViewModel.isValidInputVoteAmount(amount)
Button( Button(
modifier = Modifier.padding(horizontal = 3.dp), modifier = Modifier.padding(horizontal = 3.dp),
enabled = pollViewModel.isValidInputVoteAmount(amount), enabled = isValidInputAmount,
onClick = { onClick = {
if (amount != null) { if (amount != null && isValidInputAmount) {
accountViewModel.zap( accountViewModel.zap(
baseNote, baseNote,
amount * 1000, amount * 1000,
@@ -276,8 +286,8 @@ fun ZapVoteAmountChoicePopup(
context, context,
onError onError
) )
}
onDismiss() onDismiss()
}
}, },
shape = RoundedCornerShape(20.dp), shape = RoundedCornerShape(20.dp),
colors = ButtonDefaults colors = ButtonDefaults
@@ -291,7 +301,7 @@ fun ZapVoteAmountChoicePopup(
textAlign = TextAlign.Center, textAlign = TextAlign.Center,
modifier = Modifier.combinedClickable( modifier = Modifier.combinedClickable(
onClick = { onClick = {
if (amount != null) { if (amount != null && isValidInputAmount) {
accountViewModel.zap( accountViewModel.zap(
baseNote, baseNote,
amount * 1000, amount * 1000,
@@ -300,8 +310,8 @@ fun ZapVoteAmountChoicePopup(
context, context,
onError onError
) )
}
onDismiss() onDismiss()
}
}, },
onLongClick = {} onLongClick = {}
) )
@@ -28,12 +28,12 @@ class PollNoteViewModel {
closedAt = pollEvent?.getTagInt(CLOSED_AT) closedAt = pollEvent?.getTagInt(CLOSED_AT)
} }
fun isVoteAmountAtomic() = valueMaximum != null && valueMinimum != null && valueMinimum == valueMaximum
val isPollClosed: Boolean = closedAt?.let { // allow 2 minute leeway for zap to propagate val isPollClosed: Boolean = closedAt?.let { // allow 2 minute leeway for zap to propagate
pollNote?.createdAt()?.plus(it * (86400 + 120))!! > Date().time / 1000 pollNote?.createdAt()?.plus(it * (86400 + 120))!! > Date().time / 1000
} == true } == true
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
} else if (valueMinimum == null) { } else if (valueMinimum == null) {
+1
View File
@@ -235,5 +235,6 @@
<string name="poll_closing_time_days">days</string> <string name="poll_closing_time_days">days</string>
<string name="poll_is_closed">Poll is closed to new votes</string> <string name="poll_is_closed">Poll is closed to new votes</string>
<string name="poll_zap_amount">Zap amount</string> <string name="poll_zap_amount">Zap amount</string>
<string name="one_vote_per_user_on_atomic_votes">Only one vote per user is allowed on this type of poll</string>
</resources> </resources>