mastodon-ios/Mastodon/Scene/Share/View/TableviewCell/PollOptionTableViewCell.swift

146 lines
4.9 KiB
Swift
Raw Normal View History

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
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-23 11:47:21 +01:00
let pollOptionView = PollOptionView()
2021-03-05 06:41:48 +01:00
var attribute: PollItem.Attribute?
2021-03-02 05:49:04 +01:00
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
_init()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
_init()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
2021-03-05 06:41:48 +01:00
guard let voteState = attribute?.voteState else { return }
switch voteState {
case .hidden:
let color = Asset.Colors.Background.systemGroupedBackground.color
2021-03-23 11:47:21 +01:00
pollOptionView.roundedBackgroundView.backgroundColor = isHighlighted ? color.withAlphaComponent(0.8) : color
case .reveal:
break
}
}
override func setHighlighted(_ highlighted: Bool, animated: Bool) {
super.setHighlighted(highlighted, animated: animated)
2021-03-05 06:41:48 +01:00
guard let voteState = attribute?.voteState else { return }
switch voteState {
case .hidden:
let color = Asset.Colors.Background.systemGroupedBackground.color
2021-03-23 11:47:21 +01:00
pollOptionView.roundedBackgroundView.backgroundColor = isHighlighted ? color.withAlphaComponent(0.8) : color
case .reveal:
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-23 11:47:21 +01:00
pollOptionView.optionTextField.isUserInteractionEnabled = false
2021-03-23 11:47:21 +01:00
pollOptionView.translatesAutoresizingMaskIntoConstraints = false
contentView.addSubview(pollOptionView)
2021-03-02 05:49:04 +01:00
NSLayoutConstraint.activate([
2021-03-23 11:47:21 +01:00
pollOptionView.topAnchor.constraint(equalTo: contentView.topAnchor),
pollOptionView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
pollOptionView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
pollOptionView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor),
2021-03-02 05:49:04 +01:00
])
}
2021-03-23 11:47:21 +01:00
func updateTextAppearance() {
2021-03-05 06:41:48 +01:00
guard let voteState = attribute?.voteState else {
2021-03-23 11:47:21 +01:00
pollOptionView.optionTextField.textColor = Asset.Colors.Label.primary.color
pollOptionView.optionTextField.layer.removeShadow()
return
}
switch voteState {
case .hidden:
2021-03-23 11:47:21 +01:00
pollOptionView.optionTextField.textColor = Asset.Colors.Label.primary.color
pollOptionView.optionTextField.layer.removeShadow()
2021-03-05 06:41:48 +01:00
case .reveal(_, let percentage, _):
2021-03-23 11:47:21 +01:00
if CGFloat(percentage) * pollOptionView.voteProgressStripView.frame.width > pollOptionView.optionLabelMiddlePaddingView.frame.minX {
pollOptionView.optionTextField.textColor = .white
pollOptionView.optionTextField.layer.setupShadow(x: 0, y: 0, blur: 4, spread: 0)
} else {
2021-03-23 11:47:21 +01:00
pollOptionView.optionTextField.textColor = Asset.Colors.Label.primary.color
pollOptionView.optionTextField.layer.removeShadow()
}
2021-03-23 11:47:21 +01:00
if CGFloat(percentage) * pollOptionView.voteProgressStripView.frame.width > pollOptionView.optionLabelMiddlePaddingView.frame.maxX {
pollOptionView.optionPercentageLabel.textColor = .white
pollOptionView.optionPercentageLabel.layer.setupShadow(x: 0, y: 0, blur: 4, spread: 0)
} else {
2021-03-23 11:47:21 +01:00
pollOptionView.optionPercentageLabel.textColor = Asset.Colors.Label.primary.color
pollOptionView.optionPercentageLabel.layer.removeShadow()
}
2021-03-02 05:49:04 +01:00
}
2021-03-23 11:47:21 +01:00
}
override func layoutSubviews() {
super.layoutSubviews()
updateTextAppearance()
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()
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()
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