diff --git a/app/src/main/java/com/vitorpamplona/amethyst/model/Note.kt b/app/src/main/java/com/vitorpamplona/amethyst/model/Note.kt index 283f47262..8437d532b 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/model/Note.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/model/Note.kt @@ -203,20 +203,6 @@ open class Note(val idHex: String) { 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() - .map { - val zappedOption = it.zappedPollOption() - if (zappedOption == option) { - return true - } - } - } - return false - } - fun isReactedBy(user: User): Boolean { return reactions.any { it.author == user } } @@ -251,17 +237,6 @@ open class Note(val idHex: String) { }.sumOf { it } } - fun zappedPollOptionAmount(option: Int): BigDecimal { - return zaps.mapNotNull { it.value?.event } - .filterIsInstance() - .mapNotNull { - val zappedOption = it.zappedPollOption() - if (zappedOption == option) { - it.amount - } else { null } - }.sumOf { it } - } - fun hasAnyReports(): Boolean { val dayAgo = Date().time / 1000 - 24 * 60 * 60 return reports.isNotEmpty() || diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewPollView.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewPollView.kt index 275948e69..f16623cce 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewPollView.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewPollView.kt @@ -114,7 +114,7 @@ fun NewPollView(onClose: () -> Unit, baseReplyTo: Note? = null, quote: Note? = n } Text(stringResource(R.string.poll_heading_required)) - NewPollRecipientsField(pollViewModel, account) + // NewPollRecipientsField(pollViewModel, account) NewPollPrimaryDescription(pollViewModel) pollViewModel.pollOptions.values.forEachIndexed { index, element -> NewPollOption(pollViewModel, index) diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/PollNote.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/PollNote.kt index 0f0f9c3ab..3fc8cc7d3 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/PollNote.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/PollNote.kt @@ -166,7 +166,7 @@ fun ZapVote( ) } - if (zappedNote?.isPollOptionZappedBy(pollOption, account.userProfile()) == true) { + if (pollViewModel.isPollOptionZappedBy(pollOption, account.userProfile())) { Icon( imageVector = Icons.Default.Bolt, contentDescription = stringResource(R.string.zaps), @@ -186,7 +186,7 @@ fun ZapVote( // only show tallies after a user has zapped note if (zappedNote?.isZappedBy(account.userProfile()) == true) { Text( - showAmount(zappedNote.zappedPollOptionAmount(pollOption)), + showAmount(pollViewModel.zappedPollOptionAmount(pollOption)), fontSize = 14.sp, color = MaterialTheme.colors.onSurface.copy(alpha = 0.32f), modifier = modifier diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/PollNoteViewModel.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/PollNoteViewModel.kt index 2c4a04df4..d97d5219e 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/PollNoteViewModel.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/PollNoteViewModel.kt @@ -2,7 +2,9 @@ package com.vitorpamplona.amethyst.ui.note import com.vitorpamplona.amethyst.model.Account import com.vitorpamplona.amethyst.model.Note +import com.vitorpamplona.amethyst.model.User import com.vitorpamplona.amethyst.service.model.* +import java.math.BigDecimal import java.util.* class PollNoteViewModel { @@ -72,7 +74,7 @@ class PollNoteViewModel { } 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 0f } else { tally } @@ -81,8 +83,35 @@ class PollNoteViewModel { private fun zappedVoteTotal(): Float { var total = 0f pollOptions?.keys?.forEach { - total += pollNote?.zappedPollOptionAmount(it)?.toFloat() ?: 0f + total += zappedPollOptionAmount(it).toFloat() ?: 0f } 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() + .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() + .mapNotNull { + val zappedOption = it.zappedPollOption() + if (zappedOption == option) { + it.amount + } else { null } + }.sumOf { it } + } else { return BigDecimal(0) } + } }