diff --git a/ios/MullvadVPN/Coordinators/AccountCoordinator.swift b/ios/MullvadVPN/Coordinators/AccountCoordinator.swift index 3c58e3528740..cb4ce984782c 100644 --- a/ios/MullvadVPN/Coordinators/AccountCoordinator.swift +++ b/ios/MullvadVPN/Coordinators/AccountCoordinator.swift @@ -142,7 +142,8 @@ final class AccountCoordinator: Coordinator, Presentable, Presenting { let alertPresenter = AlertPresenter(context: self) - interactor.logout { + Task { + await interactor.logout() DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1)) { [weak self] in guard let self else { return } diff --git a/ios/MullvadVPN/Coordinators/ApplicationCoordinator.swift b/ios/MullvadVPN/Coordinators/ApplicationCoordinator.swift index 719c97066af3..ed61054537fa 100644 --- a/ios/MullvadVPN/Coordinators/ApplicationCoordinator.swift +++ b/ios/MullvadVPN/Coordinators/ApplicationCoordinator.swift @@ -342,8 +342,10 @@ final class ApplicationCoordinator: Coordinator, Presenting, RootContainerViewCo } private func logoutRevokedDevice() { - tunnelManager.unsetAccount { [weak self] in - self?.continueFlow(animated: true) + Task { [weak self] in + guard let self else { return } + await tunnelManager.unsetAccount() + continueFlow(animated: true) } } diff --git a/ios/MullvadVPN/TunnelManager/TunnelManager.swift b/ios/MullvadVPN/TunnelManager/TunnelManager.swift index 960cfd2a8af5..b0c55cdfad37 100644 --- a/ios/MullvadVPN/TunnelManager/TunnelManager.swift +++ b/ios/MullvadVPN/TunnelManager/TunnelManager.swift @@ -311,19 +311,12 @@ final class TunnelManager: StorePaymentObserver { operationQueue.addOperation(operation) } - func setNewAccount(completion: @escaping (Result) -> Void) { - setAccount(action: .new) { result in - completion(result.map { $0! }) - } + func setNewAccount() async throws -> StoredAccountData { + try await setAccount(action: .new)! } - func setExistingAccount( - accountNumber: String, - completion: @escaping (Result) -> Void - ) { - setAccount(action: .existing(accountNumber)) { result in - completion(result.map { $0! }) - } + func setExistingAccount(accountNumber: String) async throws -> StoredAccountData { + try await setAccount(action: .existing(accountNumber))! } private func setAccount( @@ -374,12 +367,18 @@ final class TunnelManager: StorePaymentObserver { operationQueue.addOperation(operation) } - func unsetAccount(completionHandler: @escaping () -> Void) { - setAccount(action: .unset) { _ in - completionHandler() + private func setAccount(action: SetAccountAction) async throws -> StoredAccountData? { + try await withCheckedThrowingContinuation { continuation in + setAccount(action: action) { result in + continuation.resume(with: result) + } } } + func unsetAccount() async { + _ = try? await setAccount(action: .unset) + } + func updateAccountData(_ completionHandler: ((Error?) -> Void)? = nil) { let operation = UpdateAccountDataOperation( dispatchQueue: internalQueue, @@ -435,13 +434,8 @@ final class TunnelManager: StorePaymentObserver { return operation } - func deleteAccount( - accountNumber: String, - completion: ((Error?) -> Void)? = nil - ) { - setAccount(action: .delete(accountNumber)) { result in - completion?(result.error) - } + func deleteAccount(accountNumber: String) async throws { + _ = try await setAccount(action: .delete(accountNumber)) } func updateDeviceData(_ completionHandler: ((Error?) -> Void)? = nil) { diff --git a/ios/MullvadVPN/View controllers/Account/AccountInteractor.swift b/ios/MullvadVPN/View controllers/Account/AccountInteractor.swift index 05d2d4046507..f5cc3a77a52b 100644 --- a/ios/MullvadVPN/View controllers/Account/AccountInteractor.swift +++ b/ios/MullvadVPN/View controllers/Account/AccountInteractor.swift @@ -53,8 +53,8 @@ final class AccountInteractor { tunnelManager.deviceState } - func logout(_ completion: @escaping () -> Void) { - tunnelManager.unsetAccount(completionHandler: completion) + func logout() async { + await tunnelManager.unsetAccount() } func addPayment(_ payment: SKPayment, for accountNumber: String) { diff --git a/ios/MullvadVPN/View controllers/AccountDeletion/AccountDeletionInteractor.swift b/ios/MullvadVPN/View controllers/AccountDeletion/AccountDeletionInteractor.swift index 1562c013b835..f0cf80ab8709 100644 --- a/ios/MullvadVPN/View controllers/AccountDeletion/AccountDeletionInteractor.swift +++ b/ios/MullvadVPN/View controllers/AccountDeletion/AccountDeletionInteractor.swift @@ -48,7 +48,7 @@ class AccountDeletionInteractor { } } - func delete(accountNumber: String, completionHandler: @escaping (Error?) -> Void) { - tunnelManager.deleteAccount(accountNumber: accountNumber, completion: completionHandler) + func delete(accountNumber: String) async throws { + try await tunnelManager.deleteAccount(accountNumber: accountNumber) } } diff --git a/ios/MullvadVPN/View controllers/AccountDeletion/AccountDeletionViewController.swift b/ios/MullvadVPN/View controllers/AccountDeletion/AccountDeletionViewController.swift index cea4095b75c1..6479134b1426 100644 --- a/ios/MullvadVPN/View controllers/AccountDeletion/AccountDeletionViewController.swift +++ b/ios/MullvadVPN/View controllers/AccountDeletion/AccountDeletionViewController.swift @@ -62,14 +62,15 @@ class AccountDeletionViewController: UIViewController { private func submit(accountNumber: String) { contentView.state = .loading - interactor.delete(accountNumber: accountNumber) { [weak self] error in + Task { [weak self] in guard let self else { return } - guard let error else { + do { + try await interactor.delete(accountNumber: accountNumber) self.contentView.state = .initial self.delegate?.deleteAccountDidSucceed(controller: self) - return + } catch { + self.contentView.state = .failure(error) } - self.contentView.state = .failure(error) } } } diff --git a/ios/MullvadVPN/View controllers/Login/LoginInteractor.swift b/ios/MullvadVPN/View controllers/Login/LoginInteractor.swift index 4ec56074b1f8..0f69fe96fdbb 100644 --- a/ios/MullvadVPN/View controllers/Login/LoginInteractor.swift +++ b/ios/MullvadVPN/View controllers/Login/LoginInteractor.swift @@ -21,17 +21,12 @@ final class LoginInteractor { self.tunnelManager = tunnelManager } - func setAccount(accountNumber: String, completion: @escaping (Error?) -> Void) { - tunnelManager.setExistingAccount(accountNumber: accountNumber) { result in - completion(result.error) - } + func setAccount(accountNumber: String) async throws { + _ = try await tunnelManager.setExistingAccount(accountNumber: accountNumber) } - func createAccount(completion: @escaping (Result) -> Void) { - tunnelManager.setNewAccount { [weak self] result in - self?.didCreateAccount?() - completion(result.map { $0.number }) - } + func createAccount() async throws -> String { + try await tunnelManager.setNewAccount().number } func getLastUsedAccount() -> String? { diff --git a/ios/MullvadVPN/View controllers/Login/LoginViewController.swift b/ios/MullvadVPN/View controllers/Login/LoginViewController.swift index e2bde94b0cb1..a2879b098a44 100644 --- a/ios/MullvadVPN/View controllers/Login/LoginViewController.swift +++ b/ios/MullvadVPN/View controllers/Login/LoginViewController.swift @@ -193,20 +193,19 @@ class LoginViewController: UIViewController, RootContainment { func start(action: LoginAction) { beginLogin(action) - - switch action { - case .createAccount: - interactor.createAccount { [weak self] result in - if let newAccountNumber = result.value { - self?.contentView.accountInputGroup.setAccount(newAccountNumber) + 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: result.error) - } - - case let .useExistingAccount(accountNumber): - interactor.setAccount(accountNumber: accountNumber) { [weak self] error in - self?.endLogin(action: action, error: error) + self.endLogin(action: action, error: nil) + } catch { + self.endLogin(action: action, error: error) } } } diff --git a/ios/MullvadVPN/View controllers/RedeemVoucher/RedeemVoucherInteractor.swift b/ios/MullvadVPN/View controllers/RedeemVoucher/RedeemVoucherInteractor.swift index 26da26f14723..88011370057a 100644 --- a/ios/MullvadVPN/View controllers/RedeemVoucher/RedeemVoucherInteractor.swift +++ b/ios/MullvadVPN/View controllers/RedeemVoucher/RedeemVoucherInteractor.swift @@ -46,16 +46,10 @@ final class RedeemVoucherInteractor { }) } - func logout(completionHandler: @escaping () -> Void) { - preferredAccountNumber.flatMap { accountNumber in - tunnelManager.unsetAccount { [weak self] in - guard let self else { - return - } - completionHandler() - didLogout?(accountNumber) - } - } + func logout() async { + guard let accountNumber = preferredAccountNumber else { return } + await tunnelManager.unsetAccount() + didLogout?(accountNumber) } func cancelAll() { diff --git a/ios/MullvadVPN/View controllers/RedeemVoucher/RedeemVoucherViewController.swift b/ios/MullvadVPN/View controllers/RedeemVoucher/RedeemVoucherViewController.swift index d5757b30268d..d88cc1e4cad4 100644 --- a/ios/MullvadVPN/View controllers/RedeemVoucher/RedeemVoucherViewController.swift +++ b/ios/MullvadVPN/View controllers/RedeemVoucher/RedeemVoucherViewController.swift @@ -138,8 +138,11 @@ class RedeemVoucherViewController: UIViewController, UINavigationControllerDeleg contentView.state = .logout - interactor.logout { [weak self] in - self?.contentView.state = .initial + Task { + [weak self] in + guard let self else { return } + await interactor.logout() + contentView.state = .initial } } }