Adds TournamentLibrary to manage TournamentStore, that do not inherit Store from now on

sync2
Laurent 1 year ago
parent fee94a2b7d
commit 4b2dbf9c46
  1. 8
      PadelClub.xcodeproj/project.pbxproj
  2. 2
      PadelClub/Data/GroupStage.swift
  3. 3
      PadelClub/Data/MatchScheduler.swift
  4. 6
      PadelClub/Data/Round.swift
  5. 2
      PadelClub/Data/TeamRegistration.swift
  6. 2
      PadelClub/Data/Tournament.swift
  7. 27
      PadelClub/Data/TournamentLibrary.swift
  8. 37
      PadelClub/Data/TournamentStore.swift
  9. 2
      PadelClub/Utils/Patcher.swift

@ -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 = "<group>"; };
C49EF0412BE23BF50077B5AA /* PaymentTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PaymentTests.swift; sourceTree = "<group>"; };
C49EF0432BE286780077B5AA /* CryptoKey.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CryptoKey.swift; sourceTree = "<group>"; };
C4A36F572CE2626A003738C6 /* TournamentLibrary.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TournamentLibrary.swift; sourceTree = "<group>"; };
C4A47D592B6D383C00ADC637 /* Tournament.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Tournament.swift; sourceTree = "<group>"; };
C4A47D5D2B6D38EC00ADC637 /* DataStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DataStore.swift; sourceTree = "<group>"; };
C4A47D622B6D3D6500ADC637 /* Club.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Club.swift; sourceTree = "<group>"; };
@ -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 */,

@ -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

@ -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? {

@ -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)
}
}

@ -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

@ -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() {

@ -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
}
}

@ -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<GroupStage> = StoredCollection.placeholder()
fileprivate(set) var matches: StoredCollection<Match> = StoredCollection.placeholder()
@ -24,13 +22,20 @@ class TournamentStore: Store, ObservableObject {
fileprivate(set) var matchSchedulers: StoredCollection<MatchScheduler> = 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.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.loadCollectionsFromServerIfNoFile()
self.store.loadCollectionsFromServerIfNoFile()
}

@ -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 {

Loading…
Cancel
Save