-
Notifications
You must be signed in to change notification settings - Fork 392
/
Copy pathSettingsDataSource.swift
298 lines (250 loc) · 9.25 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
//
// 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 basic
case daita
var reusableViewClass: AnyClass {
switch self {
case .basic:
SettingsCell.self
case .daita:
SettingsSwitchCell.self
}
}
}
private enum HeaderFooterReuseIdentifier: String, CaseIterable, HeaderFooterIdentifierProtocol {
case primary
case spacer
var headerFooterClass: AnyClass {
switch self {
case .primary:
UITableViewHeaderFooterView.self
case .spacer:
EmptyTableViewHeaderFooterView.self
}
}
}
enum Section: String {
case daita
case main
case version
case problemReport
var sectionFooter: String? {
switch self {
case .daita:
NSLocalizedString(
"SETTINGS_DAITA_SECTION_FOOTER",
tableName: "Settings",
value: "Makes it possible to use DAITA with any server and is automatically enabled.",
comment: ""
)
default:
nil
}
}
}
enum Item: String {
case vpnSettings
case version
case problemReport
case faq
case apiAccess
case daita
case daitaDirectOnly
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
case .daita:
return .daitaSwitch
case .daitaDirectOnly:
return .daitaDirectOnlySwitch
}
}
var reuseIdentifier: CellReuseIdentifiers {
switch self {
case .vpnSettings, .version, .problemReport, .faq, .apiAccess:
.basic
case .daita, .daitaDirectOnly:
.daita
}
}
}
private let interactor: SettingsInteractor
private var storedAccountData: StoredAccountData?
private let settingsCellFactory: SettingsCellFactory
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
settingsCellFactory.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 {
switch itemIdentifier(for: indexPath) {
case .vpnSettings, .problemReport, .faq, .apiAccess:
true
case .version, .daita, .daitaDirectOnly, .none:
false
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
guard let item = itemIdentifier(for: indexPath) else { return }
delegate?.didSelectItem(item: item)
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
tableView.dequeueReusableHeaderFooterView(
withIdentifier: HeaderFooterReuseIdentifier.spacer.rawValue
)
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
let sectionIdentifier = snapshot().sectionIdentifiers[section]
return switch sectionIdentifier {
case .main:
0
case .daita, .version, .problemReport:
UITableView.automaticDimension
}
}
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let sectionIdentifier = snapshot().sectionIdentifiers[section]
guard let sectionFooterText = sectionIdentifier.sectionFooter else { return nil }
guard let headerView = tableView
.dequeueReusableView(withIdentifier: HeaderFooterReuseIdentifier.primary)
else { return nil }
var contentConfiguration = UIListContentConfiguration.mullvadGroupedFooter(tableStyle: .plain)
contentConfiguration.text = sectionFooterText
headerView.contentConfiguration = contentConfiguration
// headerView.directionalLayoutMargins = UIMetrics.SettingsCell.apiAccessInsetLayoutMargins
return headerView
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
let sectionIdentifier = snapshot().sectionIdentifiers[section]
return switch sectionIdentifier {
case .daita:
UITableView.automaticDimension
case .main, .version, .problemReport:
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.headerFooterClass,
forHeaderFooterViewReuseIdentifier: reuseIdentifier.rawValue
)
}
}
private func updateDataSnapshot() {
var snapshot = NSDiffableDataSourceSnapshot<Section, Item>()
snapshot.appendSections([.daita, .main])
if interactor.deviceState.isLoggedIn {
snapshot.appendItems([.daita], toSection: .daita)
snapshot.appendItems([.daitaDirectOnly], toSection: .daita)
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)
}
}
extension SettingsDataSource: SettingsCellEventHandler {
func showInfo(for button: SettingsInfoButtonItem) {
delegate?.showInfo(for: button)
}
func switchDaitaState(_ settings: DAITASettings) {
testDaitaCompatibility(for: .daita, settings: settings) { [weak self] in
self?.reloadItem(.daitaDirectOnly)
} onDiscard: { [weak self] in
self?.reloadItem(.daita)
}
}
func switchDaitaDirectOnlyState(_ settings: DAITASettings) {
testDaitaCompatibility(for: .daitaDirectOnly, settings: settings, onDiscard: { [weak self] in
self?.reloadItem(.daitaDirectOnly)
})
}
private func reloadItem(_ item: Item) {
var snapshot = snapshot()
snapshot.reloadItems([item])
apply(snapshot, animatingDifferences: false)
}
private func testDaitaCompatibility(
for item: Item,
settings: DAITASettings,
onSave: (() -> Void)? = nil,
onDiscard: @escaping () -> Void
) {
let updateSettings = { [weak self] in
self?.settingsCellFactory.viewModel.setDAITASettings(settings)
self?.interactor.updateDAITASettings(settings)
onSave?()
}
var promptItemSetting: DAITASettingsPromptItem.Setting?
switch item {
case .daita:
promptItemSetting = .daita
case .daitaDirectOnly:
promptItemSetting = .directOnly
default:
break
}
if let promptItemSetting, let error = interactor.evaluateDaitaSettingsCompatibility(settings) {
switch error {
case .singlehop:
delegate?.showPrompt(
for: .daitaSettingIncompatibleWithSinglehop(promptItemSetting),
onSave: { updateSettings() },
onDiscard: onDiscard
)
case .multihop:
delegate?.showPrompt(
for: .daitaSettingIncompatibleWithMultihop(promptItemSetting),
onSave: { updateSettings() },
onDiscard: onDiscard
)
}
} else {
updateSettings()
}
}
}