diff --git a/PadelClub/Data/DataStore.swift b/PadelClub/Data/DataStore.swift index 4169d94..d453e1d 100644 --- a/PadelClub/Data/DataStore.swift +++ b/PadelClub/Data/DataStore.swift @@ -15,7 +15,7 @@ class DataStore: ObservableObject { @Published var user: User = User.placeHolder() { didSet { - let loggedUser = (user.username.count > 0) + let loggedUser = StoreCenter.main.userId != nil StoreCenter.main.collectionsCanSynchronize = loggedUser if loggedUser { @@ -37,15 +37,8 @@ class DataStore: ObservableObject { fileprivate(set) var clubs: StoredCollection fileprivate(set) var courts: StoredCollection fileprivate(set) var events: StoredCollection -// fileprivate(set) var groupStages: StoredCollection -// fileprivate(set) var matches: StoredCollection -// fileprivate(set) var teamRegistrations: StoredCollection -// fileprivate(set) var playerRegistrations: StoredCollection -// fileprivate(set) var rounds: StoredCollection -// fileprivate(set) var teamScores: StoredCollection fileprivate(set) var monthData: StoredCollection fileprivate(set) var dateIntervals: StoredCollection -// fileprivate(set) var matchSchedulers: StoredCollection fileprivate var userStorage: StoredSingleton @@ -90,15 +83,8 @@ class DataStore: ObservableObject { self.courts = store.registerCollection(synchronized: synchronized, indexed: indexed) self.tournaments = store.registerCollection(synchronized: synchronized, indexed: indexed) self.events = store.registerCollection(synchronized: synchronized, indexed: indexed) -// self.groupStages = store.registerCollection(synchronized: synchronized, indexed: indexed) -// self.teamScores = store.registerCollection(synchronized: synchronized, indexed: indexed) -// self.teamRegistrations = store.registerCollection(synchronized: synchronized, indexed: indexed) -// self.playerRegistrations = store.registerCollection(synchronized: synchronized, indexed: indexed) -// self.rounds = store.registerCollection(synchronized: synchronized, indexed: indexed) -// self.matches = store.registerCollection(synchronized: synchronized, indexed: indexed) self.monthData = store.registerCollection(synchronized: false, indexed: indexed) self.dateIntervals = store.registerCollection(synchronized: synchronized, indexed: indexed) -// self.matchSchedulers = store.registerCollection(synchronized: false, indexed: indexed) self.userStorage = store.registerObject(synchronized: synchronized) @@ -131,10 +117,7 @@ class DataStore: ObservableObject { self._fixMissingClubCreatorIfNecessary(clubsCollection) } else if let eventsCollection: StoredCollection = notification.object as? StoredCollection { self._fixMissingEventCreatorIfNecessary(eventsCollection) - } -// else if let purchaseCollection: StoredCollection = notification.object as? StoredCollection { -// try? purchaseCollection.deleteAll() -// } + } } fileprivate func _fixMissingClubCreatorIfNecessary(_ clubsCollection: StoredCollection) { @@ -172,9 +155,6 @@ class DataStore: ObservableObject { } } - self.user = self._temporaryLocalUser.item ?? User.placeHolder() - self.user.clubs.removeAll() - StoreCenter.main.disconnect() StoreCenter.main.collectionsCanSynchronize = false @@ -186,18 +166,13 @@ class DataStore: ObservableObject { self.clubs.reset() self.courts.reset() self.events.reset() - -// self.groupStages.reset() -// self.matches.reset() -// self.teamRegistrations.reset() -// self.playerRegistrations.reset() -// self.rounds.reset() -// self.teamScores.reset() self.dateIntervals.reset() -// self.matchSchedulers.reset() self.userStorage.reset() Guard.main.disconnect() + + self.user = self._temporaryLocalUser.item ?? User.placeHolder() + self.user.clubs.removeAll() } @@ -264,8 +239,6 @@ class DataStore: ObservableObject { // MARK: - Convenience func runningMatches() -> [Match] { -// let tournaments: [Tournament] = self.tournaments // foutu - let lastTournaments = self.tournaments.sorted(by: \Tournament.startDate).prefix(10) var runningMatches: [Match] = [] diff --git a/PadelClub/Data/TournamentStore.swift b/PadelClub/Data/TournamentStore.swift index 7a73ad1..a01e6a0 100644 --- a/PadelClub/Data/TournamentStore.swift +++ b/PadelClub/Data/TournamentStore.swift @@ -12,6 +12,9 @@ import SwiftUI class TournamentStore: Store, ObservableObject { static func instance(tournamentId: String) -> TournamentStore { +// if StoreCenter.main.userId == nil { +// fatalError("cant request store without id") +// } return StoreCenter.main.store(identifier: tournamentId, parameter: "tournament") } @@ -24,8 +27,6 @@ class TournamentStore: Store, ObservableObject { fileprivate(set) var matchSchedulers: StoredCollection = StoredCollection.placeholder() -// fileprivate(set) var loading: Bool = true - convenience init(tournament: Tournament) { self.init(identifier: tournament.id, parameter: "tournament") } @@ -51,6 +52,8 @@ class TournamentStore: Store, ObservableObject { self.teamScores = self.registerCollection(synchronized: synchronized, indexed: indexed) self.matchSchedulers = self.registerCollection(synchronized: false, indexed: indexed) + self.loadCollectionsFromServerIfNoFile() + } } diff --git a/PadelClub/Views/Tournament/Shared/TournamentCellView.swift b/PadelClub/Views/Tournament/Shared/TournamentCellView.swift index 9b2b0f3..6632893 100644 --- a/PadelClub/Views/Tournament/Shared/TournamentCellView.swift +++ b/PadelClub/Views/Tournament/Shared/TournamentCellView.swift @@ -22,8 +22,10 @@ struct TournamentCellView: View { } var body: some View { - ForEach(tournament.tournaments, id: \.id) { build in - _buildView(build, existingTournament: event?.existingBuild(build)) + if dataStore.tournaments.count > 0 { + ForEach(tournament.tournaments, id: \.id) { build in + _buildView(build, existingTournament: event?.existingBuild(build)) + } } }