2021-04-08 13:47:31 +02:00
|
|
|
//
|
|
|
|
// SettingsToggleTableViewCell.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by ihugo on 2021/4/8.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
2021-04-26 10:57:50 +02:00
|
|
|
import Combine
|
2022-01-27 14:23:39 +01:00
|
|
|
import MastodonAsset
|
|
|
|
import MastodonLocalization
|
2021-04-08 13:47:31 +02:00
|
|
|
|
2021-05-08 05:03:34 +02:00
|
|
|
protocol SettingsToggleCellDelegate: AnyObject {
|
2021-04-26 10:57:50 +02:00
|
|
|
func settingsToggleCell(_ cell: SettingsToggleTableViewCell, switchValueDidChange switch: UISwitch)
|
2021-04-08 13:47:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class SettingsToggleTableViewCell: UITableViewCell {
|
2021-04-26 10:57:50 +02:00
|
|
|
|
|
|
|
var disposeBag = Set<AnyCancellable>()
|
|
|
|
|
|
|
|
private(set) lazy var switchButton: UISwitch = {
|
2021-04-08 13:47:31 +02:00
|
|
|
let view = UISwitch(frame:.zero)
|
|
|
|
return view
|
|
|
|
}()
|
|
|
|
|
|
|
|
weak var delegate: SettingsToggleCellDelegate?
|
2021-07-22 07:47:56 +02:00
|
|
|
|
|
|
|
override func prepareForReuse() {
|
|
|
|
super.prepareForReuse()
|
|
|
|
|
|
|
|
disposeBag.removeAll()
|
|
|
|
}
|
2021-04-08 13:47:31 +02:00
|
|
|
|
|
|
|
// MARK: - Methods
|
|
|
|
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
|
|
|
|
super.init(style: .default, reuseIdentifier: reuseIdentifier)
|
|
|
|
setupUI()
|
|
|
|
}
|
|
|
|
|
|
|
|
required init?(coder: NSCoder) {
|
2021-04-26 10:57:50 +02:00
|
|
|
super.init(coder: coder)
|
|
|
|
setupUI()
|
2021-04-08 13:47:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Private methods
|
|
|
|
private func setupUI() {
|
|
|
|
selectionStyle = .none
|
2021-04-13 10:22:41 +02:00
|
|
|
accessoryView = switchButton
|
2021-08-04 10:24:19 +02:00
|
|
|
textLabel?.numberOfLines = 0
|
2021-04-08 13:47:31 +02:00
|
|
|
|
2022-02-15 07:54:59 +01:00
|
|
|
updateAppearance()
|
2021-04-26 10:57:50 +02:00
|
|
|
switchButton.addTarget(self, action: #selector(switchValueDidChange(sender:)), for: .valueChanged)
|
|
|
|
}
|
2022-02-15 07:54:59 +01:00
|
|
|
|
|
|
|
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
|
|
|
|
super.traitCollectionDidChange(previousTraitCollection)
|
|
|
|
|
|
|
|
updateAppearance()
|
|
|
|
}
|
2021-04-26 10:57:50 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Actions
|
|
|
|
extension SettingsToggleTableViewCell {
|
|
|
|
|
|
|
|
@objc private func switchValueDidChange(sender: UISwitch) {
|
|
|
|
guard let delegate = delegate else { return }
|
|
|
|
delegate.settingsToggleCell(self, switchValueDidChange: sender)
|
2021-04-08 13:47:31 +02:00
|
|
|
}
|
|
|
|
|
2021-04-26 10:57:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
extension SettingsToggleTableViewCell {
|
|
|
|
|
|
|
|
func update(enabled: Bool?) {
|
|
|
|
switchButton.isEnabled = enabled != nil
|
|
|
|
textLabel?.textColor = enabled != nil ? Asset.Colors.Label.primary.color : Asset.Colors.Label.secondary.color
|
|
|
|
switchButton.isOn = enabled ?? false
|
2021-04-08 13:47:31 +02:00
|
|
|
}
|
2021-04-26 10:57:50 +02:00
|
|
|
|
2022-02-15 07:54:59 +01:00
|
|
|
private func updateAppearance() {
|
|
|
|
switchButton.onTintColor = {
|
|
|
|
switch traitCollection.userInterfaceStyle {
|
|
|
|
case .dark:
|
|
|
|
// set default green for Dark Mode
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
// set tint black for Light Mode
|
2022-11-16 04:21:08 +01:00
|
|
|
return self.contentView.window?.tintColor
|
2022-02-15 07:54:59 +01:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2021-04-08 13:47:31 +02:00
|
|
|
}
|