mirror of
https://github.com/vitorpamplona/amethyst.git
synced 2024-09-29 16:30:49 +00:00
move option tally logic to PollNoteViewModel
This commit is contained in:
parent
12a1c3fe6d
commit
584c2860e4
@ -203,20 +203,6 @@ open class Note(val idHex: String) {
|
|||||||
return zaps.any { it.key.author == user }
|
return zaps.any { it.key.author == user }
|
||||||
}
|
}
|
||||||
|
|
||||||
fun isPollOptionZappedBy(option: Int, user: User): Boolean {
|
|
||||||
if (zaps.any { it.key.author == user }) {
|
|
||||||
zaps.mapNotNull { it.value?.event }
|
|
||||||
.filterIsInstance<LnZapEvent>()
|
|
||||||
.map {
|
|
||||||
val zappedOption = it.zappedPollOption()
|
|
||||||
if (zappedOption == option) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
fun isReactedBy(user: User): Boolean {
|
fun isReactedBy(user: User): Boolean {
|
||||||
return reactions.any { it.author == user }
|
return reactions.any { it.author == user }
|
||||||
}
|
}
|
||||||
@ -251,17 +237,6 @@ open class Note(val idHex: String) {
|
|||||||
}.sumOf { it }
|
}.sumOf { it }
|
||||||
}
|
}
|
||||||
|
|
||||||
fun zappedPollOptionAmount(option: Int): BigDecimal {
|
|
||||||
return zaps.mapNotNull { it.value?.event }
|
|
||||||
.filterIsInstance<LnZapEvent>()
|
|
||||||
.mapNotNull {
|
|
||||||
val zappedOption = it.zappedPollOption()
|
|
||||||
if (zappedOption == option) {
|
|
||||||
it.amount
|
|
||||||
} else { null }
|
|
||||||
}.sumOf { it }
|
|
||||||
}
|
|
||||||
|
|
||||||
fun hasAnyReports(): Boolean {
|
fun hasAnyReports(): Boolean {
|
||||||
val dayAgo = Date().time / 1000 - 24 * 60 * 60
|
val dayAgo = Date().time / 1000 - 24 * 60 * 60
|
||||||
return reports.isNotEmpty() ||
|
return reports.isNotEmpty() ||
|
||||||
|
@ -114,7 +114,7 @@ fun NewPollView(onClose: () -> Unit, baseReplyTo: Note? = null, quote: Note? = n
|
|||||||
}
|
}
|
||||||
|
|
||||||
Text(stringResource(R.string.poll_heading_required))
|
Text(stringResource(R.string.poll_heading_required))
|
||||||
NewPollRecipientsField(pollViewModel, account)
|
// NewPollRecipientsField(pollViewModel, account)
|
||||||
NewPollPrimaryDescription(pollViewModel)
|
NewPollPrimaryDescription(pollViewModel)
|
||||||
pollViewModel.pollOptions.values.forEachIndexed { index, element ->
|
pollViewModel.pollOptions.values.forEachIndexed { index, element ->
|
||||||
NewPollOption(pollViewModel, index)
|
NewPollOption(pollViewModel, index)
|
||||||
|
@ -166,7 +166,7 @@ fun ZapVote(
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (zappedNote?.isPollOptionZappedBy(pollOption, account.userProfile()) == true) {
|
if (pollViewModel.isPollOptionZappedBy(pollOption, account.userProfile())) {
|
||||||
Icon(
|
Icon(
|
||||||
imageVector = Icons.Default.Bolt,
|
imageVector = Icons.Default.Bolt,
|
||||||
contentDescription = stringResource(R.string.zaps),
|
contentDescription = stringResource(R.string.zaps),
|
||||||
@ -186,7 +186,7 @@ fun ZapVote(
|
|||||||
// only show tallies after a user has zapped note
|
// only show tallies after a user has zapped note
|
||||||
if (zappedNote?.isZappedBy(account.userProfile()) == true) {
|
if (zappedNote?.isZappedBy(account.userProfile()) == true) {
|
||||||
Text(
|
Text(
|
||||||
showAmount(zappedNote.zappedPollOptionAmount(pollOption)),
|
showAmount(pollViewModel.zappedPollOptionAmount(pollOption)),
|
||||||
fontSize = 14.sp,
|
fontSize = 14.sp,
|
||||||
color = MaterialTheme.colors.onSurface.copy(alpha = 0.32f),
|
color = MaterialTheme.colors.onSurface.copy(alpha = 0.32f),
|
||||||
modifier = modifier
|
modifier = modifier
|
||||||
|
@ -2,7 +2,9 @@ package com.vitorpamplona.amethyst.ui.note
|
|||||||
|
|
||||||
import com.vitorpamplona.amethyst.model.Account
|
import com.vitorpamplona.amethyst.model.Account
|
||||||
import com.vitorpamplona.amethyst.model.Note
|
import com.vitorpamplona.amethyst.model.Note
|
||||||
|
import com.vitorpamplona.amethyst.model.User
|
||||||
import com.vitorpamplona.amethyst.service.model.*
|
import com.vitorpamplona.amethyst.service.model.*
|
||||||
|
import java.math.BigDecimal
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
class PollNoteViewModel {
|
class PollNoteViewModel {
|
||||||
@ -72,7 +74,7 @@ class PollNoteViewModel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun optionVoteTally(op: Int): Float {
|
fun optionVoteTally(op: Int): Float {
|
||||||
val tally = pollNote?.zappedPollOptionAmount(op)?.toFloat()?.div(zappedVoteTotal()) ?: 0f
|
val tally = zappedPollOptionAmount(op).toFloat().div(zappedVoteTotal())
|
||||||
return if (tally.isNaN()) { // catch div by 0
|
return if (tally.isNaN()) { // catch div by 0
|
||||||
0f
|
0f
|
||||||
} else { tally }
|
} else { tally }
|
||||||
@ -81,8 +83,35 @@ class PollNoteViewModel {
|
|||||||
private fun zappedVoteTotal(): Float {
|
private fun zappedVoteTotal(): Float {
|
||||||
var total = 0f
|
var total = 0f
|
||||||
pollOptions?.keys?.forEach {
|
pollOptions?.keys?.forEach {
|
||||||
total += pollNote?.zappedPollOptionAmount(it)?.toFloat() ?: 0f
|
total += zappedPollOptionAmount(it).toFloat() ?: 0f
|
||||||
}
|
}
|
||||||
return total
|
return total
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun isPollOptionZappedBy(option: Int, user: User): Boolean {
|
||||||
|
if (pollNote?.zaps?.any { it.key.author == user } == true) {
|
||||||
|
pollNote!!.zaps.mapNotNull { it.value?.event }
|
||||||
|
.filterIsInstance<LnZapEvent>()
|
||||||
|
.map {
|
||||||
|
val zappedOption = it.zappedPollOption()
|
||||||
|
if (zappedOption == option) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
fun zappedPollOptionAmount(option: Int): BigDecimal {
|
||||||
|
if (pollNote != null) {
|
||||||
|
return pollNote!!.zaps.mapNotNull { it.value?.event }
|
||||||
|
.filterIsInstance<LnZapEvent>()
|
||||||
|
.mapNotNull {
|
||||||
|
val zappedOption = it.zappedPollOption()
|
||||||
|
if (zappedOption == option) {
|
||||||
|
it.amount
|
||||||
|
} else { null }
|
||||||
|
}.sumOf { it }
|
||||||
|
} else { return BigDecimal(0) }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user