2019-06-22 21:55:03 +02:00
|
|
|
/* Copyright 2019 Conny Duck
|
|
|
|
*
|
|
|
|
* This file is a part of Tusky.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it under the terms of the
|
|
|
|
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
|
|
|
|
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
|
|
|
* Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with Tusky; if not,
|
|
|
|
* see <http://www.gnu.org/licenses>. */
|
|
|
|
|
|
|
|
package com.keylesspalace.tusky.viewdata
|
|
|
|
|
2019-10-11 17:35:22 +02:00
|
|
|
import android.content.Context
|
|
|
|
import android.text.SpannableStringBuilder
|
|
|
|
import android.text.Spanned
|
2020-04-02 23:37:38 +02:00
|
|
|
import androidx.core.text.parseAsHtml
|
2019-10-11 17:35:22 +02:00
|
|
|
import com.keylesspalace.tusky.R
|
2019-06-22 21:55:03 +02:00
|
|
|
import com.keylesspalace.tusky.entity.Poll
|
|
|
|
import com.keylesspalace.tusky.entity.PollOption
|
2021-06-28 21:13:24 +02:00
|
|
|
import java.util.Date
|
2019-06-22 21:55:03 +02:00
|
|
|
import kotlin.math.roundToInt
|
|
|
|
|
|
|
|
data class PollViewData(
|
2021-06-28 21:13:24 +02:00
|
|
|
val id: String,
|
|
|
|
val expiresAt: Date?,
|
|
|
|
val expired: Boolean,
|
|
|
|
val multiple: Boolean,
|
|
|
|
val votesCount: Int,
|
|
|
|
val votersCount: Int?,
|
|
|
|
val options: List<PollOptionViewData>,
|
|
|
|
var voted: Boolean
|
2019-06-22 21:55:03 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
data class PollOptionViewData(
|
2021-06-28 21:13:24 +02:00
|
|
|
val title: String,
|
|
|
|
var votesCount: Int,
|
2021-09-17 22:12:17 +02:00
|
|
|
var selected: Boolean,
|
|
|
|
var voted: Boolean
|
2019-06-22 21:55:03 +02:00
|
|
|
)
|
|
|
|
|
2020-03-24 21:06:58 +01:00
|
|
|
fun calculatePercent(fraction: Int, totalVoters: Int?, totalVotes: Int): Int {
|
2019-06-22 21:55:03 +02:00
|
|
|
return if (fraction == 0) {
|
|
|
|
0
|
|
|
|
} else {
|
2020-03-24 21:06:58 +01:00
|
|
|
val total = totalVoters ?: totalVotes
|
2019-06-22 21:55:03 +02:00
|
|
|
(fraction / total.toDouble() * 100).roundToInt()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-17 22:12:17 +02:00
|
|
|
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)
|
2019-10-11 17:35:22 +02:00
|
|
|
}
|
|
|
|
|
2019-06-22 21:55:03 +02:00
|
|
|
fun Poll?.toViewData(): PollViewData? {
|
|
|
|
if (this == null) return null
|
|
|
|
return PollViewData(
|
2021-06-28 21:13:24 +02:00
|
|
|
id = id,
|
|
|
|
expiresAt = expiresAt,
|
|
|
|
expired = expired,
|
|
|
|
multiple = multiple,
|
|
|
|
votesCount = votesCount,
|
|
|
|
votersCount = votersCount,
|
2021-09-17 22:12:17 +02:00
|
|
|
options = options.mapIndexed { index, option -> option.toViewData(ownVotes?.contains(index) == true) },
|
|
|
|
voted = voted,
|
2019-06-22 21:55:03 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-09-17 22:12:17 +02:00
|
|
|
fun PollOption.toViewData(voted: Boolean): PollOptionViewData {
|
2019-06-22 21:55:03 +02:00
|
|
|
return PollOptionViewData(
|
2021-06-28 21:13:24 +02:00
|
|
|
title = title,
|
|
|
|
votesCount = votesCount,
|
2021-09-17 22:12:17 +02:00
|
|
|
selected = false,
|
|
|
|
voted = voted
|
2019-06-22 21:55:03 +02:00
|
|
|
)
|
2021-06-28 21:13:24 +02:00
|
|
|
}
|