diff options
| author | Andrej Mihajlov <and@mullvad.net> | 2022-10-27 10:09:21 +0200 |
|---|---|---|
| committer | Andrej Mihajlov <and@mullvad.net> | 2022-10-31 10:25:09 +0100 |
| commit | 295b3236d91449a60444449612fead8ae0ab5434 (patch) | |
| tree | 7b9db45e882bebc355a21ee987ec62b9fcc011af | |
| parent | 14a291bd6899843bfdaf6676eef034fd6b12b652 (diff) | |
| download | mullvadvpn-295b3236d91449a60444449612fead8ae0ab5434.tar.xz mullvadvpn-295b3236d91449a60444449612fead8ae0ab5434.zip | |
Adapt view controllers to new StorePaymentManager
| -rw-r--r-- | ios/MullvadVPN/AccountViewController.swift | 72 | ||||
| -rw-r--r-- | ios/MullvadVPN/AppDelegate.swift | 12 | ||||
| -rw-r--r-- | ios/MullvadVPN/DisplayChainedError.swift | 4 | ||||
| -rw-r--r-- | ios/MullvadVPN/OutOfTimeViewController.swift | 66 | ||||
| -rw-r--r-- | ios/MullvadVPN/TunnelManager/TunnelManager.swift | 26 |
5 files changed, 76 insertions, 104 deletions
diff --git a/ios/MullvadVPN/AccountViewController.swift b/ios/MullvadVPN/AccountViewController.swift index fdf1d271d0..11862d8f9a 100644 --- a/ios/MullvadVPN/AccountViewController.swift +++ b/ios/MullvadVPN/AccountViewController.swift @@ -16,7 +16,7 @@ protocol AccountViewControllerDelegate: AnyObject { func accountViewControllerDidLogout(_ controller: AccountViewController) } -class AccountViewController: UIViewController, AppStorePaymentObserver, TunnelObserver { +class AccountViewController: UIViewController, StorePaymentObserver, TunnelObserver { private let alertPresenter = AlertPresenter() private let contentView: AccountContentView = { @@ -83,13 +83,13 @@ class AccountViewController: UIViewController, AppStorePaymentObserver, TunnelOb ) contentView.logoutButton.addTarget(self, action: #selector(doLogout), for: .touchUpInside) - AppStorePaymentManager.shared.addPaymentObserver(self) + StorePaymentManager.shared.addPaymentObserver(self) TunnelManager.shared.addObserver(self) updateView(from: TunnelManager.shared.deviceState) applyViewState(animated: false) - if AppStorePaymentManager.canMakePayments { + if StorePaymentManager.canMakePayments { requestStoreProducts() } else { setProductState(.cannotMakePurchases, animated: false) @@ -99,11 +99,11 @@ class AccountViewController: UIViewController, AppStorePaymentObserver, TunnelOb // MARK: - Private methods private func requestStoreProducts() { - let productKind = AppStoreSubscription.thirtyDays + let productKind = StoreSubscription.thirtyDays setProductState(.fetching(productKind), animated: true) - _ = AppStorePaymentManager.shared + _ = StorePaymentManager.shared .requestProducts(with: [productKind]) { [weak self] completion in let productState: ProductState = completion.value?.products.first .map { .received($0) } ?? .failed @@ -158,14 +158,7 @@ class AccountViewController: UIViewController, AppStorePaymentObserver, TunnelOb navigationItem.setHidesBackButton(!isInteractionEnabled, animated: animated) } - private func didProcessPayment(_ payment: SKPayment) { - guard case let .makingPayment(pendingPayment) = paymentState, - pendingPayment == payment else { return } - - setPaymentState(.none, animated: true) - } - - private func showPaymentErrorAlert(error: AppStorePaymentManager.Error) { + private func showPaymentErrorAlert(error: StorePaymentManagerError) { let alertController = UIAlertController( title: NSLocalizedString( "CANNOT_COMPLETE_PURCHASE_ALERT_TITLE", @@ -191,7 +184,7 @@ class AccountViewController: UIViewController, AppStorePaymentObserver, TunnelOb alertPresenter.enqueue(alertController, presentingController: self) } - private func showRestorePurchasesErrorAlert(error: AppStorePaymentManager.Error) { + private func showRestorePurchasesErrorAlert(error: StorePaymentManagerError) { let alertController = UIAlertController( title: NSLocalizedString( "RESTORE_PURCHASES_FAILURE_ALERT_TITLE", @@ -337,35 +330,32 @@ class AccountViewController: UIViewController, AppStorePaymentObserver, TunnelOb updateView(from: deviceState) } - // MARK: - AppStorePaymentObserver + // MARK: - StorePaymentObserver - func appStorePaymentManager( - _ manager: AppStorePaymentManager, - transaction: SKPaymentTransaction?, - payment: SKPayment, - accountToken: String?, - didFailWithError error: AppStorePaymentManager.Error + func storePaymentManager( + _ manager: StorePaymentManager, + didReceiveEvent event: StorePaymentEvent ) { - switch error { - case .storePayment(SKError.paymentCancelled): - break + guard case let .makingPayment(payment) = paymentState, + payment == event.payment else { return } - default: - showPaymentErrorAlert(error: error) - } - - didProcessPayment(payment) - } + switch event { + case let .finished(paymentCompletion): + showTimeAddedConfirmationAlert( + with: paymentCompletion.serverResponse, + context: .purchase + ) - func appStorePaymentManager( - _ manager: AppStorePaymentManager, - transaction: SKPaymentTransaction, - accountToken: String, - didFinishWithResponse response: REST.CreateApplePaymentResponse - ) { - showTimeAddedConfirmationAlert(with: response, context: .purchase) + case let .failure(paymentFailure): + switch paymentFailure.error { + case .storePayment(SKError.paymentCancelled): + break + default: + showPaymentErrorAlert(error: paymentFailure.error) + } + } - didProcessPayment(transaction.payment) + setPaymentState(.none, animated: true) } // MARK: - Actions @@ -394,7 +384,7 @@ class AccountViewController: UIViewController, AppStorePaymentObserver, TunnelOb } let payment = SKPayment(product: product) - AppStorePaymentManager.shared.addPayment(payment, for: accountData.number) + StorePaymentManager.shared.addPayment(payment, for: accountData.number) setPaymentState(.makingPayment(payment), animated: true) } @@ -406,7 +396,7 @@ class AccountViewController: UIViewController, AppStorePaymentObserver, TunnelOb setPaymentState(.restoringPurchases, animated: true) - _ = AppStorePaymentManager.shared.restorePurchases(for: accountData.number) { completion in + _ = StorePaymentManager.shared.restorePurchases(for: accountData.number) { completion in switch completion { case let .success(response): self.showTimeAddedConfirmationAlert(with: response, context: .restoration) @@ -441,7 +431,7 @@ private extension AccountViewController { enum ProductState { case none - case fetching(AppStoreSubscription) + case fetching(StoreSubscription) case received(SKProduct) case failed case cannotMakePurchases diff --git a/ios/MullvadVPN/AppDelegate.swift b/ios/MullvadVPN/AppDelegate.swift index acff5835fe..f1958e9955 100644 --- a/ios/MullvadVPN/AppDelegate.swift +++ b/ios/MullvadVPN/AppDelegate.swift @@ -64,7 +64,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate { self.logger.debug("Finished initialization.") NotificationManager.shared.updateNotifications() - AppStorePaymentManager.shared.startPaymentQueueMonitoring() + StorePaymentManager.shared.startPaymentQueueMonitoring() operation.finish() } @@ -267,8 +267,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate { // MARK: - Private private func setupPaymentHandler() { - AppStorePaymentManager.shared.delegate = self - AppStorePaymentManager.shared.addPaymentObserver(TunnelManager.shared) + StorePaymentManager.shared.delegate = self + StorePaymentManager.shared.addPaymentObserver(TunnelManager.shared) } private func setupNotificationHandler() { @@ -282,9 +282,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate { // MARK: - AppStorePaymentManagerDelegate -extension AppDelegate: AppStorePaymentManagerDelegate { - func appStorePaymentManager( - _ manager: AppStorePaymentManager, +extension AppDelegate: StorePaymentManagerDelegate { + func storePaymentManager( + _ manager: StorePaymentManager, didRequestAccountTokenFor payment: SKPayment ) -> String? { // Since we do not persist the relation between payment and account number between the diff --git a/ios/MullvadVPN/DisplayChainedError.swift b/ios/MullvadVPN/DisplayChainedError.swift index 2174696ede..4bfa351f4f 100644 --- a/ios/MullvadVPN/DisplayChainedError.swift +++ b/ios/MullvadVPN/DisplayChainedError.swift @@ -107,7 +107,7 @@ extension SKError: LocalizedError { } } -extension AppStorePaymentManager.Error: DisplayChainedError { +extension StorePaymentManagerError: DisplayChainedError { var errorChainDescription: String? { switch self { case .noAccountSet: @@ -144,7 +144,7 @@ extension AppStorePaymentManager.Error: DisplayChainedError { } case let .readReceipt(readReceiptError): - if readReceiptError is AppStoreReceiptNotFound { + if readReceiptError is StoreReceiptNotFound { return NSLocalizedString( "RECEIPT_NOT_FOUND_ERROR", tableName: "AppStorePaymentManager", diff --git a/ios/MullvadVPN/OutOfTimeViewController.swift b/ios/MullvadVPN/OutOfTimeViewController.swift index 8672132abb..d55490ccc2 100644 --- a/ios/MullvadVPN/OutOfTimeViewController.swift +++ b/ios/MullvadVPN/OutOfTimeViewController.swift @@ -80,7 +80,7 @@ private extension OutOfTimeViewController { } func addObservers() { - AppStorePaymentManager.shared.addPaymentObserver(self) + StorePaymentManager.shared.addPaymentObserver(self) TunnelManager.shared.addObserver(self) } @@ -116,7 +116,7 @@ private extension OutOfTimeViewController { private extension OutOfTimeViewController { func setUpInAppPurchases() { - if AppStorePaymentManager.canMakePayments { + if StorePaymentManager.canMakePayments { requestStoreProducts() } else { setProductState(.cannotMakePurchases, animated: false) @@ -124,11 +124,11 @@ private extension OutOfTimeViewController { } func requestStoreProducts() { - let productKind = AppStoreSubscription.thirtyDays + let productKind = StoreSubscription.thirtyDays setProductState(.fetching(productKind), animated: true) - _ = AppStorePaymentManager.shared + _ = StorePaymentManager.shared .requestProducts(with: [productKind]) { [weak self] completion in let productState: ProductState = completion.value?.products.first .map { .received($0) } ?? .failed @@ -200,7 +200,7 @@ private extension OutOfTimeViewController { } let payment = SKPayment(product: product) - AppStorePaymentManager.shared.addPayment(payment, for: accountData.number) + StorePaymentManager.shared.addPayment(payment, for: accountData.number) setPaymentState(.makingPayment(payment), animated: true) } @@ -212,7 +212,7 @@ private extension OutOfTimeViewController { setPaymentState(.restoringPurchases, animated: true) - _ = AppStorePaymentManager.shared.restorePurchases(for: accountData.number) { completion in + _ = StorePaymentManager.shared.restorePurchases(for: accountData.number) { completion in switch completion { case let .success(response): self.showAlertIfNoTimeAdded(with: response, context: .restoration) @@ -253,7 +253,7 @@ private extension OutOfTimeViewController { alertPresenter.enqueue(alertController, presentingController: self) } - func showRestorePurchasesErrorAlert(error: AppStorePaymentManager.Error) { + func showRestorePurchasesErrorAlert(error: StorePaymentManagerError) { let alertController = UIAlertController( title: NSLocalizedString( "RESTORE_PURCHASES_FAILURE_ALERT_TITLE", @@ -275,7 +275,7 @@ private extension OutOfTimeViewController { alertPresenter.enqueue(alertController, presentingController: self) } - func showPaymentErrorAlert(error: AppStorePaymentManager.Error) { + func showPaymentErrorAlert(error: StorePaymentManagerError) { let alertController = UIAlertController( title: NSLocalizedString( "CANNOT_COMPLETE_PURCHASE_ALERT_TITLE", @@ -300,43 +300,33 @@ private extension OutOfTimeViewController { alertPresenter.enqueue(alertController, presentingController: self) } - - func didProcessPayment(_ payment: SKPayment) { - guard case let .makingPayment(pendingPayment) = paymentState, - pendingPayment == payment else { return } - - setPaymentState(.none, animated: true) - } } -// MARK: - AppStorePaymentObserver +// MARK: - StorePaymentObserver -extension OutOfTimeViewController: AppStorePaymentObserver { - func appStorePaymentManager( - _ manager: AppStorePaymentManager, - transaction: SKPaymentTransaction?, - payment: SKPayment, - accountToken: String?, - didFailWithError error: AppStorePaymentManager.Error +extension OutOfTimeViewController: StorePaymentObserver { + func storePaymentManager( + _ manager: StorePaymentManager, + didReceiveEvent event: StorePaymentEvent ) { - switch error { - case .storePayment(SKError.paymentCancelled): + guard case let .makingPayment(payment) = paymentState, + payment == event.payment else { return } + + switch event { + case .finished: break - default: - showPaymentErrorAlert(error: error) - } + case let .failure(paymentFailure): + switch paymentFailure.error { + case .storePayment(SKError.paymentCancelled): + break - didProcessPayment(payment) - } + default: + showPaymentErrorAlert(error: paymentFailure.error) + } + } - func appStorePaymentManager( - _ manager: AppStorePaymentManager, - transaction: SKPaymentTransaction, - accountToken: String, - didFinishWithResponse response: REST.CreateApplePaymentResponse - ) { - didProcessPayment(transaction.payment) + setPaymentState(.none, animated: true) } } @@ -394,7 +384,7 @@ private extension OutOfTimeViewController { enum ProductState { case none - case fetching(AppStoreSubscription) + case fetching(StoreSubscription) case received(SKProduct) case failed case cannotMakePurchases diff --git a/ios/MullvadVPN/TunnelManager/TunnelManager.swift b/ios/MullvadVPN/TunnelManager/TunnelManager.swift index 28ab12b8ad..9aa731551f 100644 --- a/ios/MullvadVPN/TunnelManager/TunnelManager.swift +++ b/ios/MullvadVPN/TunnelManager/TunnelManager.swift @@ -960,30 +960,22 @@ final class TunnelManager { // MARK: - AppStore payment observer -extension TunnelManager: AppStorePaymentObserver { - func appStorePaymentManager( - _ manager: AppStorePaymentManager, - transaction: SKPaymentTransaction?, - payment: SKPayment, - accountToken: String?, - didFailWithError error: AppStorePaymentManager.Error +extension TunnelManager: StorePaymentObserver { + func storePaymentManager( + _ manager: StorePaymentManager, + didReceiveEvent event: StorePaymentEvent ) { - // no-op - } + guard case let .finished(paymentCompletion) = event else { + return + } - func appStorePaymentManager( - _ manager: AppStorePaymentManager, - transaction: SKPaymentTransaction, - accountToken: String, - didFinishWithResponse response: REST.CreateApplePaymentResponse - ) { scheduleDeviceStateUpdate( taskName: "Update account expiry after in-app purchase", modificationBlock: { deviceState in switch deviceState { case .loggedIn(var accountData, let deviceData): - if accountData.number == accountToken { - accountData.expiry = response.newExpiry + if accountData.number == paymentCompletion.accountNumber { + accountData.expiry = paymentCompletion.serverResponse.newExpiry deviceState = .loggedIn(accountData, deviceData) } |
