From bee68f916f2cd76144a28a9651673c2721dc13a5 Mon Sep 17 00:00:00 2001 From: Laurent Date: Thu, 3 Apr 2025 11:17:04 +0200 Subject: [PATCH 1/2] 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 From c0ab6418d85ec28909d3adea6dc805272473d209 Mon Sep 17 00:00:00 2001 From: Laurent Date: Thu, 3 Apr 2025 17:05:06 +0200 Subject: [PATCH 2/2] Fix crap --- PadelClub/Data/DataStore.swift | 13 +-- PadelClub/Data/Tournament.swift | 29 +++-- PadelClub/Data/TournamentStore.swift | 8 -- .../Toolbox/DebugSettingsView.swift | 5 - .../Screen/InscriptionManagerView.swift | 106 +++++++++--------- 5 files changed, 75 insertions(+), 86 deletions(-) diff --git a/PadelClub/Data/DataStore.swift b/PadelClub/Data/DataStore.swift index 46a7ce3..a3783e6 100644 --- a/PadelClub/Data/DataStore.swift +++ b/PadelClub/Data/DataStore.swift @@ -15,17 +15,14 @@ class DataStore: ObservableObject { @Published var user: CustomUser = CustomUser.placeHolder() { didSet { - let loggedUser = StoreCenter.main.userId != nil - StoreCenter.main.collectionsCanSynchronize = loggedUser + let loggedUser = StoreCenter.main.isAuthenticated if loggedUser { if self.user.id != self.userStorage.item()?.id { self.userStorage.setItemNoSync(self.user) - if StoreCenter.main.collectionsCanSynchronize { - StoreCenter.main.initialSynchronization() - self._fixMissingClubCreatorIfNecessary(self.clubs) - self._fixMissingEventCreatorIfNecessary(self.events) - } + StoreCenter.main.initialSynchronization(clear: false) + self._fixMissingClubCreatorIfNecessary(self.clubs) + self._fixMissingEventCreatorIfNecessary(self.events) } } else { self._temporaryLocalUser.item = self.user @@ -224,8 +221,6 @@ class DataStore: ObservableObject { fileprivate func _localDisconnect() { - StoreCenter.main.collectionsCanSynchronize = false - let tournamendIds: [String] = self.tournaments.map { $0.id } TournamentLibrary.shared.reset() diff --git a/PadelClub/Data/Tournament.swift b/PadelClub/Data/Tournament.swift index 086a50f..886ea8d 100644 --- a/PadelClub/Data/Tournament.swift +++ b/PadelClub/Data/Tournament.swift @@ -2406,17 +2406,24 @@ defer { func insertOnServer() throws { -// DataStore.shared.tournaments.writeChangeAndInsertOnServer(instance: self) -// -// for teamRegistration in self.tournamentStore?.teamRegistrations { -// teamRegistration.insertOnServer() -// } -// for groupStage in self.tournamentStore?.groupStages { -// groupStage.insertOnServer() -// } -// for round in self.tournamentStore.rounds { -// round.insertOnServer() -// } + DataStore.shared.tournaments.writeChangeAndInsertOnServer(instance: self) + + if let teamRegistrations = self.tournamentStore?.teamRegistrations { + for teamRegistration in teamRegistrations { + teamRegistration.insertOnServer() + } + } + + if let groupStages = self.tournamentStore?.groupStages { + for groupStage in groupStages { + groupStage.insertOnServer() + } + } + if let rounds = self.tournamentStore?.rounds { + for round in rounds { + round.insertOnServer() + } + } } diff --git a/PadelClub/Data/TournamentStore.swift b/PadelClub/Data/TournamentStore.swift index fd7ea02..44125f7 100644 --- a/PadelClub/Data/TournamentStore.swift +++ b/PadelClub/Data/TournamentStore.swift @@ -36,16 +36,8 @@ class TournamentStore: ObservableObject { fileprivate func _initialize() { - var synchronized: Bool = true let indexed: Bool = true - #if DEBUG - if let sync = PListReader.readBool(plist: "local", key: "synchronized") { - 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) diff --git a/PadelClub/Views/Navigation/Toolbox/DebugSettingsView.swift b/PadelClub/Views/Navigation/Toolbox/DebugSettingsView.swift index 76716fb..7e52ec3 100644 --- a/PadelClub/Views/Navigation/Toolbox/DebugSettingsView.swift +++ b/PadelClub/Views/Navigation/Toolbox/DebugSettingsView.swift @@ -25,7 +25,6 @@ struct DebugSettingsView: View { LabeledContent("Token", value: self._token) LabeledContent("Server", value: self._apiURL) LabeledContent("Synchronized", value: self._synchronized) - LabeledContent("CollectionsCanSynchronize", value: self._canSynchronize) } } } @@ -55,10 +54,6 @@ struct DebugSettingsView: View { } } - fileprivate var _canSynchronize: String { - return "\(StoreCenter.main.collectionsCanSynchronize)" - } - fileprivate var _wsPingStatus: String { return "\(StoreCenter.main.websocketPingStatus)" } diff --git a/PadelClub/Views/Tournament/Screen/InscriptionManagerView.swift b/PadelClub/Views/Tournament/Screen/InscriptionManagerView.swift index 9155f4e..ac7d77a 100644 --- a/PadelClub/Views/Tournament/Screen/InscriptionManagerView.swift +++ b/PadelClub/Views/Tournament/Screen/InscriptionManagerView.swift @@ -1048,59 +1048,59 @@ struct InscriptionManagerView: View { } } - @ViewBuilder - private func _prioritizeClubMembersButton() -> some View { - @Bindable var tournament = tournament - if let federalClub = tournament.club() { - Menu { - Picker(selection: $tournament.prioritizeClubMembers) { - Text("Oui").tag(true) - Text("Non").tag(false) - } label: { - - } - .labelsHidden() - - Divider() - NavigationLink { - ClubsView() { club in - if let event = tournament.eventObject() { - event.club = club.id - do { - try dataStore.events.addOrUpdate(instance: event) - } catch { - Logger.error(error) - } - } - _save() - } - } label: { - Text("Changer de club") - } - } label: { - Text("Membres prioritaires") - Text(federalClub.acronym) - } - Divider() - } else { - NavigationLink { - ClubsView() { club in - if let event = tournament.eventObject() { - event.club = club.id - do { - try dataStore.events.addOrUpdate(instance: event) - } catch { - Logger.error(error) - } - } - _save() - } - } label: { - Text("Identifier le club") - } - Divider() - } - } +// @ViewBuilder +// private func _prioritizeClubMembersButton() -> some View { +// @Bindable var tournament = tournament +// if let federalClub = tournament.club() { +// Menu { +// Picker(selection: $tournament.prioritizeClubMembers) { +// Text("Oui").tag(true) +// Text("Non").tag(false) +// } label: { +// +// } +// .labelsHidden() +// +// Divider() +// NavigationLink { +// ClubsView() { club in +// if let event = tournament.eventObject() { +// event.club = club.id +// do { +// try dataStore.events.addOrUpdate(instance: event) +// } catch { +// Logger.error(error) +// } +// } +// _save() +// } +// } label: { +// Text("Changer de club") +// } +// } label: { +// Text("Membres prioritaires") +// Text(federalClub.acronym) +// } +// Divider() +// } else { +// NavigationLink { +// ClubsView() { club in +// if let event = tournament.eventObject() { +// event.club = club.id +// do { +// try dataStore.events.addOrUpdate(instance: event) +// } catch { +// Logger.error(error) +// } +// } +// _save() +// } +// } label: { +// Text("Identifier le club") +// } +// Divider() +// } +// } private func _teamFooterView(_ team: TeamRegistration) -> some View { HStack {