-
Notifications
You must be signed in to change notification settings - Fork 392
/
Copy pathCustomDNSCellFactory.swift
217 lines (188 loc) · 7.23 KB
/
CustomDNSCellFactory.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
//
// CustomDNSCellFactory.swift
// MullvadVPN
//
// Created by Jon Petersson on 2023-11-09.
// Copyright © 2023 Mullvad VPN AB. All rights reserved.
//
import MullvadSettings
import UIKit
protocol CustomDNSCellEventHandler {
func addDNSEntry()
func didChangeDNSEntry(with identifier: UUID, inputString: String) -> Bool
func didChangeState(for preference: CustomDNSDataSource.Item, isOn: Bool)
func showInfo(for button: VPNSettingsInfoButtonItem)
}
final class CustomDNSCellFactory: CellFactoryProtocol {
let tableView: UITableView
var viewModel: VPNSettingsViewModel
var delegate: CustomDNSCellEventHandler?
var isEditing = false
init(tableView: UITableView, viewModel: VPNSettingsViewModel) {
self.tableView = tableView
self.viewModel = viewModel
}
func makeCell(for item: CustomDNSDataSource.Item, indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: item.reuseIdentifier.rawValue, for: indexPath)
configureCell(cell, item: item, indexPath: indexPath)
return cell
}
func configure(
_ cell: UITableViewCell,
toggleSetting: Bool,
title: String,
for preference: CustomDNSDataSource.Item
) {
guard let cell = cell as? SettingsSwitchCell else { return }
cell.titleLabel.text = title
cell.accessibilityIdentifier = preference.accessibilityIdentifier
cell.applySubCellStyling()
cell.setOn(toggleSetting, animated: false)
cell.action = { [weak self] isOn in
self?.delegate?.didChangeState(
for: preference,
isOn: isOn
)
}
}
// swiftlint:disable:next function_body_length
func configureCell(_ cell: UITableViewCell, item: CustomDNSDataSource.Item, indexPath: IndexPath) {
switch item {
case .blockAdvertising:
let localizedString = NSLocalizedString(
"BLOCK_ADS_CELL_LABEL",
tableName: "VPNSettings",
value: "Ads",
comment: ""
)
configure(
cell,
toggleSetting: viewModel.blockAdvertising,
title: localizedString,
for: .blockAdvertising
)
case .blockTracking:
let localizedString = NSLocalizedString(
"BLOCK_TRACKERS_CELL_LABEL",
tableName: "VPNSettings",
value: "Trackers",
comment: ""
)
configure(
cell,
toggleSetting: viewModel.blockTracking,
title: localizedString,
for: .blockTracking
)
case .blockMalware:
guard let cell = cell as? SettingsSwitchCell else { return }
let localizedString = NSLocalizedString(
"BLOCK_MALWARE_CELL_LABEL",
tableName: "VPNSettings",
value: "Malware",
comment: ""
)
configure(
cell,
toggleSetting: viewModel.blockMalware,
title: localizedString,
for: .blockMalware
)
cell.infoButtonHandler = { [weak self] in
self?.delegate?.showInfo(for: .blockMalware)
}
case .blockAdultContent:
let localizedString = NSLocalizedString(
"BLOCK_ADULT_CELL_LABEL",
tableName: "VPNSettings",
value: "Adult content",
comment: ""
)
configure(
cell,
toggleSetting: viewModel.blockAdultContent,
title: localizedString,
for: .blockAdultContent
)
case .blockGambling:
let localizedString = NSLocalizedString(
"BLOCK_GAMBLING_CELL_LABEL",
tableName: "VPNSettings",
value: "Gambling",
comment: ""
)
configure(
cell,
toggleSetting: viewModel.blockGambling,
title: localizedString,
for: .blockGambling
)
case .blockSocialMedia:
let localizedString = NSLocalizedString(
"BLOCK_SOCIAL_MEDIA_CELL_LABEL",
tableName: "VPNSettings",
value: "Social media",
comment: ""
)
configure(
cell,
toggleSetting: viewModel.blockSocialMedia,
title: localizedString,
for: .blockSocialMedia
)
case .useCustomDNS:
guard let cell = cell as? SettingsSwitchCell else { return }
cell.titleLabel.text = NSLocalizedString(
"CUSTOM_DNS_CELL_LABEL",
tableName: "VPNSettings",
value: "Use custom DNS server",
comment: ""
)
cell.setEnabled(viewModel.customDNSPrecondition == .satisfied)
cell.setOn(viewModel.effectiveEnableCustomDNS, animated: false)
cell.accessibilityHint = viewModel.customDNSPrecondition
.localizedDescription(isEditing: isEditing)
cell.accessibilityIdentifier = .dnsSettingsUseCustomDNSCell
cell.action = { [weak self] isOn in
self?.delegate?.didChangeState(for: .useCustomDNS, isOn: isOn)
}
case .addDNSServer:
guard let cell = cell as? SettingsAddDNSEntryCell else { return }
cell.titleLabel.text = NSLocalizedString(
"ADD_CUSTOM_DNS_SERVER_CELL_LABEL",
tableName: "VPNSettings",
value: "Add a server",
comment: ""
)
cell.accessibilityIdentifier = .dnsSettingsAddServerCell
cell.action = { [weak self] in
self?.delegate?.addDNSEntry()
}
case let .dnsServer(entryIdentifier):
guard let cell = cell as? SettingsDNSTextCell else { return }
let dnsServerEntry = viewModel.dnsEntry(entryIdentifier: entryIdentifier)!
cell.textField.text = dnsServerEntry.address
cell.isValidInput = dnsEntryIsValid(identifier: entryIdentifier, cell: cell)
cell.accessibilityIdentifier = "\(item.accessibilityIdentifier) (\(entryIdentifier))"
cell.onTextChange = { [weak self] cell in
cell.isValidInput = self?
.dnsEntryIsValid(identifier: entryIdentifier, cell: cell) ?? false
}
cell.onReturnKey = { cell in
cell.endEditing(false)
}
case .dnsServerInfo:
guard let cell = cell as? SettingsDNSInfoCell else { return }
cell.titleLabel.attributedText = viewModel.customDNSPrecondition.attributedLocalizedDescription(
isEditing: isEditing,
preferredFont: .systemFont(ofSize: 14)
)
}
}
private func dnsEntryIsValid(identifier: UUID, cell: SettingsDNSTextCell) -> Bool {
delegate?.didChangeDNSEntry(
with: identifier,
inputString: cell.textField.text ?? ""
) ?? false
}
}