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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
|
//
// LoginViewController.swift
// MullvadVPN
//
// Created by pronebird on 19/03/2019.
// Copyright © 2025 Mullvad VPN AB. All rights reserved.
//
import MullvadLogging
import MullvadTypes
import Operations
import UIKit
enum LoginState {
case `default`
case authenticating(LoginAction)
case failure(LoginAction, Error)
case success(LoginAction)
}
enum LoginAction {
case useExistingAccount(String)
case createAccount
}
enum EndLoginAction {
/// Do nothing.
case nothing
/// Set focus on account text field.
case activateTextField
/// Wait for promise before showing login error.
case wait(Promise<Void, Error>)
}
class LoginViewController: UIViewController, RootContainment {
private lazy var contentView: LoginContentView = {
let view = LoginContentView(frame: self.view.bounds)
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
private lazy var accountInputAccessoryCancelButton = UIBarButtonItem(
barButtonSystemItem: .cancel,
target: self,
action: #selector(cancelLogin)
)
private lazy var accountInputAccessoryLoginButton: UIBarButtonItem = {
let barButtonItem = UIBarButtonItem(
title: NSLocalizedString("Login", comment: ""),
style: .done,
target: self,
action: #selector(doLogin)
)
barButtonItem.setAccessibilityIdentifier(.loginBarButton)
return barButtonItem
}()
private lazy var accountInputAccessoryToolbar: UIToolbar = {
let toolbar = UIToolbar(frame: CGRect(x: 0, y: 0, width: 320, height: 44))
toolbar.items = [
self.accountInputAccessoryCancelButton,
UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil),
self.accountInputAccessoryLoginButton,
]
toolbar.sizeToFit()
return toolbar
}()
private let logger = Logger(label: "LoginViewController")
private var loginState = LoginState.default {
didSet {
loginStateDidChange()
}
}
private var canBeginLogin: Bool {
contentView.accountInputGroup.satisfiesMinimumTokenLengthRequirement
}
var prefersDeviceInfoBarHidden: Bool {
true
}
private let interactor: LoginInteractor
var didFinishLogin: ((LoginAction, Error?) -> EndLoginAction)?
override var preferredStatusBarStyle: UIStatusBarStyle {
.lightContent
}
var preferredHeaderBarPresentation: HeaderBarPresentation {
HeaderBarPresentation(style: .transparent, showsDivider: false)
}
var prefersHeaderBarHidden: Bool {
false
}
private let alertPresenter: AlertPresenter
init(interactor: LoginInteractor, 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()
view.addSubview(contentView)
NSLayoutConstraint.activate([
contentView.topAnchor.constraint(equalTo: view.topAnchor),
contentView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
contentView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
contentView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
])
updateLastUsedAccount()
contentView.accountInputGroup.didRemoveLastUsedAccount = { [weak self] in
self?.showLastUsedAccountRemovalWarning()
}
contentView.accountInputGroup.didEnterAccount = { [weak self] in
self?.attemptLogin()
}
interactor.suggestPreferredAccountNumber = { [weak self] value in
Task { @MainActor in
self?.contentView.accountInputGroup.setAccount(value)
}
}
contentView.accountInputGroup.setOnReturnKey { [weak self] _ in
guard let self else { return true }
return attemptLogin()
}
// There is no need to set the input accessory toolbar on iPad since it has a dedicated
// button to dismiss the keyboard.
if case .phone = UIDevice.current.userInterfaceIdiom {
contentView.accountInputGroup.textField.inputAccessoryView = accountInputAccessoryToolbar
} else {
contentView.accountInputGroup.textField.inputAccessoryView = nil
}
updateDisplayedMessage()
updateStatusIcon()
updateKeyboardToolbar()
let notificationCenter = NotificationCenter.default
contentView.createAccountButton.addTarget(
self,
action: #selector(createNewAccount),
for: .touchUpInside
)
notificationCenter.addObserver(
self,
selector: #selector(textDidChange(_:)),
name: UITextField.textDidChangeNotification,
object: contentView.accountInputGroup.textField
)
}
// MARK: - Public
func start(action: LoginAction) {
beginLogin(action)
Task { [weak self] in
guard let self else { return }
do {
switch action {
case .createAccount:
self.contentView.accountInputGroup.setAccount(try await interactor.createAccount())
case let .useExistingAccount(accountNumber):
try await interactor.setAccount(accountNumber: accountNumber)
}
self.endLogin(action: action, error: nil)
} catch {
self.endLogin(action: action, error: error)
}
}
}
func reset() {
contentView.accountInputGroup.clearAccount()
loginState = .default
updateKeyboardToolbar()
updateLastUsedAccount()
}
// MARK: - UITextField notifications
@objc func textDidChange(_ notification: Notification) {
// Reset the text style as user start typing
if case .failure = loginState {
loginState = .default
}
// Enable the log in button in the keyboard toolbar.
updateKeyboardToolbar()
// Update "create account" button state.
updateCreateButtonEnabled()
}
// MARK: - Actions
@objc private func cancelLogin() {
view.endEditing(true)
}
@objc private func doLogin() {
let accountNumber = contentView.accountInputGroup.parsedToken
start(action: .useExistingAccount(accountNumber))
}
@objc private func createNewAccount() {
if interactor.hasLastAccountNumber {
let message = NSMutableAttributedString(
markdownString: [
NSLocalizedString(
"You already have a saved account number, by creating a new account the "
+ "saved account number will be removed from this device. This cannot be undone.",
comment: ""
),
NSLocalizedString("Do you want to create a new account?", comment: ""),
].joinedParagraphs(lineBreaks: 1),
options: MarkdownStylingOptions(font: .preferredFont(forTextStyle: .body))
)
let presentation = AlertPresentation(
id: "create-account-confirmation-dialog",
icon: .info,
attributedMessage: message,
buttons: [
AlertAction(
title: NSLocalizedString("Create new account", comment: ""),
style: .default,
accessibilityId: .createAccountConfirmationButton,
handler: {
self.start(action: .createAccount)
}
),
AlertAction(
title: NSLocalizedString("Cancel", comment: ""),
style: .default,
accessibilityId: .createAccountCancelButton
),
]
)
alertPresenter.showAlert(presentation: presentation, animated: true)
} else {
start(action: .createAccount)
}
}
// MARK: - Private
private func showLastUsedAccountRemovalWarning() {
let message = NSMutableAttributedString(
markdownString: NSLocalizedString(
"""
Removing the saved account number from this device cannot be undone.
Do you want to remove the saved account number?
""",
comment: ""
),
options: MarkdownStylingOptions(font: .preferredFont(forTextStyle: .body))
)
let presentation = AlertPresentation(
id: "remove-saved-account-number-dialog",
icon: .info,
attributedMessage: message,
buttons: [
AlertAction(
title: NSLocalizedString("Remove", comment: ""),
style: .destructive,
accessibilityId: .removeLastUsedAccountButton,
handler: {
self.interactor.removeLastUsedAccount()
self.contentView.accountInputGroup.setLastUsedAccount(
nil,
animated: true
)
}
),
AlertAction(
title: NSLocalizedString("Cancel", comment: ""),
style: .default,
accessibilityId: .cancelRemoveLastUsedAccountButton
),
]
)
self.alertPresenter.showAlert(presentation: presentation, animated: true)
}
private func updateLastUsedAccount() {
contentView.accountInputGroup.setLastUsedAccount(
interactor.getLastUsedAccount(),
animated: false
)
}
private func loginStateDidChange() {
contentView.accountInputGroup.setLoginState(loginState, animated: true)
updateDisplayedMessage()
updateStatusIcon()
updateCreateButtonEnabled()
}
private func updateStatusIcon() {
contentView.statusActivityView.state = loginState.statusActivityState
}
private func beginLogin(_ action: LoginAction) {
loginState = .authenticating(action)
view.endEditing(true)
}
private func endLogin(action: LoginAction, error: Error?) {
let nextLoginState: LoginState = error.map { .failure(action, $0) } ?? .success(action)
let endAction = didFinishLogin?(action, error) ?? .nothing
switch endAction {
case .activateTextField:
contentView.accountInputGroup.textField.becomeFirstResponder()
loginState = nextLoginState
case .nothing:
loginState = nextLoginState
case let .wait(promise):
promise.observe { result in
self.loginState = result.error.map { .failure(action, $0) } ?? nextLoginState
}
}
}
private func updateDisplayedMessage() {
contentView.titleLabel.text = loginState.localizedTitle
contentView.messageLabel.text = loginState.localizedMessage
}
private func updateKeyboardToolbar() {
accountInputAccessoryLoginButton.isEnabled = canBeginLogin
}
private func updateCreateButtonEnabled() {
let isEnabled: Bool
switch loginState {
case .failure, .default:
isEnabled = true
case .success, .authenticating:
isEnabled = false
}
contentView.createAccountButton.isEnabled = isEnabled
}
@discardableResult private func attemptLogin() -> Bool {
if canBeginLogin {
doLogin()
return true
} else {
return false
}
}
}
/// Private extension that brings localizable messages displayed in the Login view controller
private extension LoginState {
var localizedTitle: String {
switch self {
case .default:
return NSLocalizedString("Login", comment: "")
case .authenticating:
return NSLocalizedString("Logging in...", comment: "")
case .failure:
return NSLocalizedString("Login failed", comment: "")
case .success:
return NSLocalizedString("Logged in", comment: "")
}
}
var localizedMessage: String {
switch self {
case .default:
return NSLocalizedString("Enter your account number", comment: "")
case let .authenticating(method):
switch method {
case .useExistingAccount:
return NSLocalizedString("Checking account number", comment: "")
case .createAccount:
return NSLocalizedString("Creating account...", comment: "")
}
case let .failure(_, error):
return (error as? DisplayError)?.displayErrorDescription ?? error.localizedDescription
case let .success(method):
switch method {
case .useExistingAccount:
return NSLocalizedString("Valid account number", comment: "")
case .createAccount:
return NSLocalizedString("Account created", comment: "")
}
}
}
var statusActivityState: StatusActivityView.State {
switch self {
case .failure:
return .failure
case .success:
return .success
case .authenticating:
return .activity
case .default:
return .hidden
}
}
} // swiftlint:disable:this file_length
|