-
Notifications
You must be signed in to change notification settings - Fork 384
/
Copy pathIPOverrideViewController.swift
248 lines (214 loc) · 8.54 KB
/
IPOverrideViewController.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
//
// IPOverrideViewController.swift
// MullvadVPN
//
// Created by Jon Petersson on 2024-01-15.
// Copyright © 2024 Mullvad VPN AB. All rights reserved.
//
import Combine
import UIKit
class IPOverrideViewController: UIViewController {
private let interactor: IPOverrideInteractor
private var cancellables = Set<AnyCancellable>()
private let alertPresenter: AlertPresenter
weak var delegate: IPOverrideViewControllerDelegate?
private lazy var containerView: UIStackView = {
let view = UIStackView()
view.axis = .vertical
view.spacing = 20
return view
}()
private lazy var clearButton: AppButton = {
let button = AppButton(style: .danger)
button.addTarget(self, action: #selector(didTapClearButton), for: .touchUpInside)
button.setTitle(NSLocalizedString(
"IP_OVERRIDE_CLEAR_BUTTON",
tableName: "IPOverride",
value: "Clear all overrides",
comment: ""
), for: .normal)
return button
}()
private let statusView = IPOverrideStatusView()
init(interactor: IPOverrideInteractor, alertPresenter: AlertPresenter) {
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()
navigationController?.navigationItem.largeTitleDisplayMode = .never
view.backgroundColor = .secondaryColor
addHeader()
addPreamble()
addImportButtons()
addStatusLabel()
view.addConstrainedSubviews([containerView, clearButton]) {
containerView.pinEdgesToSuperviewMargins(.all().excluding(.bottom))
clearButton.pinEdgesToSuperviewMargins(PinnableEdges([.leading(0), .trailing(0), .bottom(16)]))
}
interactor.statusPublisher.sink { [weak self] status in
self?.statusView.setStatus(status)
self?.clearButton.isEnabled = self?.interactor.defaultStatus == .active
}.store(in: &cancellables)
}
private func addHeader() {
let label = UILabel()
label.font = .systemFont(ofSize: 32, weight: .bold)
label.textColor = .white
label.text = NSLocalizedString(
"IP_OVERRIDE_HEADER",
tableName: "IPOverride",
value: "Server IP override",
comment: ""
)
let infoButton = UIButton(type: .custom)
infoButton.tintColor = .white
infoButton.setImage(UIImage(resource: .iconInfo), for: .normal)
infoButton.addTarget(self, action: #selector(didTapInfoButton), for: .touchUpInside)
infoButton.heightAnchor.constraint(equalToConstant: 24).isActive = true
infoButton.widthAnchor.constraint(equalTo: infoButton.heightAnchor, multiplier: 1).isActive = true
let headerView = UIStackView(arrangedSubviews: [label, infoButton, UIView()])
headerView.spacing = 8
containerView.addArrangedSubview(headerView)
containerView.setCustomSpacing(14, after: headerView)
}
private func addPreamble() {
let label = UILabel()
label.font = .systemFont(ofSize: 12, weight: .semibold)
label.textColor = .white.withAlphaComponent(0.6)
label.numberOfLines = 0
label.text = NSLocalizedString(
"IP_OVERRIDE_PREAMBLE",
tableName: "IPOverride",
value: "Import files or text with new IP addresses for the servers in the Select location view.",
comment: ""
)
containerView.addArrangedSubview(label)
}
private func addImportButtons() {
let importTextButton = AppButton(style: .default)
importTextButton.addTarget(self, action: #selector(didTapImportTextButton), for: .touchUpInside)
importTextButton.setTitle(NSLocalizedString(
"IP_OVERRIDE_IMPORT_TEXT_BUTTON",
tableName: "IPOverride",
value: "Import via text",
comment: ""
), for: .normal)
let importFileButton = AppButton(style: .default)
importFileButton.addTarget(self, action: #selector(didTapImportFileButton), for: .touchUpInside)
importFileButton.setTitle(NSLocalizedString(
"IP_OVERRIDE_IMPORT_FILE_BUTTON",
tableName: "IPOverride",
value: "Import file",
comment: ""
), for: .normal)
let stackView = UIStackView(arrangedSubviews: [importTextButton, importFileButton])
stackView.distribution = .fillEqually
stackView.spacing = 12
containerView.addArrangedSubview(stackView)
}
private func addStatusLabel() {
containerView.addArrangedSubview(statusView)
}
@objc private func didTapInfoButton() {
let message = NSLocalizedString(
"IP_OVERRIDE_DIALOG_MESSAGE",
tableName: "IPOverride",
value: """
On some networks, where various types of censorship are being used, our server IP addresses are \
sometimes blocked.
To circumvent this you can import a file or a text, provided by our support team, \
with new IP addresses that override the default addresses of the servers in the Select location view.
If you are having issues connecting to VPN servers, please contact support.
""",
comment: ""
)
let presentation = AlertPresentation(
id: "ip-override-info-alert",
icon: .info,
title: NSLocalizedString(
"IP_OVERRIDE_INFO_DIALOG_TITLE",
tableName: "IPOverride",
value: "Server IP override",
comment: ""
),
message: message,
buttons: [AlertAction(
title: NSLocalizedString(
"IP_OVERRIDE_INFO_DIALOG_OK_BUTTON",
tableName: "IPOverride",
value: "Got it!",
comment: ""
),
style: .default
)]
)
alertPresenter.showAlert(presentation: presentation, animated: true)
}
@objc private func didTapClearButton() {
let presentation = AlertPresentation(
id: "ip-override-clear-alert",
icon: .alert,
title: NSLocalizedString(
"IP_OVERRIDE_CLEAR_DIALOG_TITLE",
tableName: "IPOverride",
value: "Clear all overrides?",
comment: ""
),
message: NSLocalizedString(
"IP_OVERRIDE_CLEAR_DIALOG_MESSAGE",
tableName: "IPOverride",
value: """
Clearing the imported overrides changes the server IPs, in the Select location view, \
back to default.
""",
comment: ""
),
buttons: [
AlertAction(
title: NSLocalizedString(
"IP_OVERRIDE_CLEAR_DIALOG_CLEAR_BUTTON",
tableName: "IPOverride",
value: "Clear",
comment: ""
),
style: .destructive,
handler: { [weak self] in
self?.interactor.deleteAllOverrides()
}
),
AlertAction(
title: NSLocalizedString(
"IP_OVERRIDE_CLEAR_DIALOG_CANCEL_BUTTON",
tableName: "IPOverride",
value: "Cancel",
comment: ""
),
style: .default
),
]
)
alertPresenter.showAlert(presentation: presentation, animated: true)
}
@objc private func didTapImportTextButton() {
delegate?.presentImportTextController()
}
@objc private func didTapImportFileButton() {
let documentPicker = UIDocumentPickerViewController(forOpeningContentTypes: [.json, .text])
documentPicker.delegate = self
present(documentPicker, animated: true)
}
}
extension IPOverrideViewController: UIDocumentPickerDelegate {
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
if let url = urls.first {
url.securelyScoped { [weak self] scopedUrl in
scopedUrl.flatMap { self?.interactor.import(url: $0) }
}
}
}
}