Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor TunnelManager account setting methods replacing callbacks with async #5748

Merged
merged 3 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion ios/MullvadVPN/Coordinators/AccountCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down
6 changes: 4 additions & 2 deletions ios/MullvadVPN/Coordinators/ApplicationCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
36 changes: 15 additions & 21 deletions ios/MullvadVPN/TunnelManager/TunnelManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -311,19 +311,12 @@ final class TunnelManager: StorePaymentObserver {
operationQueue.addOperation(operation)
}

func setNewAccount(completion: @escaping (Result<StoredAccountData, Error>) -> 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<StoredAccountData, Error>) -> 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(
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Expand Down
13 changes: 4 additions & 9 deletions ios/MullvadVPN/View controllers/Login/LoginInteractor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Error>) -> 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? {
Expand Down
25 changes: 12 additions & 13 deletions ios/MullvadVPN/View controllers/Login/LoginViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
Loading