Fixes and improvements

multistore
Laurent 1 year ago
parent 00a1ccf5f5
commit c2ad10e297
  1. 35
      PadelClub/Data/DataStore.swift
  2. 7
      PadelClub/Data/TournamentStore.swift
  3. 2
      PadelClub/Views/Tournament/Shared/TournamentCellView.swift

@ -15,7 +15,7 @@ class DataStore: ObservableObject {
@Published var user: User = User.placeHolder() { @Published var user: User = User.placeHolder() {
didSet { didSet {
let loggedUser = (user.username.count > 0) let loggedUser = StoreCenter.main.userId != nil
StoreCenter.main.collectionsCanSynchronize = loggedUser StoreCenter.main.collectionsCanSynchronize = loggedUser
if loggedUser { if loggedUser {
@ -37,15 +37,8 @@ class DataStore: ObservableObject {
fileprivate(set) var clubs: StoredCollection<Club> fileprivate(set) var clubs: StoredCollection<Club>
fileprivate(set) var courts: StoredCollection<Court> fileprivate(set) var courts: StoredCollection<Court>
fileprivate(set) var events: StoredCollection<Event> fileprivate(set) var events: StoredCollection<Event>
// fileprivate(set) var groupStages: StoredCollection<GroupStage>
// fileprivate(set) var matches: StoredCollection<Match>
// fileprivate(set) var teamRegistrations: StoredCollection<TeamRegistration>
// fileprivate(set) var playerRegistrations: StoredCollection<PlayerRegistration>
// fileprivate(set) var rounds: StoredCollection<Round>
// fileprivate(set) var teamScores: StoredCollection<TeamScore>
fileprivate(set) var monthData: StoredCollection<MonthData> fileprivate(set) var monthData: StoredCollection<MonthData>
fileprivate(set) var dateIntervals: StoredCollection<DateInterval> fileprivate(set) var dateIntervals: StoredCollection<DateInterval>
// fileprivate(set) var matchSchedulers: StoredCollection<MatchScheduler>
fileprivate var userStorage: StoredSingleton<User> fileprivate var userStorage: StoredSingleton<User>
@ -90,15 +83,8 @@ class DataStore: ObservableObject {
self.courts = store.registerCollection(synchronized: synchronized, indexed: indexed) self.courts = store.registerCollection(synchronized: synchronized, indexed: indexed)
self.tournaments = store.registerCollection(synchronized: synchronized, indexed: indexed) self.tournaments = store.registerCollection(synchronized: synchronized, indexed: indexed)
self.events = 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.monthData = store.registerCollection(synchronized: false, indexed: indexed)
self.dateIntervals = store.registerCollection(synchronized: synchronized, indexed: indexed) self.dateIntervals = store.registerCollection(synchronized: synchronized, indexed: indexed)
// self.matchSchedulers = store.registerCollection(synchronized: false, indexed: indexed)
self.userStorage = store.registerObject(synchronized: synchronized) self.userStorage = store.registerObject(synchronized: synchronized)
@ -132,9 +118,6 @@ class DataStore: ObservableObject {
} else if let eventsCollection: StoredCollection<Event> = notification.object as? StoredCollection<Event> { } else if let eventsCollection: StoredCollection<Event> = notification.object as? StoredCollection<Event> {
self._fixMissingEventCreatorIfNecessary(eventsCollection) self._fixMissingEventCreatorIfNecessary(eventsCollection)
} }
// else if let purchaseCollection: StoredCollection<Purchase> = notification.object as? StoredCollection<Purchase> {
// try? purchaseCollection.deleteAll()
// }
} }
fileprivate func _fixMissingClubCreatorIfNecessary(_ clubsCollection: StoredCollection<Club>) { fileprivate func _fixMissingClubCreatorIfNecessary(_ clubsCollection: StoredCollection<Club>) {
@ -172,9 +155,6 @@ class DataStore: ObservableObject {
} }
} }
self.user = self._temporaryLocalUser.item ?? User.placeHolder()
self.user.clubs.removeAll()
StoreCenter.main.disconnect() StoreCenter.main.disconnect()
StoreCenter.main.collectionsCanSynchronize = false StoreCenter.main.collectionsCanSynchronize = false
@ -186,19 +166,14 @@ class DataStore: ObservableObject {
self.clubs.reset() self.clubs.reset()
self.courts.reset() self.courts.reset()
self.events.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.dateIntervals.reset()
// self.matchSchedulers.reset()
self.userStorage.reset() self.userStorage.reset()
Guard.main.disconnect() Guard.main.disconnect()
self.user = self._temporaryLocalUser.item ?? User.placeHolder()
self.user.clubs.removeAll()
} }
func copyToLocalServer(tournament: Tournament) { func copyToLocalServer(tournament: Tournament) {
@ -264,8 +239,6 @@ class DataStore: ObservableObject {
// MARK: - Convenience // MARK: - Convenience
func runningMatches() -> [Match] { func runningMatches() -> [Match] {
// let tournaments: [Tournament] = self.tournaments // foutu
let lastTournaments = self.tournaments.sorted(by: \Tournament.startDate).prefix(10) let lastTournaments = self.tournaments.sorted(by: \Tournament.startDate).prefix(10)
var runningMatches: [Match] = [] var runningMatches: [Match] = []

@ -12,6 +12,9 @@ import SwiftUI
class TournamentStore: Store, ObservableObject { class TournamentStore: Store, ObservableObject {
static func instance(tournamentId: String) -> TournamentStore { 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") return StoreCenter.main.store(identifier: tournamentId, parameter: "tournament")
} }
@ -24,8 +27,6 @@ class TournamentStore: Store, ObservableObject {
fileprivate(set) var matchSchedulers: StoredCollection<MatchScheduler> = StoredCollection.placeholder() fileprivate(set) var matchSchedulers: StoredCollection<MatchScheduler> = StoredCollection.placeholder()
// fileprivate(set) var loading: Bool = true
convenience init(tournament: Tournament) { convenience init(tournament: Tournament) {
self.init(identifier: tournament.id, parameter: "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.teamScores = self.registerCollection(synchronized: synchronized, indexed: indexed)
self.matchSchedulers = self.registerCollection(synchronized: false, indexed: indexed) self.matchSchedulers = self.registerCollection(synchronized: false, indexed: indexed)
self.loadCollectionsFromServerIfNoFile()
} }
} }

@ -22,10 +22,12 @@ struct TournamentCellView: View {
} }
var body: some View { var body: some View {
if dataStore.tournaments.count > 0 {
ForEach(tournament.tournaments, id: \.id) { build in ForEach(tournament.tournaments, id: \.id) { build in
_buildView(build, existingTournament: event?.existingBuild(build)) _buildView(build, existingTournament: event?.existingBuild(build))
} }
} }
}
var teamCount: Int? { var teamCount: Int? {
if let tournament = tournament as? Tournament { if let tournament = tournament as? Tournament {

Loading…
Cancel
Save