Show percentage of voters for multi-choice polls

This commit is contained in:
Shadowfacts 2023-02-19 18:21:20 -05:00
parent aaa031f212
commit 073a1afbde
4 changed files with 17 additions and 3 deletions

View File

@ -101,6 +101,10 @@ struct InstanceFeatures {
hasMastodonVersion(3, 5, 0) hasMastodonVersion(3, 5, 0)
} }
var pollVotersCount: Bool {
instanceType.isMastodon
}
mutating func update(instance: Instance, nodeInfo: NodeInfo?) { mutating func update(instance: Instance, nodeInfo: NodeInfo?) {
let ver = instance.version.lowercased() let ver = instance.version.lowercased()
if ver.contains("glitch") { if ver.contains("glitch") {

View File

@ -15,7 +15,7 @@ class PollOptionView: UIView {
let checkbox: PollOptionCheckboxView let checkbox: PollOptionCheckboxView
init(poll: Poll, option: Poll.Option) { init(poll: Poll, option: Poll.Option, mastodonController: MastodonController) {
checkbox = PollOptionCheckboxView(multiple: poll.multiple) checkbox = PollOptionCheckboxView(multiple: poll.multiple)
super.init(frame: .zero) super.init(frame: .zero)
@ -48,7 +48,14 @@ class PollOptionView: UIView {
if (poll.voted ?? false) || poll.effectiveExpired, if (poll.voted ?? false) || poll.effectiveExpired,
let optionVotes = option.votesCount { let optionVotes = option.votesCount {
let frac = poll.votesCount == 0 ? 0 : CGFloat(optionVotes) / CGFloat(poll.votesCount) let frac: CGFloat
if poll.multiple,
let votersCount = poll.votersCount,
mastodonController.instanceFeatures.pollVotersCount {
frac = votersCount == 0 ? 0 : CGFloat(optionVotes) / CGFloat(votersCount)
} else {
frac = poll.votesCount == 0 ? 0 : CGFloat(optionVotes) / CGFloat(poll.votesCount)
}
let percent = String(format: "%.0f%%", frac * 100) let percent = String(format: "%.0f%%", frac * 100)
percentLabel.isHidden = false percentLabel.isHidden = false

View File

@ -11,6 +11,8 @@ import Pachyderm
class PollOptionsView: UIControl { class PollOptionsView: UIControl {
var mastodonController: MastodonController!
var checkedOptionIndices: [Int] { var checkedOptionIndices: [Int] {
options.enumerated().filter { $0.element.checkbox.isChecked }.map(\.offset) options.enumerated().filter { $0.element.checkbox.isChecked }.map(\.offset)
} }
@ -62,7 +64,7 @@ class PollOptionsView: UIControl {
options.forEach { $0.removeFromSuperview() } options.forEach { $0.removeFromSuperview() }
options = poll.options.enumerated().map { (index, opt) in options = poll.options.enumerated().map { (index, opt) in
let optionView = PollOptionView(poll: poll, option: opt) let optionView = PollOptionView(poll: poll, option: opt, mastodonController: mastodonController)
optionView.checkbox.readOnly = !isEnabled optionView.checkbox.readOnly = !isEnabled
optionView.checkbox.isChecked = poll.ownVotes?.contains(index) ?? false optionView.checkbox.isChecked = poll.ownVotes?.contains(index) ?? false
optionView.checkbox.voted = poll.voted ?? false optionView.checkbox.voted = poll.voted ?? false

View File

@ -101,6 +101,7 @@ class StatusPollView: UIView {
canVote = true canVote = true
} }
optionsView.mastodonController = mastodonController
optionsView.isEnabled = canVote optionsView.isEnabled = canVote
optionsView.updateUI(poll: poll) optionsView.updateUI(poll: poll)