-
Notifications
You must be signed in to change notification settings - Fork 384
/
Copy pathEditAccessMethodViewController.swift
409 lines (335 loc) · 15.5 KB
/
EditAccessMethodViewController.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
//
// EditAccessMethodViewController.swift
// MullvadVPN
//
// Created by pronebird on 17/11/2023.
// Copyright © 2025 Mullvad VPN AB. All rights reserved.
//
import Combine
import MullvadSettings
import UIKit
/// The view controller providing the interface for editing the existing access method.
class EditAccessMethodViewController: UIViewController {
typealias EditAccessMethodDataSource = UITableViewDiffableDataSource<
EditAccessMethodSectionIdentifier,
EditAccessMethodItemIdentifier
>
private let tableView = UITableView(frame: .zero, style: .insetGrouped)
private let subject: CurrentValueSubject<AccessMethodViewModel, Never>
private let interactor: EditAccessMethodInteractorProtocol
private var alertPresenter: AlertPresenter
private var cancellables = Set<AnyCancellable>()
private var dataSource: EditAccessMethodDataSource?
weak var delegate: EditAccessMethodViewControllerDelegate?
init(
subject: CurrentValueSubject<AccessMethodViewModel, Never>,
interactor: EditAccessMethodInteractorProtocol,
alertPresenter: AlertPresenter
) {
self.subject = subject
self.interactor = interactor
self.alertPresenter = alertPresenter
super.init(nibName: nil, bundle: nil)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .secondaryColor
tableView.setAccessibilityIdentifier(.editAccessMethodView)
tableView.backgroundColor = .secondaryColor
tableView.delegate = self
isModalInPresentation = true
let headerView = createHeaderView()
view.addConstrainedSubviews([headerView, tableView]) {
headerView.pinEdgesToSuperviewMargins(PinnableEdges([.leading(8), .trailing(8), .top(0)]))
tableView.pinEdgesToSuperview(.all().excluding(.top))
tableView.topAnchor.constraint(equalTo: headerView.bottomAnchor, constant: 20)
}
configureDataSource()
configureNavigationItem()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
interactor.cancelProxyConfigurationTest()
}
private func createHeaderView() -> UIView {
var headerView: InfoHeaderView?
if let headerConfig = subject.value.infoHeaderConfig {
headerView = InfoHeaderView(config: headerConfig)
headerView?.onAbout = { [weak self] in
guard let self, let infoModalConfig = subject.value.infoModalConfig else { return }
delegate?.controllerShouldShowMethodInfo(self, config: infoModalConfig)
}
}
return headerView ?? UIView()
}
}
// MARK: - UITableViewDelegate
extension EditAccessMethodViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, shouldHighlightRowAt indexPath: IndexPath) -> Bool {
guard let itemIdentifier = dataSource?.itemIdentifier(for: indexPath) else { return false }
return itemIdentifier.isSelectable
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
guard let itemIdentifier = dataSource?.itemIdentifier(for: indexPath) else { return }
if case .methodSettings = itemIdentifier {
delegate?.controllerShouldShowMethodSettings(self)
}
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UIMetrics.SettingsCell.apiAccessCellHeight
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
return nil
}
// Header height shenanigans to avoid extra spacing in testing sections when testing is NOT ongoing.
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
guard let sectionIdentifier = dataSource?.snapshot().sectionIdentifiers[section] else { return 0 }
switch sectionIdentifier {
case .methodSettings, .deleteMethod, .testMethod:
return UITableView.automaticDimension
case .testingStatus:
return subject.value.testingStatus == .initial ? 0 : UITableView.automaticDimension
case .enableMethod, .cancelTest:
return 0
}
}
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
guard let sectionIdentifier = dataSource?.snapshot().sectionIdentifiers[section] else { return nil }
guard let sectionFooterText = sectionIdentifier.sectionFooter else { return nil }
guard let headerView = tableView
.dequeueReusableView(withIdentifier: AccessMethodHeaderFooterReuseIdentifier.primary)
else { return nil }
var contentConfiguration = UIListContentConfiguration.mullvadGroupedFooter(tableStyle: tableView.style)
contentConfiguration.text = sectionFooterText
headerView.contentConfiguration = contentConfiguration
return headerView
}
// Footer height shenanigans to avoid extra spacing in testing sections when testing is NOT ongoing.
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
guard let sectionIdentifier = dataSource?.snapshot().sectionIdentifiers[section] else { return 0 }
let marginToDeleteMethodItem: CGFloat = 24
switch sectionIdentifier {
case .enableMethod, .methodSettings, .deleteMethod, .testMethod:
return UITableView.automaticDimension
case .testingStatus:
switch subject.value.testingStatus {
case .initial, .inProgress:
return 0
case .succeeded, .failed:
return marginToDeleteMethodItem
}
case .cancelTest:
return subject.value.testingStatus == .inProgress ? marginToDeleteMethodItem : 0
}
}
// MARK: - Cell configuration
private func dequeueCell(at indexPath: IndexPath, for itemIdentifier: EditAccessMethodItemIdentifier)
-> UITableViewCell {
let cell = tableView.dequeueReusableView(withIdentifier: itemIdentifier.cellIdentifier, for: indexPath)
configureBackground(cell: cell, itemIdentifier: itemIdentifier)
switch itemIdentifier {
case .testMethod:
configureTestMethod(cell, itemIdentifier: itemIdentifier)
case .cancelTest:
configureCancelTest(cell, itemIdentifier: itemIdentifier)
case .testingStatus:
configureTestingStatus(cell, itemIdentifier: itemIdentifier)
case .deleteMethod:
configureDeleteMethod(cell, itemIdentifier: itemIdentifier)
case .enableMethod:
configureEnableMethod(cell, itemIdentifier: itemIdentifier)
case .methodSettings:
configureMethodSettings(cell, itemIdentifier: itemIdentifier)
}
return cell
}
private func configureBackground(cell: UITableViewCell, itemIdentifier: EditAccessMethodItemIdentifier) {
guard let cell = cell as? DynamicBackgroundConfiguration else { return }
guard !itemIdentifier.isClearBackground else {
cell.setAutoAdaptingClearBackgroundConfiguration()
return
}
cell.setAutoAdaptingBackgroundConfiguration(.mullvadListGroupedCell(), selectionType: .dimmed)
}
private func configureTestMethod(_ cell: UITableViewCell, itemIdentifier: EditAccessMethodItemIdentifier) {
var contentConfiguration = ButtonCellContentConfiguration()
contentConfiguration.accessibilityIdentifier = .accessMethodTestButton
contentConfiguration.text = itemIdentifier.text
contentConfiguration.isEnabled = subject.value.testingStatus != .inProgress
contentConfiguration.primaryAction = UIAction { [weak self] _ in
self?.onTest()
}
cell.contentConfiguration = contentConfiguration
}
private func configureCancelTest(_ cell: UITableViewCell, itemIdentifier: EditAccessMethodItemIdentifier) {
var contentConfiguration = ButtonCellContentConfiguration()
contentConfiguration.accessibilityIdentifier = .accessMethodTestButton
contentConfiguration.text = itemIdentifier.text
contentConfiguration.isEnabled = subject.value.testingStatus == .inProgress
contentConfiguration.primaryAction = UIAction { [weak self] _ in
self?.onCancelTest()
}
cell.contentConfiguration = contentConfiguration
}
private func configureTestingStatus(_ cell: UITableViewCell, itemIdentifier: EditAccessMethodItemIdentifier) {
var contentConfiguration = MethodTestingStatusCellContentConfiguration()
contentConfiguration.status = subject.value.testingStatus.viewStatus
cell.contentConfiguration = contentConfiguration
}
private func configureEnableMethod(_ cell: UITableViewCell, itemIdentifier: EditAccessMethodItemIdentifier) {
var contentConfiguration = SwitchCellContentConfiguration()
contentConfiguration.accessibilityIdentifier = .accessMethodEnableSwitch
contentConfiguration.text = itemIdentifier.text
contentConfiguration.isOn = subject.value.isEnabled
contentConfiguration.onChange = UIAction { [weak self] action in
if let customSwitch = action.sender as? UISwitch {
self?.subject.value.isEnabled = customSwitch.isOn
self?.onSave()
}
}
cell.contentConfiguration = contentConfiguration
}
private func configureMethodSettings(_ cell: UITableViewCell, itemIdentifier: EditAccessMethodItemIdentifier) {
var contentConfiguration = UIListContentConfiguration.mullvadCell(tableStyle: tableView.style)
contentConfiguration.text = itemIdentifier.text
cell.contentConfiguration = contentConfiguration
if let cell = cell as? CustomCellDisclosureHandling {
cell.disclosureType = .chevron
}
}
private func configureDeleteMethod(_ cell: UITableViewCell, itemIdentifier: EditAccessMethodItemIdentifier) {
var contentConfiguration = ButtonCellContentConfiguration()
contentConfiguration.style = .tableInsetGroupedDanger
contentConfiguration.text = itemIdentifier.text
contentConfiguration.primaryAction = UIAction { [weak self] _ in
self?.onDelete()
}
cell.contentConfiguration = contentConfiguration
}
// MARK: - Data source handling
private func configureDataSource() {
tableView.registerReusableViews(from: AccessMethodCellReuseIdentifier.self)
tableView.registerReusableViews(from: AccessMethodHeaderFooterReuseIdentifier.self)
dataSource = UITableViewDiffableDataSource(
tableView: tableView,
cellProvider: { [weak self] _, indexPath, itemIdentifier in
self?.dequeueCell(at: indexPath, for: itemIdentifier)
}
)
subject.withPreviousValue()
.sink { [weak self] previousValue, newValue in
self?.viewModelDidChange(previousValue: previousValue, newValue: newValue)
}
.store(in: &cancellables)
}
private func viewModelDidChange(previousValue: AccessMethodViewModel?, newValue: AccessMethodViewModel) {
let animated = view.window != nil
configureNavigationItem()
updateDataSource(
previousValue: previousValue,
newValue: newValue,
animated: animated
)
}
private func updateDataSource(
previousValue: AccessMethodViewModel?,
newValue: AccessMethodViewModel,
animated: Bool
) {
var snapshot = NSDiffableDataSourceSnapshot<EditAccessMethodSectionIdentifier, EditAccessMethodItemIdentifier>()
snapshot.appendSections([.enableMethod])
snapshot.appendItems([.enableMethod], toSection: .enableMethod)
// Add method settings if the access method is configurable.
if newValue.method.hasProxyConfiguration {
snapshot.appendSections([.methodSettings])
snapshot.appendItems([.methodSettings], toSection: .methodSettings)
}
snapshot.appendSections([.testMethod])
snapshot.appendItems([.testMethod], toSection: .testMethod)
// Reconfigure the test button on status changes.
if let previousValue, previousValue.testingStatus != newValue.testingStatus {
snapshot.reconfigureItems([.testMethod])
}
snapshot.appendSections([.testingStatus])
snapshot.appendSections([.cancelTest])
// Add test status below the test button.
if newValue.testingStatus != .initial {
snapshot.appendItems([.testingStatus], toSection: .testingStatus)
if let previousValue, previousValue.testingStatus != newValue.testingStatus {
snapshot.reconfigureItems([.testingStatus])
}
// Show cancel test button below test status.
if newValue.testingStatus == .inProgress {
snapshot.appendItems([.cancelTest], toSection: .cancelTest)
}
}
// Add delete button for user-defined access methods.
if !newValue.method.isPermanent {
snapshot.appendSections([.deleteMethod])
snapshot.appendItems([.deleteMethod], toSection: .deleteMethod)
}
dataSource?.apply(snapshot, animatingDifferences: animated)
}
// MARK: - Misc
private func configureNavigationItem() {
navigationItem.title = subject.value.navigationItemTitle
}
private func onSave() {
interactor.saveAccessMethod()
}
private func onDelete() {
let methodName = subject.value.name.isEmpty
? NSLocalizedString(
"METHOD_SETTINGS_SAVE_PROMPT",
tableName: "APIAccess",
value: "method?",
comment: ""
)
: subject.value.name
let presentation = AlertPresentation(
id: "api-access-methods-delete-method-alert",
icon: .alert,
message: NSLocalizedString(
"METHOD_SETTINGS_DELETE_PROMPT",
tableName: "APIAccess",
value: "Delete \(methodName)?",
comment: ""
),
buttons: [
AlertAction(
title: NSLocalizedString(
"METHOD_SETTINGS_DELETE_BUTTON",
tableName: "APIAccess",
value: "Delete",
comment: ""
),
style: .destructive,
handler: { [weak self] in
guard let self else { return }
interactor.deleteAccessMethod()
delegate?.controllerDidDeleteAccessMethod(self)
}
),
AlertAction(
title: NSLocalizedString(
"METHOD_SETTINGS_CANCEL_BUTTON",
tableName: "APIAccess",
value: "Cancel",
comment: ""
),
style: .default
),
]
)
alertPresenter.showAlert(presentation: presentation, animated: true)
}
private func onTest() {
interactor.startProxyConfigurationTest()
}
private func onCancelTest() {
interactor.cancelProxyConfigurationTest()
}
} // swiftlint:disable:this file_length