From bee68f916f2cd76144a28a9651673c2721dc13a5 Mon Sep 17 00:00:00 2001 From: Laurent Date: Thu, 3 Apr 2025 11:17:04 +0200 Subject: [PATCH] LeStorage fixes --- PadelClub/Data/DataStore.swift | 20 +++++++++---------- PadelClub/Data/TournamentStore.swift | 19 +++++++++--------- PadelClub/Utils/Patcher.swift | 7 +++++++ .../Navigation/Agenda/EventListView.swift | 2 +- .../Views/Tournament/Subscription/Guard.swift | 20 +++++++++---------- .../Tournament/Subscription/Purchase.swift | 2 +- 6 files changed, 39 insertions(+), 31 deletions(-) diff --git a/PadelClub/Data/DataStore.swift b/PadelClub/Data/DataStore.swift index 164d6c9..46a7ce3 100644 --- a/PadelClub/Data/DataStore.swift +++ b/PadelClub/Data/DataStore.swift @@ -33,13 +33,13 @@ class DataStore: ObservableObject { } } - fileprivate(set) var tournaments: StoredCollection - fileprivate(set) var clubs: StoredCollection - fileprivate(set) var courts: StoredCollection - fileprivate(set) var events: StoredCollection + fileprivate(set) var tournaments: SyncedCollection + fileprivate(set) var clubs: SyncedCollection + fileprivate(set) var courts: SyncedCollection + fileprivate(set) var events: SyncedCollection fileprivate(set) var monthData: StoredCollection - fileprivate(set) var dateIntervals: StoredCollection - fileprivate(set) var purchases: StoredCollection + fileprivate(set) var dateIntervals: SyncedCollection + fileprivate(set) var purchases: SyncedCollection fileprivate var userStorage: StoredSingleton @@ -127,9 +127,9 @@ class DataStore: ObservableObject { if let userSingleton: StoredSingleton = notification.object as? StoredSingleton { self.user = userSingleton.item() ?? self._temporaryLocalUser.item ?? CustomUser.placeHolder() - } else if let clubsCollection: StoredCollection = notification.object as? StoredCollection { + } else if let clubsCollection: SyncedCollection = notification.object as? SyncedCollection { self._fixMissingClubCreatorIfNecessary(clubsCollection) - } else if let eventsCollection: StoredCollection = notification.object as? StoredCollection { + } else if let eventsCollection: SyncedCollection = notification.object as? SyncedCollection { self._fixMissingEventCreatorIfNecessary(eventsCollection) } @@ -139,7 +139,7 @@ class DataStore: ObservableObject { } - fileprivate func _fixMissingClubCreatorIfNecessary(_ clubsCollection: StoredCollection) { + fileprivate func _fixMissingClubCreatorIfNecessary(_ clubsCollection: SyncedCollection) { for club in clubsCollection { if let userId = StoreCenter.main.userId, club.creator == nil { club.creator = userId @@ -150,7 +150,7 @@ class DataStore: ObservableObject { } } - fileprivate func _fixMissingEventCreatorIfNecessary(_ eventsCollection: StoredCollection) { + fileprivate func _fixMissingEventCreatorIfNecessary(_ eventsCollection: SyncedCollection) { for event in eventsCollection { if let userId = StoreCenter.main.userId, event.creator == nil { event.creator = userId diff --git a/PadelClub/Data/TournamentStore.swift b/PadelClub/Data/TournamentStore.swift index 3168ccd..fd7ea02 100644 --- a/PadelClub/Data/TournamentStore.swift +++ b/PadelClub/Data/TournamentStore.swift @@ -13,15 +13,15 @@ class TournamentStore: ObservableObject { var store: Store - fileprivate(set) var groupStages: StoredCollection = StoredCollection.placeholder() - fileprivate(set) var matches: StoredCollection = StoredCollection.placeholder() - fileprivate(set) var teamRegistrations: StoredCollection = StoredCollection.placeholder() - fileprivate(set) var playerRegistrations: StoredCollection = StoredCollection.placeholder() - fileprivate(set) var rounds: StoredCollection = StoredCollection.placeholder() - fileprivate(set) var teamScores: StoredCollection = StoredCollection.placeholder() + fileprivate(set) var groupStages: SyncedCollection = SyncedCollection.placeholder() + fileprivate(set) var matches: SyncedCollection = SyncedCollection.placeholder() + fileprivate(set) var teamRegistrations: SyncedCollection = SyncedCollection.placeholder() + fileprivate(set) var playerRegistrations: SyncedCollection = SyncedCollection.placeholder() + fileprivate(set) var rounds: SyncedCollection = SyncedCollection.placeholder() + fileprivate(set) var teamScores: SyncedCollection = SyncedCollection.placeholder() fileprivate(set) var matchSchedulers: StoredCollection = StoredCollection.placeholder() - fileprivate(set) var drawLogs: StoredCollection = StoredCollection.placeholder() + fileprivate(set) var drawLogs: SyncedCollection = SyncedCollection.placeholder() // convenience init(tournament: Tournament) { // let store = StoreCenter.main.store(identifier: tournament.id) @@ -44,7 +44,8 @@ class TournamentStore: ObservableObject { synchronized = sync } #endif - + StoreCenter.main.collectionsCanSynchronize = synchronized + self.groupStages = self.store.registerSynchronizedCollection(indexed: indexed) self.rounds = self.store.registerSynchronizedCollection(indexed: indexed) self.teamRegistrations = self.store.registerSynchronizedCollection(indexed: indexed) @@ -52,7 +53,7 @@ class TournamentStore: ObservableObject { self.matches = self.store.registerSynchronizedCollection(indexed: indexed) self.teamScores = self.store.registerSynchronizedCollection(indexed: indexed) self.matchSchedulers = self.store.registerCollection(indexed: indexed) - self.drawLogs = self.store.registerCollection(indexed: indexed) + self.drawLogs = self.store.registerSynchronizedCollection(indexed: indexed) self.store.loadCollectionsFromServerIfNoFile() diff --git a/PadelClub/Utils/Patcher.swift b/PadelClub/Utils/Patcher.swift index 48ca002..8bde529 100644 --- a/PadelClub/Utils/Patcher.swift +++ b/PadelClub/Utils/Patcher.swift @@ -54,6 +54,7 @@ enum Patch: String, CaseIterable { case cleanLogs case syncUpgrade case updateTournaments + case cleanPurchaseApiCalls var id: String { return "padelclub.app.patch.\(self.rawValue)" @@ -85,6 +86,7 @@ class AutomaticPatcher { case .cleanLogs: self._cleanLogs() case .syncUpgrade: self._syncUpgrade() case .updateTournaments: self._updateTournaments() + case .cleanPurchaseApiCalls: self._cleanPurchaseApiCalls() } } @@ -133,4 +135,9 @@ class AutomaticPatcher { fileprivate static func _updateTournaments() { DataStore.shared.tournaments.addOrUpdate(contentOfs: DataStore.shared.tournaments) } + + fileprivate static func _cleanPurchaseApiCalls() { + StoreCenter.main.resetApiCalls(type: Purchase.self) + } + } diff --git a/PadelClub/Views/Navigation/Agenda/EventListView.swift b/PadelClub/Views/Navigation/Agenda/EventListView.swift index dfc6429..31a67e8 100644 --- a/PadelClub/Views/Navigation/Agenda/EventListView.swift +++ b/PadelClub/Views/Navigation/Agenda/EventListView.swift @@ -314,7 +314,7 @@ struct EventListView: View { TournamentCellView(tournament: tournament) .onReceive(NotificationCenter.default.publisher(for: NSNotification.Name.CollectionDidLoad), perform: { notification in - if let store = notification.object as? StoredCollection { + if let store = notification.object as? SyncedCollection { if store.storeId == tournament.id { tournament.lastTeamRefresh = Date() } diff --git a/PadelClub/Views/Tournament/Subscription/Guard.swift b/PadelClub/Views/Tournament/Subscription/Guard.swift index d701e9c..570ccc7 100644 --- a/PadelClub/Views/Tournament/Subscription/Guard.swift +++ b/PadelClub/Views/Tournament/Subscription/Guard.swift @@ -187,18 +187,18 @@ import LeStorage } var currentPlan: StoreItem? { - #if DEBUG - return .monthlyUnlimited - #elseif TESTFLIGHT - return .monthlyUnlimited - #elseif PRODTEST - return .monthlyUnlimited - #else +// #if DEBUG +// return .monthlyUnlimited +// #elseif TESTFLIGHT +// return .monthlyUnlimited +// #elseif PRODTEST +// return .monthlyUnlimited +// #else if let currentBestPurchase = self.currentBestPurchase, let plan = StoreItem(rawValue: currentBestPurchase.productId) { return plan } return nil - #endif +// #endif } func userFilteredPurchases() -> [StoreKit.Transaction] { @@ -316,8 +316,8 @@ fileprivate extension StoreKit.Transaction { throw StoreError.missingUserId } - return Purchase(user: userId, - transactionId: self.originalID, + return Purchase(transactionId: self.originalID, + user: userId, purchaseDate: self.purchaseDate, productId: self.productID, quantity: self.purchasedQuantity, diff --git a/PadelClub/Views/Tournament/Subscription/Purchase.swift b/PadelClub/Views/Tournament/Subscription/Purchase.swift index 14cb860..5a66fa8 100644 --- a/PadelClub/Views/Tournament/Subscription/Purchase.swift +++ b/PadelClub/Views/Tournament/Subscription/Purchase.swift @@ -24,7 +24,7 @@ class Purchase: BasePurchase { // var revocationDate: Date? = nil // var expirationDate: Date? = nil - init(user: String, transactionId: UInt64, purchaseDate: Date, productId: String, quantity: Int? = nil, revocationDate: Date? = nil, expirationDate: Date? = nil) { + init(transactionId: UInt64, user: String, purchaseDate: Date, productId: String, quantity: Int? = nil, revocationDate: Date? = nil, expirationDate: Date? = nil) { super.init(id: transactionId, user: user, purchaseDate: purchaseDate, productId: productId, quantity: quantity, revocationDate: revocationDate, expirationDate: expirationDate) // self.id = transactionId