mastodon-ios/Mastodon/Scene/Onboarding/PickServer/TableViewCell/PickServerCell.swift

163 lines
5.9 KiB
Swift
Raw Normal View History

//
// PickServerCell.swift
// Mastodon
//
// Created by BradGao on 2021/2/24.
//
import os.log
import UIKit
import Combine
import MastodonSDK
2021-03-05 12:30:17 +01:00
import AlamofireImage
import Kanna
import MastodonAsset
import MastodonLocalization
class PickServerCell: UITableViewCell {
var disposeBag = Set<AnyCancellable>()
2022-01-04 11:30:21 +01:00
let containerView: UIStackView = {
let view = UIStackView()
view.axis = .vertical
view.spacing = 4
return view
}()
let domainLabel: UILabel = {
let label = UILabel()
2022-01-04 11:30:21 +01:00
label.font = UIFontMetrics(forTextStyle: .headline).scaledFont(for: .systemFont(ofSize: 20, weight: .semibold))
2021-04-06 10:42:45 +02:00
label.textColor = Asset.Colors.Label.primary.color
label.adjustsFontForContentSizeCategory = true
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
let checkbox: UIImageView = {
let imageView = UIImageView()
2021-02-25 07:09:19 +01:00
imageView.preferredSymbolConfiguration = UIImage.SymbolConfiguration(textStyle: .body)
2021-04-06 10:42:45 +02:00
imageView.tintColor = Asset.Colors.Label.secondary.color
imageView.contentMode = .scaleAspectFill
imageView.translatesAutoresizingMaskIntoConstraints = false
return imageView
}()
let descriptionLabel: UILabel = {
let label = UILabel()
2022-01-04 11:30:21 +01:00
label.font = UIFontMetrics(forTextStyle: .caption1).scaledFont(for: .systemFont(ofSize: 13, weight: .regular))
label.numberOfLines = 0
2021-04-06 10:42:45 +02:00
label.textColor = Asset.Colors.Label.primary.color
label.adjustsFontForContentSizeCategory = true
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
let infoStackView: UIStackView = {
let stackView = UIStackView()
stackView.axis = .horizontal
2022-01-04 11:30:21 +01:00
stackView.spacing = 16
return stackView
}()
let separator: UIView = {
let view = UIView()
2022-01-04 11:30:21 +01:00
view.backgroundColor = Asset.Theme.System.separator.color
return view
}()
let langValueLabel: UILabel = {
let label = UILabel()
2021-04-06 10:42:45 +02:00
label.textColor = Asset.Colors.Label.primary.color
2022-01-04 11:30:21 +01:00
label.font = UIFontMetrics(forTextStyle: .caption1).scaledFont(for: .systemFont(ofSize: 12, weight: .regular))
label.textAlignment = .center
label.adjustsFontForContentSizeCategory = true
return label
}()
let usersValueLabel: UILabel = {
let label = UILabel()
2021-04-06 10:42:45 +02:00
label.textColor = Asset.Colors.Label.primary.color
2022-01-04 11:30:21 +01:00
label.font = UIFontMetrics(forTextStyle: .caption1).scaledFont(for: .systemFont(ofSize: 12, weight: .regular))
label.adjustsFontForContentSizeCategory = true
return label
}()
private var collapseConstraints: [NSLayoutConstraint] = []
private var expandConstraints: [NSLayoutConstraint] = []
override func prepareForReuse() {
super.prepareForReuse()
disposeBag.removeAll()
}
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
_init()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
_init()
}
}
// MARK: - Methods to configure appearance
extension PickServerCell {
private func _init() {
selectionStyle = .none
2022-02-15 11:15:58 +01:00
backgroundColor = Asset.Scene.Onboarding.background.color
2022-01-04 11:30:21 +01:00
checkbox.translatesAutoresizingMaskIntoConstraints = false
contentView.addSubview(checkbox)
NSLayoutConstraint.activate([
2022-01-04 11:30:21 +01:00
checkbox.leadingAnchor.constraint(equalTo: contentView.readableContentGuide.leadingAnchor, constant: 1),
checkbox.heightAnchor.constraint(equalToConstant: 32).priority(.required - 1),
checkbox.widthAnchor.constraint(equalToConstant: 32).priority(.required - 1),
])
2022-01-04 11:30:21 +01:00
containerView.translatesAutoresizingMaskIntoConstraints = false
contentView.addSubview(containerView)
NSLayoutConstraint.activate([
2022-01-04 11:30:21 +01:00
containerView.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 11),
containerView.leadingAnchor.constraint(equalTo: checkbox.trailingAnchor, constant: 22),
containerView.trailingAnchor.constraint(equalTo: contentView.readableContentGuide.trailingAnchor),
contentView.bottomAnchor.constraint(equalTo: containerView.bottomAnchor, constant: 11),
checkbox.centerYAnchor.constraint(equalTo: containerView.centerYAnchor),
])
2022-01-04 11:30:21 +01:00
containerView.addArrangedSubview(domainLabel)
containerView.addArrangedSubview(descriptionLabel)
containerView.setCustomSpacing(6, after: descriptionLabel)
containerView.addArrangedSubview(infoStackView)
2022-01-04 11:30:21 +01:00
infoStackView.addArrangedSubview(usersValueLabel)
infoStackView.addArrangedSubview(langValueLabel)
infoStackView.addArrangedSubview(UIView())
2022-01-04 11:30:21 +01:00
separator.translatesAutoresizingMaskIntoConstraints = false
contentView.addSubview(separator)
NSLayoutConstraint.activate([
separator.leadingAnchor.constraint(equalTo: contentView.readableContentGuide.leadingAnchor),
contentView.readableContentGuide.trailingAnchor.constraint(equalTo: separator.trailingAnchor),
separator.bottomAnchor.constraint(equalTo: contentView.bottomAnchor),
separator.heightAnchor.constraint(equalToConstant: UIView.separatorLineHeight(of: contentView)).priority(.required - 1),
])
}
2021-02-25 07:09:19 +01:00
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
if selected {
checkbox.image = UIImage(systemName: "checkmark.circle.fill")
2022-01-04 11:30:21 +01:00
checkbox.tintColor = Asset.Colors.Label.primary.color
2021-02-25 07:09:19 +01:00
} else {
checkbox.image = UIImage(systemName: "circle")
2022-01-04 11:30:21 +01:00
checkbox.tintColor = Asset.Colors.Label.secondary.color
2021-02-25 07:09:19 +01:00
}
}
}