mastodon-ios/Mastodon/Scene/Settings/Cell/SettingsAppearanceTableView...

136 lines
3.9 KiB
Swift
Raw Normal View History

2022-02-08 11:17:17 +01:00
//
// SettingsAppearanceTableViewCell.swift
// Mastodon
//
// Created by ihugo on 2021/4/8.
//
import UIKit
import Combine
import MastodonAsset
import MastodonLocalization
protocol SettingsAppearanceTableViewCellDelegate: AnyObject {
func settingsAppearanceTableViewCell(_ cell: SettingsAppearanceTableViewCell, didSelectAppearanceMode appearanceMode: SettingsItem.AppearanceMode)
}
class SettingsAppearanceTableViewCell: UITableViewCell {
var disposeBag = Set<AnyCancellable>()
var observations = Set<NSKeyValueObservation>()
static let spacing: CGFloat = 28
weak var delegate: SettingsAppearanceTableViewCellDelegate?
2023-03-16 09:05:41 +01:00
public private(set) var viewModel = ViewModel()
2022-02-08 11:17:17 +01:00
lazy var stackView: UIStackView = {
let view = UIStackView()
view.axis = .horizontal
view.distribution = .fillEqually
view.spacing = SettingsAppearanceTableViewCell.spacing
return view
}()
let systemAppearanceView = AppearanceView(
2022-03-10 09:20:25 +01:00
image: Asset.Settings.automatic.image,
2022-02-14 12:57:15 +01:00
title: L10n.Scene.Settings.Section.Appearance.automatic
2022-02-08 11:17:17 +01:00
)
2022-02-14 12:57:15 +01:00
let darkAppearanceView = AppearanceView(
2022-02-08 11:17:17 +01:00
image: Asset.Settings.dark.image,
2022-02-14 12:57:15 +01:00
title: L10n.Scene.Settings.Section.Appearance.dark
2022-02-08 11:17:17 +01:00
)
let lightAppearanceView = AppearanceView(
image: Asset.Settings.light.image,
2022-02-14 12:57:15 +01:00
title: L10n.Scene.Settings.Section.Appearance.light
2022-02-08 11:17:17 +01:00
)
var appearanceViews: [AppearanceView] {
return [
systemAppearanceView,
2022-02-14 12:57:15 +01:00
darkAppearanceView,
2022-02-08 11:17:17 +01:00
lightAppearanceView,
]
}
override func prepareForReuse() {
super.prepareForReuse()
disposeBag.removeAll()
observations.removeAll()
viewModel.prepareForReuse()
}
// MARK: - Methods
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setupUI()
2023-03-16 09:05:41 +01:00
viewModel.bind(cell: self)
2022-02-08 11:17:17 +01:00
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
super.layoutSubviews()
// remove separator line in section of group tableview
for subview in self.subviews {
if subview != self.contentView && subview.frame.width == self.frame.width {
subview.removeFromSuperview()
}
}
// remove grouped style table corner radius
layer.cornerRadius = 0
2022-02-08 11:17:17 +01:00
}
}
extension SettingsAppearanceTableViewCell {
// MARK: Private methods
private func setupUI() {
backgroundColor = .clear
selectionStyle = .none
stackView.translatesAutoresizingMaskIntoConstraints = false
contentView.addSubview(stackView)
stackView.pinToParent()
2022-02-08 11:17:17 +01:00
stackView.addArrangedSubview(systemAppearanceView)
2022-02-14 12:57:15 +01:00
stackView.addArrangedSubview(darkAppearanceView)
2022-02-08 11:17:17 +01:00
stackView.addArrangedSubview(lightAppearanceView)
appearanceViews.forEach { view in
let tapGestureRecognizer = UITapGestureRecognizer.singleTapGestureRecognizer
view.addGestureRecognizer(tapGestureRecognizer)
tapGestureRecognizer.addTarget(self, action: #selector(SettingsAppearanceTableViewCell.appearanceViewDidPressed(_:)))
}
}
}
// MARK: - Actions
extension SettingsAppearanceTableViewCell {
@objc func appearanceViewDidPressed(_ sender: UITapGestureRecognizer) {
let mode: SettingsItem.AppearanceMode
switch sender.view {
case systemAppearanceView:
mode = .system
2022-02-14 12:57:15 +01:00
case darkAppearanceView:
mode = .dark
2022-02-08 11:17:17 +01:00
case lightAppearanceView:
mode = .light
default:
assertionFailure()
return
}
delegate?.settingsAppearanceTableViewCell(self, didSelectAppearanceMode: mode)
}
}