Highlight your own votes when displaying poll results (#2242)

* Highlight your own votes when displaying poll results

* Unbreak tests

* Add a checkmark to the description of self-voted options
This commit is contained in:
Levi Bard 2021-09-17 22:12:17 +02:00 committed by GitHub
commit d07c1b098e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 47 additions and 15 deletions

View file

@ -39,7 +39,8 @@ data class PollViewData(
data class PollOptionViewData(
val title: String,
var votesCount: Int,
var selected: Boolean
var selected: Boolean,
var voted: Boolean
)
fun calculatePercent(fraction: Int, totalVoters: Int?, totalVotes: Int): Int {
@ -51,10 +52,14 @@ fun calculatePercent(fraction: Int, totalVoters: Int?, totalVotes: Int): Int {
}
}
fun buildDescription(title: String, percent: Int, context: Context): Spanned {
return SpannableStringBuilder(context.getString(R.string.poll_percent_format, percent).parseAsHtml())
.append(" ")
.append(title)
fun buildDescription(title: String, percent: Int, voted: Boolean, context: Context): Spanned {
val builder = SpannableStringBuilder(context.getString(R.string.poll_percent_format, percent).parseAsHtml())
if (voted) {
builder.append("")
} else {
builder.append(" ")
}
return builder.append(title)
}
fun Poll?.toViewData(): PollViewData? {
@ -66,15 +71,16 @@ fun Poll?.toViewData(): PollViewData? {
multiple = multiple,
votesCount = votesCount,
votersCount = votersCount,
options = options.map { it.toViewData() },
voted = voted
options = options.mapIndexed { index, option -> option.toViewData(ownVotes?.contains(index) == true) },
voted = voted,
)
}
fun PollOption.toViewData(): PollOptionViewData {
fun PollOption.toViewData(voted: Boolean): PollOptionViewData {
return PollOptionViewData(
title = title,
votesCount = votesCount,
selected = false
selected = false,
voted = voted
)
}