From 9c2b93ad62433dbccd713a83a7de97ffae02b767 Mon Sep 17 00:00:00 2001 From: Laurent Date: Thu, 12 Sep 2024 11:13:57 +0200 Subject: [PATCH 1/3] Fix an issue where payed tournament units did not show up --- PadelClub/Data/DataStore.swift | 5 +- .../Views/Tournament/Subscription/Guard.swift | 46 +++++++++++-------- 2 files changed, 32 insertions(+), 19 deletions(-) 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() } } From 1dba6633eebb5b51aa6a7d968a8875772c13452c Mon Sep 17 00:00:00 2001 From: Laurent Date: Thu, 12 Sep 2024 11:20:29 +0200 Subject: [PATCH 2/3] put back debug plans --- PadelClub/Views/Tournament/Subscription/Guard.swift | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/PadelClub/Views/Tournament/Subscription/Guard.swift b/PadelClub/Views/Tournament/Subscription/Guard.swift index 2804a6a..951a961 100644 --- a/PadelClub/Views/Tournament/Subscription/Guard.swift +++ b/PadelClub/Views/Tournament/Subscription/Guard.swift @@ -161,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] { From 19c548598f9e5866ba9822b1744ec5db2e73b228 Mon Sep 17 00:00:00 2001 From: Laurent Date: Thu, 12 Sep 2024 12:31:12 +0200 Subject: [PATCH 3/3] Adds promotional transactions to valid transactions --- PadelClub/Views/Tournament/Subscription/Guard.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PadelClub/Views/Tournament/Subscription/Guard.swift b/PadelClub/Views/Tournament/Subscription/Guard.swift index 951a961..89f4cb4 100644 --- a/PadelClub/Views/Tournament/Subscription/Guard.swift +++ b/PadelClub/Views/Tournament/Subscription/Guard.swift @@ -179,7 +179,7 @@ import LeStorage return [] } - let userTransactions = self.purchasedTransactions.filter { currentUserUUID == $0.appAccountToken } + let userTransactions = self.purchasedTransactions.filter { currentUserUUID == $0.appAccountToken || $0.offerType == .promotional } let now: Date = Date() // print("now = \(now)")