-
Notifications
You must be signed in to change notification settings - Fork 384
/
Copy pathSettingsDataSource.swift
160 lines (127 loc) · 4.61 KB
/
SettingsDataSource.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
//
// SettingsDataSource.swift
// MullvadVPN
//
// Created by pronebird on 19/10/2021.
// Copyright © 2021 Mullvad VPN AB. All rights reserved.
//
import MullvadSettings
import UIKit
final class SettingsDataSource: UITableViewDiffableDataSource<SettingsDataSource.Section, SettingsDataSource.Item>,
UITableViewDelegate {
enum CellReuseIdentifiers: String, CaseIterable {
case basicCell
var reusableViewClass: AnyClass {
SettingsCell.self
}
}
private enum HeaderFooterReuseIdentifier: String, CaseIterable {
case spacer
var reusableViewClass: AnyClass {
EmptyTableViewHeaderFooterView.self
}
}
enum Section: String {
case main
case version
case problemReport
}
enum Item: String {
case vpnSettings
case version
case problemReport
case faq
case apiAccess
var accessibilityIdentifier: AccessibilityIdentifier {
switch self {
case .vpnSettings:
return .vpnSettingsCell
case .version:
return .versionCell
case .problemReport:
return .problemReportCell
case .faq:
return .faqCell
case .apiAccess:
return .apiAccessCell
}
}
var reuseIdentifier: CellReuseIdentifiers {
.basicCell
}
}
private let interactor: SettingsInteractor
private let settingsCellFactory: SettingsCellFactory
private var storedAccountData: StoredAccountData?
private weak var tableView: UITableView?
weak var delegate: SettingsDataSourceDelegate?
init(tableView: UITableView, interactor: SettingsInteractor) {
self.tableView = tableView
self.interactor = interactor
let settingsCellFactory = SettingsCellFactory(tableView: tableView, interactor: interactor)
self.settingsCellFactory = settingsCellFactory
super.init(tableView: tableView) { _, indexPath, itemIdentifier in
settingsCellFactory.makeCell(for: itemIdentifier, indexPath: indexPath)
}
tableView.delegate = self
registerClasses()
updateDataSnapshot()
interactor.didUpdateDeviceState = { [weak self] _ in
self?.updateDataSnapshot()
}
storedAccountData = interactor.deviceState.accountData
}
// MARK: - UITableViewDelegate
func tableView(_ tableView: UITableView, shouldHighlightRowAt indexPath: IndexPath) -> Bool {
if case .version = itemIdentifier(for: indexPath) {
return false
} else {
return true
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
guard let item = itemIdentifier(for: indexPath) else { return }
delegate?.settingsDataSource(self, didSelectItem: item)
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
tableView.dequeueReusableHeaderFooterView(
withIdentifier: HeaderFooterReuseIdentifier.spacer.rawValue
)
}
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
nil
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return UIMetrics.TableView.sectionSpacing
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
0
}
// MARK: - Private
private func registerClasses() {
CellReuseIdentifiers.allCases.forEach { cellIdentifier in
tableView?.register(
cellIdentifier.reusableViewClass,
forCellReuseIdentifier: cellIdentifier.rawValue
)
}
HeaderFooterReuseIdentifier.allCases.forEach { reuseIdentifier in
tableView?.register(
reuseIdentifier.reusableViewClass,
forHeaderFooterViewReuseIdentifier: reuseIdentifier.rawValue
)
}
}
private func updateDataSnapshot() {
var snapshot = NSDiffableDataSourceSnapshot<Section, Item>()
snapshot.appendSections([.main])
if interactor.deviceState.isLoggedIn {
snapshot.appendItems([.vpnSettings], toSection: .main)
}
snapshot.appendItems([.apiAccess], toSection: .main)
snapshot.appendSections([.version, .problemReport])
snapshot.appendItems([.version], toSection: .version)
snapshot.appendItems([.problemReport, .faq], toSection: .problemReport)
apply(snapshot)
}
}