diff --git a/IVPNClient/AppDelegate.swift b/IVPNClient/AppDelegate.swift index 5b949f994..e74ced6f5 100644 --- a/IVPNClient/AppDelegate.swift +++ b/IVPNClient/AppDelegate.swift @@ -80,24 +80,6 @@ class AppDelegate: UIResponder { FileSystemManager.createLogFiles() } - private func listenTransactionUpdates() { - PurchaseManager.shared.listenTransactionUpdates { serviceStatus, error in - DispatchQueue.main.async { - guard let viewController = UIApplication.topViewController() else { - return - } - - if let error = error { - viewController.showErrorAlert(title: "Error", message: error.message) - } - - if let serviceStatus = serviceStatus { - viewController.showSubscriptionActivatedAlert(serviceStatus: serviceStatus) - } - } - } - } - private func resetLastPingTimestamp() { UserDefaults.shared.set(0, forKey: "LastPingTimestamp") } @@ -298,7 +280,6 @@ extension AppDelegate: UIApplicationDelegate { func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { evaluateUITests() registerUserDefaults() - listenTransactionUpdates() createLogFiles() resetLastPingTimestamp() clearURLCache() diff --git a/IVPNClient/Managers/PurchaseManager.swift b/IVPNClient/Managers/PurchaseManager.swift index be4cfeccc..8b9db7f2f 100644 --- a/IVPNClient/Managers/PurchaseManager.swift +++ b/IVPNClient/Managers/PurchaseManager.swift @@ -33,7 +33,7 @@ class PurchaseManager: NSObject { return SKPaymentQueue.canMakePayments() } - var updateListenerTask: Task? = nil + var observerTask: Task? = nil private(set) var products: [Product] = [] @@ -46,7 +46,7 @@ class PurchaseManager: NSObject { } deinit { - updateListenerTask?.cancel() + observerTask?.cancel() } // MARK: - Methods - @@ -96,8 +96,8 @@ class PurchaseManager: NSObject { return result } - func listenTransactionUpdates(completion: @escaping (ServiceStatus?, ErrorResult?) -> Void) { - updateListenerTask = Task { + func startObserver(completion: @escaping (ServiceStatus?, ErrorResult?) -> Void) { + observerTask = Task { for await result in Transaction.updates { guard case .verified(let transaction) = result else { continue @@ -113,6 +113,10 @@ class PurchaseManager: NSObject { } } + func stopObserver() { + observerTask?.cancel() + } + func restorePurchases(completion: @escaping (Account?, ErrorResult?) -> Void) { Task { for await result in Transaction.currentEntitlements { diff --git a/IVPNClient/Scenes/MainScreen/MainViewController.swift b/IVPNClient/Scenes/MainScreen/MainViewController.swift index efaa9804d..b9e236f1a 100644 --- a/IVPNClient/Scenes/MainScreen/MainViewController.swift +++ b/IVPNClient/Scenes/MainScreen/MainViewController.swift @@ -71,6 +71,7 @@ class MainViewController: UIViewController { addObservers() startAPIUpdate() startVPNStatusObserver() + startPurchaseObserver() } override func viewWillAppear(_ animated: Bool) { @@ -198,6 +199,8 @@ class MainViewController: UIViewController { NotificationCenter.default.addObserver(self, selector: #selector(vpnConfigurationDisabled), name: Notification.Name.VPNConfigurationDisabled, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(subscriptionActivated), name: Notification.Name.SubscriptionActivated, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(updateGeoLocation), name: Notification.Name.UpdateGeoLocation, object: nil) + NotificationCenter.default.addObserver(self, selector: #selector(startPurchaseObserver), name: Notification.Name.StartPurchaseObserver, object: nil) + NotificationCenter.default.addObserver(self, selector: #selector(stopPurchaseObserver), name: Notification.Name.StopPurchaseObserver, object: nil) } // MARK: - Private methods - @@ -236,6 +239,28 @@ class MainViewController: UIViewController { mainView.updateInfoAlert() } + @objc private func startPurchaseObserver() { + PurchaseManager.shared.startObserver { serviceStatus, error in + DispatchQueue.main.async { + guard let viewController = UIApplication.topViewController() else { + return + } + + if let error = error { + viewController.showErrorAlert(title: "Error", message: error.message) + } + + if let serviceStatus = serviceStatus { + viewController.showSubscriptionActivatedAlert(serviceStatus: serviceStatus) + } + } + } + } + + @objc private func stopPurchaseObserver() { + PurchaseManager.shared.stopObserver() + } + private func initFloatingPanel() { floatingPanel = FloatingPanelController() floatingPanel.setup() diff --git a/IVPNClient/Scenes/Signup/Payment/PaymentViewController.swift b/IVPNClient/Scenes/Signup/Payment/PaymentViewController.swift index bab78ce30..fc6f08939 100644 --- a/IVPNClient/Scenes/Signup/Payment/PaymentViewController.swift +++ b/IVPNClient/Scenes/Signup/Payment/PaymentViewController.swift @@ -200,6 +200,7 @@ class PaymentViewController: UITableViewController { return } + NotificationCenter.default.post(name: Notification.Name.StopPurchaseObserver, object: nil) hud.indicatorView = JGProgressHUDIndeterminateIndicatorView() hud.detailTextLabel.text = "Processing payment..." hud.show(in: (navigationController?.view)!) @@ -221,9 +222,11 @@ class PaymentViewController: UITableViewController { } } + NotificationCenter.default.post(name: Notification.Name.StartPurchaseObserver, object: nil) hud.dismiss() } catch { showErrorAlert(title: "Error", message: error.localizedDescription) + NotificationCenter.default.post(name: Notification.Name.StartPurchaseObserver, object: nil) hud.dismiss() } } @@ -242,6 +245,7 @@ class PaymentViewController: UITableViewController { self.navigationController?.dismiss(animated: true, completion: nil) } } + NotificationCenter.default.post(name: Notification.Name.StartPurchaseObserver, object: nil) return } diff --git a/IVPNClient/Utilities/Extensions/NotificationName+Ext.swift b/IVPNClient/Utilities/Extensions/NotificationName+Ext.swift index dd63512ec..14fe76ba6 100644 --- a/IVPNClient/Utilities/Extensions/NotificationName+Ext.swift +++ b/IVPNClient/Utilities/Extensions/NotificationName+Ext.swift @@ -58,5 +58,7 @@ extension Notification.Name { public static let CustomDNSUpdated = Notification.Name("customDNSUpdatedUpdated") public static let EvaluateReconnect = Notification.Name("evaluateReconnect") public static let EvaluatePlanUpdate = Notification.Name("evaluatePlanUpdate") + public static let StartPurchaseObserver = Notification.Name("startPurchaseObserver") + public static let StopPurchaseObserver = Notification.Name("stopPurchaseObserver") }