Show vote percentages on completed polls

This commit is contained in:
Shadowfacts 2021-05-06 21:41:41 -04:00
parent fdb21cd1fb
commit f7127b84d8
Signed by untrusted user: shadowfacts
GPG Key ID: 94A5AB95422746E5
1 changed files with 17 additions and 2 deletions

View File

@ -34,8 +34,19 @@ class PollOptionView: UIView {
label.setEmojis(poll.emojis, identifier: poll.id)
addSubview(label)
let percentLabel = UILabel()
percentLabel.translatesAutoresizingMaskIntoConstraints = false
percentLabel.text = "100%"
percentLabel.isHidden = true
addSubview(percentLabel)
if (poll.voted ?? false) || poll.effectiveExpired,
let optionVotes = option.votesCount {
let frac = poll.votesCount == 0 ? 0 : CGFloat(optionVotes) / CGFloat(poll.votesCount)
percentLabel.isHidden = false
percentLabel.text = String(format: "%.0f%%", frac * 100)
let fillView = UIView()
fillView.translatesAutoresizingMaskIntoConstraints = false
fillView.backgroundColor = tintColor.withAlphaComponent(0.6)
@ -45,7 +56,7 @@ class PollOptionView: UIView {
NSLayoutConstraint.activate([
fillView.leadingAnchor.constraint(equalTo: leadingAnchor),
fillView.widthAnchor.constraint(equalTo: widthAnchor, multiplier: poll.votesCount == 0 ? 0 : CGFloat(optionVotes) / CGFloat(poll.votesCount)),
fillView.widthAnchor.constraint(equalTo: widthAnchor, multiplier: frac),
fillView.topAnchor.constraint(equalTo: topAnchor),
fillView.bottomAnchor.constraint(equalTo: bottomAnchor),
])
@ -65,7 +76,11 @@ class PollOptionView: UIView {
label.topAnchor.constraint(equalTo: topAnchor),
label.bottomAnchor.constraint(equalTo: bottomAnchor),
label.leadingAnchor.constraint(equalTo: checkbox.trailingAnchor, constant: 8),
label.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -8),
label.trailingAnchor.constraint(equalTo: percentLabel.leadingAnchor),
percentLabel.topAnchor.constraint(equalTo: topAnchor),
percentLabel.bottomAnchor.constraint(equalTo: bottomAnchor),
percentLabel.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -8),
])
}