2021-03-02 05:49:04 +01:00
|
|
|
//
|
2021-03-02 12:10:45 +01:00
|
|
|
// PollOptionTableViewCell.swift
|
2021-03-02 05:49:04 +01:00
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by MainasuK Cirno on 2021-2-25.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
2021-03-03 09:12:48 +01:00
|
|
|
import Combine
|
2021-03-02 05:49:04 +01:00
|
|
|
|
2021-03-02 12:10:45 +01:00
|
|
|
final class PollOptionTableViewCell: UITableViewCell {
|
2021-03-02 05:49:04 +01:00
|
|
|
|
2021-03-02 12:10:45 +01:00
|
|
|
static let height: CGFloat = optionHeight + 2 * verticalMargin
|
|
|
|
static let optionHeight: CGFloat = 44
|
|
|
|
static let verticalMargin: CGFloat = 5
|
2021-03-02 05:49:04 +01:00
|
|
|
static let checkmarkImageSize = CGSize(width: 26, height: 26)
|
|
|
|
|
2021-03-03 09:12:48 +01:00
|
|
|
private var viewStateDisposeBag = Set<AnyCancellable>()
|
2021-03-03 12:34:29 +01:00
|
|
|
var selectState: PollItem.Attribute.SelectState = .off
|
|
|
|
var voteState: PollItem.Attribute.VoteState?
|
2021-03-03 09:12:48 +01:00
|
|
|
|
2021-03-02 05:49:04 +01:00
|
|
|
let roundedBackgroundView = UIView()
|
2021-03-03 12:34:29 +01:00
|
|
|
let voteProgressStripView: VoteProgressStripView = {
|
|
|
|
let view = VoteProgressStripView()
|
|
|
|
view.tintColor = Asset.Colors.Background.Poll.highlight.color
|
|
|
|
return view
|
|
|
|
}()
|
2021-03-02 05:49:04 +01:00
|
|
|
|
|
|
|
let checkmarkBackgroundView: UIView = {
|
|
|
|
let view = UIView()
|
|
|
|
view.backgroundColor = .systemBackground
|
|
|
|
return view
|
|
|
|
}()
|
|
|
|
|
|
|
|
let checkmarkImageView: UIView = {
|
|
|
|
let imageView = UIImageView()
|
|
|
|
let image = UIImage(systemName: "checkmark", withConfiguration: UIImage.SymbolConfiguration(pointSize: 14, weight: .bold))!
|
|
|
|
imageView.image = image.withRenderingMode(.alwaysTemplate)
|
|
|
|
imageView.tintColor = Asset.Colors.Button.highlight.color
|
|
|
|
return imageView
|
|
|
|
}()
|
|
|
|
|
|
|
|
let optionLabel: UILabel = {
|
|
|
|
let label = UILabel()
|
|
|
|
label.font = .systemFont(ofSize: 15, weight: .medium)
|
|
|
|
label.textColor = Asset.Colors.Label.primary.color
|
|
|
|
label.text = "Option"
|
|
|
|
label.textAlignment = UIApplication.shared.userInterfaceLayoutDirection == .leftToRight ? .left : .right
|
|
|
|
return label
|
|
|
|
}()
|
|
|
|
|
2021-03-03 12:34:29 +01:00
|
|
|
let optionLabelMiddlePaddingView = UIView()
|
|
|
|
|
2021-03-02 05:49:04 +01:00
|
|
|
let optionPercentageLabel: UILabel = {
|
|
|
|
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
|
|
|
|
}()
|
|
|
|
|
|
|
|
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
|
|
|
|
super.init(style: style, reuseIdentifier: reuseIdentifier)
|
|
|
|
_init()
|
|
|
|
}
|
|
|
|
|
|
|
|
required init?(coder: NSCoder) {
|
|
|
|
super.init(coder: coder)
|
|
|
|
_init()
|
|
|
|
}
|
2021-03-03 09:12:48 +01:00
|
|
|
|
|
|
|
override func setSelected(_ selected: Bool, animated: Bool) {
|
|
|
|
super.setSelected(selected, animated: animated)
|
2021-03-03 12:34:29 +01:00
|
|
|
|
|
|
|
guard let voteState = voteState else { return }
|
|
|
|
switch voteState {
|
|
|
|
case .hidden:
|
|
|
|
let color = Asset.Colors.Background.systemGroupedBackground.color
|
|
|
|
self.roundedBackgroundView.backgroundColor = isHighlighted ? color.withAlphaComponent(0.8) : color
|
|
|
|
case .reveal:
|
|
|
|
break
|
|
|
|
}
|
2021-03-03 09:12:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
override func setHighlighted(_ highlighted: Bool, animated: Bool) {
|
|
|
|
super.setHighlighted(highlighted, animated: animated)
|
|
|
|
|
2021-03-03 12:34:29 +01:00
|
|
|
guard let voteState = voteState else { return }
|
|
|
|
switch voteState {
|
|
|
|
case .hidden:
|
2021-03-03 09:12:48 +01:00
|
|
|
let color = Asset.Colors.Background.systemGroupedBackground.color
|
|
|
|
self.roundedBackgroundView.backgroundColor = isHighlighted ? color.withAlphaComponent(0.8) : color
|
2021-03-03 12:34:29 +01:00
|
|
|
case .reveal:
|
2021-03-03 09:12:48 +01:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2021-03-02 05:49:04 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-03-02 12:10:45 +01:00
|
|
|
extension PollOptionTableViewCell {
|
2021-03-02 05:49:04 +01:00
|
|
|
|
|
|
|
private func _init() {
|
2021-03-02 12:10:45 +01:00
|
|
|
selectionStyle = .none
|
|
|
|
backgroundColor = .clear
|
2021-03-02 05:49:04 +01:00
|
|
|
roundedBackgroundView.backgroundColor = Asset.Colors.Background.systemGroupedBackground.color
|
|
|
|
|
|
|
|
roundedBackgroundView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
contentView.addSubview(roundedBackgroundView)
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
roundedBackgroundView.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 5),
|
|
|
|
roundedBackgroundView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
|
|
|
|
roundedBackgroundView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
|
|
|
|
contentView.bottomAnchor.constraint(equalTo: roundedBackgroundView.bottomAnchor, constant: 5),
|
2021-03-02 12:10:45 +01:00
|
|
|
roundedBackgroundView.heightAnchor.constraint(equalToConstant: PollOptionTableViewCell.optionHeight).priority(.defaultHigh),
|
2021-03-02 05:49:04 +01:00
|
|
|
])
|
|
|
|
|
2021-03-03 12:34:29 +01:00
|
|
|
voteProgressStripView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
roundedBackgroundView.addSubview(voteProgressStripView)
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
voteProgressStripView.topAnchor.constraint(equalTo: roundedBackgroundView.topAnchor),
|
|
|
|
voteProgressStripView.leadingAnchor.constraint(equalTo: roundedBackgroundView.leadingAnchor),
|
|
|
|
voteProgressStripView.trailingAnchor.constraint(equalTo: roundedBackgroundView.trailingAnchor),
|
|
|
|
voteProgressStripView.bottomAnchor.constraint(equalTo: roundedBackgroundView.bottomAnchor),
|
|
|
|
])
|
|
|
|
|
2021-03-02 05:49:04 +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: 9),
|
|
|
|
roundedBackgroundView.bottomAnchor.constraint(equalTo: checkmarkBackgroundView.bottomAnchor, constant: 9),
|
2021-03-02 12:10:45 +01:00
|
|
|
checkmarkBackgroundView.widthAnchor.constraint(equalToConstant: PollOptionTableViewCell.checkmarkImageSize.width).priority(.defaultHigh),
|
|
|
|
checkmarkBackgroundView.heightAnchor.constraint(equalToConstant: PollOptionTableViewCell.checkmarkImageSize.height).priority(.defaultHigh),
|
2021-03-02 05:49:04 +01:00
|
|
|
])
|
|
|
|
|
|
|
|
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),
|
|
|
|
])
|
|
|
|
|
|
|
|
optionLabel.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
roundedBackgroundView.addSubview(optionLabel)
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
optionLabel.leadingAnchor.constraint(equalTo: checkmarkBackgroundView.trailingAnchor, constant: 14),
|
|
|
|
optionLabel.centerYAnchor.constraint(equalTo: roundedBackgroundView.centerYAnchor),
|
|
|
|
])
|
|
|
|
|
2021-03-03 12:34:29 +01:00
|
|
|
optionLabelMiddlePaddingView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
roundedBackgroundView.addSubview(optionLabelMiddlePaddingView)
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
optionLabelMiddlePaddingView.leadingAnchor.constraint(equalTo: optionLabel.trailingAnchor),
|
|
|
|
optionLabelMiddlePaddingView.centerYAnchor.constraint(equalTo: roundedBackgroundView.centerYAnchor),
|
|
|
|
optionLabelMiddlePaddingView.heightAnchor.constraint(equalToConstant: 4).priority(.defaultHigh),
|
|
|
|
optionLabelMiddlePaddingView.widthAnchor.constraint(greaterThanOrEqualToConstant: 8).priority(.defaultLow),
|
|
|
|
])
|
|
|
|
optionLabelMiddlePaddingView.setContentHuggingPriority(.defaultLow, for: .horizontal)
|
|
|
|
|
2021-03-02 05:49:04 +01:00
|
|
|
optionPercentageLabel.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
roundedBackgroundView.addSubview(optionPercentageLabel)
|
|
|
|
NSLayoutConstraint.activate([
|
2021-03-03 12:34:29 +01:00
|
|
|
optionPercentageLabel.leadingAnchor.constraint(equalTo: optionLabelMiddlePaddingView.trailingAnchor),
|
2021-03-02 05:49:04 +01:00
|
|
|
roundedBackgroundView.trailingAnchor.constraint(equalTo: optionPercentageLabel.trailingAnchor, constant: 18),
|
|
|
|
optionPercentageLabel.centerYAnchor.constraint(equalTo: roundedBackgroundView.centerYAnchor),
|
|
|
|
])
|
2021-03-02 12:10:45 +01:00
|
|
|
optionPercentageLabel.setContentHuggingPriority(.required - 1, for: .horizontal)
|
2021-03-03 12:34:29 +01:00
|
|
|
optionPercentageLabel.setContentCompressionResistancePriority(.required - 1, for: .horizontal)
|
2021-03-02 05:49:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
override func layoutSubviews() {
|
|
|
|
super.layoutSubviews()
|
|
|
|
|
2021-03-02 12:10:45 +01:00
|
|
|
updateCornerRadius()
|
2021-03-03 12:34:29 +01:00
|
|
|
updateTextAppearance()
|
2021-03-02 12:10:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private func updateCornerRadius() {
|
2021-03-02 05:49:04 +01:00
|
|
|
roundedBackgroundView.layer.masksToBounds = true
|
2021-03-02 12:10:45 +01:00
|
|
|
roundedBackgroundView.layer.cornerRadius = PollOptionTableViewCell.optionHeight * 0.5
|
2021-03-02 05:49:04 +01:00
|
|
|
roundedBackgroundView.layer.cornerCurve = .circular
|
|
|
|
|
|
|
|
checkmarkBackgroundView.layer.masksToBounds = true
|
2021-03-03 05:46:38 +01:00
|
|
|
checkmarkBackgroundView.layer.cornerRadius = PollOptionTableViewCell.checkmarkImageSize.width * 0.5
|
2021-03-02 05:49:04 +01:00
|
|
|
checkmarkBackgroundView.layer.cornerCurve = .circular
|
|
|
|
}
|
|
|
|
|
2021-03-03 12:34:29 +01:00
|
|
|
func updateTextAppearance() {
|
|
|
|
guard let voteState = voteState else {
|
2021-03-02 05:49:04 +01:00
|
|
|
optionLabel.textColor = Asset.Colors.Label.primary.color
|
|
|
|
optionLabel.layer.removeShadow()
|
2021-03-03 12:34:29 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch voteState {
|
|
|
|
case .hidden:
|
2021-03-02 05:49:04 +01:00
|
|
|
optionLabel.textColor = Asset.Colors.Label.primary.color
|
|
|
|
optionLabel.layer.removeShadow()
|
2021-03-03 12:34:29 +01:00
|
|
|
case .reveal(_, let percentage):
|
|
|
|
if CGFloat(percentage) * voteProgressStripView.frame.width > optionLabelMiddlePaddingView.frame.minX {
|
|
|
|
optionLabel.textColor = .white
|
|
|
|
optionLabel.layer.setupShadow(x: 0, y: 0, blur: 4, spread: 0)
|
|
|
|
} else {
|
|
|
|
optionLabel.textColor = Asset.Colors.Label.primary.color
|
|
|
|
optionLabel.layer.removeShadow()
|
|
|
|
}
|
|
|
|
|
|
|
|
if CGFloat(percentage) * voteProgressStripView.frame.width > optionLabelMiddlePaddingView.frame.maxX {
|
|
|
|
optionPercentageLabel.textColor = .white
|
|
|
|
optionPercentageLabel.layer.setupShadow(x: 0, y: 0, blur: 4, spread: 0)
|
|
|
|
} else {
|
|
|
|
optionPercentageLabel.textColor = Asset.Colors.Label.primary.color
|
|
|
|
optionPercentageLabel.layer.removeShadow()
|
|
|
|
}
|
2021-03-02 05:49:04 +01:00
|
|
|
}
|
2021-03-03 12:34:29 +01:00
|
|
|
|
2021-03-02 05:49:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#if canImport(SwiftUI) && DEBUG
|
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
struct PollTableViewCell_Previews: PreviewProvider {
|
|
|
|
|
|
|
|
static var controls: some View {
|
|
|
|
Group {
|
|
|
|
UIViewPreview() {
|
2021-03-02 12:10:45 +01:00
|
|
|
PollOptionTableViewCell()
|
2021-03-02 05:49:04 +01:00
|
|
|
}
|
|
|
|
.previewLayout(.fixed(width: 375, height: 44 + 10))
|
|
|
|
UIViewPreview() {
|
2021-03-02 12:10:45 +01:00
|
|
|
let cell = PollOptionTableViewCell()
|
2021-03-03 12:34:29 +01:00
|
|
|
PollSection.configure(cell: cell, selectState: .off)
|
2021-03-02 05:49:04 +01:00
|
|
|
return cell
|
|
|
|
}
|
|
|
|
.previewLayout(.fixed(width: 375, height: 44 + 10))
|
|
|
|
UIViewPreview() {
|
2021-03-02 12:10:45 +01:00
|
|
|
let cell = PollOptionTableViewCell()
|
2021-03-03 12:34:29 +01:00
|
|
|
PollSection.configure(cell: cell, selectState: .on)
|
2021-03-02 05:49:04 +01:00
|
|
|
return cell
|
|
|
|
}
|
|
|
|
.previewLayout(.fixed(width: 375, height: 44 + 10))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static var previews: some View {
|
|
|
|
Group {
|
|
|
|
controls.colorScheme(.light)
|
|
|
|
controls.colorScheme(.dark)
|
|
|
|
}
|
|
|
|
.background(Color.gray)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|