-
Notifications
You must be signed in to change notification settings - Fork 384
/
Copy pathAccountCoordinator.swift
230 lines (200 loc) · 7.62 KB
/
AccountCoordinator.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
//
// AccountCoordinator.swift
// MullvadVPN
//
// Created by Jon Petersson on 2023-04-14.
// Copyright © 2023 Mullvad VPN AB. All rights reserved.
//
import Routing
import UIKit
enum AccountDismissReason: Equatable {
case none
case userLoggedOut
case accountDeletion
}
enum AddedMoreCreditOption: Equatable {
case redeemingVoucher
case inAppPurchase
}
final class AccountCoordinator: Coordinator, Presentable, Presenting {
private let interactor: AccountInteractor
private var accountController: AccountViewController?
let navigationController: UINavigationController
var presentedViewController: UIViewController {
navigationController
}
var didFinish: ((AccountCoordinator, AccountDismissReason) -> Void)?
init(
navigationController: UINavigationController,
interactor: AccountInteractor
) {
self.navigationController = navigationController
self.interactor = interactor
}
func start(animated: Bool) {
navigationController.navigationBar.prefersLargeTitles = true
let accountController = AccountViewController(
interactor: interactor,
errorPresenter: PaymentAlertPresenter(alertContext: self)
)
accountController.actionHandler = handleViewControllerAction
navigationController.pushViewController(accountController, animated: animated)
self.accountController = accountController
}
private func handleViewControllerAction(_ action: AccountViewControllerAction) {
switch action {
case .deviceInfo:
showAccountDeviceInfo()
case .finish:
didFinish?(self, .none)
case .logOut:
logOut()
case .navigateToVoucher:
navigateToRedeemVoucher()
case .navigateToDeleteAccount:
navigateToDeleteAccount()
case .restorePurchasesInfo:
showRestorePurchasesInfo()
}
}
private func navigateToRedeemVoucher() {
let coordinator = ProfileVoucherCoordinator(
navigationController: CustomNavigationController(),
interactor: RedeemVoucherInteractor(
tunnelManager: interactor.tunnelManager,
accountsProxy: interactor.accountsProxy,
verifyVoucherAsAccount: false
)
)
coordinator.didFinish = { coordinator in
coordinator.dismiss(animated: true)
}
coordinator.didCancel = { coordinator in
coordinator.dismiss(animated: true)
}
coordinator.start()
presentChild(
coordinator,
animated: true,
configuration: ModalPresentationConfiguration(
preferredContentSize: UIMetrics.SettingsRedeemVoucher.preferredContentSize,
modalPresentationStyle: .custom,
transitioningDelegate: FormSheetTransitioningDelegate(options: FormSheetPresentationOptions(
useFullScreenPresentationInCompactWidth: false,
adjustViewWhenKeyboardAppears: true
))
)
)
}
private func navigateToDeleteAccount() {
let coordinator = AccountDeletionCoordinator(
navigationController: CustomNavigationController(),
interactor: AccountDeletionInteractor(tunnelManager: interactor.tunnelManager)
)
coordinator.start()
coordinator.didCancel = { accountDeletionCoordinator in
accountDeletionCoordinator.dismiss(animated: true)
}
coordinator.didFinish = { accountDeletionCoordinator in
accountDeletionCoordinator.dismiss(animated: true) {
self.didFinish?(self, .userLoggedOut)
}
}
presentChild(
coordinator,
animated: true,
configuration: ModalPresentationConfiguration(
preferredContentSize: UIMetrics.AccountDeletion.preferredContentSize,
modalPresentationStyle: .custom,
transitioningDelegate: FormSheetTransitioningDelegate(options: FormSheetPresentationOptions(
useFullScreenPresentationInCompactWidth: true,
adjustViewWhenKeyboardAppears: false
))
)
)
}
// MARK: - Alerts
private func logOut() {
let presentation = AlertPresentation(
id: "account-logout-alert",
accessibilityIdentifier: .logOutSpinnerAlertView,
icon: .spinner,
message: nil,
buttons: []
)
let alertPresenter = AlertPresenter(context: self)
Task {
await interactor.logout()
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1)) { [weak self] in
guard let self else { return }
alertPresenter.dismissAlert(presentation: presentation, animated: true)
self.didFinish?(self, .userLoggedOut)
}
}
alertPresenter.showAlert(presentation: presentation, animated: true)
}
private func showAccountDeviceInfo() {
let message = NSLocalizedString(
"DEVICE_INFO_DIALOG_MESSAGE_PART_1",
tableName: "Account",
value: """
This is the name assigned to the device. Each device logged in on a Mullvad account gets a unique name \
that helps you identify it when you manage your devices in the app or on the website.
You can have up to 5 devices logged in on one Mullvad account.
If you log out, the device and the device name is removed. When \
you log back in again, the device will get a new name.
""",
comment: ""
)
let presentation = AlertPresentation(
id: "account-device-info-alert",
icon: .info,
message: message,
buttons: [AlertAction(
title: NSLocalizedString(
"DEVICE_INFO_DIALOG_OK_ACTION",
tableName: "Account",
value: "Got it!",
comment: ""
),
style: .default
)]
)
let presenter = AlertPresenter(context: self)
presenter.showAlert(presentation: presentation, animated: true)
}
private func showRestorePurchasesInfo() {
let message = NSLocalizedString(
"RESTORE_PURCHASES_DIALOG_MESSAGE",
tableName: "Account",
value: """
You can use the “restore purchases” function to check for any in-app payments \
made via Apple services. If there is a payment that has not been credited, it will \
add the time to the currently logged in Mullvad account.
""",
comment: ""
)
let presentation = AlertPresentation(
id: "account-device-info-alert",
icon: .info,
title: NSLocalizedString(
"RESTORE_PURCHASES_DIALOG_TITLE",
tableName: "Account",
value: "If you haven’t received additional VPN time after purchasing",
comment: ""
),
message: message,
buttons: [AlertAction(
title: NSLocalizedString(
"RESTORE_PURCHASES_DIALOG_OK_ACTION",
tableName: "Account",
value: "Got it!",
comment: ""
),
style: .default
)]
)
let presenter = AlertPresenter(context: self)
presenter.showAlert(presentation: presentation, animated: true)
}
}