1
0
mirror of https://github.com/mastodon/mastodon-ios.git synced 2024-12-14 18:05:50 +01:00
mastodon-app-ufficiale-ipho.../MastodonSDK/Sources/MastodonUI/View/Content/PollOptionView.swift

259 lines
10 KiB
Swift
Raw Normal View History

2021-03-23 11:47:21 +01:00
//
// PollOptionView.swift
// Mastodon
//
// Created by MainasuK Cirno on 2021-3-23.
//
import UIKit
import Combine
import MastodonAsset
import MastodonLocalization
2021-03-23 11:47:21 +01:00
public final class PollOptionView: UIView {
2021-03-23 11:47:21 +01:00
public static let height: CGFloat = optionHeight + 2 * verticalMargin
public static let optionHeight: CGFloat = 44
public static let verticalMargin: CGFloat = 5
public static let checkmarkImageSize = CGSize(width: 26, height: 26)
public static let checkmarkBackgroundLeadingMargin: CGFloat = 9
2021-03-23 11:47:21 +01:00
private var viewStateDisposeBag = Set<AnyCancellable>()
2021-07-05 10:07:17 +02:00
public var disposeBag = Set<AnyCancellable>()
public private(set) lazy var viewModel: ViewModel = {
let viewModel = ViewModel()
viewModel.bind(view: self)
return viewModel
}()
public private(set) var style: Style?
2021-03-23 11:47:21 +01:00
public let roundedBackgroundView = UIView()
public let voteProgressStripView: StripProgressView = {
2021-03-23 11:47:21 +01:00
let view = StripProgressView()
2022-06-02 11:31:23 +02:00
view.tintColor = Asset.Colors.brand.color
2021-03-23 11:47:21 +01:00
return view
}()
public let checkmarkBackgroundView: UIView = {
2021-03-23 11:47:21 +01:00
let view = UIView()
return view
}()
public let checkmarkImageView: UIImageView = {
2021-03-23 11:47:21 +01:00
let imageView = UIImageView()
let image = UIImage(systemName: "checkmark", withConfiguration: UIImage.SymbolConfiguration(pointSize: 14, weight: .bold))!
imageView.image = image.withRenderingMode(.alwaysTemplate)
2022-06-02 11:31:23 +02:00
imageView.tintColor = Asset.Colors.brand.color
2021-03-23 11:47:21 +01:00
return imageView
}()
public let plusCircleImageView: UIImageView = {
2021-03-23 11:47:21 +01:00
let imageView = UIImageView()
let image = Asset.Circles.plusCircle.image
imageView.image = image.withRenderingMode(.alwaysTemplate)
2022-06-02 11:31:23 +02:00
imageView.tintColor = Asset.Colors.brand.color
2021-03-23 11:47:21 +01:00
return imageView
}()
public let optionTextField: DeleteBackwardResponseTextField = {
2021-03-23 11:47:21 +01:00
let textField = DeleteBackwardResponseTextField()
textField.font = .systemFont(ofSize: 15, weight: .medium)
textField.textColor = Asset.Colors.Label.primary.color
textField.text = "Option"
textField.textAlignment = UIApplication.shared.userInterfaceLayoutDirection == .leftToRight ? .left : .right
return textField
}()
public let optionLabelMiddlePaddingView = UIView()
2021-03-23 11:47:21 +01:00
public let optionPercentageLabel: UILabel = {
2021-03-23 11:47:21 +01:00
let label = UILabel()
label.font = .systemFont(ofSize: 13, weight: .regular)
label.textColor = Asset.Colors.Label.primary.color
label.text = "50%"
label.textAlignment = UIApplication.shared.userInterfaceLayoutDirection == .leftToRight ? .right : .left
return label
}()
public func prepareForReuse() {
disposeBag.removeAll()
viewModel.objects.removeAll()
viewModel.percentage = nil
voteProgressStripView.setProgress(0, animated: false)
}
public override init(frame: CGRect) {
2021-03-23 11:47:21 +01:00
super.init(frame: frame)
_init()
}
public required init?(coder: NSCoder) {
2021-03-23 11:47:21 +01:00
super.init(coder: coder)
_init()
}
}
extension PollOptionView {
private func _init() {
roundedBackgroundView.translatesAutoresizingMaskIntoConstraints = false
addSubview(roundedBackgroundView)
NSLayoutConstraint.activate([
roundedBackgroundView.topAnchor.constraint(equalTo: topAnchor, constant: 5),
roundedBackgroundView.leadingAnchor.constraint(equalTo: leadingAnchor),
roundedBackgroundView.trailingAnchor.constraint(equalTo: trailingAnchor),
bottomAnchor.constraint(equalTo: roundedBackgroundView.bottomAnchor, constant: 5),
roundedBackgroundView.heightAnchor.constraint(equalToConstant: PollOptionView.optionHeight).priority(.defaultHigh),
])
voteProgressStripView.translatesAutoresizingMaskIntoConstraints = false
roundedBackgroundView.addSubview(voteProgressStripView)
voteProgressStripView.pinToParent()
2021-03-23 11:47:21 +01:00
checkmarkBackgroundView.translatesAutoresizingMaskIntoConstraints = false
roundedBackgroundView.addSubview(checkmarkBackgroundView)
NSLayoutConstraint.activate([
checkmarkBackgroundView.topAnchor.constraint(equalTo: roundedBackgroundView.topAnchor, constant: 9),
checkmarkBackgroundView.leadingAnchor.constraint(equalTo: roundedBackgroundView.leadingAnchor, constant: PollOptionView.checkmarkBackgroundLeadingMargin),
2021-03-23 11:47:21 +01:00
roundedBackgroundView.bottomAnchor.constraint(equalTo: checkmarkBackgroundView.bottomAnchor, constant: 9),
checkmarkBackgroundView.widthAnchor.constraint(equalToConstant: PollOptionView.checkmarkImageSize.width).priority(.required - 1),
checkmarkBackgroundView.heightAnchor.constraint(equalToConstant: PollOptionView.checkmarkImageSize.height).priority(.required - 1),
])
checkmarkImageView.translatesAutoresizingMaskIntoConstraints = false
checkmarkBackgroundView.addSubview(checkmarkImageView)
NSLayoutConstraint.activate([
checkmarkImageView.topAnchor.constraint(equalTo: checkmarkBackgroundView.topAnchor, constant: 5),
checkmarkImageView.leadingAnchor.constraint(equalTo: checkmarkBackgroundView.leadingAnchor, constant: 5),
checkmarkBackgroundView.trailingAnchor.constraint(equalTo: checkmarkImageView.trailingAnchor, constant: 5),
checkmarkBackgroundView.bottomAnchor.constraint(equalTo: checkmarkImageView.bottomAnchor, constant: 5),
])
plusCircleImageView.translatesAutoresizingMaskIntoConstraints = false
addSubview(plusCircleImageView)
plusCircleImageView.pinTo(to: checkmarkBackgroundView)
2021-03-23 11:47:21 +01:00
optionTextField.translatesAutoresizingMaskIntoConstraints = false
roundedBackgroundView.addSubview(optionTextField)
NSLayoutConstraint.activate([
optionTextField.leadingAnchor.constraint(equalTo: checkmarkBackgroundView.trailingAnchor, constant: 14),
optionTextField.centerYAnchor.constraint(equalTo: roundedBackgroundView.centerYAnchor),
optionTextField.widthAnchor.constraint(greaterThanOrEqualToConstant: 44).priority(.defaultHigh),
])
optionLabelMiddlePaddingView.translatesAutoresizingMaskIntoConstraints = false
roundedBackgroundView.addSubview(optionLabelMiddlePaddingView)
NSLayoutConstraint.activate([
optionLabelMiddlePaddingView.leadingAnchor.constraint(equalTo: optionTextField.trailingAnchor),
optionLabelMiddlePaddingView.centerYAnchor.constraint(equalTo: roundedBackgroundView.centerYAnchor),
optionLabelMiddlePaddingView.heightAnchor.constraint(equalToConstant: 4).priority(.defaultHigh),
optionLabelMiddlePaddingView.widthAnchor.constraint(greaterThanOrEqualToConstant: 8).priority(.defaultLow),
])
optionLabelMiddlePaddingView.setContentHuggingPriority(.required - 1, for: .horizontal)
optionLabelMiddlePaddingView.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
optionPercentageLabel.translatesAutoresizingMaskIntoConstraints = false
roundedBackgroundView.addSubview(optionPercentageLabel)
NSLayoutConstraint.activate([
optionPercentageLabel.leadingAnchor.constraint(equalTo: optionLabelMiddlePaddingView.trailingAnchor),
roundedBackgroundView.trailingAnchor.constraint(equalTo: optionPercentageLabel.trailingAnchor, constant: 18),
optionPercentageLabel.centerYAnchor.constraint(equalTo: roundedBackgroundView.centerYAnchor),
])
optionPercentageLabel.setContentHuggingPriority(.defaultHigh, for: .horizontal)
optionPercentageLabel.setContentCompressionResistancePriority(.required - 1, for: .horizontal)
plusCircleImageView.isHidden = true
updateCornerRadius()
2022-02-14 12:34:22 +01:00
isAccessibilityElement = true
}
public override var accessibilityLabel: String? {
get {
switch viewModel.voteState {
case .reveal:
return [
optionTextField,
optionPercentageLabel
]
.compactMap { $0.accessibilityLabel }
.joined(separator: ", ")
case .hidden:
return optionTextField.accessibilityLabel
}
}
set { }
2021-03-23 11:47:21 +01:00
}
public override func layoutSubviews() {
2021-03-23 11:47:21 +01:00
super.layoutSubviews()
2021-03-23 11:47:21 +01:00
updateCornerRadius()
2022-06-14 07:16:54 +02:00
viewModel.layoutDidUpdate.send()
2021-03-23 11:47:21 +01:00
}
public override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
2022-06-14 07:16:54 +02:00
viewModel.layoutDidUpdate.send()
}
}
extension PollOptionView {
public enum Style {
case plain
case edit
}
public func setup(style: Style) {
guard self.style == nil else {
assertionFailure("Should only setup once")
return
}
self.style = style
self.viewModel.style = style
}
2021-03-23 11:47:21 +01:00
}
extension PollOptionView {
private func updateCornerRadius() {
roundedBackgroundView.layer.masksToBounds = true
roundedBackgroundView.layer.cornerRadius = PollOptionView.optionHeight * 0.5
roundedBackgroundView.layer.cornerCurve = .circular
checkmarkBackgroundView.layer.masksToBounds = true
checkmarkBackgroundView.layer.cornerRadius = PollOptionView.checkmarkImageSize.width * 0.5
checkmarkBackgroundView.layer.cornerCurve = .circular
}
}
#if canImport(SwiftUI) && DEBUG
import SwiftUI
struct PollOptionView_Previews: PreviewProvider {
static var previews: some View {
Group {
UIViewPreview(width: 375) {
PollOptionView()
}
.previewLayout(.fixed(width: 375, height: 100))
UIViewPreview(width: 375) {
PollOptionView()
}
.preferredColorScheme(.dark)
.previewLayout(.fixed(width: 375, height: 100))
2021-03-23 11:47:21 +01:00
}
}
}
#endif