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
|
//
// SettingsDataSource.swift
// MullvadVPN
//
// Created by pronebird on 19/10/2021.
// Copyright © 2021 Mullvad VPN AB. All rights reserved.
//
import UIKit
final class SettingsDataSource: NSObject, UITableViewDataSource, UITableViewDelegate {
private enum CellReuseIdentifiers: String, CaseIterable {
case accountCell
case basicCell
var reusableViewClass: AnyClass {
switch self {
case .accountCell:
return SettingsAccountCell.self
case .basicCell:
return SettingsCell.self
}
}
}
private enum HeaderFooterReuseIdentifier: String, CaseIterable {
case spacer
var reusableViewClass: AnyClass {
switch self {
case .spacer:
return EmptyTableViewHeaderFooterView.self
}
}
}
enum Section: String {
case main
case version
case problemReport
}
enum Item: String {
case account
case preferences
case version
case problemReport
case faq
}
private var snapshot = DataSourceSnapshot<Section, Item>()
private let interactor: SettingsInteractor
private var storedAccountData: StoredAccountData?
weak var delegate: SettingsDataSourceDelegate?
weak var tableView: UITableView? {
didSet {
tableView?.delegate = self
tableView?.dataSource = self
registerClasses()
}
}
init(interactor: SettingsInteractor) {
self.interactor = interactor
super.init()
interactor.didUpdateDeviceState = { [weak self] deviceState in
self?.didUpdateDeviceState(deviceState)
}
storedAccountData = interactor.deviceState.accountData
updateDataSnapshot()
}
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 newSnapshot = DataSourceSnapshot<Section, Item>()
if interactor.deviceState.isLoggedIn {
newSnapshot.appendSections([.main])
newSnapshot.appendItems([.account, .preferences], in: .main)
}
newSnapshot.appendSections([.version, .problemReport])
newSnapshot.appendItems([.version], in: .version)
newSnapshot.appendItems([.problemReport, .faq], in: .problemReport)
snapshot = newSnapshot
}
// MARK: - UITableViewDataSource
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let sectionIdentifier = snapshot.section(at: section)!
return snapshot.numberOfItems(in: sectionIdentifier) ?? 0
}
func numberOfSections(in tableView: UITableView) -> Int {
return snapshot.numberOfSections()
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let item = snapshot.itemForIndexPath(indexPath)!
switch item {
case .account:
let cell = tableView.dequeueReusableCell(
withIdentifier: CellReuseIdentifiers.accountCell.rawValue,
for: indexPath
) as! SettingsAccountCell
cell.titleLabel.text = NSLocalizedString(
"ACCOUNT_CELL_LABEL",
tableName: "Settings",
value: "Account",
comment: ""
)
cell.accountExpiryDate = interactor.deviceState.accountData?.expiry
cell.accessibilityIdentifier = "AccountCell"
cell.disclosureType = .chevron
return cell
case .preferences:
let cell = tableView.dequeueReusableCell(
withIdentifier: CellReuseIdentifiers.basicCell.rawValue,
for: indexPath
) as! SettingsCell
cell.titleLabel.text = NSLocalizedString(
"PREFERENCES_CELL_LABEL",
tableName: "Settings",
value: "Preferences",
comment: ""
)
cell.detailTitleLabel.text = nil
cell.accessibilityIdentifier = nil
cell.disclosureType = .chevron
return cell
case .version:
let cell = tableView.dequeueReusableCell(
withIdentifier: CellReuseIdentifiers.basicCell.rawValue,
for: indexPath
) as! SettingsCell
cell.titleLabel.text = NSLocalizedString(
"APP_VERSION_CELL_LABEL",
tableName: "Settings",
value: "App version",
comment: ""
)
cell.detailTitleLabel.text = Bundle.main.productVersion
cell.accessibilityIdentifier = nil
cell.disclosureType = .none
return cell
case .problemReport:
let cell = tableView.dequeueReusableCell(
withIdentifier: CellReuseIdentifiers.basicCell.rawValue,
for: indexPath
) as! SettingsCell
cell.titleLabel.text = NSLocalizedString(
"REPORT_PROBLEM_CELL_LABEL",
tableName: "Settings",
value: "Report a problem",
comment: ""
)
cell.detailTitleLabel.text = nil
cell.accessibilityIdentifier = nil
cell.disclosureType = .chevron
return cell
case .faq:
let cell = tableView.dequeueReusableCell(
withIdentifier: CellReuseIdentifiers.basicCell.rawValue,
for: indexPath
) as! SettingsCell
cell.titleLabel.text = NSLocalizedString(
"FAQ_AND_GUIDES_CELL_LABEL",
tableName: "Settings",
value: "FAQ & Guides",
comment: ""
)
cell.detailTitleLabel.text = nil
cell.accessibilityIdentifier = nil
cell.disclosureType = .externalLink
return cell
}
}
// MARK: - UITableViewDelegate
func tableView(_ tableView: UITableView, shouldHighlightRowAt indexPath: IndexPath) -> Bool {
if case .version = snapshot.itemForIndexPath(indexPath) {
return false
} else {
return true
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
guard let item = snapshot.itemForIndexPath(indexPath) else { return }
delegate?.settingsDataSource(self, didSelectItem: item)
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
return tableView
.dequeueReusableHeaderFooterView(
withIdentifier: HeaderFooterReuseIdentifier.spacer
.rawValue
)
}
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
return nil
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return UIMetrics.sectionSpacing
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 0
}
// MARK: - Private
private func didUpdateDeviceState(_ deviceState: DeviceState) {
let newAccountData = deviceState.accountData
let oldAccountData = storedAccountData
storedAccountData = newAccountData
// Refresh individual row if expiry changed.
if let newAccountData = newAccountData, let oldAccountData = oldAccountData,
oldAccountData.number == newAccountData.number,
oldAccountData.expiry != newAccountData.expiry
{
tableView?.performBatchUpdates {
if let indexPath = snapshot.indexPathForItem(.account) {
tableView?.reloadRows(at: [indexPath], with: .none)
}
}
return
}
updateDataSnapshot()
tableView?.reloadData()
}
}
|