2020-10-25 03:31:44 +01:00
|
|
|
// Copyright © 2020 Metabolist. All rights reserved.
|
|
|
|
|
|
|
|
import Mastodon
|
|
|
|
import UIKit
|
2021-02-22 08:10:34 +01:00
|
|
|
import ViewModels
|
2020-10-25 03:31:44 +01:00
|
|
|
|
2020-11-09 07:22:20 +01:00
|
|
|
final class PollResultView: UIView {
|
2021-02-22 08:10:34 +01:00
|
|
|
let titleLabel = AnimatedAttachmentLabel()
|
2021-02-02 19:02:30 +01:00
|
|
|
let percentLabel = UILabel()
|
2020-10-25 03:31:44 +01:00
|
|
|
private let verticalStackView = UIStackView()
|
|
|
|
private let horizontalStackView = UIStackView()
|
|
|
|
private let percentView = UIProgressView()
|
|
|
|
|
2021-02-22 08:10:34 +01:00
|
|
|
init(option: Poll.Option,
|
|
|
|
emojis: [Emoji],
|
|
|
|
selected: Bool,
|
|
|
|
multipleSelection: Bool,
|
|
|
|
votersCount: Int,
|
|
|
|
identityContext: IdentityContext) {
|
2020-10-25 03:31:44 +01:00
|
|
|
super.init(frame: .zero)
|
|
|
|
|
|
|
|
addSubview(verticalStackView)
|
|
|
|
verticalStackView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
verticalStackView.axis = .vertical
|
|
|
|
verticalStackView.spacing = .compactSpacing
|
|
|
|
|
|
|
|
verticalStackView.addArrangedSubview(horizontalStackView)
|
|
|
|
horizontalStackView.spacing = .compactSpacing
|
|
|
|
|
|
|
|
verticalStackView.addArrangedSubview(percentView)
|
|
|
|
|
|
|
|
if selected {
|
|
|
|
let imageView = UIImageView(
|
|
|
|
image: UIImage(
|
|
|
|
systemName: multipleSelection ? "checkmark.square" : "checkmark.circle",
|
|
|
|
withConfiguration: UIImage.SymbolConfiguration(scale: .medium)))
|
|
|
|
|
2021-02-22 08:10:34 +01:00
|
|
|
imageView.contentMode = .scaleAspectFit
|
2020-10-25 03:31:44 +01:00
|
|
|
imageView.setContentHuggingPriority(.required, for: .horizontal)
|
|
|
|
horizontalStackView.addArrangedSubview(imageView)
|
|
|
|
}
|
|
|
|
|
|
|
|
horizontalStackView.addArrangedSubview(titleLabel)
|
|
|
|
titleLabel.font = .preferredFont(forTextStyle: .callout)
|
|
|
|
titleLabel.adjustsFontForContentSizeCategory = true
|
|
|
|
titleLabel.numberOfLines = 0
|
|
|
|
|
|
|
|
horizontalStackView.addArrangedSubview(percentLabel)
|
|
|
|
percentLabel.font = .preferredFont(forTextStyle: .callout)
|
|
|
|
percentLabel.adjustsFontForContentSizeCategory = true
|
|
|
|
percentLabel.setContentHuggingPriority(.required, for: .horizontal)
|
|
|
|
|
|
|
|
let attributedTitle = NSMutableAttributedString(string: option.title)
|
|
|
|
|
2021-02-22 08:10:34 +01:00
|
|
|
attributedTitle.insert(emojis: emojis, view: titleLabel, identityContext: identityContext)
|
2020-10-25 03:31:44 +01:00
|
|
|
attributedTitle.resizeAttachments(toLineHeight: titleLabel.font.lineHeight)
|
|
|
|
titleLabel.attributedText = attributedTitle
|
|
|
|
|
|
|
|
let percent: Float
|
|
|
|
|
|
|
|
if votersCount == 0 {
|
|
|
|
percent = 0
|
|
|
|
} else {
|
|
|
|
percent = Float(option.votesCount) / Float(votersCount)
|
|
|
|
}
|
|
|
|
|
|
|
|
percentLabel.text = Self.percentFormatter.string(from: NSNumber(value: percent))
|
|
|
|
percentView.progress = percent
|
|
|
|
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
verticalStackView.leadingAnchor.constraint(equalTo: leadingAnchor),
|
|
|
|
verticalStackView.topAnchor.constraint(equalTo: topAnchor),
|
|
|
|
verticalStackView.trailingAnchor.constraint(equalTo: trailingAnchor),
|
|
|
|
verticalStackView.bottomAnchor.constraint(equalTo: bottomAnchor)
|
|
|
|
])
|
|
|
|
}
|
|
|
|
|
|
|
|
@available(*, unavailable)
|
|
|
|
required init?(coder: NSCoder) {
|
|
|
|
fatalError("init(coder:) has not been implemented")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-19 01:46:38 +01:00
|
|
|
extension PollResultView {
|
|
|
|
static func estimatedHeight(width: CGFloat, title: String) -> CGFloat {
|
|
|
|
title.height(width: width, font: .preferredFont(forTextStyle: .callout))
|
|
|
|
+ .compactSpacing
|
|
|
|
+ 4 // progress view height
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-25 03:31:44 +01:00
|
|
|
private extension PollResultView {
|
|
|
|
private static var percentFormatter: NumberFormatter = {
|
|
|
|
let percentageFormatter = NumberFormatter()
|
|
|
|
|
|
|
|
percentageFormatter.numberStyle = .percent
|
|
|
|
|
|
|
|
return percentageFormatter
|
|
|
|
}()
|
|
|
|
}
|