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

@ -0,0 +1,66 @@
/* 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
import com.keylesspalace.tusky.entity.Poll
import com.keylesspalace.tusky.entity.PollOption
import java.util.*
import kotlin.math.roundToInt
data class PollViewData(
val id: String,
val expiresAt: Date?,
val expired: Boolean,
val multiple: Boolean,
val votesCount: Int,
val options: List<PollOptionViewData>,
var voted: Boolean
)
data class PollOptionViewData(
val title: String,
var votesCount: Int,
var selected: Boolean
)
fun calculatePercent(fraction: Int, total: Int): Int {
return if (fraction == 0) {
0
} else {
(fraction / total.toDouble() * 100).roundToInt()
}
}
fun Poll?.toViewData(): PollViewData? {
if (this == null) return null
return PollViewData(
id,
expiresAt,
expired,
multiple,
votesCount,
options.map { it.toViewData() },
voted
)
}
fun PollOption.toViewData(): PollOptionViewData {
return PollOptionViewData(
title,
votesCount,
false
)
}

View file

@ -89,7 +89,7 @@ public abstract class StatusViewData {
private final boolean isCollapsible; /** Whether the status meets the requirement to be collapse */
final boolean isCollapsed; /** Whether the status is shown partially or fully */
@Nullable
private final Poll poll;
private final PollViewData poll;
private final boolean isBot;
public Concrete(String id, Spanned content, boolean reblogged, boolean favourited,
@ -99,7 +99,7 @@ public abstract class StatusViewData {
Date createdAt, int reblogsCount, int favouritesCount, @Nullable String inReplyToId,
@Nullable Status.Mention[] mentions, String senderId, boolean rebloggingEnabled,
Status.Application application, List<Emoji> statusEmojis, List<Emoji> accountEmojis, @Nullable Card card,
boolean isCollapsible, boolean isCollapsed, @Nullable Poll poll, boolean isBot) {
boolean isCollapsible, boolean isCollapsed, @Nullable PollViewData poll, boolean isBot) {
this.id = id;
if (Build.VERSION.SDK_INT == 23) {
@ -273,7 +273,7 @@ public abstract class StatusViewData {
}
@Nullable
public Poll getPoll() {
public PollViewData getPoll() {
return poll;
}
@ -418,7 +418,7 @@ public abstract class StatusViewData {
private Card card;
private boolean isCollapsible; /** Whether the status meets the requirement to be collapsed */
private boolean isCollapsed; /** Whether the status is shown partially or fully */
private Poll poll;
private PollViewData poll;
private boolean isBot;
public Builder() {
@ -617,7 +617,7 @@ public abstract class StatusViewData {
}
public Builder setPoll(Poll poll) {
this.poll = poll;
this.poll = PollViewDataKt.toViewData(poll);
return this;
}