Unlimited number of poll options (#1340)

* implement unlimited number of poll options

* fixes

* extract percent calculation into function so it can be used anywhere

* add license header
This commit is contained in:
Konrad Pozniak 2019-06-22 21:55:03 +02:00 committed by GitHub
commit b95ff10a3c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 325 additions and 689 deletions

View file

@ -27,12 +27,14 @@ import com.bumptech.glide.Glide
import com.keylesspalace.tusky.R
import com.keylesspalace.tusky.entity.Attachment
import com.keylesspalace.tusky.entity.Emoji
import com.keylesspalace.tusky.entity.Poll
import com.keylesspalace.tusky.entity.Status
import com.keylesspalace.tusky.view.MediaPreviewImageView
import com.keylesspalace.tusky.viewdata.PollViewData
import com.keylesspalace.tusky.viewdata.calculatePercent
import java.text.NumberFormat
import java.text.SimpleDateFormat
import java.util.*
import kotlin.math.min
class StatusViewHelper(private val itemView: View) {
interface MediaPreviewListener {
@ -85,7 +87,7 @@ class StatusViewHelper(private val itemView: View) {
val mediaPreviewUnloadedId = ThemeUtils.getDrawableId(context, R.attr.media_preview_unloaded_drawable, android.R.color.black)
val n = Math.min(attachments.size, Status.MAX_MEDIA_ATTACHMENTS)
val n = min(attachments.size, Status.MAX_MEDIA_ATTACHMENTS)
for (i in 0 until n) {
val previewUrl = attachments[i].previewUrl
@ -229,7 +231,7 @@ class StatusViewHelper(private val itemView: View) {
}
}
fun setupPollReadonly(poll: Poll?, emojis: List<Emoji>, useAbsoluteTime: Boolean) {
fun setupPollReadonly(poll: PollViewData?, emojis: List<Emoji>, useAbsoluteTime: Boolean) {
val pollResults = listOf<TextView>(
itemView.findViewById(R.id.status_poll_option_result_0),
itemView.findViewById(R.id.status_poll_option_result_1),
@ -254,7 +256,7 @@ class StatusViewHelper(private val itemView: View) {
}
}
private fun getPollInfoText(timestamp: Long, poll: Poll, pollDescription: TextView, useAbsoluteTime: Boolean): CharSequence {
private fun getPollInfoText(timestamp: Long, poll: PollViewData, pollDescription: TextView, useAbsoluteTime: Boolean): CharSequence {
val context = pollDescription.context
val votes = NumberFormat.getNumberInstance().format(poll.votesCount.toLong())
val votesText = context.resources.getQuantityString(R.plurals.poll_info_votes, poll.votesCount, votes)
@ -274,12 +276,12 @@ class StatusViewHelper(private val itemView: View) {
}
private fun setupPollResult(poll: Poll, emojis: List<Emoji>, pollResults: List<TextView>) {
private fun setupPollResult(poll: PollViewData, emojis: List<Emoji>, pollResults: List<TextView>) {
val options = poll.options
for (i in 0 until Status.MAX_POLL_OPTIONS) {
if (i < options.size) {
val percent = options[i].getPercent(poll.votesCount)
val percent = calculatePercent(options[i].votesCount, poll.votesCount)
val pollOptionText = pollResults[i].context.getString(R.string.poll_option_format, percent, options[i].title)
pollResults[i].text = CustomEmojiHelper.emojifyText(HtmlUtils.fromHtml(pollOptionText), emojis, pollResults[i])