Fix an issue where payed tournament units did not show up

sync2
Laurent 1 year ago
parent 3e503bfcd1
commit 9c2b93ad62
  1. 5
      PadelClub/Data/DataStore.swift
  2. 46
      PadelClub/Views/Tournament/Subscription/Guard.swift

@ -39,6 +39,7 @@ class DataStore: ObservableObject {
fileprivate(set) var events: StoredCollection<Event> fileprivate(set) var events: StoredCollection<Event>
fileprivate(set) var monthData: StoredCollection<MonthData> fileprivate(set) var monthData: StoredCollection<MonthData>
fileprivate(set) var dateIntervals: StoredCollection<DateInterval> fileprivate(set) var dateIntervals: StoredCollection<DateInterval>
fileprivate(set) var purchases: StoredCollection<Purchase>
fileprivate var userStorage: StoredSingleton<User> fileprivate var userStorage: StoredSingleton<User>
@ -85,7 +86,8 @@ class DataStore: ObservableObject {
self.dateIntervals = store.registerCollection(synchronized: synchronized, indexed: indexed) self.dateIntervals = store.registerCollection(synchronized: synchronized, indexed: indexed)
self.userStorage = store.registerObject(synchronized: synchronized) 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(collectionDidLoad), name: NSNotification.Name.CollectionDidLoad, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(collectionDidUpdate), name: NSNotification.Name.CollectionDidChange, 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.events.reset()
self.dateIntervals.reset() self.dateIntervals.reset()
self.userStorage.reset() self.userStorage.reset()
self.purchases.reset()
for tournament in self.tournaments { for tournament in self.tournaments {
StoreCenter.main.destroyStore(identifier: tournament.id) StoreCenter.main.destroyStore(identifier: tournament.id)

@ -20,11 +20,8 @@ import LeStorage
var updateListenerTask: Task<Void, Never>? = nil var updateListenerTask: Task<Void, Never>? = nil
fileprivate(set) var purchases: StoredCollection<Purchase>
override init() { override init() {
self.purchases = Store.main.registerCollection(synchronized: true, inMemory: true)
super.init() super.init()
@ -61,6 +58,7 @@ import LeStorage
// Iterate through the user's purchased products. // Iterate through the user's purchased products.
for await verificationResult in Transaction.currentEntitlements { for await verificationResult in Transaction.currentEntitlements {
let transaction = try await self.processTransactionResult(verificationResult) let transaction = try await self.processTransactionResult(verificationResult)
print("processs product id = \(transaction.productID)")
DispatchQueue.main.async { DispatchQueue.main.async {
NotificationCenter.default.post(name: Notification.Name.StoreEventHappened, object: nil) NotificationCenter.default.post(name: Notification.Name.StoreEventHappened, object: nil)
} }
@ -72,7 +70,7 @@ import LeStorage
return Task(priority: .background) { return Task(priority: .background) {
//Iterate through any transactions which didn't come from a direct call to `purchase()`. //Iterate through any transactions which didn't come from a direct call to `purchase()`.
for await result in Transaction.updates { for await result in Transaction.updates {
Logger.log(">>> update = \(result)") // Logger.log(">>> update = \(result)")
do { do {
let transaction = try self.checkVerified(result) let transaction = try self.checkVerified(result)
@ -126,25 +124,30 @@ import LeStorage
} }
fileprivate func _addPurchaseIfPossible(transaction: StoreKit.Transaction) throws { 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 { if self._purchaseById(transaction.originalID) == nil {
let purchase: Purchase = try transaction.purchase() let purchase: Purchase = try transaction.purchase()
try self.purchases.addOrUpdate(instance: purchase) try purchases.addOrUpdate(instance: purchase)
} }
} }
} }
fileprivate func _updatePurchaseIfPossible(transaction: StoreKit.Transaction) throws { 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) { if let existing: Purchase = self._purchaseById(transaction.originalID) {
existing.revocationDate = transaction.revocationDate existing.revocationDate = transaction.revocationDate
try self.purchases.addOrUpdate(instance: existing) try purchases.addOrUpdate(instance: existing)
} }
} }
} }
fileprivate func _purchaseById(_ transactionId: UInt64) -> Purchase? { 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<StoreKit.Transaction>) async throws -> StoreKit.Transaction { func processTransactionResult(_ result: VerificationResult<StoreKit.Transaction>) async throws -> StoreKit.Transaction {
@ -158,16 +161,16 @@ import LeStorage
} }
var currentPlan: StoreItem? { var currentPlan: StoreItem? {
#if DEBUG // #if DEBUG
return .monthlyUnlimited // return .monthlyUnlimited
#elseif TESTFLIGHT // #elseif TESTFLIGHT
return .monthlyUnlimited // return .monthlyUnlimited
#else // #else
if let currentBestPlan = self.currentBestPlan, let plan = StoreItem(rawValue: currentBestPlan.productID) { if let currentBestPlan = self.currentBestPlan, let plan = StoreItem(rawValue: currentBestPlan.productID) {
return plan return plan
} }
return nil return nil
#endif // #endif
} }
func userFilteredPurchases() -> [StoreKit.Transaction] { func userFilteredPurchases() -> [StoreKit.Transaction] {
@ -208,8 +211,14 @@ import LeStorage
} }
fileprivate func _purchasedTournamentCount() -> Int { 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? { func paymentForNewTournament() -> Tournament.TournamentPayment? {
@ -254,7 +263,8 @@ import LeStorage
} }
func disconnect() { func disconnect() {
self.purchases.reset() let purchases = DataStore.shared.purchases
purchases.reset()
} }
} }

Loading…
Cancel
Save