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
|
//
// CustomDNSCellFactory.swift
// MullvadVPN
//
// Created by Jon Petersson on 2023-11-09.
// Copyright © 2025 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)
}
@MainActor
final class CustomDNSCellFactory: @preconcurrency 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.setAccessibilityIdentifier(preference.accessibilityIdentifier)
cell.applySubCellStyling()
cell.setOn(toggleSetting, animated: true)
cell.action = { [weak self] isOn in
self?.delegate?.didChangeState(
for: preference,
isOn: isOn
)
}
}
func configureCell(_ cell: UITableViewCell, item: CustomDNSDataSource.Item, indexPath: IndexPath) {
switch item {
case .blockAll:
let localizedString = NSLocalizedString("All", comment: "")
configure(
cell,
toggleSetting: viewModel.blockAll,
title: localizedString,
for: .blockAll
)
case .blockAdvertising:
let localizedString = NSLocalizedString("Ads", comment: "")
configure(
cell,
toggleSetting: viewModel.blockAdvertising,
title: localizedString,
for: .blockAdvertising
)
case .blockTracking:
let localizedString = NSLocalizedString("Trackers", comment: "")
configure(
cell,
toggleSetting: viewModel.blockTracking,
title: localizedString,
for: .blockTracking
)
case .blockMalware:
guard let cell = cell as? SettingsSwitchCell else { return }
let localizedString = NSLocalizedString("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("Adult content", comment: "")
configure(
cell,
toggleSetting: viewModel.blockAdultContent,
title: localizedString,
for: .blockAdultContent
)
case .blockGambling:
let localizedString = NSLocalizedString("Gambling", comment: "")
configure(
cell,
toggleSetting: viewModel.blockGambling,
title: localizedString,
for: .blockGambling
)
case .blockSocialMedia:
let localizedString = NSLocalizedString("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("Use custom DNS server", comment: "")
cell.setSwitchEnabled(viewModel.customDNSPrecondition == .satisfied)
cell.setOn(viewModel.effectiveEnableCustomDNS, animated: false)
cell.accessibilityHint = viewModel.customDNSPrecondition
.localizedDescription(isEditing: isEditing)
cell.setAccessibilityIdentifier(.dnsSettingsUseCustomDNSCell)
cell.action = { [weak self] isOn in
self?.delegate?.didChangeState(for: .useCustomDNS, isOn: isOn)
}
cell.backgroundView?.backgroundColor = UIColor.Cell.Background.normal
case .addDNSServer:
guard let cell = cell as? SettingsAddDNSEntryCell else { return }
cell.titleLabel.text = NSLocalizedString("Add a server", comment: "")
cell.setAccessibilityIdentifier(.dnsSettingsAddServerCell)
cell.tapAction = { [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: .mullvadTiny
)
}
}
private func dnsEntryIsValid(identifier: UUID, cell: SettingsDNSTextCell) -> Bool {
delegate?.didChangeDNSEntry(
with: identifier,
inputString: cell.textField.text ?? ""
) ?? false
}
}
|