diff --git a/PadelClub.xcodeproj/project.pbxproj b/PadelClub.xcodeproj/project.pbxproj index 8b64a60..1531660 100644 --- a/PadelClub.xcodeproj/project.pbxproj +++ b/PadelClub.xcodeproj/project.pbxproj @@ -126,6 +126,9 @@ C49EF03C2BE15AF80077B5AA /* String+Crypto.swift in Sources */ = {isa = PBXBuildFile; fileRef = C49EF03B2BE15AF80077B5AA /* String+Crypto.swift */; }; C49EF0422BE23BF50077B5AA /* PaymentTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C49EF0412BE23BF50077B5AA /* PaymentTests.swift */; }; C49EF0442BE286780077B5AA /* CryptoKey.swift in Sources */ = {isa = PBXBuildFile; fileRef = C49EF0432BE286780077B5AA /* CryptoKey.swift */; }; + C4A36F582CE2626A003738C6 /* TournamentLibrary.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4A36F572CE2626A003738C6 /* TournamentLibrary.swift */; }; + C4A36F592CE2626A003738C6 /* TournamentLibrary.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4A36F572CE2626A003738C6 /* TournamentLibrary.swift */; }; + C4A36F5A2CE2626A003738C6 /* TournamentLibrary.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4A36F572CE2626A003738C6 /* TournamentLibrary.swift */; }; C4A47D5A2B6D383C00ADC637 /* Tournament.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4A47D592B6D383C00ADC637 /* Tournament.swift */; }; C4A47D5E2B6D38EC00ADC637 /* DataStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4A47D5D2B6D38EC00ADC637 /* DataStore.swift */; }; C4A47D632B6D3D6500ADC637 /* Club.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4A47D622B6D3D6500ADC637 /* Club.swift */; }; @@ -1014,6 +1017,7 @@ C49EF03B2BE15AF80077B5AA /* String+Crypto.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "String+Crypto.swift"; sourceTree = ""; }; C49EF0412BE23BF50077B5AA /* PaymentTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PaymentTests.swift; sourceTree = ""; }; C49EF0432BE286780077B5AA /* CryptoKey.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CryptoKey.swift; sourceTree = ""; }; + C4A36F572CE2626A003738C6 /* TournamentLibrary.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TournamentLibrary.swift; sourceTree = ""; }; C4A47D592B6D383C00ADC637 /* Tournament.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Tournament.swift; sourceTree = ""; }; C4A47D5D2B6D38EC00ADC637 /* DataStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DataStore.swift; sourceTree = ""; }; C4A47D622B6D3D6500ADC637 /* Club.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Club.swift; sourceTree = ""; }; @@ -1489,6 +1493,7 @@ C411C9CC2BF21DAF003017AD /* README.md */, C4A47D5D2B6D38EC00ADC637 /* DataStore.swift */, C4FC2E2A2C2C0E4D0021F3BF /* TournamentStore.swift */, + C4A36F572CE2626A003738C6 /* TournamentLibrary.swift */, C4A47DAC2B85FCCD00ADC637 /* CustomUser.swift */, C4A47D592B6D383C00ADC637 /* Tournament.swift */, FF967CE72BAEC70100A9A3BD /* GroupStage.swift */, @@ -2537,6 +2542,7 @@ FF4AB6BB2B9256D50002987F /* SearchViewModel.swift in Sources */, FF967CF32BAECC0B00A9A3BD /* PlayerRegistration.swift in Sources */, FF4AB6BF2B92577A0002987F /* ImportedPlayerView.swift in Sources */, + C4A36F5A2CE2626A003738C6 /* TournamentLibrary.swift in Sources */, FF1162872BD004AD000C4809 /* EditingTeamView.swift in Sources */, FF6EC9062B947A1000EA7F5A /* NetworkManagerError.swift in Sources */, C4A47D5A2B6D383C00ADC637 /* Tournament.swift in Sources */, @@ -2915,6 +2921,7 @@ FF4CBFF32C996C0600151637 /* MatchListView.swift in Sources */, FF4CBFF42C996C0600151637 /* PadelClubApp.swift in Sources */, FF4CBFF52C996C0600151637 /* TournamentSettingsView.swift in Sources */, + C4A36F582CE2626A003738C6 /* TournamentLibrary.swift in Sources */, FF4CBFF62C996C0600151637 /* String+Crypto.swift in Sources */, FF4CBFF72C996C0600151637 /* GroupStageTeamReplacementView.swift in Sources */, FF4CBFF82C996C0600151637 /* TabItemModifier.swift in Sources */, @@ -3180,6 +3187,7 @@ FF70FB722C90584900129CC2 /* MatchListView.swift in Sources */, FF70FB732C90584900129CC2 /* PadelClubApp.swift in Sources */, FF70FB742C90584900129CC2 /* TournamentSettingsView.swift in Sources */, + C4A36F592CE2626A003738C6 /* TournamentLibrary.swift in Sources */, FF70FB752C90584900129CC2 /* String+Crypto.swift in Sources */, FF70FB762C90584900129CC2 /* GroupStageTeamReplacementView.swift in Sources */, FF70FB772C90584900129CC2 /* TabItemModifier.swift in Sources */, diff --git a/PadelClub/Data/GroupStage.swift b/PadelClub/Data/GroupStage.swift index e544016..c67559c 100644 --- a/PadelClub/Data/GroupStage.swift +++ b/PadelClub/Data/GroupStage.swift @@ -58,7 +58,7 @@ final class GroupStage: BaseGroupStage, SideStorable { } var tournamentStore: TournamentStore { - return TournamentStore.instance(tournamentId: self.tournament) + return TournamentLibrary.shared.store(tournamentId: self.tournament) } // MARK: - Computed dependencies diff --git a/PadelClub/Data/MatchScheduler.swift b/PadelClub/Data/MatchScheduler.swift index 27ee626..eccb7a4 100644 --- a/PadelClub/Data/MatchScheduler.swift +++ b/PadelClub/Data/MatchScheduler.swift @@ -90,7 +90,8 @@ final class MatchScheduler: BaseMatchScheduler, SideStorable { } var tournamentStore: TournamentStore { - return TournamentStore.instance(tournamentId: self.tournament) + return TournamentLibrary.shared.store(tournamentId: self.tournament) +// TournamentStore.instance(tournamentId: self.tournament) } func tournamentObject() -> Tournament? { diff --git a/PadelClub/Data/Round.swift b/PadelClub/Data/Round.swift index 957cdac..941f29e 100644 --- a/PadelClub/Data/Round.swift +++ b/PadelClub/Data/Round.swift @@ -50,7 +50,7 @@ final class Round: BaseRound, SideStorable { // MARK: - Computed dependencies var tournamentStore: TournamentStore { - return TournamentStore.instance(tournamentId: self.tournament) + return TournamentLibrary.shared.store(tournamentId: self.tournament) } func tournamentObject() -> Tournament? { @@ -200,7 +200,7 @@ defer { return previousMatch.teams().first } } else if let parent = upperBracketTopMatch(ofMatchIndex: match.index, previousRound: previousRound)?.losingTeamId { - return tournamentStore.findById(parent) + return self.store?.findById(parent) } case .two: if let luckyLoser = match.teamScores.first(where: { $0.luckyLoser == match.index * 2 + 1 }) { @@ -212,7 +212,7 @@ defer { return previousMatch.teams().first } } else if let parent = upperBracketBottomMatch(ofMatchIndex: match.index, previousRound: previousRound)?.losingTeamId { - return tournamentStore.findById(parent) + return self.store?.findById(parent) } } diff --git a/PadelClub/Data/TeamRegistration.swift b/PadelClub/Data/TeamRegistration.swift index 0b51e7d..996b737 100644 --- a/PadelClub/Data/TeamRegistration.swift +++ b/PadelClub/Data/TeamRegistration.swift @@ -73,7 +73,7 @@ final class TeamRegistration: BaseTeamRegistration, SideStorable { } var tournamentStore: TournamentStore { - return TournamentStore.instance(tournamentId: self.tournament) + return TournamentLibrary.shared.store(tournamentId: self.tournament) } // MARK: - Computed dependencies diff --git a/PadelClub/Data/Tournament.swift b/PadelClub/Data/Tournament.swift index f9d60eb..2ec009b 100644 --- a/PadelClub/Data/Tournament.swift +++ b/PadelClub/Data/Tournament.swift @@ -331,7 +331,7 @@ final class Tournament: BaseTournament { // } var tournamentStore: TournamentStore { - return TournamentStore.instance(tournamentId: self.id) + return TournamentLibrary.shared.store(tournamentId: self.id) } override func deleteDependencies() { diff --git a/PadelClub/Data/TournamentLibrary.swift b/PadelClub/Data/TournamentLibrary.swift new file mode 100644 index 0000000..948a65e --- /dev/null +++ b/PadelClub/Data/TournamentLibrary.swift @@ -0,0 +1,27 @@ +// +// TournamentLibrary.swift +// PadelClub +// +// Created by Laurent Morvillier on 11/11/2024. +// + +import Foundation +import LeStorage + +class TournamentLibrary { + + static let shared: TournamentLibrary = TournamentLibrary() + + fileprivate var _stores: [String : TournamentStore] = [:] + + func store(tournamentId: String) -> TournamentStore { + if let store = self._stores[tournamentId] { + return store + } + let store = StoreCenter.main.store(identifier: tournamentId) + let tournamentStore = TournamentStore(store: store) + self._stores[tournamentId] = tournamentStore + return tournamentStore + } + +} diff --git a/PadelClub/Data/TournamentStore.swift b/PadelClub/Data/TournamentStore.swift index 3413068..ef27a3a 100644 --- a/PadelClub/Data/TournamentStore.swift +++ b/PadelClub/Data/TournamentStore.swift @@ -9,11 +9,9 @@ import Foundation import LeStorage import SwiftUI -class TournamentStore: Store, ObservableObject { +class TournamentStore: ObservableObject { - static func instance(tournamentId: String) -> TournamentStore { - return StoreCenter.main.store(identifier: tournamentId, parameter: "tournament") - } + var store: Store fileprivate(set) var groupStages: StoredCollection = StoredCollection.placeholder() fileprivate(set) var matches: StoredCollection = StoredCollection.placeholder() @@ -24,13 +22,20 @@ class TournamentStore: Store, ObservableObject { fileprivate(set) var matchSchedulers: StoredCollection = StoredCollection.placeholder() - convenience init(tournament: Tournament) { - self.init(identifier: tournament.id, parameter: "tournament") +// convenience init(tournament: Tournament) { +// let store = StoreCenter.main.store(identifier: tournament.id) +// self.init(store: store) +// self._initialize() +// } + + init(store: Store) { + self.store = store + self._initialize() } - required init(identifier: String, parameter: String) { + fileprivate func _initialize() { - super.init(identifier: identifier, parameter: parameter) +// super.init(identifier: identifier, parameter: parameter) var synchronized: Bool = true let indexed: Bool = true @@ -41,15 +46,15 @@ class TournamentStore: Store, ObservableObject { } #endif - self.groupStages = self.registerSynchronizedCollection(indexed: indexed) - self.rounds = self.registerSynchronizedCollection(indexed: indexed) - self.teamRegistrations = self.registerSynchronizedCollection(indexed: indexed) - self.playerRegistrations = self.registerSynchronizedCollection(indexed: indexed) - self.matches = self.registerSynchronizedCollection(indexed: indexed) - self.teamScores = self.registerSynchronizedCollection(indexed: indexed) - self.matchSchedulers = self.registerCollection(indexed: indexed) - - self.loadCollectionsFromServerIfNoFile() + self.groupStages = self.store.registerSynchronizedCollection(indexed: indexed) + self.rounds = self.store.registerSynchronizedCollection(indexed: indexed) + self.teamRegistrations = self.store.registerSynchronizedCollection(indexed: indexed) + self.playerRegistrations = self.store.registerSynchronizedCollection(indexed: indexed) + self.matches = self.store.registerSynchronizedCollection(indexed: indexed) + self.teamScores = self.store.registerSynchronizedCollection(indexed: indexed) + self.matchSchedulers = self.store.registerCollection(indexed: indexed) + + self.store.loadCollectionsFromServerIfNoFile() } diff --git a/PadelClub/Utils/Patcher.swift b/PadelClub/Utils/Patcher.swift index 66a71ee..0ef8dc6 100644 --- a/PadelClub/Utils/Patcher.swift +++ b/PadelClub/Utils/Patcher.swift @@ -131,7 +131,7 @@ class Patcher { for tournament in DataStore.shared.tournaments { let store = tournament.tournamentStore - let identifier = StoreIdentifier(value: tournament.id, parameterName: "tournament") + let identifier = tournament.id Task {