2023-06-28 18:46:54 +02:00
|
|
|
// Copyright © 2023 Mastodon gGmbH. All rights reserved.
|
|
|
|
|
|
|
|
import UIKit
|
2023-06-29 11:05:49 +02:00
|
|
|
import MastodonAsset
|
2023-06-28 18:46:54 +02:00
|
|
|
|
|
|
|
protocol GeneralSettingToggleCellDelegate: AnyObject {
|
2023-06-29 11:05:49 +02:00
|
|
|
func toggle(_ cell: GeneralSettingToggleCell, setting: GeneralSetting, isOn: Bool)
|
2023-06-28 18:46:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class GeneralSettingToggleCell: UITableViewCell {
|
|
|
|
static let reuseIdentifier = "GeneralSettingToggleCell"
|
|
|
|
|
2023-06-29 11:05:49 +02:00
|
|
|
let label: UILabel
|
|
|
|
let toggle: UISwitch
|
|
|
|
weak var delegate: GeneralSettingToggleCellDelegate?
|
|
|
|
|
|
|
|
var setting: GeneralSetting?
|
|
|
|
|
|
|
|
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
|
|
|
|
|
|
|
|
label = UILabel()
|
|
|
|
label.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
label.font = UIFontMetrics(forTextStyle: .body).scaledFont(for: .systemFont(ofSize: 17, weight: .regular))
|
|
|
|
label.numberOfLines = 0
|
|
|
|
|
|
|
|
toggle = UISwitch()
|
|
|
|
toggle.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
toggle.onTintColor = Asset.Colors.Brand.blurple.color
|
|
|
|
|
|
|
|
super.init(style: style, reuseIdentifier: reuseIdentifier)
|
|
|
|
|
|
|
|
toggle.addTarget(self, action: #selector(GeneralSettingToggleCell.toggleValueChanged(_:)), for: .valueChanged)
|
|
|
|
|
|
|
|
contentView.addSubview(label)
|
|
|
|
contentView.addSubview(toggle)
|
|
|
|
setupConstraints()
|
|
|
|
}
|
|
|
|
|
|
|
|
required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") }
|
|
|
|
|
|
|
|
private func setupConstraints() {
|
|
|
|
let constraints = [
|
|
|
|
label.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 11),
|
|
|
|
label.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 16),
|
|
|
|
contentView.bottomAnchor.constraint(equalTo: label.bottomAnchor, constant: 11),
|
|
|
|
|
|
|
|
toggle.leadingAnchor.constraint(greaterThanOrEqualTo: label.trailingAnchor, constant: 16),
|
|
|
|
toggle.centerYAnchor.constraint(equalTo: contentView.centerYAnchor),
|
|
|
|
contentView.trailingAnchor.constraint(equalTo: toggle.trailingAnchor, constant: 16)
|
|
|
|
|
|
|
|
]
|
|
|
|
NSLayoutConstraint.activate(constraints)
|
|
|
|
}
|
2023-06-28 18:46:54 +02:00
|
|
|
|
|
|
|
func configure(with setting: GeneralSetting, viewModel: GeneralSettingsViewModel) {
|
2023-06-29 11:05:49 +02:00
|
|
|
|
|
|
|
self.setting = setting
|
|
|
|
|
2023-06-28 18:46:54 +02:00
|
|
|
switch setting {
|
|
|
|
case .appearance(_), .openLinksIn(_):
|
|
|
|
assertionFailure("Only for Design")
|
|
|
|
case .design(let designSetting):
|
2023-06-29 11:05:49 +02:00
|
|
|
label.text = designSetting.title
|
2023-06-28 18:46:54 +02:00
|
|
|
|
|
|
|
switch designSetting {
|
|
|
|
case .showAnimations:
|
2023-06-29 11:05:49 +02:00
|
|
|
toggle.isOn = viewModel.playAnimations
|
2023-06-28 18:46:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-06-29 11:05:49 +02:00
|
|
|
|
|
|
|
@objc
|
|
|
|
func toggleValueChanged(_ sender: UISwitch) {
|
|
|
|
guard let setting else { return }
|
|
|
|
|
|
|
|
delegate?.toggle(self, setting: setting, isOn: sender.isOn)
|
|
|
|
|
|
|
|
}
|
2023-06-28 18:46:54 +02:00
|
|
|
}
|