Refactor Toggle-cell to be (more) reusable (IOS-14)

This commit is contained in:
Nathan Mattes 2023-06-30 09:10:58 +02:00
parent 5d58010907
commit 9ec380cccd
4 changed files with 79 additions and 50 deletions

View File

@ -136,6 +136,7 @@
D81A22782AB4782400905D71 /* SearchResultOverviewSection.swift in Sources */ = {isa = PBXBuildFile; fileRef = D81A22772AB4782400905D71 /* SearchResultOverviewSection.swift */; };
D81A227B2AB47B9A00905D71 /* SearchResultDefaultSectionTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = D81A227A2AB47B9A00905D71 /* SearchResultDefaultSectionTableViewCell.swift */; };
D82BD7532ABC44C2009A374A /* Coordinator.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8F9170E2A4B47EF008A5370 /* Coordinator.swift */; };
D81D124B2A4E1914005009D4 /* ToggleTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = D81D124A2A4E1914005009D4 /* ToggleTableViewCell.swift */; };
D8318A802A4466D300C0FB73 /* SettingsCoordinator.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8318A7F2A4466D300C0FB73 /* SettingsCoordinator.swift */; };
D8318A862A4468C700C0FB73 /* SettingsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8318A852A4468C700C0FB73 /* SettingsViewController.swift */; };
D8318A882A4468D300C0FB73 /* NotificationSettingsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8318A872A4468D300C0FB73 /* NotificationSettingsViewController.swift */; };
@ -164,7 +165,7 @@
D8F9170B2A4B2C80008A5370 /* AboutSettings.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8F9170A2A4B2C80008A5370 /* AboutSettings.swift */; };
D8F9170D2A4B3C6F008A5370 /* AboutMastodonTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8F9170C2A4B3C6F008A5370 /* AboutMastodonTableViewCell.swift */; };
D8F9170F2A4B47EF008A5370 /* Coordinator.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8F9170E2A4B47EF008A5370 /* Coordinator.swift */; };
D8F917112A4C6B40008A5370 /* GeneralSettingToggleCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8F917102A4C6B40008A5370 /* GeneralSettingToggleCell.swift */; };
D8F917112A4C6B40008A5370 /* GeneralSettingToggleTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8F917102A4C6B40008A5370 /* GeneralSettingToggleTableViewCell.swift */; };
D8F917122A4C6B67008A5370 /* GeneralSettingsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8318A832A4468A800C0FB73 /* GeneralSettingsViewController.swift */; };
D8F917142A4D74C3008A5370 /* GeneralSettingsDiffableTableViewDataSource.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8F917132A4D74C3008A5370 /* GeneralSettingsDiffableTableViewDataSource.swift */; };
DB0009A626AEE5DC009B9D2D /* Intents.intentdefinition in Sources */ = {isa = PBXBuildFile; fileRef = DB0009A926AEE5DC009B9D2D /* Intents.intentdefinition */; settings = {ATTRIBUTES = (codegen, ); }; };
@ -786,6 +787,7 @@
D82463542A52B47B00A3DBDD /* be */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = be; path = be.lproj/InfoPlist.strings; sourceTree = "<group>"; };
D82463552A52B47B00A3DBDD /* be */ = {isa = PBXFileReference; lastKnownFileType = text.plist.stringsdict; name = be; path = be.lproj/Intents.stringsdict; sourceTree = "<group>"; };
D82BD7512ABC42D6009A374A /* Coordinator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Coordinator.swift; sourceTree = "<group>"; };
D81D124A2A4E1914005009D4 /* ToggleTableViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ToggleTableViewCell.swift; sourceTree = "<group>"; };
D8318A7F2A4466D300C0FB73 /* SettingsCoordinator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsCoordinator.swift; sourceTree = "<group>"; };
D8318A832A4468A800C0FB73 /* GeneralSettingsViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GeneralSettingsViewController.swift; sourceTree = "<group>"; };
D8318A852A4468C700C0FB73 /* SettingsViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsViewController.swift; sourceTree = "<group>"; };
@ -825,7 +827,7 @@
D8F9170A2A4B2C80008A5370 /* AboutSettings.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AboutSettings.swift; sourceTree = "<group>"; };
D8F9170C2A4B3C6F008A5370 /* AboutMastodonTableViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AboutMastodonTableViewCell.swift; sourceTree = "<group>"; };
D8F9170E2A4B47EF008A5370 /* Coordinator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Coordinator.swift; sourceTree = "<group>"; };
D8F917102A4C6B40008A5370 /* GeneralSettingToggleCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GeneralSettingToggleCell.swift; sourceTree = "<group>"; };
D8F917102A4C6B40008A5370 /* GeneralSettingToggleTableViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GeneralSettingToggleTableViewCell.swift; sourceTree = "<group>"; };
D8F917132A4D74C3008A5370 /* GeneralSettingsDiffableTableViewDataSource.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GeneralSettingsDiffableTableViewDataSource.swift; sourceTree = "<group>"; };
DB0009A826AEE5DC009B9D2D /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.intentdefinition; name = Base; path = Base.lproj/Intents.intentdefinition; sourceTree = "<group>"; };
DB0009AD26AEE5E4009B9D2D /* ar */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = ar; path = ar.lproj/Intents.strings; sourceTree = "<group>"; };
@ -1750,9 +1752,10 @@
5B90C455262599800002E742 /* Settings */ = {
isa = PBXGroup;
children = (
D8F917092A4B2AFF008A5370 /* About Mastodon */,
D81D12492A4E190A005009D4 /* Shared */,
D8F916FF2A4AD898008A5370 /* Settings Overview */,
D8F917042A4B0657008A5370 /* General Settings */,
D8F917092A4B2AFF008A5370 /* About Mastodon */,
D8318A7E2A4466C900C0FB73 /* Legacy */,
D8318A7F2A4466D300C0FB73 /* SettingsCoordinator.swift */,
D8318A872A4468D300C0FB73 /* NotificationSettingsViewController.swift */,
@ -1832,6 +1835,14 @@
path = Cells;
sourceTree = "<group>";
};
D81D12492A4E190A005009D4 /* Shared */ = {
isa = PBXGroup;
children = (
D81D124A2A4E1914005009D4 /* ToggleTableViewCell.swift */,
);
path = Shared;
sourceTree = "<group>";
};
D8318A7E2A4466C900C0FB73 /* Legacy */ = {
isa = PBXGroup;
children = (
@ -1905,7 +1916,7 @@
children = (
D8318A832A4468A800C0FB73 /* GeneralSettingsViewController.swift */,
D8F917052A4B0791008A5370 /* GeneralSettings.swift */,
D8F917102A4C6B40008A5370 /* GeneralSettingToggleCell.swift */,
D8F917102A4C6B40008A5370 /* GeneralSettingToggleTableViewCell.swift */,
D8F917072A4B0B16008A5370 /* GeneralSettingSelectionCell.swift */,
D8F917132A4D74C3008A5370 /* GeneralSettingsDiffableTableViewDataSource.swift */,
);
@ -3747,6 +3758,7 @@
DB697DD4278F4927004EF2F7 /* StatusTableViewCellDelegate.swift in Sources */,
2A506CF6292D040100059C37 /* HashtagTimelineHeaderView.swift in Sources */,
DBA5E7A5263BD28C004598BB /* ContextMenuImagePreviewViewModel.swift in Sources */,
D81D124B2A4E1914005009D4 /* ToggleTableViewCell.swift in Sources */,
DB3E6FF52807C40300B035AE /* DiscoveryForYouViewController.swift in Sources */,
D809907A294BC9390050219F /* PrivacyTableViewCell.swift in Sources */,
2D38F1E525CD46C100561493 /* HomeTimelineViewModel.swift in Sources */,
@ -3947,7 +3959,7 @@
DBB525412611ED54002F1F29 /* ProfileHeaderViewController.swift in Sources */,
DBDFF19A28055A1400557A48 /* DiscoveryViewController.swift in Sources */,
DB9D6BFF25E4F5940051B173 /* ProfileViewController.swift in Sources */,
D8F917112A4C6B40008A5370 /* GeneralSettingToggleCell.swift in Sources */,
D8F917112A4C6B40008A5370 /* GeneralSettingToggleTableViewCell.swift in Sources */,
DB3EA8F1281B9EF600598866 /* DiscoveryCommunityViewModel+Diffable.swift in Sources */,
D8318A8A2A4468DC00C0FB73 /* AboutViewController.swift in Sources */,
85BC11B32932414900E191CD /* AltTextViewController.swift in Sources */,

View File

@ -0,0 +1,48 @@
// Copyright © 2023 Mastodon gGmbH. All rights reserved.
import UIKit
import MastodonAsset
protocol GeneralSettingToggleCellDelegate: AnyObject {
func toggle(_ cell: GeneralSettingToggleTableViewCell, setting: GeneralSetting, isOn: Bool)
}
class GeneralSettingToggleTableViewCell: ToggleTableViewCell {
override class var reuseIdentifier: String {
return "GeneralSettingToggleCell"
}
weak var delegate: GeneralSettingToggleCellDelegate?
var setting: GeneralSetting?
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
toggle.addTarget(self, action: #selector(GeneralSettingToggleTableViewCell.toggleValueChanged(_:)), for: .valueChanged)
}
required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") }
func configure(with setting: GeneralSetting, viewModel: GeneralSettingsViewModel) {
self.setting = setting
switch setting {
case .appearance(_), .openLinksIn(_):
assertionFailure("Only for Design")
case .design(let designSetting):
label.text = designSetting.title
switch designSetting {
case .showAnimations:
toggle.isOn = viewModel.playAnimations
}
}
}
@objc
func toggleValueChanged(_ sender: UISwitch) {
guard let setting else { return }
delegate?.toggle(self, setting: setting, isOn: sender.isOn)
}
}

View File

@ -31,7 +31,7 @@ class GeneralSettingsViewController: UIViewController {
tableView = UITableView(frame: .zero, style: .insetGrouped)
tableView.translatesAutoresizingMaskIntoConstraints = false
tableView.register(GeneralSettingSelectionCell.self, forCellReuseIdentifier: GeneralSettingSelectionCell.reuseIdentifier)
tableView.register(GeneralSettingToggleCell.self, forCellReuseIdentifier: GeneralSettingToggleCell.reuseIdentifier)
tableView.register(GeneralSettingToggleTableViewCell.self, forCellReuseIdentifier: GeneralSettingToggleTableViewCell.reuseIdentifier)
sections = [
GeneralSettingsSection(type: .appearance, entries: [
@ -76,7 +76,7 @@ class GeneralSettingsViewController: UIViewController {
selectionCell.configure(with: .appearance(setting), viewModel: self.viewModel)
cell = selectionCell
case .design(let setting):
guard let toggleCell = tableView.dequeueReusableCell(withIdentifier: GeneralSettingToggleCell.reuseIdentifier, for: indexPath) as? GeneralSettingToggleCell else { fatalError("WTF? Wrong Cell!") }
guard let toggleCell = tableView.dequeueReusableCell(withIdentifier: GeneralSettingToggleTableViewCell.reuseIdentifier, for: indexPath) as? GeneralSettingToggleTableViewCell else { fatalError("WTF? Wrong Cell!") }
toggleCell.configure(with: .design(setting), viewModel: self.viewModel)
toggleCell.delegate = self
@ -145,7 +145,7 @@ extension GeneralSettingsViewController: UITableViewDelegate {
}
extension GeneralSettingsViewController: GeneralSettingToggleCellDelegate {
func toggle(_ cell: GeneralSettingToggleCell, setting: GeneralSetting, isOn: Bool) {
func toggle(_ cell: GeneralSettingToggleTableViewCell, setting: GeneralSetting, isOn: Bool) {
switch setting {
case .appearance(_), .openLinksIn(_):
assertionFailure("No toggle")

View File

@ -3,18 +3,13 @@
import UIKit
import MastodonAsset
protocol GeneralSettingToggleCellDelegate: AnyObject {
func toggle(_ cell: GeneralSettingToggleCell, setting: GeneralSetting, isOn: Bool)
}
class GeneralSettingToggleCell: UITableViewCell {
static let reuseIdentifier = "GeneralSettingToggleCell"
class ToggleTableViewCell: UITableViewCell {
class var reuseIdentifier: String {
return "ToggleTableViewCell"
}
let label: UILabel
let toggle: UISwitch
weak var delegate: GeneralSettingToggleCellDelegate?
var setting: GeneralSetting?
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
@ -22,58 +17,32 @@ class GeneralSettingToggleCell: UITableViewCell {
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)
}
func configure(with setting: GeneralSetting, viewModel: GeneralSettingsViewModel) {
self.setting = setting
switch setting {
case .appearance(_), .openLinksIn(_):
assertionFailure("Only for Design")
case .design(let designSetting):
label.text = designSetting.title
switch designSetting {
case .showAnimations:
toggle.isOn = viewModel.playAnimations
}
}
}
@objc
func toggleValueChanged(_ sender: UISwitch) {
guard let setting else { return }
delegate?.toggle(self, setting: setting, isOn: sender.isOn)
}
}