diff --git a/PadelClub/Data/DataStore.swift b/PadelClub/Data/DataStore.swift index d9d04ac..380c87d 100644 --- a/PadelClub/Data/DataStore.swift +++ b/PadelClub/Data/DataStore.swift @@ -39,6 +39,7 @@ class DataStore: ObservableObject { fileprivate(set) var events: StoredCollection fileprivate(set) var monthData: StoredCollection fileprivate(set) var dateIntervals: StoredCollection + fileprivate(set) var purchases: StoredCollection fileprivate var userStorage: StoredSingleton @@ -85,7 +86,8 @@ class DataStore: ObservableObject { self.dateIntervals = store.registerCollection(synchronized: synchronized, indexed: indexed) self.userStorage = store.registerObject(synchronized: synchronized) - + self.purchases = Store.main.registerCollection(synchronized: true, inMemory: true) + NotificationCenter.default.addObserver(self, selector: #selector(collectionDidLoad), name: NSNotification.Name.CollectionDidLoad, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(collectionDidUpdate), name: NSNotification.Name.CollectionDidChange, object: nil) @@ -204,6 +206,7 @@ class DataStore: ObservableObject { self.events.reset() self.dateIntervals.reset() self.userStorage.reset() + self.purchases.reset() for tournament in self.tournaments { StoreCenter.main.destroyStore(identifier: tournament.id) diff --git a/PadelClub/Views/Tournament/Subscription/Guard.swift b/PadelClub/Views/Tournament/Subscription/Guard.swift index 3a6d6be..2804a6a 100644 --- a/PadelClub/Views/Tournament/Subscription/Guard.swift +++ b/PadelClub/Views/Tournament/Subscription/Guard.swift @@ -20,11 +20,8 @@ import LeStorage var updateListenerTask: Task? = nil - fileprivate(set) var purchases: StoredCollection override init() { - - self.purchases = Store.main.registerCollection(synchronized: true, inMemory: true) super.init() @@ -61,6 +58,7 @@ import LeStorage // Iterate through the user's purchased products. for await verificationResult in Transaction.currentEntitlements { let transaction = try await self.processTransactionResult(verificationResult) + print("processs product id = \(transaction.productID)") DispatchQueue.main.async { NotificationCenter.default.post(name: Notification.Name.StoreEventHappened, object: nil) } @@ -72,7 +70,7 @@ import LeStorage return Task(priority: .background) { //Iterate through any transactions which didn't come from a direct call to `purchase()`. for await result in Transaction.updates { - Logger.log(">>> update = \(result)") +// Logger.log(">>> update = \(result)") do { let transaction = try self.checkVerified(result) @@ -126,25 +124,30 @@ import LeStorage } fileprivate func _addPurchaseIfPossible(transaction: StoreKit.Transaction) throws { - if self.purchases.hasLoaded { + + let purchases = DataStore.shared.purchases + + if purchases.hasLoaded { if self._purchaseById(transaction.originalID) == nil { let purchase: Purchase = try transaction.purchase() - try self.purchases.addOrUpdate(instance: purchase) + try purchases.addOrUpdate(instance: purchase) } } } fileprivate func _updatePurchaseIfPossible(transaction: StoreKit.Transaction) throws { - if self.purchases.hasLoaded { + let purchases = DataStore.shared.purchases + if purchases.hasLoaded { if let existing: Purchase = self._purchaseById(transaction.originalID) { existing.revocationDate = transaction.revocationDate - try self.purchases.addOrUpdate(instance: existing) + try purchases.addOrUpdate(instance: existing) } } } fileprivate func _purchaseById(_ transactionId: UInt64) -> Purchase? { - return self.purchases.first(where: { $0.identifier == transactionId }) + let purchases = DataStore.shared.purchases + return purchases.first(where: { $0.identifier == transactionId }) } func processTransactionResult(_ result: VerificationResult) async throws -> StoreKit.Transaction { @@ -158,16 +161,16 @@ import LeStorage } var currentPlan: StoreItem? { - #if DEBUG - return .monthlyUnlimited - #elseif TESTFLIGHT - return .monthlyUnlimited - #else +// #if DEBUG +// return .monthlyUnlimited +// #elseif TESTFLIGHT +// return .monthlyUnlimited +// #else if let currentBestPlan = self.currentBestPlan, let plan = StoreItem(rawValue: currentBestPlan.productID) { return plan } return nil - #endif +// #endif } func userFilteredPurchases() -> [StoreKit.Transaction] { @@ -208,8 +211,14 @@ import LeStorage } fileprivate func _purchasedTournamentCount() -> Int { - let units = self.userFilteredPurchases().filter { $0.productID == StoreItem.unit.rawValue } - return units.reduce(0) { $0 + $1.purchasedQuantity } + + let purchases = DataStore.shared.purchases + + let units = purchases.filter { $0.productId == StoreItem.unit.rawValue } + return units.reduce(0) { $0 + ($1.quantity ?? 0) } + +// let units = self.userFilteredPurchases().filter { $0.productID == StoreItem.unit.rawValue } +// return units.reduce(0) { $0 + $1.purchasedQuantity } } func paymentForNewTournament() -> Tournament.TournamentPayment? { @@ -254,7 +263,8 @@ import LeStorage } func disconnect() { - self.purchases.reset() + let purchases = DataStore.shared.purchases + purchases.reset() } }