-
Notifications
You must be signed in to change notification settings - Fork 384
/
Copy pathVPNSettingsCellFactory.swift
231 lines (193 loc) · 8.38 KB
/
VPNSettingsCellFactory.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
//
// VPNSettingsCellFactory.swift
// MullvadVPN
//
// Created by Jon Petersson on 2023-03-09.
// Copyright © 2023 Mullvad VPN AB. All rights reserved.
//
import MullvadSettings
import UIKit
protocol VPNSettingsCellEventHandler {
func showInfo(for button: VPNSettingsInfoButtonItem)
func addCustomPort(_ port: UInt16)
func selectCustomPortEntry(_ port: UInt16) -> Bool
func selectObfuscationState(_ state: WireGuardObfuscationState)
func switchMultihop(_ state: MultihopState)
}
final class VPNSettingsCellFactory: CellFactoryProtocol {
let tableView: UITableView
var viewModel: VPNSettingsViewModel
var delegate: VPNSettingsCellEventHandler?
var isEditing = false
init(tableView: UITableView, viewModel: VPNSettingsViewModel) {
self.tableView = tableView
self.viewModel = viewModel
}
func makeCell(for item: VPNSettingsDataSource.Item, indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: item.reuseIdentifier.rawValue, for: indexPath)
configureCell(cell, item: item, indexPath: indexPath)
return cell
}
// swiftlint:disable:next cyclomatic_complexity function_body_length
func configureCell(_ cell: UITableViewCell, item: VPNSettingsDataSource.Item, indexPath: IndexPath) {
switch item {
case .dnsSettings:
guard let cell = cell as? SettingsCell else { return }
cell.titleLabel.text = NSLocalizedString(
"DNS_SETTINGS_CELL_LABEL",
tableName: "VPNSettings",
value: "DNS settings",
comment: ""
)
cell.disclosureType = .chevron
cell.accessibilityIdentifier = item.accessibilityIdentifier
case .ipOverrides:
guard let cell = cell as? SettingsCell else { return }
cell.titleLabel.text = NSLocalizedString(
"IP_OVERRIDE_CELL_LABEL",
tableName: "VPNSettings",
value: "Server IP override",
comment: ""
)
cell.disclosureType = .chevron
cell.accessibilityIdentifier = item.accessibilityIdentifier
case let .wireGuardPort(port):
guard let cell = cell as? SelectableSettingsCell else { return }
var portString = NSLocalizedString(
"WIRE_GUARD_PORT_CELL_LABEL",
tableName: "VPNSettings",
value: "Automatic",
comment: ""
)
if let port {
portString = String(port)
}
cell.titleLabel.text = portString
cell.accessibilityIdentifier = "\(item.accessibilityIdentifier.rawValue) (\(portString))"
cell.applySubCellStyling()
case .wireGuardCustomPort:
guard let cell = cell as? SettingsInputCell else { return }
cell.titleLabel.text = NSLocalizedString(
"WIRE_GUARD_CUSTOM_PORT_CELL_LABEL",
tableName: "VPNSettings",
value: "Custom",
comment: ""
)
cell.textField.placeholder = NSLocalizedString(
"WIRE_GUARD_CUSTOM_PORT_CELL_INPUT_PLACEHOLDER",
tableName: "VPNSettings",
value: "Port",
comment: ""
)
cell.textField.accessibilityIdentifier = .customWireGuardPortTextField
cell.accessibilityIdentifier = item.accessibilityIdentifier
cell.applySubCellStyling()
cell.inputDidChange = { [weak self] text in
let port = UInt16(text) ?? UInt16()
cell.isValidInput = self?.delegate?.selectCustomPortEntry(port) ?? false
}
cell.inputWasConfirmed = { [weak self] text in
if let port = UInt16(text), cell.isValidInput {
self?.delegate?.addCustomPort(port)
}
}
if let port = viewModel.customWireGuardPort {
cell.textField.text = String(port)
// Only update validity if input is invalid. Otherwise the textcolor will be wrong
// (active text field color rather than the expected inactive color).
let isValidInput = delegate?.selectCustomPortEntry(port) ?? false
if !isValidInput {
cell.isValidInput = false
}
}
case .wireGuardObfuscationAutomatic:
guard let cell = cell as? SelectableSettingsCell else { return }
cell.titleLabel.text = NSLocalizedString(
"WIRE_GUARD_OBFUSCATION_AUTOMATIC_LABEL",
tableName: "VPNSettings",
value: "Automatic",
comment: ""
)
cell.accessibilityIdentifier = item.accessibilityIdentifier
cell.applySubCellStyling()
case .wireGuardObfuscationOn:
guard let cell = cell as? SelectableSettingsCell else { return }
cell.titleLabel.text = NSLocalizedString(
"WIRE_GUARD_OBFUSCATION_ON_LABEL",
tableName: "VPNSettings",
value: "On (UDP-over-TCP)",
comment: ""
)
cell.accessibilityIdentifier = item.accessibilityIdentifier
cell.applySubCellStyling()
case .wireGuardObfuscationOff:
guard let cell = cell as? SelectableSettingsCell else { return }
cell.titleLabel.text = NSLocalizedString(
"WIRE_GUARD_OBFUSCATION_OFF_LABEL",
tableName: "VPNSettings",
value: "Off",
comment: ""
)
cell.accessibilityIdentifier = item.accessibilityIdentifier
cell.applySubCellStyling()
case let .wireGuardObfuscationPort(port):
guard let cell = cell as? SelectableSettingsCell else { return }
let portString = port == 0 ? "Automatic" : "\(port)"
cell.titleLabel.text = NSLocalizedString(
"WIRE_GUARD_OBFUSCATION_PORT_LABEL",
tableName: "VPNSettings",
value: portString,
comment: ""
)
cell.accessibilityIdentifier = "\(item.accessibilityIdentifier.rawValue)\(portString)"
cell.applySubCellStyling()
case .quantumResistanceAutomatic:
guard let cell = cell as? SelectableSettingsCell else { return }
cell.titleLabel.text = NSLocalizedString(
"QUANTUM_RESISTANCE_AUTOMATIC_LABEL",
tableName: "VPNSettings",
value: "Automatic",
comment: ""
)
cell.accessibilityIdentifier = item.accessibilityIdentifier
cell.applySubCellStyling()
case .quantumResistanceOn:
guard let cell = cell as? SelectableSettingsCell else { return }
cell.titleLabel.text = NSLocalizedString(
"QUANTUM_RESISTANCE_ON_LABEL",
tableName: "VPNSettings",
value: "On",
comment: ""
)
cell.accessibilityIdentifier = item.accessibilityIdentifier
cell.applySubCellStyling()
case .quantumResistanceOff:
guard let cell = cell as? SelectableSettingsCell else { return }
cell.titleLabel.text = NSLocalizedString(
"QUANTUM_RESISTANCE_OFF_LABEL",
tableName: "VPNSettings",
value: "Off",
comment: ""
)
cell.accessibilityIdentifier = item.accessibilityIdentifier
cell.applySubCellStyling()
case .multihop:
guard let cell = cell as? SettingsSwitchCell else { return }
cell.titleLabel.text = NSLocalizedString(
"MULTIHOP_LABEL",
tableName: "VPNSettings",
value: "Enable multihop",
comment: ""
)
cell.accessibilityIdentifier = item.accessibilityIdentifier
cell.setOn(viewModel.multihopState.isEnabled, animated: false)
cell.infoButtonHandler = { [weak self] in
self?.delegate?.showInfo(for: .multihop)
}
cell.action = { [weak self] isEnabled in
let state: MultihopState = isEnabled ? .on : .off
self?.delegate?.switchMultihop(state)
}
}
}
}