Skip to content

Commit

Permalink
refactor(payments): update PurchaseManager
Browse files Browse the repository at this point in the history
  • Loading branch information
jurajhilje committed Feb 20, 2024
1 parent f74c085 commit 3f04a18
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 23 deletions.
19 changes: 0 additions & 19 deletions IVPNClient/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down Expand Up @@ -298,7 +280,6 @@ extension AppDelegate: UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
evaluateUITests()
registerUserDefaults()
listenTransactionUpdates()
createLogFiles()
resetLastPingTimestamp()
clearURLCache()
Expand Down
12 changes: 8 additions & 4 deletions IVPNClient/Managers/PurchaseManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class PurchaseManager: NSObject {
return SKPaymentQueue.canMakePayments()
}

var updateListenerTask: Task<Void, Error>? = nil
var observerTask: Task<Void, Error>? = nil

private(set) var products: [Product] = []

Expand All @@ -46,7 +46,7 @@ class PurchaseManager: NSObject {
}

deinit {
updateListenerTask?.cancel()
observerTask?.cancel()
}

// MARK: - Methods -
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down
25 changes: 25 additions & 0 deletions IVPNClient/Scenes/MainScreen/MainViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class MainViewController: UIViewController {
addObservers()
startAPIUpdate()
startVPNStatusObserver()
startPurchaseObserver()
}

override func viewWillAppear(_ animated: Bool) {
Expand Down Expand Up @@ -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 -
Expand Down Expand Up @@ -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()
Expand Down
4 changes: 4 additions & 0 deletions IVPNClient/Scenes/Signup/Payment/PaymentViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)!)
Expand All @@ -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()
}
}
Expand All @@ -242,6 +245,7 @@ class PaymentViewController: UITableViewController {
self.navigationController?.dismiss(animated: true, completion: nil)
}
}
NotificationCenter.default.post(name: Notification.Name.StartPurchaseObserver, object: nil)
return
}

Expand Down
2 changes: 2 additions & 0 deletions IVPNClient/Utilities/Extensions/NotificationName+Ext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")

}

0 comments on commit 3f04a18

Please sign in to comment.