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
|
//
// 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
var reusableViewClass: AnyClass {
SettingsCell.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 vpnSettings
case apiAccess
case version
case problemReport
}
enum Item: String {
case vpnSettings
case changelog
case problemReport
case faq
case apiAccess
case daita
case multihop
var accessibilityIdentifier: AccessibilityIdentifier {
switch self {
case .vpnSettings:
return .vpnSettingsCell
case .changelog:
return .versionCell
case .problemReport:
return .problemReportCell
case .faq:
return .faqCell
case .apiAccess:
return .apiAccessCell
case .daita:
return .daitaCell
case .multihop:
return .multihopCell
}
}
var reuseIdentifier: CellReuseIdentifiers {
.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.delegate = self
settingsCellFactory.delegate = self
registerClasses()
updateDataSnapshot()
interactor.didUpdateDeviceState = { [weak self] _ in
self?.updateDataSnapshot()
}
storedAccountData = interactor.deviceState.accountData
}
func reload(from tunnelSettings: LatestTunnelSettings) {
settingsCellFactory.viewModel = SettingsViewModel(from: 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
)
}
// 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>()
if interactor.deviceState.isLoggedIn {
snapshot.appendSections([.vpnSettings])
snapshot.appendItems([
.daita,
.multihop,
.vpnSettings,
], toSection: .vpnSettings)
}
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)
}
}
extension SettingsDataSource: @preconcurrency SettingsCellEventHandler {
func showInfo(for button: SettingsInfoButtonItem) {
delegate?.showInfo(for: button)
}
}
|