diff --git a/Tusker/Views/Poll/PollOptionView.swift b/Tusker/Views/Poll/PollOptionView.swift index 6d851fd2..bf5e5467 100644 --- a/Tusker/Views/Poll/PollOptionView.swift +++ b/Tusker/Views/Poll/PollOptionView.swift @@ -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), ]) }