Skip to content

Commit f786eae

Browse files
Andrew Bulhakbuggmagnet
Andrew Bulhak
authored andcommitted
Refactor TunnelManager account setting methods replacing callbacks with async
1 parent d805c6e commit f786eae

10 files changed

+60
-69
lines changed

ios/MullvadVPN/Coordinators/AccountCoordinator.swift

+3-2
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,9 @@ final class AccountCoordinator: Coordinator, Presentable, Presenting {
141141
)
142142

143143
let alertPresenter = AlertPresenter(context: self)
144-
145-
interactor.logout {
144+
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

+17-22
Original file line numberDiff line numberDiff line change
@@ -311,19 +311,13 @@ 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+
315+
func setNewAccount() async throws -> StoredAccountData {
316+
try await setAccount(action: .new)!
318317
}
319318

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-
}
319+
func setExistingAccount(accountNumber: String) async throws -> StoredAccountData {
320+
try await setAccount(action: .existing(accountNumber))!
327321
}
328322

329323
private func setAccount(
@@ -373,13 +367,19 @@ final class TunnelManager: StorePaymentObserver {
373367

374368
operationQueue.addOperation(operation)
375369
}
376-
377-
func unsetAccount(completionHandler: @escaping () -> Void) {
378-
setAccount(action: .unset) { _ in
379-
completionHandler()
370+
371+
private func setAccount(action: SetAccountAction) async throws -> StoredAccountData? {
372+
try await withCheckedThrowingContinuation { continuation in
373+
setAccount(action: action) { result in
374+
continuation.resume(with: result)
375+
}
380376
}
381377
}
382378

379+
func unsetAccount() async {
380+
_ = try? await setAccount(action: .unset)
381+
}
382+
383383
func updateAccountData(_ completionHandler: ((Error?) -> Void)? = nil) {
384384
let operation = UpdateAccountDataOperation(
385385
dispatchQueue: internalQueue,
@@ -435,13 +435,8 @@ final class TunnelManager: StorePaymentObserver {
435435
return operation
436436
}
437437

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

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

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ final class AccountInteractor {
5252
var deviceState: DeviceState {
5353
tunnelManager.deviceState
5454
}
55-
56-
func logout(_ completion: @escaping () -> Void) {
57-
tunnelManager.unsetAccount(completionHandler: completion)
55+
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-12
Original file line numberDiff line numberDiff line change
@@ -193,21 +193,21 @@ 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)
206+
self.endLogin(action: action, error: nil)
207+
} catch {
208+
self.endLogin(action: action, error: error)
205209
}
206210

207-
case let .useExistingAccount(accountNumber):
208-
interactor.setAccount(accountNumber: accountNumber) { [weak self] error in
209-
self?.endLogin(action: action, error: error)
210-
}
211211
}
212212
}
213213

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

+6-3
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,12 @@ class RedeemVoucherViewController: UIViewController, UINavigationControllerDeleg
137137
contentView.isEditing = false
138138

139139
contentView.state = .logout
140-
141-
interactor.logout { [weak self] in
142-
self?.contentView.state = .initial
140+
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)