From 073a1afbde5edbb830c8ef375bdd283f37f6d6cc Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sun, 19 Feb 2023 18:21:20 -0500 Subject: [PATCH] Show percentage of voters for multi-choice polls --- Tusker/API/InstanceFeatures.swift | 4 ++++ Tusker/Views/Poll/PollOptionView.swift | 11 +++++++++-- Tusker/Views/Poll/PollOptionsView.swift | 4 +++- Tusker/Views/Poll/StatusPollView.swift | 1 + 4 files changed, 17 insertions(+), 3 deletions(-) diff --git a/Tusker/API/InstanceFeatures.swift b/Tusker/API/InstanceFeatures.swift index 4c4ca4bb..23e3700e 100644 --- a/Tusker/API/InstanceFeatures.swift +++ b/Tusker/API/InstanceFeatures.swift @@ -101,6 +101,10 @@ struct InstanceFeatures { hasMastodonVersion(3, 5, 0) } + var pollVotersCount: Bool { + instanceType.isMastodon + } + mutating func update(instance: Instance, nodeInfo: NodeInfo?) { let ver = instance.version.lowercased() if ver.contains("glitch") { diff --git a/Tusker/Views/Poll/PollOptionView.swift b/Tusker/Views/Poll/PollOptionView.swift index 57ea343b..e2e89226 100644 --- a/Tusker/Views/Poll/PollOptionView.swift +++ b/Tusker/Views/Poll/PollOptionView.swift @@ -15,7 +15,7 @@ class PollOptionView: UIView { let checkbox: PollOptionCheckboxView - init(poll: Poll, option: Poll.Option) { + init(poll: Poll, option: Poll.Option, mastodonController: MastodonController) { checkbox = PollOptionCheckboxView(multiple: poll.multiple) super.init(frame: .zero) @@ -48,7 +48,14 @@ class PollOptionView: UIView { if (poll.voted ?? false) || poll.effectiveExpired, 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) percentLabel.isHidden = false diff --git a/Tusker/Views/Poll/PollOptionsView.swift b/Tusker/Views/Poll/PollOptionsView.swift index 8526b271..39ec9733 100644 --- a/Tusker/Views/Poll/PollOptionsView.swift +++ b/Tusker/Views/Poll/PollOptionsView.swift @@ -10,6 +10,8 @@ import UIKit import Pachyderm class PollOptionsView: UIControl { + + var mastodonController: MastodonController! var checkedOptionIndices: [Int] { options.enumerated().filter { $0.element.checkbox.isChecked }.map(\.offset) @@ -62,7 +64,7 @@ class PollOptionsView: UIControl { options.forEach { $0.removeFromSuperview() } 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.isChecked = poll.ownVotes?.contains(index) ?? false optionView.checkbox.voted = poll.voted ?? false diff --git a/Tusker/Views/Poll/StatusPollView.swift b/Tusker/Views/Poll/StatusPollView.swift index 44023954..9f4309d0 100644 --- a/Tusker/Views/Poll/StatusPollView.swift +++ b/Tusker/Views/Poll/StatusPollView.swift @@ -101,6 +101,7 @@ class StatusPollView: UIView { canVote = true } + optionsView.mastodonController = mastodonController optionsView.isEnabled = canVote optionsView.updateUI(poll: poll)