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
|
//
// SettingsDataSource.swift
// MullvadVPN
//
// Created by pronebird on 19/10/2021.
// Copyright © 2025 Mullvad VPN AB. All rights reserved.
//
import MullvadSettings
import UIKit
final class SettingsDataSource: UITableViewDiffableDataSource<SettingsDataSource.Section, SettingsDataSource.Item>,
UITableViewDelegate
{
enum CellReuseIdentifier: String, CaseIterable {
case basic
case changelog
var reusableViewClass: AnyClass {
SettingsCell.self
}
var cellStyle: UITableViewCell.CellStyle {
switch self {
case .basic: .default
case .changelog: .subtitle
}
}
}
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 vpnSettings
case apiAccess
case version
case problemReport
case language
}
enum Item: String {
case vpnSettings
case changelog
case problemReport
case faq
case apiAccess
case daita
case multihop
case language
var accessibilityIdentifier: AccessibilityIdentifier {
switch self {
case .vpnSettings:
.vpnSettingsCell
case .changelog:
.versionCell
case .problemReport:
.problemReportCell
case .faq:
.faqCell
case .apiAccess:
.apiAccessCell
case .daita:
.daitaCell
case .multihop:
.multihopCell
case .language:
.languageCell
}
}
var reuseIdentifier: CellReuseIdentifier {
switch self {
case .changelog: .changelog
default: .basic
}
}
}
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.sectionFooterHeight = 0
tableView.tableHeaderView = UIView(
frame: CGRect(
origin: .zero,
size: CGSize(width: 0, height: UIMetrics.TableView.emptyHeaderHeight)
))
tableView.delegate = self
registerClasses()
updateDataSnapshot()
interactor.didUpdateSettings = { [weak self] in
self?.updateDataSnapshot()
}
storedAccountData = interactor.deviceState.accountData
}
func reload() {
settingsCellFactory.viewModel = SettingsViewModel(from: interactor.tunnelSettings)
var snapshot = snapshot()
snapshot.reconfigureItems(snapshot.itemIdentifiers)
apply(snapshot, animatingDifferences: false)
}
// MARK: - UITableViewDelegate
func tableView(_ tableView: UITableView, shouldHighlightRowAt indexPath: IndexPath) -> Bool {
true
}
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 {
guard let section = sectionIdentifier(for: section) else { return 0 }
return switch section {
case .vpnSettings:
0
default:
UIMetrics.TableView.sectionSpacing
}
}
// MARK: - Private
private func registerClasses() {
HeaderFooterReuseIdentifier.allCases.forEach { reuseIdentifier in
tableView?.register(
reuseIdentifier.headerFooterClass,
forHeaderFooterViewReuseIdentifier: reuseIdentifier.rawValue
)
}
}
private func updateDataSnapshot() {
var snapshot = NSDiffableDataSourceSnapshot<Section, Item>()
if interactor.deviceState.isLoggedIn {
snapshot.appendSections([.vpnSettings])
snapshot.appendItems(
[
.daita,
.multihop,
.vpnSettings,
], toSection: .vpnSettings)
}
#if DEBUG
snapshot.appendSections([.language])
snapshot.appendItems([.language], toSection: .language)
#endif
snapshot.appendSections([.apiAccess])
snapshot.appendItems([.apiAccess], toSection: .apiAccess)
snapshot.appendSections([.version, .problemReport])
snapshot.appendItems([.changelog], toSection: .version)
snapshot.appendItems([.problemReport, .faq], toSection: .problemReport)
apply(snapshot)
}
}
|