summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadVPN/View controllers/VPNSettings/VPNSettingsCellFactory.swift
blob: 91a873e7ed985fe059cdf1bff475967fe0d7607f (plain)
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
//
//  VPNSettingsCellFactory.swift
//  MullvadVPN
//
//  Created by Jon Petersson on 2023-03-09.
//  Copyright © 2025 Mullvad VPN AB. All rights reserved.
//

import MullvadSettings
import UIKit

protocol VPNSettingsCellEventHandler {
    func showInfo(for button: VPNSettingsInfoButtonItem)
    func showDetails(for button: VPNSettingsDetailsButtonItem)
    func addCustomPort(_ port: UInt16)
    func selectCustomPortEntry(_ port: UInt16) -> Bool
    func selectObfuscationState(_ state: WireGuardObfuscationState)
    func switchMultihop(_ state: MultihopState)
    func setLocalNetworkSharing(_ enabled: Bool, onCancel: @escaping () -> Void)
    func setIncludeAllNetworks(_ enabled: Bool, onCancel: @escaping () -> Void)
}

@MainActor
final class VPNSettingsCellFactory: @preconcurrency 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
    }

    func configureCell(_ cell: UITableViewCell, item: VPNSettingsDataSource.Item, indexPath: IndexPath) {
        (cell as? SettingsCell)?.detailTitleLabel.accessibilityIdentifier = nil
        switch item {
        case .includeAllNetworks:
            guard let cell = cell as? SettingsSwitchCell else { return }
            cell.action = { enable in
                self.delegate?.setIncludeAllNetworks(enable) {
                    cell.setOn(self.viewModel.includeAllNetworks, animated: true)
                }
            }

            cell.titleLabel.text = NSLocalizedString("Include all networks", comment: "")
            cell.setAccessibilityIdentifier(item.accessibilityIdentifier)
            cell.setOn(viewModel.includeAllNetworks, animated: true)
        case .localNetworkSharing:
            guard let cell = cell as? SettingsSwitchCell else { return }
            cell.infoButtonHandler = { self.delegate?.showInfo(for: .localNetworkSharing) }
            cell.action = { enable in
                self.delegate?.setLocalNetworkSharing(enable) {
                    cell.setOn(self.viewModel.localNetworkSharing, animated: true)
                }
            }

            cell.titleLabel.text = NSLocalizedString("Local network sharing", comment: "")
            cell.setAccessibilityIdentifier(item.accessibilityIdentifier)
            cell.setOn(viewModel.localNetworkSharing, animated: true)
            cell.setSwitchEnabled(viewModel.includeAllNetworks)

        case .dnsSettings:
            guard let cell = cell as? SettingsCell else { return }

            cell.titleLabel.text = NSLocalizedString("DNS settings", comment: "")

            cell.disclosureType = .chevron
            cell.setAccessibilityIdentifier(item.accessibilityIdentifier)

        case .ipOverrides:
            guard let cell = cell as? SettingsCell else { return }

            cell.titleLabel.text = NSLocalizedString("Server IP override", comment: "")

            cell.disclosureType = .chevron
            cell.setAccessibilityIdentifier(item.accessibilityIdentifier)

        case let .wireGuardPort(port):
            guard let cell = cell as? SelectableSettingsCell else { return }

            var portString = NSLocalizedString("Automatic", comment: "")
            if let port {
                portString = String(port)
            }

            cell.titleLabel.text = portString
            cell.accessibilityIdentifier = "\(item.accessibilityIdentifier.asString)"
            cell.applySubCellStyling()

        case .wireGuardCustomPort:
            guard let cell = cell as? SettingsInputCell else { return }

            cell.titleLabel.text = NSLocalizedString("Custom", comment: "")
            cell.textField.placeholder = NSLocalizedString("Port", comment: "")

            cell.textField.setAccessibilityIdentifier(.customWireGuardPortTextField)
            cell.setAccessibilityIdentifier(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("Automatic", comment: "")
            cell.setAccessibilityIdentifier(item.accessibilityIdentifier)
            cell.applySubCellStyling()

        case .wireGuardObfuscationUdpOverTcp:
            guard let cell = cell as? SelectableSettingsDetailsCell else { return }

            cell.titleLabel.text = NSLocalizedString("UDP-over-TCP", comment: "")

            cell.detailTitleLabel.text = String(
                format: NSLocalizedString("Port: %@", comment: ""),
                viewModel.obfuscationUpdOverTcpPort.description
            )

            cell.setAccessibilityIdentifier(item.accessibilityIdentifier)
            cell.detailTitleLabel.setAccessibilityIdentifier(.wireGuardObfuscationUdpOverTcpPort)
            cell.applySubCellStyling()

            cell.buttonAction = { [weak self] in
                self?.delegate?.showDetails(for: .udpOverTcp)
            }

        case .wireGuardObfuscationShadowsocks:
            guard let cell = cell as? SelectableSettingsDetailsCell else { return }

            cell.titleLabel.text = NSLocalizedString("Shadowsocks", comment: "")

            cell.detailTitleLabel.text = String(
                format: NSLocalizedString("Port: %@", comment: ""),
                viewModel.obfuscationShadowsocksPort.description
            )

            cell.setAccessibilityIdentifier(item.accessibilityIdentifier)
            cell.detailTitleLabel.setAccessibilityIdentifier(.wireGuardObfuscationShadowsocksPort)
            cell.applySubCellStyling()

            cell.buttonAction = { [weak self] in
                self?.delegate?.showDetails(for: .wireguardOverShadowsocks)
            }

        case .wireGuardObfuscationQuic:
            guard let cell = cell as? SelectableSettingsCell else { return }

            cell.titleLabel.text = NSLocalizedString("QUIC", comment: "")

            cell.setAccessibilityIdentifier(item.accessibilityIdentifier)
            cell.detailTitleLabel.setAccessibilityIdentifier(.wireGuardObfuscationQuic)
            cell.applySubCellStyling()

        case .wireGuardObfuscationOff:
            guard let cell = cell as? SelectableSettingsCell else { return }

            cell.titleLabel.text = NSLocalizedString("Off", comment: "")
            cell.setAccessibilityIdentifier(item.accessibilityIdentifier)
            cell.applySubCellStyling()

        case let .wireGuardObfuscationPort(port):
            guard let cell = cell as? SelectableSettingsCell else { return }

            let portString = port.description
            cell.titleLabel.text = portString
            cell.accessibilityIdentifier = "\(item.accessibilityIdentifier)\(portString)"
            cell.applySubCellStyling()

        case .quantumResistanceAutomatic:
            guard let cell = cell as? SelectableSettingsCell else { return }

            cell.titleLabel.text = NSLocalizedString("Automatic", comment: "")
            cell.setAccessibilityIdentifier(item.accessibilityIdentifier)
            cell.applySubCellStyling()

        case .quantumResistanceOn:
            guard let cell = cell as? SelectableSettingsCell else { return }

            cell.titleLabel.text = NSLocalizedString("On", comment: "")
            cell.setAccessibilityIdentifier(item.accessibilityIdentifier)
            cell.applySubCellStyling()

        case .quantumResistanceOff:
            guard let cell = cell as? SelectableSettingsCell else { return }

            cell.titleLabel.text = NSLocalizedString("Off", comment: "")
            cell.setAccessibilityIdentifier(item.accessibilityIdentifier)
            cell.applySubCellStyling()
        }
    }
}