Skip to content

Commit f42dcb3

Browse files
committed
Merge branch 'asynchronize-tunnelmanagersetaccount-ios-474'
2 parents d805c6e + 83347b3 commit f42dcb3

10 files changed

+55
-66
lines changed

ios/MullvadVPN/Coordinators/AccountCoordinator.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,8 @@ final class AccountCoordinator: Coordinator, Presentable, Presenting {
142142

143143
let alertPresenter = AlertPresenter(context: self)
144144

145-
interactor.logout {
145+
Task {
146+
await interactor.logout()
146147
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1)) { [weak self] in
147148
guard let self else { return }
148149

ios/MullvadVPN/Coordinators/ApplicationCoordinator.swift

+4-2
Original file line numberDiff line numberDiff line change
@@ -342,8 +342,10 @@ final class ApplicationCoordinator: Coordinator, Presenting, RootContainerViewCo
342342
}
343343

344344
private func logoutRevokedDevice() {
345-
tunnelManager.unsetAccount { [weak self] in
346-
self?.continueFlow(animated: true)
345+
Task { [weak self] in
346+
guard let self else { return }
347+
await tunnelManager.unsetAccount()
348+
continueFlow(animated: true)
347349
}
348350
}
349351

ios/MullvadVPN/TunnelManager/TunnelManager.swift

+15-21
Original file line numberDiff line numberDiff line change
@@ -311,19 +311,12 @@ final class TunnelManager: StorePaymentObserver {
311311
operationQueue.addOperation(operation)
312312
}
313313

314-
func setNewAccount(completion: @escaping (Result<StoredAccountData, Error>) -> Void) {
315-
setAccount(action: .new) { result in
316-
completion(result.map { $0! })
317-
}
314+
func setNewAccount() async throws -> StoredAccountData {
315+
try await setAccount(action: .new)!
318316
}
319317

320-
func setExistingAccount(
321-
accountNumber: String,
322-
completion: @escaping (Result<StoredAccountData, Error>) -> Void
323-
) {
324-
setAccount(action: .existing(accountNumber)) { result in
325-
completion(result.map { $0! })
326-
}
318+
func setExistingAccount(accountNumber: String) async throws -> StoredAccountData {
319+
try await setAccount(action: .existing(accountNumber))!
327320
}
328321

329322
private func setAccount(
@@ -374,12 +367,18 @@ final class TunnelManager: StorePaymentObserver {
374367
operationQueue.addOperation(operation)
375368
}
376369

377-
func unsetAccount(completionHandler: @escaping () -> Void) {
378-
setAccount(action: .unset) { _ in
379-
completionHandler()
370+
private func setAccount(action: SetAccountAction) async throws -> StoredAccountData? {
371+
try await withCheckedThrowingContinuation { continuation in
372+
setAccount(action: action) { result in
373+
continuation.resume(with: result)
374+
}
380375
}
381376
}
382377

378+
func unsetAccount() async {
379+
_ = try? await setAccount(action: .unset)
380+
}
381+
383382
func updateAccountData(_ completionHandler: ((Error?) -> Void)? = nil) {
384383
let operation = UpdateAccountDataOperation(
385384
dispatchQueue: internalQueue,
@@ -435,13 +434,8 @@ final class TunnelManager: StorePaymentObserver {
435434
return operation
436435
}
437436

438-
func deleteAccount(
439-
accountNumber: String,
440-
completion: ((Error?) -> Void)? = nil
441-
) {
442-
setAccount(action: .delete(accountNumber)) { result in
443-
completion?(result.error)
444-
}
437+
func deleteAccount(accountNumber: String) async throws {
438+
_ = try await setAccount(action: .delete(accountNumber))
445439
}
446440

447441
func updateDeviceData(_ completionHandler: ((Error?) -> Void)? = nil) {

ios/MullvadVPN/View controllers/Account/AccountInteractor.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ final class AccountInteractor {
5353
tunnelManager.deviceState
5454
}
5555

56-
func logout(_ completion: @escaping () -> Void) {
57-
tunnelManager.unsetAccount(completionHandler: completion)
56+
func logout() async {
57+
await tunnelManager.unsetAccount()
5858
}
5959

6060
func addPayment(_ payment: SKPayment, for accountNumber: String) {

ios/MullvadVPN/View controllers/AccountDeletion/AccountDeletionInteractor.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class AccountDeletionInteractor {
4848
}
4949
}
5050

51-
func delete(accountNumber: String, completionHandler: @escaping (Error?) -> Void) {
52-
tunnelManager.deleteAccount(accountNumber: accountNumber, completion: completionHandler)
51+
func delete(accountNumber: String) async throws {
52+
try await tunnelManager.deleteAccount(accountNumber: accountNumber)
5353
}
5454
}

ios/MullvadVPN/View controllers/AccountDeletion/AccountDeletionViewController.swift

+5-4
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,15 @@ class AccountDeletionViewController: UIViewController {
6262

6363
private func submit(accountNumber: String) {
6464
contentView.state = .loading
65-
interactor.delete(accountNumber: accountNumber) { [weak self] error in
65+
Task { [weak self] in
6666
guard let self else { return }
67-
guard let error else {
67+
do {
68+
try await interactor.delete(accountNumber: accountNumber)
6869
self.contentView.state = .initial
6970
self.delegate?.deleteAccountDidSucceed(controller: self)
70-
return
71+
} catch {
72+
self.contentView.state = .failure(error)
7173
}
72-
self.contentView.state = .failure(error)
7374
}
7475
}
7576
}

ios/MullvadVPN/View controllers/Login/LoginInteractor.swift

+4-9
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,12 @@ final class LoginInteractor {
2121
self.tunnelManager = tunnelManager
2222
}
2323

24-
func setAccount(accountNumber: String, completion: @escaping (Error?) -> Void) {
25-
tunnelManager.setExistingAccount(accountNumber: accountNumber) { result in
26-
completion(result.error)
27-
}
24+
func setAccount(accountNumber: String) async throws {
25+
_ = try await tunnelManager.setExistingAccount(accountNumber: accountNumber)
2826
}
2927

30-
func createAccount(completion: @escaping (Result<String, Error>) -> Void) {
31-
tunnelManager.setNewAccount { [weak self] result in
32-
self?.didCreateAccount?()
33-
completion(result.map { $0.number })
34-
}
28+
func createAccount() async throws -> String {
29+
try await tunnelManager.setNewAccount().number
3530
}
3631

3732
func getLastUsedAccount() -> String? {

ios/MullvadVPN/View controllers/Login/LoginViewController.swift

+12-13
Original file line numberDiff line numberDiff line change
@@ -193,20 +193,19 @@ class LoginViewController: UIViewController, RootContainment {
193193

194194
func start(action: LoginAction) {
195195
beginLogin(action)
196-
197-
switch action {
198-
case .createAccount:
199-
interactor.createAccount { [weak self] result in
200-
if let newAccountNumber = result.value {
201-
self?.contentView.accountInputGroup.setAccount(newAccountNumber)
196+
Task { [weak self] in
197+
guard let self else { return }
198+
do {
199+
switch action {
200+
case .createAccount:
201+
self.contentView.accountInputGroup.setAccount(try await interactor.createAccount())
202+
203+
case let .useExistingAccount(accountNumber):
204+
try await interactor.setAccount(accountNumber: accountNumber)
202205
}
203-
204-
self?.endLogin(action: action, error: result.error)
205-
}
206-
207-
case let .useExistingAccount(accountNumber):
208-
interactor.setAccount(accountNumber: accountNumber) { [weak self] error in
209-
self?.endLogin(action: action, error: error)
206+
self.endLogin(action: action, error: nil)
207+
} catch {
208+
self.endLogin(action: action, error: error)
210209
}
211210
}
212211
}

ios/MullvadVPN/View controllers/RedeemVoucher/RedeemVoucherInteractor.swift

+4-10
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,10 @@ final class RedeemVoucherInteractor {
4646
})
4747
}
4848

49-
func logout(completionHandler: @escaping () -> Void) {
50-
preferredAccountNumber.flatMap { accountNumber in
51-
tunnelManager.unsetAccount { [weak self] in
52-
guard let self else {
53-
return
54-
}
55-
completionHandler()
56-
didLogout?(accountNumber)
57-
}
58-
}
49+
func logout() async {
50+
guard let accountNumber = preferredAccountNumber else { return }
51+
await tunnelManager.unsetAccount()
52+
didLogout?(accountNumber)
5953
}
6054

6155
func cancelAll() {

ios/MullvadVPN/View controllers/RedeemVoucher/RedeemVoucherViewController.swift

+5-2
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,11 @@ class RedeemVoucherViewController: UIViewController, UINavigationControllerDeleg
138138

139139
contentView.state = .logout
140140

141-
interactor.logout { [weak self] in
142-
self?.contentView.state = .initial
141+
Task {
142+
[weak self] in
143+
guard let self else { return }
144+
await interactor.logout()
145+
contentView.state = .initial
143146
}
144147
}
145148
}

0 commit comments

Comments
 (0)