From edc00f05151fa10ce111b21b959ea851cb138022 Mon Sep 17 00:00:00 2001 From: Laurent Date: Thu, 30 Jan 2025 17:52:00 +0100 Subject: [PATCH] fix issue with disconnection by making TournamentStore optional --- PadelClub/Data/DataStore.swift | 24 +-- PadelClub/Data/DrawLog.swift | 4 +- PadelClub/Data/GroupStage.swift | 32 ++-- PadelClub/Data/Match.swift | 62 ++++---- PadelClub/Data/MatchScheduler.swift | 8 +- PadelClub/Data/PlayerRegistration.swift | 6 +- PadelClub/Data/Round.swift | 82 +++++----- PadelClub/Data/TeamRegistration.swift | 58 ++++--- PadelClub/Data/TeamScore.swift | 8 +- PadelClub/Data/Tournament.swift | 146 ++++++++++-------- PadelClub/Data/TournamentLibrary.swift | 4 +- PadelClub/Utils/Patcher.swift | 2 +- .../Views/Calling/CallSettingsView.swift | 6 +- PadelClub/Views/Calling/CallView.swift | 4 +- PadelClub/Views/Calling/SendToAllView.swift | 8 +- .../Views/Calling/TeamsCallingView.swift | 6 +- .../Views/Cashier/CashierSettingsView.swift | 2 +- .../Components/GroupStageSettingsView.swift | 12 +- .../Components/GroupStageTeamView.swift | 4 +- .../Views/GroupStage/GroupStageView.swift | 10 +- .../GroupStage/GroupStagesSettingsView.swift | 22 +-- .../Views/GroupStage/GroupStagesView.swift | 2 +- .../LoserBracketFromGroupStageView.swift | 12 +- .../Match/Components/MatchDateView.swift | 2 +- PadelClub/Views/Match/MatchDetailView.swift | 22 ++- PadelClub/Views/Match/MatchRowView.swift | 2 +- PadelClub/Views/Match/MatchSetupView.swift | 34 ++-- .../Navigation/Toolbox/ToolboxView.swift | 53 ++++--- .../GroupStageScheduleEditorView.swift | 4 +- .../LoserRoundScheduleEditorView.swift | 4 +- .../LoserRoundStepScheduleEditorView.swift | 4 +- .../Planning/MatchScheduleEditorView.swift | 4 +- .../Views/Planning/PlanningSettingsView.swift | 24 +-- PadelClub/Views/Planning/PlanningView.swift | 4 +- .../Planning/RoundScheduleEditorView.swift | 4 +- PadelClub/Views/Planning/SchedulerView.swift | 10 +- .../Components/PlayerSexPickerView.swift | 6 +- PadelClub/Views/Player/PlayerDetailView.swift | 6 +- PadelClub/Views/Player/PlayerView.swift | 4 +- PadelClub/Views/Round/DrawLogsView.swift | 2 +- .../Views/Round/LoserRoundSettingsView.swift | 8 +- PadelClub/Views/Round/LoserRoundView.swift | 2 +- PadelClub/Views/Round/RoundSettingsView.swift | 14 +- PadelClub/Views/Round/RoundView.swift | 8 +- PadelClub/Views/Score/EditScoreView.swift | 2 +- PadelClub/Views/Team/CoachListView.swift | 2 +- PadelClub/Views/Team/EditingTeamView.swift | 16 +- .../Views/Tournament/FileImportView.swift | 6 +- .../Views/Tournament/Screen/AddTeamView.swift | 10 +- .../Components/InscriptionInfoView.swift | 16 +- .../TournamentGeneralSettingsView.swift | 4 +- .../TournamentMatchFormatsSettingsView.swift | 6 +- .../Components/UpdateSourceRankDateView.swift | 6 +- .../Screen/InscriptionManagerView.swift | 6 +- .../Screen/TableStructureView.swift | 2 +- .../Screen/TournamentCashierView.swift | 52 ++++--- .../Screen/TournamentRankView.swift | 12 +- 57 files changed, 477 insertions(+), 408 deletions(-) diff --git a/PadelClub/Data/DataStore.swift b/PadelClub/Data/DataStore.swift index 67fed87..05a2984 100644 --- a/PadelClub/Data/DataStore.swift +++ b/PadelClub/Data/DataStore.swift @@ -302,10 +302,12 @@ class DataStore: ObservableObject { var runningMatches: [Match] = [] for tournament in lastTournaments { - let matches = tournament.tournamentStore.matches.filter { match in - match.disabled == false && match.isRunning() + if let store = tournament.tournamentStore { + let matches = store.matches.filter { match in + match.disabled == false && match.isRunning() + } + runningMatches.append(contentsOf: matches) } - runningMatches.append(contentsOf: matches) } return runningMatches } @@ -317,9 +319,11 @@ class DataStore: ObservableObject { var runningMatches: [Match] = [] for tournament in lastTournaments { - let matches = tournament.tournamentStore.matches.filter { match in - match.disabled == false && match.startDate != nil && match.endDate == nil } - runningMatches.append(contentsOf: matches) + if let store = tournament.tournamentStore { + let matches = store.matches.filter { match in + match.disabled == false && match.startDate != nil && match.endDate == nil } + runningMatches.append(contentsOf: matches) + } } return runningMatches } @@ -330,9 +334,11 @@ class DataStore: ObservableObject { var runningMatches: [Match] = [] for tournament in lastTournaments { - let matches = tournament.tournamentStore.matches.filter { match in - match.disabled == false && match.hasEnded() } - runningMatches.append(contentsOf: matches) + if let store = tournament.tournamentStore { + let matches = store.matches.filter { match in + match.disabled == false && match.hasEnded() } + runningMatches.append(contentsOf: matches) + } } return runningMatches.sorted(by: \.endDate!, order: .descending) } diff --git a/PadelClub/Data/DrawLog.swift b/PadelClub/Data/DrawLog.swift index f2195ab..a0be6e3 100644 --- a/PadelClub/Data/DrawLog.swift +++ b/PadelClub/Data/DrawLog.swift @@ -52,7 +52,7 @@ final class DrawLog: BaseDrawLog, SideStorable { switch drawType { case .seed: let roundIndex = RoundRule.roundIndex(fromMatchIndex: drawMatchIndex) - return tournamentStore.rounds.first(where: { $0.parent == nil && $0.index == roundIndex })?._matches().first(where: { $0.index == drawMatchIndex }) + return tournamentStore?.rounds.first(where: { $0.parent == nil && $0.index == roundIndex })?._matches().first(where: { $0.index == drawMatchIndex }) default: return nil } @@ -70,7 +70,7 @@ final class DrawLog: BaseDrawLog, SideStorable { return drawMatch()?.matchTitle() ?? "" } - var tournamentStore: TournamentStore { + var tournamentStore: TournamentStore? { return TournamentLibrary.shared.store(tournamentId: self.tournament) } diff --git a/PadelClub/Data/GroupStage.swift b/PadelClub/Data/GroupStage.swift index d2b5319..3542da6 100644 --- a/PadelClub/Data/GroupStage.swift +++ b/PadelClub/Data/GroupStage.swift @@ -22,14 +22,15 @@ final class GroupStage: BaseGroupStage, SideStorable { } } - var tournamentStore: TournamentStore { + var tournamentStore: TournamentStore? { return TournamentLibrary.shared.store(tournamentId: self.tournament) } // MARK: - Computed dependencies func _matches() -> [Match] { - return self.tournamentStore.matches.filter { $0.groupStage == self.id }.sorted(by: \.index) + guard let tournamentStore = self.tournamentStore else { return [] } + return tournamentStore.matches.filter { $0.groupStage == self.id }.sorted(by: \.index) // Store.main.filter { $0.groupStage == self.id } } @@ -101,7 +102,7 @@ final class GroupStage: BaseGroupStage, SideStorable { returnMatches = returnMatches.filter({ $0.index >= matchCount * matchPhaseCount }) } do { - try self.tournamentStore.matches.delete(contentOfs: returnMatches) + try self.tournamentStore?.matches.delete(contentOfs: returnMatches) } catch { Logger.error(error) } @@ -127,8 +128,8 @@ final class GroupStage: BaseGroupStage, SideStorable { matches.append(newMatch) } - self.tournamentStore.matches.addOrUpdate(contentOfs: matches) - self.tournamentStore.teamScores.addOrUpdate(contentOfs: teamScores) + self.tournamentStore?.matches.addOrUpdate(contentOfs: matches) + self.tournamentStore?.teamScores.addOrUpdate(contentOfs: teamScores) } func buildMatches(keepExistingMatches: Bool = false) { @@ -153,8 +154,8 @@ final class GroupStage: BaseGroupStage, SideStorable { } do { - try self.tournamentStore.matches.addOrUpdate(contentOfs: matches) - try self.tournamentStore.teamScores.addOrUpdate(contentOfs: teamScores) + try self.tournamentStore?.matches.addOrUpdate(contentOfs: matches) + try self.tournamentStore?.teamScores.addOrUpdate(contentOfs: teamScores) } catch { Logger.error(error) } @@ -192,11 +193,11 @@ final class GroupStage: BaseGroupStage, SideStorable { tournamentObject()?.shouldVerifyBracket = true } } - try self.tournamentStore.teamRegistrations.addOrUpdate(contentOfs: teams) + try self.tournamentStore?.teamRegistrations.addOrUpdate(contentOfs: teams) if let tournamentObject = tournamentObject() { try DataStore.shared.tournaments.addOrUpdate(instance: tournamentObject) } - self.tournamentStore.teamRegistrations.addOrUpdate(contentOfs: teams) + self.tournamentStore?.teamRegistrations.addOrUpdate(contentOfs: teams) let groupStagesAreOverAtFirstStep = tournament.groupStagesAreOver(atStep: 0) let nextStepGroupStages = tournament.groupStages(atStep: 1) @@ -388,7 +389,7 @@ final class GroupStage: BaseGroupStage, SideStorable { } private func _removeMatches() { - self.tournamentStore.matches.delete(contentOfs: _matches()) + self.tournamentStore?.matches.delete(contentOfs: _matches()) } private func _numberOfMatchesToBuild() -> Int { @@ -444,10 +445,11 @@ final class GroupStage: BaseGroupStage, SideStorable { } func unsortedTeams() -> [TeamRegistration] { + guard let tournamentStore = self.tournamentStore else { return [] } if step > 0 { - return self.tournamentStore.groupStages.filter({ $0.step == step - 1 }).compactMap({ $0.teams(true)[safe: index] }) + return tournamentStore.groupStages.filter({ $0.step == step - 1 }).compactMap({ $0.teams(true)[safe: index] }) } - return self.tournamentStore.teamRegistrations.filter { $0.groupStage == self.id && $0.groupStagePosition != nil } + return tournamentStore.teamRegistrations.filter { $0.groupStage == self.id && $0.groupStagePosition != nil } } @@ -570,7 +572,7 @@ final class GroupStage: BaseGroupStage, SideStorable { playedMatches.forEach { match in match.matchFormat = matchFormat } - self.tournamentStore.matches.addOrUpdate(contentOfs: playedMatches) + self.tournamentStore?.matches.addOrUpdate(contentOfs: playedMatches) } func pasteData() -> String { @@ -593,7 +595,7 @@ final class GroupStage: BaseGroupStage, SideStorable { for match in matches { match.deleteDependencies() } - self.tournamentStore.matches.deleteDependencies(matches) + self.tournamentStore?.matches.deleteDependencies(matches) } // init(from decoder: Decoder) throws { @@ -627,7 +629,7 @@ final class GroupStage: BaseGroupStage, SideStorable { // } func insertOnServer() { - self.tournamentStore.groupStages.writeChangeAndInsertOnServer(instance: self) + self.tournamentStore?.groupStages.writeChangeAndInsertOnServer(instance: self) for match in self._matches() { match.insertOnServer() } diff --git a/PadelClub/Data/Match.swift b/PadelClub/Data/Match.swift index 43f177b..385913f 100644 --- a/PadelClub/Data/Match.swift +++ b/PadelClub/Data/Match.swift @@ -46,7 +46,7 @@ final class Match: BaseMatch, SideStorable { } } - var tournamentStore: TournamentStore { + var tournamentStore: TournamentStore? { if let id = self.store?.identifier { return TournamentLibrary.shared.store(tournamentId: id) } @@ -60,7 +60,8 @@ final class Match: BaseMatch, SideStorable { // MARK: - Computed dependencies var teamScores: [TeamScore] { - return self.tournamentStore.teamScores.filter { $0.match == self.id } + guard let tournamentStore = self.tournamentStore else { return [] } + return tournamentStore.teamScores.filter { $0.match == self.id } } // MARK: - @@ -70,7 +71,7 @@ final class Match: BaseMatch, SideStorable { for teamScore in teamScores { teamScore.deleteDependencies() } - self.tournamentStore.teamScores.deleteDependencies(teamScores) + self.tournamentStore?.teamScores.deleteDependencies(teamScores) } func indexInRound(in matches: [Match]? = nil) -> Int { @@ -149,12 +150,12 @@ defer { func winner() -> TeamRegistration? { guard let winningTeamId else { return nil } - return self.tournamentStore.teamRegistrations.findById(winningTeamId) + return self.tournamentStore?.teamRegistrations.findById(winningTeamId) } func loser() -> TeamRegistration? { guard let losingTeamId else { return nil } - return self.tournamentStore.teamRegistrations.findById(losingTeamId) + return self.tournamentStore?.teamRegistrations.findById(losingTeamId) } @@ -183,7 +184,7 @@ defer { endDate = nil followingMatch()?.cleanScheduleAndSave(nil) _loserMatch()?.cleanScheduleAndSave(nil) - self.tournamentStore.matches.addOrUpdate(instance: self) + self.tournamentStore?.matches.addOrUpdate(instance: self) } func resetMatch() { @@ -200,14 +201,14 @@ defer { func resetScores() { teamScores.forEach({ $0.score = nil }) - self.tournamentStore.teamScores.addOrUpdate(contentOfs: teamScores) + self.tournamentStore?.teamScores.addOrUpdate(contentOfs: teamScores) } func teamWillBeWalkOut(_ team: TeamRegistration) { resetMatch() let existingTeamScore = teamScore(ofTeam: team) ?? TeamScore(match: id, team: team) existingTeamScore.walkOut = 1 - self.tournamentStore.teamScores.addOrUpdate(instance: existingTeamScore) + self.tournamentStore?.teamScores.addOrUpdate(instance: existingTeamScore) } func luckyLosers() -> [TeamRegistration] { @@ -225,11 +226,11 @@ defer { let position = matchIndex * 2 + teamPosition.rawValue let previousScores = teamScores.filter({ $0.luckyLoser == position }) - self.tournamentStore.teamScores.delete(contentOfs: previousScores) + self.tournamentStore?.teamScores.delete(contentOfs: previousScores) let teamScoreLuckyLoser = teamScore(ofTeam: team) ?? TeamScore(match: id, team: team) teamScoreLuckyLoser.luckyLoser = position - self.tournamentStore.teamScores.addOrUpdate(instance: teamScoreLuckyLoser) + self.tournamentStore?.teamScores.addOrUpdate(instance: teamScoreLuckyLoser) } func disableMatch() { @@ -264,14 +265,14 @@ defer { let isTopMatch = topMatchIndex + 1 == index let lookingForIndex = isTopMatch ? topMatchIndex : bottomMatchIndex - return self.tournamentStore.matches.first(where: { $0.round == round && $0.index == lookingForIndex }) + return self.tournamentStore?.matches.first(where: { $0.round == round && $0.index == lookingForIndex }) } private func _forwardMatch(inRound round: Round) -> Match? { guard let roundObjectNextRound = round.nextRound() else { return nil } let nextIndex = (index - 1) / 2 - return self.tournamentStore.matches.first(where: { $0.round == roundObjectNextRound.id && $0.index == nextIndex }) + return self.tournamentStore?.matches.first(where: { $0.round == roundObjectNextRound.id && $0.index == nextIndex }) } func _toggleForwardMatchDisableState(_ state: Bool) { @@ -339,7 +340,7 @@ defer { if disabled != currentState { do { - try self.tournamentStore.teamScores.delete(contentOfs: teamScores) + try self.tournamentStore?.teamScores.delete(contentOfs: teamScores) } catch { Logger.error(error) } @@ -349,7 +350,7 @@ defer { for team in teams { if isSeededBy(team: team) { team.bracketPosition = nil - self.tournamentStore.teamRegistrations.addOrUpdate(instance: team) + self.tournamentStore?.teamRegistrations.addOrUpdate(instance: team) } } } @@ -357,7 +358,7 @@ defer { roundObject?._cachedSeedInterval = nil name = nil do { - try self.tournamentStore.matches.addOrUpdate(instance: self) + try self.tournamentStore?.matches.addOrUpdate(instance: self) } catch { Logger.error(error) } @@ -374,7 +375,8 @@ defer { } func next() -> Match? { - let matches: [Match] = self.tournamentStore.matches.filter { $0.round == round && $0.index > index && $0.disabled == false } + guard let tournamentStore = self.tournamentStore else { return nil } + let matches: [Match] = tournamentStore.matches.filter { $0.round == round && $0.index > index && $0.disabled == false } return matches.sorted(by: \.index).first } @@ -384,7 +386,7 @@ defer { } func getFollowingMatch(fromNextRoundId nextRoundId: String) -> Match? { - return self.tournamentStore.matches.first(where: { $0.round == nextRoundId && $0.index == (index - 1) / 2 }) + return self.tournamentStore?.matches.first(where: { $0.round == nextRoundId && $0.index == (index - 1) / 2 }) } func getDuration() -> Int { @@ -417,7 +419,7 @@ defer { guard let roundObject else { return nil } let topPreviousRoundMatchIndex = topPreviousRoundMatchIndex() let roundObjectPreviousRoundId = roundObject.previousRound()?.id - return self.tournamentStore.matches.first(where: { match in + return self.tournamentStore?.matches.first(where: { match in match.round != nil && match.round == roundObjectPreviousRoundId && match.index == topPreviousRoundMatchIndex }) } @@ -426,7 +428,7 @@ defer { guard let roundObject else { return nil } let bottomPreviousRoundMatchIndex = bottomPreviousRoundMatchIndex() let roundObjectPreviousRoundId = roundObject.previousRound()?.id - return self.tournamentStore.matches.first(where: { match in + return self.tournamentStore?.matches.first(where: { match in match.round != nil && match.round == roundObjectPreviousRoundId && match.index == bottomPreviousRoundMatchIndex }) } @@ -463,8 +465,10 @@ defer { func previousMatches() -> [Match] { guard let roundObject else { return [] } + guard let tournamentStore = self.tournamentStore else { return [] } + let roundObjectPreviousRoundId = roundObject.previousRound()?.id - return self.tournamentStore.matches.filter { match in + return tournamentStore.matches.filter { match in match.round == roundObjectPreviousRoundId && (match.index == topPreviousRoundMatchIndex() || match.index == bottomPreviousRoundMatchIndex()) }.sorted(by: \.index) } @@ -483,7 +487,7 @@ defer { teamScoreWalkout.walkOut = 0 let teamScoreWinning = teamScore(teamPosition.otherTeam) ?? TeamScore(match: id, team: team(teamPosition.otherTeam)) teamScoreWinning.walkOut = nil - self.tournamentStore.teamScores.addOrUpdate(contentOfs: [teamScoreWalkout, teamScoreWinning]) + self.tournamentStore?.teamScores.addOrUpdate(contentOfs: [teamScoreWalkout, teamScoreWinning]) if endDate == nil { endDate = Date() @@ -542,7 +546,7 @@ defer { teamScoreOne.score = matchDescriptor.teamOneScores.joined(separator: ",") let teamScoreTwo = teamScore(.two) ?? TeamScore(match: id, team: team(.two)) teamScoreTwo.score = matchDescriptor.teamTwoScores.joined(separator: ",") - self.tournamentStore.teamScores.addOrUpdate(contentOfs: [teamScoreOne, teamScoreTwo]) + self.tournamentStore?.teamScores.addOrUpdate(contentOfs: [teamScoreOne, teamScoreTwo]) matchFormat = matchDescriptor.matchFormat } @@ -555,7 +559,7 @@ defer { let ids = newTeamScores.map { $0.id } let teamScores = teamScores.filter({ ids.contains($0.id) == false }) if teamScores.isEmpty == false { - self.tournamentStore.teamScores.delete(contentOfs: teamScores) + self.tournamentStore?.teamScores.delete(contentOfs: teamScores) followingMatch()?.resetTeamScores(outsideOf: []) _loserMatch()?.resetTeamScores(outsideOf: []) } @@ -578,7 +582,7 @@ defer { func updateTeamScores() { let teams = getOrCreateTeamScores() - self.tournamentStore.teamScores.addOrUpdate(contentOfs: teams) + self.tournamentStore?.teamScores.addOrUpdate(contentOfs: teams) resetTeamScores(outsideOf: teams) if teams.isEmpty == false { updateFollowingMatchTeamScore() @@ -732,7 +736,8 @@ defer { } func scores() -> [TeamScore] { - return self.tournamentStore.teamScores.filter { $0.match == id } + guard let tournamentStore = self.tournamentStore else { return [] } + return tournamentStore.teamScores.filter { $0.match == id } } func teams() -> [TeamRegistration] { @@ -864,13 +869,12 @@ defer { var roundObject: Round? { guard let round else { return nil } - return self.tournamentStore.rounds.findById(round) + return self.tournamentStore?.rounds.findById(round) } var groupStageObject: GroupStage? { guard let groupStage else { return nil } - return self.tournamentStore.groupStages.findById(groupStage) -// self.store?.findById(group) + return self.tournamentStore?.groupStages.findById(groupStage) } var isLoserBracket: Bool { @@ -999,7 +1003,7 @@ defer { } func insertOnServer() { - self.tournamentStore.matches.writeChangeAndInsertOnServer(instance: self) + self.tournamentStore?.matches.writeChangeAndInsertOnServer(instance: self) for teamScore in self.teamScores { teamScore.insertOnServer() } diff --git a/PadelClub/Data/MatchScheduler.swift b/PadelClub/Data/MatchScheduler.swift index c0845a4..e7ad809 100644 --- a/PadelClub/Data/MatchScheduler.swift +++ b/PadelClub/Data/MatchScheduler.swift @@ -58,7 +58,7 @@ final class MatchScheduler: BaseMatchScheduler, SideStorable { return tournamentObject()?.additionalEstimationDuration ?? 0 } - var tournamentStore: TournamentStore { + var tournamentStore: TournamentStore? { return TournamentLibrary.shared.store(tournamentId: self.tournament) // TournamentStore.instance(tournamentId: self.tournament) } @@ -120,7 +120,7 @@ final class MatchScheduler: BaseMatchScheduler, SideStorable { groupStages.filter({ $0.startDate == nil || times.contains($0.startDate!) == false }).chunked(into: computedGroupStageChunkCount).forEach { groups in groups.forEach({ $0.startDate = lastDate }) do { - try self.tournamentStore.groupStages.addOrUpdate(contentOfs: groups) + try self.tournamentStore?.groupStages.addOrUpdate(contentOfs: groups) } catch { Logger.error(error) } @@ -141,7 +141,7 @@ final class MatchScheduler: BaseMatchScheduler, SideStorable { } } do { - try self.tournamentStore.matches.addOrUpdate(contentOfs: matches) + try self.tournamentStore?.matches.addOrUpdate(contentOfs: matches) } catch { Logger.error(error) } @@ -739,7 +739,7 @@ final class MatchScheduler: BaseMatchScheduler, SideStorable { } do { - try self.tournamentStore.matches.addOrUpdate(contentOfs: allMatches) + try self.tournamentStore?.matches.addOrUpdate(contentOfs: allMatches) } catch { Logger.error(error) } diff --git a/PadelClub/Data/PlayerRegistration.swift b/PadelClub/Data/PlayerRegistration.swift index 86efd29..e6713b8 100644 --- a/PadelClub/Data/PlayerRegistration.swift +++ b/PadelClub/Data/PlayerRegistration.swift @@ -124,7 +124,7 @@ final class PlayerRegistration: BasePlayerRegistration, SideStorable { try super.init(from: decoder) } - var tournamentStore: TournamentStore { + var tournamentStore: TournamentStore? { guard let storeId else { fatalError("missing store id for \(String(describing: type(of: self)))") } @@ -198,7 +198,7 @@ final class PlayerRegistration: BasePlayerRegistration, SideStorable { func team() -> TeamRegistration? { guard let teamRegistration else { return nil } - return self.tournamentStore.teamRegistrations.findById(teamRegistration) + return self.tournamentStore?.teamRegistrations.findById(teamRegistration) } func isHere() -> Bool { @@ -417,7 +417,7 @@ final class PlayerRegistration: BasePlayerRegistration, SideStorable { } func insertOnServer() { - self.tournamentStore.playerRegistrations.writeChangeAndInsertOnServer(instance: self) + self.tournamentStore?.playerRegistrations.writeChangeAndInsertOnServer(instance: self) } } diff --git a/PadelClub/Data/Round.swift b/PadelClub/Data/Round.swift index ddc0270..f0514d1 100644 --- a/PadelClub/Data/Round.swift +++ b/PadelClub/Data/Round.swift @@ -34,7 +34,7 @@ final class Round: BaseRound, SideStorable { // MARK: - Computed dependencies - var tournamentStore: TournamentStore { + var tournamentStore: TournamentStore? { return TournamentLibrary.shared.store(tournamentId: self.tournament) } @@ -43,13 +43,13 @@ final class Round: BaseRound, SideStorable { } func _matches() -> [Match] { - return self.tournamentStore.matches.filter { $0.round == self.id }.sorted(by: \.index) -// return Store.main.filter { $0.round == self.id } + guard let tournamentStore = self.tournamentStore else { return [] } + return tournamentStore.matches.filter { $0.round == self.id }.sorted(by: \.index) } func getDisabledMatches() -> [Match] { - return self.tournamentStore.matches.filter { $0.round == self.id && $0.disabled == true } -// return Store.main.filter { $0.round == self.id && $0.disabled == true } + guard let tournamentStore = self.tournamentStore else { return [] } + return tournamentStore.matches.filter { $0.round == self.id && $0.disabled == true } } // MARK: - @@ -86,8 +86,9 @@ final class Round: BaseRound, SideStorable { func previousMatches(ofMatch match: Match) -> [Match] { guard let previousRound = previousRound() else { return [] } + guard let tournamentStore = self.tournamentStore else { return [] } - return self.tournamentStore.matches.filter { + return tournamentStore.matches.filter { $0.round == previousRound.id && ($0.index == match.topPreviousRoundMatchIndex() || $0.index == match.bottomPreviousRoundMatchIndex()) } @@ -114,7 +115,7 @@ final class Round: BaseRound, SideStorable { } func seed(_ team: TeamPosition, inMatchIndex matchIndex: Int) -> TeamRegistration? { - return self.tournamentStore.teamRegistrations.first(where: { + return self.tournamentStore?.teamRegistrations.first(where: { $0.bracketPosition != nil && ($0.bracketPosition! / 2) == matchIndex && ($0.bracketPosition! % 2) == team.rawValue @@ -122,7 +123,8 @@ final class Round: BaseRound, SideStorable { } func seeds(inMatchIndex matchIndex: Int) -> [TeamRegistration] { - return self.tournamentStore.teamRegistrations.filter { + guard let tournamentStore = self.tournamentStore else { return [] } + return tournamentStore.teamRegistrations.filter { $0.tournament == tournament && $0.bracketPosition != nil @@ -137,9 +139,10 @@ final class Round: BaseRound, SideStorable { } func seeds() -> [TeamRegistration] { + guard let tournamentStore = self.tournamentStore else { return [] } let initialMatchIndex = RoundRule.matchIndex(fromRoundIndex: index) let numberOfMatches = RoundRule.numberOfMatches(forRoundIndex: index) - return self.tournamentStore.teamRegistrations.filter { + return tournamentStore.teamRegistrations.filter { $0.bracketPosition != nil && ($0.bracketPosition! / 2) >= initialMatchIndex && ($0.bracketPosition! / 2) < initialMatchIndex + numberOfMatches @@ -158,12 +161,12 @@ final class Round: BaseRound, SideStorable { func losers() -> [TeamRegistration] { let teamIds: [String] = self._matches().compactMap { $0.losingTeamId } - return teamIds.compactMap { self.tournamentStore.teamRegistrations.findById($0) } + return teamIds.compactMap { self.tournamentStore?.teamRegistrations.findById($0) } } func winners() -> [TeamRegistration] { let teamIds: [String] = self._matches().compactMap { $0.winningTeamId } - return teamIds.compactMap { self.tournamentStore.teamRegistrations.findById($0) } + return teamIds.compactMap { self.tournamentStore?.teamRegistrations.findById($0) } } func teams() -> [TeamRegistration] { @@ -188,7 +191,7 @@ defer { return luckyLoser.team } else if let previousMatch = topPreviousRoundMatch(ofMatch: match, previousRound: previousRound) { if let teamId = previousMatch.winningTeamId { - return self.tournamentStore.teamRegistrations.findById(teamId) + return self.tournamentStore?.teamRegistrations.findById(teamId) } else if previousMatch.disabled { return previousMatch.teams().first } @@ -200,7 +203,7 @@ defer { return luckyLoser.team } else if let previousMatch = bottomPreviousRoundMatch(ofMatch: match, previousRound: previousRound) { if let teamId = previousMatch.winningTeamId { - return self.tournamentStore.teamRegistrations.findById(teamId) + return self.tournamentStore?.teamRegistrations.findById(teamId) } else if previousMatch.disabled { return previousMatch.teams().first } @@ -265,7 +268,7 @@ defer { guard let previousRound else { return nil } let topPreviousRoundMatchIndex = match.topPreviousRoundMatchIndex() - return self.tournamentStore.matches.first(where: { + return self.tournamentStore?.matches.first(where: { $0.round == previousRound.id && $0.index == topPreviousRoundMatchIndex }) } @@ -281,20 +284,21 @@ defer { guard let previousRound else { return nil } let bottomPreviousRoundMatchIndex = match.bottomPreviousRoundMatchIndex() - return self.tournamentStore.matches.first(where: { + return self.tournamentStore?.matches.first(where: { $0.round == previousRound.id && $0.index == bottomPreviousRoundMatchIndex }) } func getMatch(atMatchIndexInRound matchIndexInRound: Int) -> Match? { - self.tournamentStore.matches.first(where: { + self.tournamentStore?.matches.first(where: { let index = RoundRule.matchIndexWithinRound(fromMatchIndex: $0.index) return $0.round == id && index == matchIndexInRound }) } func enabledMatches() -> [Match] { - return self.tournamentStore.matches.filter { $0.round == self.id && $0.disabled == false }.sorted(by: \.index) + guard let tournamentStore = self.tournamentStore else { return [] } + return tournamentStore.matches.filter { $0.round == self.id && $0.disabled == false }.sorted(by: \.index) } // func displayableMatches() -> [Match] { @@ -331,11 +335,11 @@ defer { print("func previousRound of: ", id, duration.formatted(.units(allowed: [.seconds, .milliseconds]))) } #endif - return self.tournamentStore.rounds.first(where: { $0.parent == parent && $0.index == index + 1 }) + return self.tournamentStore?.rounds.first(where: { $0.parent == parent && $0.index == index + 1 }) } func nextRound() -> Round? { - return self.tournamentStore.rounds.first(where: { $0.parent == parent && $0.index == index - 1 }) + return self.tournamentStore?.rounds.first(where: { $0.parent == parent && $0.index == index - 1 }) } func loserRounds(forRoundIndex roundIndex: Int) -> [Round] { @@ -402,7 +406,7 @@ defer { // Logger.error(error) // } } - self.tournamentStore.matches.addOrUpdate(contentOfs: _matches) + self.tournamentStore?.matches.addOrUpdate(contentOfs: _matches) } var cumulativeMatchCount: Int { @@ -462,9 +466,12 @@ defer { } #endif let initialMatchIndexFromRoundIndex = RoundRule.matchIndex(fromRoundIndex: index) - let seedsAfterThisRound: [TeamRegistration] = self.tournamentStore.teamRegistrations.filter { - $0.bracketPosition != nil - && ($0.bracketPosition! / 2) < initialMatchIndexFromRoundIndex + var seedsAfterThisRound: [TeamRegistration] = [] + if let tournamentStore = tournamentStore { + seedsAfterThisRound = tournamentStore.teamRegistrations.filter { + $0.bracketPosition != nil + && ($0.bracketPosition! / 2) < initialMatchIndexFromRoundIndex + } } // let seedsAfterThisRound : [TeamRegistration] = Store.main.filter(isIncluded: { @@ -523,9 +530,12 @@ defer { //print(seedInterval.localizedLabel()) return seedInterval } else { - let seedsAfterThisRound : [TeamRegistration] = self.tournamentStore.teamRegistrations.filter { - $0.bracketPosition != nil - && ($0.bracketPosition! / 2) < initialMatchIndexFromRoundIndex + var seedsAfterThisRound : [TeamRegistration] = [] + if let tournamentStore = self.tournamentStore { + seedsAfterThisRound = tournamentStore.teamRegistrations.filter { + $0.bracketPosition != nil + && ($0.bracketPosition! / 2) < initialMatchIndexFromRoundIndex + } } var seedsCount = seedsAfterThisRound.count @@ -594,7 +604,7 @@ defer { } func loserRounds() -> [Round] { - + guard let tournamentStore = self.tournamentStore else { return [] } #if _DEBUG_TIME //DEBUGING TIME let start = Date() defer { @@ -603,7 +613,7 @@ defer { } #endif - return self.tournamentStore.rounds.filter( { $0.parent == id }).sorted(by: \.index).reversed() + return tournamentStore.rounds.filter( { $0.parent == id }).sorted(by: \.index).reversed() } func loserRoundsAndChildren() -> [Round] { @@ -621,7 +631,7 @@ defer { func deleteLoserBracket() { let loserRounds = loserRounds() - self.tournamentStore.rounds.delete(contentOfs: loserRounds) + self.tournamentStore?.rounds.delete(contentOfs: loserRounds) } func buildLoserBracket() { @@ -640,7 +650,7 @@ defer { round.parent = id //parent return round } - self.tournamentStore.rounds.addOrUpdate(contentOfs: rounds) + self.tournamentStore?.rounds.addOrUpdate(contentOfs: rounds) let matchCount = RoundRule.numberOfMatches(forTeams: currentRoundMatchCount) let matches = (0.. Int { @@ -691,7 +701,7 @@ defer { playedMatches.forEach { match in match.matchFormat = updatedMatchFormat } - self.tournamentStore.matches.addOrUpdate(contentOfs: playedMatches) + self.tournamentStore?.matches.addOrUpdate(contentOfs: playedMatches) } override func deleteDependencies() { @@ -700,14 +710,14 @@ defer { match.deleteDependencies() } - self.tournamentStore.matches.deleteDependencies(matches) + self.tournamentStore?.matches.deleteDependencies(matches) let loserRounds = self.loserRounds() for round in loserRounds { round.deleteDependencies() } - self.tournamentStore.rounds.deleteDependencies(loserRounds) + self.tournamentStore?.rounds.deleteDependencies(loserRounds) } // enum CodingKeys: String, CodingKey { @@ -755,7 +765,7 @@ defer { // } func insertOnServer() { - self.tournamentStore.rounds.writeChangeAndInsertOnServer(instance: self) + self.tournamentStore?.rounds.writeChangeAndInsertOnServer(instance: self) for match in self._matches() { match.insertOnServer() } diff --git a/PadelClub/Data/TeamRegistration.swift b/PadelClub/Data/TeamRegistration.swift index 7c4807b..0acad7f 100644 --- a/PadelClub/Data/TeamRegistration.swift +++ b/PadelClub/Data/TeamRegistration.swift @@ -72,21 +72,23 @@ final class TeamRegistration: BaseTeamRegistration, SideStorable { try super.init(from: decoder) } - var tournamentStore: TournamentStore { + var tournamentStore: TournamentStore? { return TournamentLibrary.shared.store(tournamentId: self.tournament) } // MARK: - Computed dependencies func unsortedPlayers() -> [PlayerRegistration] { - return self.tournamentStore.playerRegistrations.filter { $0.teamRegistration == self.id } + guard let tournamentStore = self.tournamentStore else { return [] } + return tournamentStore.playerRegistrations.filter { $0.teamRegistration == self.id } } // MARK: - func deleteTeamScores() { - let ts = self.tournamentStore.teamScores.filter({ $0.teamRegistration == id }) - self.tournamentStore.teamScores.delete(contentOfs: ts) + guard let tournamentStore = self.tournamentStore else { return } + let ts = tournamentStore.teamScores.filter({ $0.teamRegistration == id }) + tournamentStore.teamScores.delete(contentOfs: ts) } override func deleteDependencies() { @@ -94,19 +96,19 @@ final class TeamRegistration: BaseTeamRegistration, SideStorable { for player in unsortedPlayers { player.deleteDependencies() } - self.tournamentStore.playerRegistrations.deleteDependencies(unsortedPlayers) + self.tournamentStore?.playerRegistrations.deleteDependencies(unsortedPlayers) let teamScores = teamScores() for teamScore in teamScores { teamScore.deleteDependencies() } - self.tournamentStore.teamScores.deleteDependencies(teamScores) + self.tournamentStore?.teamScores.deleteDependencies(teamScores) } func hasArrived(isHere: Bool = false) { let unsortedPlayers = unsortedPlayers() unsortedPlayers.forEach({ $0.hasArrived = !isHere }) - self.tournamentStore.playerRegistrations.addOrUpdate(contentOfs: unsortedPlayers) + self.tournamentStore?.playerRegistrations.addOrUpdate(contentOfs: unsortedPlayers) } func isHere() -> Bool { @@ -146,7 +148,7 @@ final class TeamRegistration: BaseTeamRegistration, SideStorable { if let index = index(in: tournament.selectedSortedTeams()) { let drawLog = DrawLog(tournament: tournament.id, drawSeed: index, drawMatchIndex: match.index, drawTeamPosition: teamPosition, drawType: .seed) do { - try tournamentStore.drawLogs.addOrUpdate(instance: drawLog) + try tournamentStore?.drawLogs.addOrUpdate(instance: drawLog) } catch { Logger.error(error) } @@ -202,19 +204,23 @@ final class TeamRegistration: BaseTeamRegistration, SideStorable { } func teamScores() -> [TeamScore] { - return self.tournamentStore.teamScores.filter({ $0.teamRegistration == id }) + guard let tournamentStore = self.tournamentStore else { return [] } + return tournamentStore.teamScores.filter({ $0.teamRegistration == id }) } func wins() -> [Match] { - return self.tournamentStore.matches.filter({ $0.winningTeamId == id }) + guard let tournamentStore = self.tournamentStore else { return [] } + return tournamentStore.matches.filter({ $0.winningTeamId == id }) } func loses() -> [Match] { - return self.tournamentStore.matches.filter({ $0.losingTeamId == id }) + guard let tournamentStore = self.tournamentStore else { return [] } + return tournamentStore.matches.filter({ $0.losingTeamId == id }) } func matches() -> [Match] { - return self.tournamentStore.matches.filter({ $0.losingTeamId == id || $0.winningTeamId == id }) + guard let tournamentStore = self.tournamentStore else { return [] } + return tournamentStore.matches.filter({ $0.losingTeamId == id || $0.winningTeamId == id }) } var tournamentCategory: TournamentCategory { @@ -328,10 +334,11 @@ final class TeamRegistration: BaseTeamRegistration, SideStorable { } func resetGroupeStagePosition() { + guard let tournamentStore = self.tournamentStore else { return } if let groupStage { - let matches = self.tournamentStore.matches.filter({ $0.groupStage == groupStage }).map { $0.id } - let teamScores = self.tournamentStore.teamScores.filter({ $0.teamRegistration == id && matches.contains($0.match) }) - self.tournamentStore.teamScores.delete(contentOfs: teamScores) + let matches = tournamentStore.matches.filter({ $0.groupStage == groupStage }).map { $0.id } + let teamScores = tournamentStore.teamScores.filter({ $0.teamRegistration == id && matches.contains($0.match) }) + tournamentStore.teamScores.delete(contentOfs: teamScores) } //groupStageObject()?._matches().forEach({ $0.updateTeamScores() }) groupStage = nil @@ -339,9 +346,10 @@ final class TeamRegistration: BaseTeamRegistration, SideStorable { } func resetBracketPosition() { - let matches = self.tournamentStore.matches.filter({ $0.groupStage == nil }).map { $0.id } - let teamScores = self.tournamentStore.teamScores.filter({ $0.teamRegistration == id && matches.contains($0.match) }) - self.tournamentStore.teamScores.delete(contentOfs: teamScores) + guard let tournamentStore = self.tournamentStore else { return } + let matches = tournamentStore.matches.filter({ $0.groupStage == nil }).map { $0.id } + let teamScores = tournamentStore.teamScores.filter({ $0.teamRegistration == id && matches.contains($0.match) }) + tournamentStore.teamScores.delete(contentOfs: teamScores) self.bracketPosition = nil } @@ -411,7 +419,7 @@ final class TeamRegistration: BaseTeamRegistration, SideStorable { func updatePlayers(_ players: Set, inTournamentCategory tournamentCategory: TournamentCategory) { let previousPlayers = Set(unsortedPlayers()) let playersToRemove = previousPlayers.subtracting(players) - self.tournamentStore.playerRegistrations.delete(contentOfs: playersToRemove) + self.tournamentStore?.playerRegistrations.delete(contentOfs: playersToRemove) setWeight(from: Array(players), inTournamentCategory: tournamentCategory) players.forEach { player in @@ -454,8 +462,8 @@ final class TeamRegistration: BaseTeamRegistration, SideStorable { typealias AreInIncreasingOrder = (PlayerRegistration, PlayerRegistration) -> Bool func players() -> [PlayerRegistration] { - - self.tournamentStore.playerRegistrations.filter { $0.teamRegistration == self.id }.sorted { (lhs, rhs) in + guard let tournamentStore = self.tournamentStore else { return [] } + return tournamentStore.playerRegistrations.filter { $0.teamRegistration == self.id }.sorted { (lhs, rhs) in let predicates: [AreInIncreasingOrder] = [ { $0.sex?.rawValue ?? 0 < $1.sex?.rawValue ?? 0 }, { $0.rank ?? Int.max < $1.rank ?? Int.max }, @@ -503,19 +511,19 @@ final class TeamRegistration: BaseTeamRegistration, SideStorable { func groupStageObject() -> GroupStage? { guard let groupStage else { return nil } - return self.tournamentStore.groupStages.findById(groupStage) + return self.tournamentStore?.groupStages.findById(groupStage) } func initialRound() -> Round? { guard let bracketPosition else { return nil } let roundIndex = RoundRule.roundIndex(fromMatchIndex: bracketPosition / 2) - return self.tournamentStore.rounds.first(where: { $0.index == roundIndex }) + return self.tournamentStore?.rounds.first(where: { $0.index == roundIndex }) } func initialMatch() -> Match? { guard let bracketPosition else { return nil } guard let initialRoundObject = initialRound() else { return nil } - return self.tournamentStore.matches.first(where: { $0.round == initialRoundObject.id && $0.index == bracketPosition / 2 }) + return self.tournamentStore?.matches.first(where: { $0.round == initialRoundObject.id && $0.index == bracketPosition / 2 }) } func toggleSummonConfirmation() { @@ -587,7 +595,7 @@ final class TeamRegistration: BaseTeamRegistration, SideStorable { } func insertOnServer() { - self.tournamentStore.teamRegistrations.writeChangeAndInsertOnServer(instance: self) + self.tournamentStore?.teamRegistrations.writeChangeAndInsertOnServer(instance: self) for playerRegistration in self.unsortedPlayers() { playerRegistration.insertOnServer() } diff --git a/PadelClub/Data/TeamScore.swift b/PadelClub/Data/TeamScore.swift index c3fedde..cc54a41 100644 --- a/PadelClub/Data/TeamScore.swift +++ b/PadelClub/Data/TeamScore.swift @@ -50,7 +50,7 @@ final class TeamScore: BaseTeamScore, SideStorable { try super.init(from: decoder) } - var tournamentStore: TournamentStore { + var tournamentStore: TournamentStore? { guard let storeId else { fatalError("missing store id for \(String(describing: type(of: self)))") } @@ -65,14 +65,14 @@ final class TeamScore: BaseTeamScore, SideStorable { // MARK: - Computed dependencies func matchObject() -> Match? { - return self.tournamentStore.matches.findById(self.match) + return self.tournamentStore?.matches.findById(self.match) } var team: TeamRegistration? { guard let teamRegistration else { return nil } - return self.tournamentStore.teamRegistrations.findById(teamRegistration) + return self.tournamentStore?.teamRegistrations.findById(teamRegistration) } // MARK: - @@ -107,7 +107,7 @@ final class TeamScore: BaseTeamScore, SideStorable { // } func insertOnServer() { - self.tournamentStore.teamScores.writeChangeAndInsertOnServer(instance: self) + self.tournamentStore?.teamScores.writeChangeAndInsertOnServer(instance: self) } } diff --git a/PadelClub/Data/Tournament.swift b/PadelClub/Data/Tournament.swift index fdb5b4f..32b397c 100644 --- a/PadelClub/Data/Tournament.swift +++ b/PadelClub/Data/Tournament.swift @@ -77,31 +77,31 @@ final class Tournament: BaseTournament { try super.init(from: decoder) } - var tournamentStore: TournamentStore { + var tournamentStore: TournamentStore? { return TournamentLibrary.shared.store(tournamentId: self.id) } override func deleteDependencies() { - let store = self.tournamentStore - let drawLogs = Array(self.tournamentStore.drawLogs) + guard let store = self.tournamentStore else { return } + let drawLogs = Array(store.drawLogs) for drawLog in drawLogs { drawLog.deleteDependencies() } store.drawLogs.deleteDependencies(drawLogs) - let teams = self.tournamentStore.teamRegistrations + let teams = store.teamRegistrations for team in Array(teams) { team.deleteDependencies() } store.teamRegistrations.deleteDependencies(teams) - let groups = Array(self.tournamentStore.groupStages) + let groups = Array(store.groupStages) for group in groups { group.deleteDependencies() } store.groupStages.deleteDependencies(groups) - let rounds = Array(self.tournamentStore.rounds) + let rounds = Array(store.rounds) for round in rounds { round.deleteDependencies() } @@ -120,20 +120,24 @@ final class Tournament: BaseTournament { // MARK: - Computed Dependencies func unsortedTeams() -> [TeamRegistration] { - return Array(self.tournamentStore.teamRegistrations) + guard let tournamentStore = self.tournamentStore else { return [] } + return Array(tournamentStore.teamRegistrations) } func groupStages(atStep step: Int = 0) -> [GroupStage] { - let groupStages: [GroupStage] = self.tournamentStore.groupStages.filter { $0.tournament == self.id && $0.step == step } + guard let tournamentStore = self.tournamentStore else { return [] } + let groupStages: [GroupStage] = tournamentStore.groupStages.filter { $0.tournament == self.id && $0.step == step } return groupStages.sorted(by: \.index) } func allGroupStages() -> [GroupStage] { - return self.tournamentStore.groupStages.sorted(by: \GroupStage.computedOrder) + guard let tournamentStore = self.tournamentStore else { return [] } + return tournamentStore.groupStages.sorted(by: \GroupStage.computedOrder) } func allRounds() -> [Round] { - return Array(self.tournamentStore.rounds) + guard let tournamentStore = self.tournamentStore else { return [] } + return Array(tournamentStore.rounds) } // MARK: - @@ -354,7 +358,7 @@ defer { } func getRound(atRoundIndex roundIndex: Int) -> Round? { - return self.tournamentStore.rounds.first(where: { $0.index == roundIndex }) + return self.tournamentStore?.rounds.first(where: { $0.index == roundIndex }) // return Store.main.filter(isIncluded: { $0.tournament == id && $0.index == roundIndex }).first } @@ -546,15 +550,18 @@ defer { } func allMatches() -> [Match] { - return self.tournamentStore.matches.filter { $0.disabled == false } + guard let tournamentStore = self.tournamentStore else { return [] } + return tournamentStore.matches.filter { $0.disabled == false } } func _allMatchesIncludingDisabled() -> [Match] { - return Array(self.tournamentStore.matches) + guard let tournamentStore = self.tournamentStore else { return [] } + return Array(tournamentStore.matches) } func rounds() -> [Round] { - let rounds: [Round] = self.tournamentStore.rounds.filter { $0.isUpperBracket() } + guard let tournamentStore = self.tournamentStore else { return [] } + let rounds: [Round] = tournamentStore.rounds.filter { $0.isUpperBracket() } return rounds.sorted(by: \.index).reversed() } @@ -677,12 +684,14 @@ defer { } func unsortedTeamsWithoutWO() -> [TeamRegistration] { - return self.tournamentStore.teamRegistrations.filter { $0.walkOut == false } + guard let tournamentStore = self.tournamentStore else { return [] } + return tournamentStore.teamRegistrations.filter { $0.walkOut == false } // return Store.main.filter { $0.tournament == self.id && $0.walkOut == false } } func walkoutTeams() -> [TeamRegistration] { - return self.tournamentStore.teamRegistrations.filter { $0.walkOut == true } + guard let tournamentStore = self.tournamentStore else { return [] } + return tournamentStore.teamRegistrations.filter { $0.walkOut == true } // return Store.main.filter { $0.tournament == self.id && $0.walkOut == true } } @@ -702,7 +711,8 @@ defer { } func unsortedPlayers() -> [PlayerRegistration] { - return Array(self.tournamentStore.playerRegistrations) + guard let tournamentStore = self.tournamentStore else { return [] } + return Array(tournamentStore.playerRegistrations) } func selectedPlayers() -> [PlayerRegistration] { @@ -718,7 +728,8 @@ defer { } func players() -> [PlayerRegistration] { - return self.tournamentStore.playerRegistrations.sorted(by: \.computedRank) + guard let tournamentStore = self.tournamentStore else { return [] } + return tournamentStore.playerRegistrations.sorted(by: \.computedRank) } func unrankValue(for malePlayer: Bool) -> Int? { @@ -838,8 +849,10 @@ defer { } } - self.tournamentStore.teamRegistrations.addOrUpdate(contentOfs: teamsToImport) - self.tournamentStore.playerRegistrations.addOrUpdate(contentOfs: teams.flatMap { $0.players }) + if let tournamentStore = self.tournamentStore { + tournamentStore.teamRegistrations.addOrUpdate(contentOfs: teamsToImport) + tournamentStore.playerRegistrations.addOrUpdate(contentOfs: teams.flatMap { $0.players }) + } if state() == .build && groupStageCount > 0 && groupStageTeams().isEmpty { setGroupStage(randomize: groupStageSortMode == .random) @@ -1020,7 +1033,7 @@ defer { _removeStrings(from: &teams, stringsToRemove: disabledIds) teams[interval.last] = disabledIds let teamNames : [String] = disabledIds.compactMap { - let t : TeamRegistration? = tournamentStore.teamRegistrations.findById($0) + let t : TeamRegistration? = self.tournamentStore?.teamRegistrations.findById($0) return t }.map { $0.canonicalName } print("winners.isEmpty", "\(interval.last) : ", teamNames) @@ -1033,7 +1046,7 @@ defer { _removeStrings(from: &teams, stringsToRemove: winners) teams[interval.first + winners.count - 1] = winners let teamNames : [String] = winners.compactMap { - let t: TeamRegistration? = tournamentStore.teamRegistrations.findById($0) + let t: TeamRegistration? = tournamentStore?.teamRegistrations.findById($0) return t }.map { $0.canonicalName } print("winners", "\(interval.last + winners.count - 1) : ", teamNames) @@ -1044,7 +1057,7 @@ defer { _removeStrings(from: &teams, stringsToRemove: losers) teams[interval.first + winners.count] = losers let loserTeamNames : [String] = losers.compactMap { - let t: TeamRegistration? = tournamentStore.teamRegistrations.findById($0) + let t: TeamRegistration? = tournamentStore?.teamRegistrations.findById($0) return t }.map { $0.canonicalName } print("losers", "\(interval.first + winners.count) : ", loserTeamNames) @@ -1088,11 +1101,13 @@ defer { } func setRankings(finalRanks: [Int: [String]]) async -> [Int: [TeamRegistration]] { + guard let tournamentStore = self.tournamentStore else { return [:] } + var rankings: [Int: [TeamRegistration]] = [:] finalRanks.keys.sorted().forEach { rank in if let rankedTeamIds = finalRanks[rank] { - let teams: [TeamRegistration] = rankedTeamIds.compactMap { self.tournamentStore.teamRegistrations.findById($0) } + let teams: [TeamRegistration] = rankedTeamIds.compactMap { tournamentStore.teamRegistrations.findById($0) } rankings[rank] = teams } } @@ -1106,7 +1121,7 @@ defer { } } - self.tournamentStore.teamRegistrations.addOrUpdate(contentOfs: unsortedTeams()) + tournamentStore.teamRegistrations.addOrUpdate(contentOfs: unsortedTeams()) return rankings } @@ -1121,7 +1136,7 @@ defer { teams.forEach { team in team.lockedWeight = team.weight } - self.tournamentStore.teamRegistrations.addOrUpdate(contentOfs: teams) + self.tournamentStore?.teamRegistrations.addOrUpdate(contentOfs: teams) } func unlockRegistration() { @@ -1130,7 +1145,7 @@ defer { teams.forEach { team in team.lockedWeight = nil } - self.tournamentStore.teamRegistrations.addOrUpdate(contentOfs: teams) + self.tournamentStore?.teamRegistrations.addOrUpdate(contentOfs: teams) } func updateWeights() { @@ -1139,9 +1154,9 @@ defer { let players = team.unsortedPlayers() players.forEach { $0.setComputedRank(in: self) } team.setWeight(from: players, inTournamentCategory: tournamentCategory) - self.tournamentStore.playerRegistrations.addOrUpdate(contentOfs: players) + self.tournamentStore?.playerRegistrations.addOrUpdate(contentOfs: players) } - self.tournamentStore.teamRegistrations.addOrUpdate(contentOfs: teams) + self.tournamentStore?.teamRegistrations.addOrUpdate(contentOfs: teams) } func updateRank(to newDate: Date?) async throws { @@ -1472,27 +1487,30 @@ defer { } do { - try self.tournamentStore.teamRegistrations.addOrUpdate(contentOfs: wcs) + try self.tournamentStore?.teamRegistrations.addOrUpdate(contentOfs: wcs) } catch { Logger.error(error) } } func addEmptyTeamRegistration(_ count: Int) { + + guard let tournamentStore = self.tournamentStore else { return } + let teams = (0.. Bool { @@ -1792,7 +1810,8 @@ defer { private let _currentSelectionSorting : [MySortDescriptor] = [.keyPath(\.weight), .keyPath(\.id)] private func _matchSchedulers() -> [MatchScheduler] { - return self.tournamentStore.matchSchedulers.filter { $0.tournament == self.id } + guard let tournamentStore = self.tournamentStore else { return [] } + return tournamentStore.matchSchedulers.filter { $0.tournament == self.id } // DataStore.shared.matchSchedulers.filter(isIncluded: { $0.tournament == self.id }) } @@ -1827,7 +1846,7 @@ defer { } func tournamentWinner() -> TeamRegistration? { - let finals: Round? = self.tournamentStore.rounds.first(where: { $0.index == 0 && $0.isUpperBracket() }) + let finals: Round? = self.tournamentStore?.rounds.first(where: { $0.index == 0 && $0.isUpperBracket() }) return finals?.playedMatches().first?.winner() } @@ -1890,7 +1909,7 @@ defer { } func groupStageLoserBracket() -> Round? { - tournamentStore.rounds.first(where: { $0.groupStageLoserBracket }) + self.tournamentStore?.rounds.first(where: { $0.groupStageLoserBracket }) } func groupStageLoserBracketsInitialPlace() -> Int { @@ -1901,14 +1920,14 @@ defer { let lastStep = lastStep() + 1 for i in 0.. Int { - self.tournamentStore.groupStages.sorted(by: \.step).last?.step ?? 0 + self.tournamentStore?.groupStages.sorted(by: \.step).last?.step ?? 0 } func generateSmartLoserGroupStageBracket() { @@ -1919,7 +1938,7 @@ defer { let match = Match(round: groupStageLoserBracket.id, index: placeCount, format: groupStageLoserBracket.matchFormat) match.setMatchName("\(placeCount)\(placeCount.ordinalFormattedSuffix(feminine: true)) place") do { - try tournamentStore.matches.addOrUpdate(instance: match) + try tournamentStore?.matches.addOrUpdate(instance: match) } catch { Logger.error(error) } @@ -1958,7 +1977,8 @@ defer { } func drawLogs() -> [DrawLog] { - self.tournamentStore.drawLogs.sorted(by: \.drawDate) + guard let tournamentStore = self.tournamentStore else { return [] } + return tournamentStore.drawLogs.sorted(by: \.drawDate) } func seedSpotsLeft() -> Bool { @@ -1989,7 +2009,7 @@ defer { } do { - try tournamentStore.teamRegistrations.addOrUpdate(contentOfs: seeds) + try tournamentStore?.teamRegistrations.addOrUpdate(contentOfs: seeds) } catch { Logger.error(error) } @@ -2004,12 +2024,12 @@ defer { } do { - try tournamentStore.teamScores.delete(contentOfs: ts) + try tournamentStore?.teamScores.delete(contentOfs: ts) } catch { Logger.error(error) } do { - try tournamentStore.teamRegistrations.addOrUpdate(contentOfs: unsortedTeams()) + try tournamentStore?.teamRegistrations.addOrUpdate(contentOfs: unsortedTeams()) } catch { Logger.error(error) } @@ -2028,7 +2048,7 @@ defer { let matches = (0.. TournamentStore { + func store(tournamentId: String) -> TournamentStore? { + guard let tournament = DataStore.shared.tournaments.first(where: { $0.id == tournamentId }) else { return nil } + if let store = self._stores[tournamentId] { return store } diff --git a/PadelClub/Utils/Patcher.swift b/PadelClub/Utils/Patcher.swift index 885348f..4b76f79 100644 --- a/PadelClub/Utils/Patcher.swift +++ b/PadelClub/Utils/Patcher.swift @@ -56,7 +56,7 @@ class Patcher { for tournament in DataStore.shared.tournaments { let id = tournament.id - let store = TournamentLibrary.shared.store(tournamentId: tournament.id) + guard let store = TournamentLibrary.shared.store(tournamentId: tournament.id) else { continue } for round in store.rounds { round.storeId = id diff --git a/PadelClub/Views/Calling/CallSettingsView.swift b/PadelClub/Views/Calling/CallSettingsView.swift index 7834e0e..334cd41 100644 --- a/PadelClub/Views/Calling/CallSettingsView.swift +++ b/PadelClub/Views/Calling/CallSettingsView.swift @@ -15,7 +15,7 @@ struct CallSettingsView: View { @State private var showSendToAllView: Bool = false @State private var addLink: Bool = false - var tournamentStore: TournamentStore { + var tournamentStore: TournamentStore? { return self.tournament.tournamentStore } @@ -64,7 +64,7 @@ struct CallSettingsView: View { team.callDate = nil } do { - try tournamentStore.teamRegistrations.addOrUpdate(contentOfs: teams) + try tournamentStore?.teamRegistrations.addOrUpdate(contentOfs: teams) } catch { Logger.error(error) } @@ -82,7 +82,7 @@ struct CallSettingsView: View { } } do { - try tournamentStore.teamRegistrations.addOrUpdate(contentOfs: teams) + try tournamentStore?.teamRegistrations.addOrUpdate(contentOfs: teams) } catch { Logger.error(error) } diff --git a/PadelClub/Views/Calling/CallView.swift b/PadelClub/Views/Calling/CallView.swift index 1be6236..47ba573 100644 --- a/PadelClub/Views/Calling/CallView.swift +++ b/PadelClub/Views/Calling/CallView.swift @@ -104,7 +104,7 @@ struct CallView: View { } } - var tournamentStore: TournamentStore { + var tournamentStore: TournamentStore? { return self.tournament.tournamentStore } @@ -130,7 +130,7 @@ struct CallView: View { } } do { - try self.tournamentStore.teamRegistrations.addOrUpdate(contentOfs: calledTeams) + try self.tournamentStore?.teamRegistrations.addOrUpdate(contentOfs: calledTeams) } catch { Logger.error(error) } diff --git a/PadelClub/Views/Calling/SendToAllView.swift b/PadelClub/Views/Calling/SendToAllView.swift index d261f35..3c06250 100644 --- a/PadelClub/Views/Calling/SendToAllView.swift +++ b/PadelClub/Views/Calling/SendToAllView.swift @@ -32,7 +32,7 @@ struct SendToAllView: View { @State var summonParamByMessage: Bool = false @State var summonParamReSummon: Bool = false - var tournamentStore: TournamentStore { + var tournamentStore: TournamentStore? { return self.tournament.tournamentStore } @@ -233,12 +233,14 @@ struct SendToAllView: View { } func _roundTeams() -> [TeamRegistration] { - let rounds: [Round] = contactRecipients.compactMap { self.tournamentStore.rounds.findById($0) } + guard let tournamentStore = self.tournamentStore else { return [] } + let rounds: [Round] = contactRecipients.compactMap { tournamentStore.rounds.findById($0) } return rounds.flatMap { $0.teams() } } func _groupStagesTeams() -> [TeamRegistration] { - let groupStages : [GroupStage] = contactRecipients.compactMap { self.tournamentStore.groupStages.findById($0) } + guard let tournamentStore = self.tournamentStore else { return [] } + let groupStages : [GroupStage] = contactRecipients.compactMap { tournamentStore.groupStages.findById($0) } return groupStages.flatMap { $0.teams() } } diff --git a/PadelClub/Views/Calling/TeamsCallingView.swift b/PadelClub/Views/Calling/TeamsCallingView.swift index 8a5d1e9..677fb2b 100644 --- a/PadelClub/Views/Calling/TeamsCallingView.swift +++ b/PadelClub/Views/Calling/TeamsCallingView.swift @@ -117,7 +117,7 @@ struct CallMenuOptionsView: View { } set: { _ in team.toggleSummonConfirmation() do { - try self.tournament.tournamentStore.teamRegistrations.addOrUpdate(instance: team) + try self.tournament.tournamentStore?.teamRegistrations.addOrUpdate(instance: team) } catch { Logger.error(error) } @@ -152,7 +152,7 @@ struct CallMenuOptionsView: View { RowButtonView("Effacer la date de convocation", role: .destructive) { team.callDate = nil do { - try self.tournament.tournamentStore.teamRegistrations.addOrUpdate(instance: team) + try self.tournament.tournamentStore?.teamRegistrations.addOrUpdate(instance: team) } catch { Logger.error(error) } @@ -165,7 +165,7 @@ struct CallMenuOptionsView: View { RowButtonView("Indiquer comme convoquée", role: .destructive) { team.callDate = team.initialMatch()?.startDate ?? tournament.startDate do { - try self.tournament.tournamentStore.teamRegistrations.addOrUpdate(instance: team) + try self.tournament.tournamentStore?.teamRegistrations.addOrUpdate(instance: team) } catch { Logger.error(error) } diff --git a/PadelClub/Views/Cashier/CashierSettingsView.swift b/PadelClub/Views/Cashier/CashierSettingsView.swift index fe93c84..2090e0d 100644 --- a/PadelClub/Views/Cashier/CashierSettingsView.swift +++ b/PadelClub/Views/Cashier/CashierSettingsView.swift @@ -137,7 +137,7 @@ struct CashierSettingsView: View { } private func _save(players: [PlayerRegistration]) { - tournament.tournamentStore.playerRegistrations.addOrUpdate(contentOfs: players) + tournament.tournamentStore?.playerRegistrations.addOrUpdate(contentOfs: players) } private func _save() { diff --git a/PadelClub/Views/GroupStage/Components/GroupStageSettingsView.swift b/PadelClub/Views/GroupStage/Components/GroupStageSettingsView.swift index 521b328..50e5155 100644 --- a/PadelClub/Views/GroupStage/Components/GroupStageSettingsView.swift +++ b/PadelClub/Views/GroupStage/Components/GroupStageSettingsView.swift @@ -29,7 +29,7 @@ struct GroupStageSettingsView: View { _courtIndex = .init(wrappedValue: groupStage._matches().first?.courtIndex ?? 0) } - var tournamentStore: TournamentStore { + var tournamentStore: TournamentStore? { return self.tournament.tournamentStore } @@ -70,7 +70,7 @@ struct GroupStageSettingsView: View { } do { - try tournamentStore.matches.addOrUpdate(contentOfs: groupStage._matches()) + try tournamentStore?.matches.addOrUpdate(contentOfs: groupStage._matches()) } catch { Logger.error(error) } @@ -93,7 +93,7 @@ struct GroupStageSettingsView: View { groupStage._matches().forEach({ $0.updateTeamScores() }) } do { - try tournamentStore.teamRegistrations.addOrUpdate(contentOfs: teams) + try tournamentStore?.teamRegistrations.addOrUpdate(contentOfs: teams) } catch { Logger.error(error) } @@ -116,7 +116,7 @@ struct GroupStageSettingsView: View { } do { - try tournamentStore.matches.addOrUpdate(contentOfs: groupStage._matches()) + try tournamentStore?.matches.addOrUpdate(contentOfs: groupStage._matches()) } catch { Logger.error(error) } @@ -132,7 +132,7 @@ struct GroupStageSettingsView: View { groupStage._matches().forEach({ $0.updateTeamScores() }) } do { - try tournamentStore.teamRegistrations.addOrUpdate(contentOfs: teams) + try tournamentStore?.teamRegistrations.addOrUpdate(contentOfs: teams) } catch { Logger.error(error) } @@ -243,7 +243,7 @@ struct GroupStageSettingsView: View { private func _save() { do { - try tournamentStore.groupStages.addOrUpdate(instance: groupStage) + try tournamentStore?.groupStages.addOrUpdate(instance: groupStage) } catch { Logger.error(error) } diff --git a/PadelClub/Views/GroupStage/Components/GroupStageTeamView.swift b/PadelClub/Views/GroupStage/Components/GroupStageTeamView.swift index 18a3786..74a181f 100644 --- a/PadelClub/Views/GroupStage/Components/GroupStageTeamView.swift +++ b/PadelClub/Views/GroupStage/Components/GroupStageTeamView.swift @@ -23,7 +23,7 @@ struct GroupStageTeamView: View { let groupStage: GroupStage var team: TeamRegistration - var tournamentStore: TournamentStore { + var tournamentStore: TournamentStore? { return self.tournament.tournamentStore } @@ -189,7 +189,7 @@ struct GroupStageTeamView: View { private func _save() { do { - try tournamentStore.teamRegistrations.addOrUpdate(instance: team) + try tournamentStore?.teamRegistrations.addOrUpdate(instance: team) } catch { Logger.error(error) } diff --git a/PadelClub/Views/GroupStage/GroupStageView.swift b/PadelClub/Views/GroupStage/GroupStageView.swift index 9fc489f..1473da7 100644 --- a/PadelClub/Views/GroupStage/GroupStageView.swift +++ b/PadelClub/Views/GroupStage/GroupStageView.swift @@ -22,7 +22,7 @@ struct GroupStageView: View { _sortingMode = .init(wrappedValue: groupStage.hasEnded() ? .score : .weight) } - var tournamentStore: TournamentStore { + var tournamentStore: TournamentStore? { return self.tournament.tournamentStore } @@ -118,7 +118,7 @@ struct GroupStageView: View { let scores: [GroupStage.TeamGroupStageScore]? let teams: [TeamRegistration] - var tournamentStore: TournamentStore { + var tournamentStore: TournamentStore? { return self.tournament.tournamentStore } @@ -207,7 +207,7 @@ struct GroupStageView: View { Button { player.hasArrived.toggle() do { - try self.tournamentStore.playerRegistrations.addOrUpdate(instance: player) + try self.tournamentStore?.playerRegistrations.addOrUpdate(instance: player) } catch { Logger.error(error) } @@ -236,7 +236,7 @@ struct GroupStageView: View { team.groupStagePosition = index groupStage._matches().forEach({ $0.updateTeamScores() }) do { - try tournamentStore.teamRegistrations.addOrUpdate(instance: team) + try tournamentStore?.teamRegistrations.addOrUpdate(instance: team) } catch { Logger.error(error) } @@ -266,7 +266,7 @@ struct GroupStageView: View { private func _save() { do { - try tournamentStore.groupStages.addOrUpdate(instance: groupStage) + try tournamentStore?.groupStages.addOrUpdate(instance: groupStage) } catch { Logger.error(error) } diff --git a/PadelClub/Views/GroupStage/GroupStagesSettingsView.swift b/PadelClub/Views/GroupStage/GroupStagesSettingsView.swift index 6961bae..c037ca9 100644 --- a/PadelClub/Views/GroupStage/GroupStagesSettingsView.swift +++ b/PadelClub/Views/GroupStage/GroupStagesSettingsView.swift @@ -15,7 +15,7 @@ struct GroupStagesSettingsView: View { @State private var generationDoneMessage: String? let step: Int - var tournamentStore: TournamentStore { + var tournamentStore: TournamentStore? { return self.tournament.tournamentStore } @@ -26,7 +26,7 @@ struct GroupStagesSettingsView: View { let issues = tournament.groupStageTeamPlacementIssue() DisclosureGroup { ForEach(issues.shouldBeInIt, id: \.self) { id in - if let team: TeamRegistration = self.tournamentStore.teamRegistrations.findById(id) { + if let team: TeamRegistration = self.tournamentStore?.teamRegistrations.findById(id) { TeamRowView(team: team) } } @@ -39,12 +39,12 @@ struct GroupStagesSettingsView: View { } DisclosureGroup { ForEach(issues.shouldNotBeInIt, id: \.self) { id in - if let team = self.tournamentStore.teamRegistrations.findById(id) { + if let team = self.tournamentStore?.teamRegistrations.findById(id) { Menu { Button("Retirer de sa poule") { team.resetGroupeStagePosition() do { - try self.tournamentStore.teamRegistrations.addOrUpdate(instance: team) + try self.tournamentStore?.teamRegistrations.addOrUpdate(instance: team) } catch { Logger.error(error) } @@ -80,7 +80,7 @@ struct GroupStagesSettingsView: View { let round = Round(tournament: tournament.id, index: 0, matchFormat: tournament.loserRoundFormat, groupStageLoserBracket: true) do { - try tournamentStore.rounds.addOrUpdate(instance: round) + try tournamentStore?.rounds.addOrUpdate(instance: round) } catch { Logger.error(error) } @@ -89,7 +89,7 @@ struct GroupStagesSettingsView: View { } else if let groupStageLoserBracket = tournament.groupStageLoserBracket() { RowButtonView("Supprimer les matchs de classements", role: .destructive) { do { - try tournamentStore.rounds.delete(instance: groupStageLoserBracket) + try tournamentStore?.rounds.delete(instance: groupStageLoserBracket) } catch { Logger.error(error) } @@ -110,7 +110,7 @@ struct GroupStagesSettingsView: View { RowButtonView("Supprimer cette phase de poule", role: .destructive) { let groupStages = tournament.groupStages(atStep: tournament.lastStep()) do { - try tournament.tournamentStore.groupStages.delete(contentOfs: groupStages) + try self.tournamentStore?.groupStages.delete(contentOfs: groupStages) } catch { Logger.error(error) } @@ -138,7 +138,7 @@ struct GroupStagesSettingsView: View { } do { - try tournamentStore.matches.addOrUpdate(contentOfs: matches) + try tournamentStore?.matches.addOrUpdate(contentOfs: matches) } catch { Logger.error(error) } @@ -211,7 +211,7 @@ struct GroupStagesSettingsView: View { } } do { - try self.tournament.tournamentStore.groupStages.addOrUpdate(contentOfs: groupStages) + try self.tournament.tournamentStore?.groupStages.addOrUpdate(contentOfs: groupStages) } catch { Logger.error(error) } @@ -225,7 +225,7 @@ struct GroupStagesSettingsView: View { groupStage.name = nil } do { - try self.tournament.tournamentStore.groupStages.addOrUpdate(contentOfs: groupStages) + try self.tournament.tournamentStore?.groupStages.addOrUpdate(contentOfs: groupStages) } catch { Logger.error(error) } @@ -242,7 +242,7 @@ struct GroupStagesSettingsView: View { groupStage._matches().forEach({ $0.updateTeamScores() }) } do { - try tournamentStore.teamRegistrations.addOrUpdate(contentOfs: teams) + try tournamentStore?.teamRegistrations.addOrUpdate(contentOfs: teams) } catch { Logger.error(error) } diff --git a/PadelClub/Views/GroupStage/GroupStagesView.swift b/PadelClub/Views/GroupStage/GroupStagesView.swift index 13cd9e2..05446a2 100644 --- a/PadelClub/Views/GroupStage/GroupStagesView.swift +++ b/PadelClub/Views/GroupStage/GroupStagesView.swift @@ -124,7 +124,7 @@ struct GroupStagesView: View { results.forEach { drawResult in missingQualifiedFromGroupStages[drawResult.drawIndex].qualified = true do { - try self.tournament.tournamentStore.teamRegistrations.addOrUpdate(instance: missingQualifiedFromGroupStages[drawResult.drawIndex]) + try self.tournament.tournamentStore?.teamRegistrations.addOrUpdate(instance: missingQualifiedFromGroupStages[drawResult.drawIndex]) } catch { Logger.error(error) } diff --git a/PadelClub/Views/GroupStage/LoserBracketFromGroupStageView.swift b/PadelClub/Views/GroupStage/LoserBracketFromGroupStageView.swift index 5c42a24..b5e2096 100644 --- a/PadelClub/Views/GroupStage/LoserBracketFromGroupStageView.swift +++ b/PadelClub/Views/GroupStage/LoserBracketFromGroupStageView.swift @@ -21,7 +21,7 @@ struct LoserBracketFromGroupStageView: View { _isEditingLoserBracketGroupStage = .init(wrappedValue: loserBracket._matches().isEmpty) } - var tournamentStore: TournamentStore { + var tournamentStore: TournamentStore? { return self.tournament.tournamentStore } @@ -110,13 +110,13 @@ struct LoserBracketFromGroupStageView: View { let match = Match(round: loserBracket.id, index: placeCount, format: loserBracket.matchFormat) match.setMatchName("\(placeCount)\(placeCount.ordinalFormattedSuffix()) place") - tournamentStore.matches.addOrUpdate(instance: match) + tournamentStore?.matches.addOrUpdate(instance: match) } private func _deleteAllMatches() { let displayableMatches = loserBracket.playedMatches().sorted(by: \.index) - tournamentStore.matches.delete(contentOfs: displayableMatches) + tournamentStore?.matches.delete(contentOfs: displayableMatches) } @@ -164,7 +164,7 @@ struct GroupStageLoserBracketMatchFooterView: View { Spacer() FooterButtonView("Effacer", role: .destructive) { do { - try match.tournamentStore.matches.delete(instance: match) + try match.tournamentStore?.matches.delete(instance: match) } catch { Logger.error(error) } @@ -199,7 +199,7 @@ struct GroupStageLoserBracketMatchFooterView: View { match.setMatchName("\(newIndexValidated)\(newIndexValidated.ordinalFormattedSuffix()) place") - match.tournamentStore.teamScores.addOrUpdate(contentOfs: teamScores) - match.tournamentStore.matches.addOrUpdate(instance: match) + match.tournamentStore?.teamScores.addOrUpdate(contentOfs: teamScores) + match.tournamentStore?.matches.addOrUpdate(instance: match) } } diff --git a/PadelClub/Views/Match/Components/MatchDateView.swift b/PadelClub/Views/Match/Components/MatchDateView.swift index 51ccc19..0c7ec65 100644 --- a/PadelClub/Views/Match/Components/MatchDateView.swift +++ b/PadelClub/Views/Match/Components/MatchDateView.swift @@ -191,7 +191,7 @@ struct MatchDateView: View { } do { - try self.match.tournamentStore.matches.addOrUpdate(instance: match) + try self.match.tournamentStore?.matches.addOrUpdate(instance: match) } catch { Logger.error(error) } diff --git a/PadelClub/Views/Match/MatchDetailView.swift b/PadelClub/Views/Match/MatchDetailView.swift index 45f0444..a81ea94 100644 --- a/PadelClub/Views/Match/MatchDetailView.swift +++ b/PadelClub/Views/Match/MatchDetailView.swift @@ -38,7 +38,7 @@ struct MatchDetailView: View { @State private var presentRanking: Bool = false @State private var confirmScoreEdition: Bool = false - var tournamentStore: TournamentStore { + var tournamentStore: TournamentStore? { return match.tournamentStore } @@ -121,8 +121,10 @@ struct MatchDetailView: View { DisclosureGroup { ForEach(unpaid) { player in LabeledContent { - PlayerPayView(player: player) - .environmentObject(tournamentStore) + if let store = self.tournamentStore { + PlayerPayView(player: player) + .environmentObject(store) + } } label: { Text(player.playerLabel()) } @@ -140,8 +142,12 @@ struct MatchDetailView: View { menuView } .sheet(isPresented: $showDetails) { - MatchTeamDetailView(match: match).tint(.master) - .environmentObject(tournamentStore) + if let store = self.tournamentStore { + MatchTeamDetailView(match: match).tint(.master) + .environmentObject(store) + } else { + Text("no store") + } } .sheet(isPresented: self.$showSubscriptionView, content: { NavigationStack { @@ -364,7 +370,7 @@ struct MatchDetailView: View { ForEach(match.teamScores) { teamScore in Button(role: .destructive) { do { - try tournamentStore.teamScores.delete(instance: teamScore) + try tournamentStore?.teamScores.delete(instance: teamScore) } catch { Logger.error(error) } @@ -566,7 +572,7 @@ struct MatchDetailView: View { if match.isReady() == false && match.teams().count == 2 { let teamsScores = match.getOrCreateTeamScores() do { - try tournamentStore.teamScores.addOrUpdate(contentOfs: teamsScores) + try tournamentStore?.teamScores.addOrUpdate(contentOfs: teamsScores) } catch { Logger.error(error) } @@ -615,7 +621,7 @@ struct MatchDetailView: View { private func _saveMatch() { do { - try tournamentStore.matches.addOrUpdate(instance: match) + try tournamentStore?.matches.addOrUpdate(instance: match) } catch { Logger.error(error) } diff --git a/PadelClub/Views/Match/MatchRowView.swift b/PadelClub/Views/Match/MatchRowView.swift index 643078e..e74bd44 100644 --- a/PadelClub/Views/Match/MatchRowView.swift +++ b/PadelClub/Views/Match/MatchRowView.swift @@ -69,7 +69,7 @@ struct MatchRowView: View { Button { player.hasArrived.toggle() do { - try player.tournamentStore.playerRegistrations.addOrUpdate(instance: player) + try player.tournamentStore?.playerRegistrations.addOrUpdate(instance: player) } catch { Logger.error(error) } diff --git a/PadelClub/Views/Match/MatchSetupView.swift b/PadelClub/Views/Match/MatchSetupView.swift index dc932bd..29c47a8 100644 --- a/PadelClub/Views/Match/MatchSetupView.swift +++ b/PadelClub/Views/Match/MatchSetupView.swift @@ -16,7 +16,7 @@ struct MatchSetupView: View { @State var match: Match @State private var seedGroup: SeedInterval? - var tournamentStore: TournamentStore { + var tournamentStore: TournamentStore? { return match.tournamentStore } @@ -68,19 +68,19 @@ struct MatchSetupView: View { if walkOutSpot || team.bracketPosition != nil || matchTypeContext == .loserBracket { match.setLuckyLoser(team: team, teamPosition: teamPosition) do { - try tournamentStore.matches.addOrUpdate(instance: match) + try tournamentStore?.matches.addOrUpdate(instance: match) } catch { Logger.error(error) } } else if team.bracketPosition == nil { team.setSeedPosition(inSpot: match, slot: teamPosition, opposingSeeding: false) do { - try tournamentStore.matches.addOrUpdate(instance: match) + try tournamentStore?.matches.addOrUpdate(instance: match) } catch { Logger.error(error) } do { - try tournamentStore.teamRegistrations.addOrUpdate(instance: team) + try tournamentStore?.teamRegistrations.addOrUpdate(instance: team) } catch { Logger.error(error) } @@ -97,7 +97,7 @@ struct MatchSetupView: View { if let randomTeam = luckyLosers.randomElement() { match.setLuckyLoser(team: randomTeam, teamPosition: teamPosition) do { - try tournamentStore.matches.addOrUpdate(instance: match) + try tournamentStore?.matches.addOrUpdate(instance: match) } catch { Logger.error(error) } @@ -112,12 +112,12 @@ struct MatchSetupView: View { if let randomTeam = availableQualifiedTeams.randomElement() { randomTeam.setSeedPosition(inSpot: match, slot: teamPosition, opposingSeeding: false) do { - try tournamentStore.matches.addOrUpdate(instance: match) + try tournamentStore?.matches.addOrUpdate(instance: match) } catch { Logger.error(error) } do { - try tournamentStore.teamRegistrations.addOrUpdate(instance: randomTeam) + try tournamentStore?.teamRegistrations.addOrUpdate(instance: randomTeam) } catch { Logger.error(error) } @@ -132,12 +132,12 @@ struct MatchSetupView: View { if let randomTeam = tournament.randomSeed(fromSeedGroup: seedGroup) { randomTeam.setSeedPosition(inSpot: match, slot: teamPosition, opposingSeeding: false) do { - try tournamentStore.matches.addOrUpdate(instance: match) + try tournamentStore?.matches.addOrUpdate(instance: match) } catch { Logger.error(error) } do { - try tournamentStore.teamRegistrations.addOrUpdate(instance: randomTeam) + try tournamentStore?.teamRegistrations.addOrUpdate(instance: randomTeam) } catch { Logger.error(error) } @@ -158,7 +158,7 @@ struct MatchSetupView: View { Button { match.unlockSeedPosition(atTeamPosition: teamPosition) do { - try tournamentStore.matches.addOrUpdate(instance: match) + try tournamentStore?.matches.addOrUpdate(instance: match) } catch { Logger.error(error) } @@ -170,7 +170,7 @@ struct MatchSetupView: View { ConfirmButtonView(shouldConfirm: shouldConfirm, message: MatchSetupView.confirmationMessage) { _ = match.lockAndGetSeedPosition(atTeamPosition: teamPosition) do { - try tournamentStore.matches.addOrUpdate(instance: match) + try tournamentStore?.matches.addOrUpdate(instance: match) } catch { Logger.error(error) } @@ -193,7 +193,7 @@ struct MatchSetupView: View { if match.isSeededBy(team: team, inTeamPosition: teamPosition) { if let score = match.teamScore(ofTeam: team) { do { - try tournamentStore.teamScores.delete(instance: score) + try tournamentStore?.teamScores.delete(instance: score) } catch { Logger.error(error) } @@ -201,7 +201,7 @@ struct MatchSetupView: View { team.bracketPosition = nil do { - try tournamentStore.teamRegistrations.addOrUpdate(instance: team) + try tournamentStore?.teamRegistrations.addOrUpdate(instance: team) } catch { Logger.error(error) } @@ -209,7 +209,7 @@ struct MatchSetupView: View { if previousMatch.disabled { previousMatch.enableMatch() do { - try tournamentStore.matches.addOrUpdate(instance: previousMatch) + try tournamentStore?.matches.addOrUpdate(instance: previousMatch) } catch { Logger.error(error) } @@ -217,14 +217,14 @@ struct MatchSetupView: View { } do { - try tournamentStore.matches.addOrUpdate(instance: match) + try tournamentStore?.matches.addOrUpdate(instance: match) } catch { Logger.error(error) } } else if match.isLoserBracket { if let score = match.teamScore(ofTeam: team) { do { - try tournamentStore.teamScores.delete(instance: score) + try tournamentStore?.teamScores.delete(instance: score) } catch { Logger.error(error) } @@ -232,7 +232,7 @@ struct MatchSetupView: View { } else { match.teamWillBeWalkOut(team) do { - try tournamentStore.matches.addOrUpdate(instance: match) + try tournamentStore?.matches.addOrUpdate(instance: match) } catch { Logger.error(error) } diff --git a/PadelClub/Views/Navigation/Toolbox/ToolboxView.swift b/PadelClub/Views/Navigation/Toolbox/ToolboxView.swift index 487f80c..c660ffe 100644 --- a/PadelClub/Views/Navigation/Toolbox/ToolboxView.swift +++ b/PadelClub/Views/Navigation/Toolbox/ToolboxView.swift @@ -73,17 +73,18 @@ struct ToolboxView: View { for tournament in dataStore.tournaments { - let store = tournament.tournamentStore - let playerRegistrations = store.playerRegistrations - playerRegistrations.forEach { player in - player.firstName = player.firstName.trimmed.capitalized - player.lastName = player.lastName.trimmed.uppercased() - } - - do { - try store.playerRegistrations.addOrUpdate(contentOfs: playerRegistrations) - } catch { - Logger.error(error) + if let store = tournament.tournamentStore { + let playerRegistrations = store.playerRegistrations + playerRegistrations.forEach { player in + player.firstName = player.firstName.trimmed.capitalized + player.lastName = player.lastName.trimmed.uppercased() + } + + do { + try store.playerRegistrations.addOrUpdate(contentOfs: playerRegistrations) + } catch { + Logger.error(error) + } } } @@ -95,13 +96,14 @@ struct ToolboxView: View { for tournament in DataStore.shared.tournaments { - let store: TournamentStore = tournament.tournamentStore - - let teamRegistrations = store.teamRegistrations.filter({ $0.tournamentObject() == nil }) - do { - try store.teamRegistrations.delete(contentOfs: teamRegistrations) - } catch { - Logger.error(error) + if let store: TournamentStore = tournament.tournamentStore { + + let teamRegistrations = store.teamRegistrations.filter({ $0.tournamentObject() == nil }) + do { + try store.teamRegistrations.delete(contentOfs: teamRegistrations) + } catch { + Logger.error(error) + } } } } @@ -112,13 +114,14 @@ struct ToolboxView: View { RowButtonView("Delete players") { for tournament in DataStore.shared.tournaments { - let store: TournamentStore = tournament.tournamentStore - - let playersRegistrations = store.playerRegistrations.filter({ $0.team() == nil }) - do { - try store.playerRegistrations.delete(contentOfs: playersRegistrations) - } catch { - Logger.error(error) + if let store: TournamentStore = tournament.tournamentStore { + + let playersRegistrations = store.playerRegistrations.filter({ $0.team() == nil }) + do { + try store.playerRegistrations.delete(contentOfs: playersRegistrations) + } catch { + Logger.error(error) + } } } diff --git a/PadelClub/Views/Planning/GroupStageScheduleEditorView.swift b/PadelClub/Views/Planning/GroupStageScheduleEditorView.swift index ec29423..a0e6866 100644 --- a/PadelClub/Views/Planning/GroupStageScheduleEditorView.swift +++ b/PadelClub/Views/Planning/GroupStageScheduleEditorView.swift @@ -17,7 +17,7 @@ struct GroupStageScheduleEditorView: View { @State private var startDate: Date @State private var currentDate: Date? - var tournamentStore: TournamentStore { + var tournamentStore: TournamentStore? { return self.tournament.tournamentStore } @@ -42,7 +42,7 @@ struct GroupStageScheduleEditorView: View { private func _save() { do { - try tournamentStore.groupStages.addOrUpdate(instance: groupStage) + try tournamentStore?.groupStages.addOrUpdate(instance: groupStage) } catch { Logger.error(error) } diff --git a/PadelClub/Views/Planning/LoserRoundScheduleEditorView.swift b/PadelClub/Views/Planning/LoserRoundScheduleEditorView.swift index e14ad4b..0d5eafb 100644 --- a/PadelClub/Views/Planning/LoserRoundScheduleEditorView.swift +++ b/PadelClub/Views/Planning/LoserRoundScheduleEditorView.swift @@ -19,7 +19,7 @@ struct LoserRoundScheduleEditorView: View { @State private var matchFormat: MatchFormat @State private var currentDate: Date? - var tournamentStore: TournamentStore { + var tournamentStore: TournamentStore? { return self.tournament.tournamentStore } @@ -92,7 +92,7 @@ struct LoserRoundScheduleEditorView: View { private func _save() { do { - try self.tournamentStore.rounds.addOrUpdate(contentOfs: upperRound.loserRounds()) + try self.tournamentStore?.rounds.addOrUpdate(contentOfs: upperRound.loserRounds()) } catch { Logger.error(error) } diff --git a/PadelClub/Views/Planning/LoserRoundStepScheduleEditorView.swift b/PadelClub/Views/Planning/LoserRoundStepScheduleEditorView.swift index 6ee09ec..567c182 100644 --- a/PadelClub/Views/Planning/LoserRoundStepScheduleEditorView.swift +++ b/PadelClub/Views/Planning/LoserRoundStepScheduleEditorView.swift @@ -20,7 +20,7 @@ struct LoserRoundStepScheduleEditorView: View { @State private var startDate: Date //@State private var matchFormat: MatchFormat - var tournamentStore: TournamentStore { + var tournamentStore: TournamentStore? { return self.tournament.tournamentStore } @@ -79,7 +79,7 @@ struct LoserRoundStepScheduleEditorView: View { private func _save() { do { - try tournamentStore.rounds.addOrUpdate(contentOfs: upperRound.loserRounds(forRoundIndex: round.index)) + try tournamentStore?.rounds.addOrUpdate(contentOfs: upperRound.loserRounds(forRoundIndex: round.index)) } catch { Logger.error(error) } diff --git a/PadelClub/Views/Planning/MatchScheduleEditorView.swift b/PadelClub/Views/Planning/MatchScheduleEditorView.swift index 592a4ac..24f534a 100644 --- a/PadelClub/Views/Planning/MatchScheduleEditorView.swift +++ b/PadelClub/Views/Planning/MatchScheduleEditorView.swift @@ -14,7 +14,7 @@ struct MatchScheduleEditorView: View { @State private var startDate: Date @State private var currentDate: Date? - var tournamentStore: TournamentStore { + var tournamentStore: TournamentStore? { return self.tournament.tournamentStore } @@ -54,7 +54,7 @@ struct MatchScheduleEditorView: View { private func _save() { do { - try tournamentStore.matches.addOrUpdate(instance: match) + try tournamentStore?.matches.addOrUpdate(instance: match) } catch { Logger.error(error) } diff --git a/PadelClub/Views/Planning/PlanningSettingsView.swift b/PadelClub/Views/Planning/PlanningSettingsView.swift index 8a0d080..22312e4 100644 --- a/PadelClub/Views/Planning/PlanningSettingsView.swift +++ b/PadelClub/Views/Planning/PlanningSettingsView.swift @@ -24,7 +24,7 @@ struct PlanningSettingsView: View { @State private var deletingDateMatchesDone: Bool = false @State private var deletingDone: Bool = false - var tournamentStore: TournamentStore { + var tournamentStore: TournamentStore? { return self.tournament.tournamentStore } @@ -148,7 +148,7 @@ struct PlanningSettingsView: View { .headerProminence(.increased) .onAppear { do { - try self.tournamentStore.matchSchedulers.addOrUpdate(instance: matchScheduler) + try self.tournamentStore?.matchSchedulers.addOrUpdate(instance: matchScheduler) } catch { Logger.error(error) } @@ -228,7 +228,7 @@ struct PlanningSettingsView: View { $0.startDate = nil $0.confirmed = false }) - try self.tournamentStore.matches.addOrUpdate(contentOfs: allMatches) + try self.tournamentStore?.matches.addOrUpdate(contentOfs: allMatches) deletingDateMatchesDone = true } catch { Logger.error(error) @@ -245,7 +245,7 @@ struct PlanningSettingsView: View { do { deletingDone = false allGroupStages.forEach({ $0.startDate = nil }) - try self.tournamentStore.groupStages.addOrUpdate(contentOfs: allGroupStages) + try self.tournamentStore?.groupStages.addOrUpdate(contentOfs: allGroupStages) deletingDone = true } catch { @@ -261,7 +261,7 @@ struct PlanningSettingsView: View { do { deletingDone = false allRounds.forEach({ $0.startDate = nil }) - try self.tournamentStore.rounds.addOrUpdate(contentOfs: allRounds) + try self.tournamentStore?.rounds.addOrUpdate(contentOfs: allRounds) deletingDone = true } catch { Logger.error(error) @@ -281,13 +281,13 @@ struct PlanningSettingsView: View { $0.startDate = nil $0.confirmed = false }) - try self.tournamentStore.matches.addOrUpdate(contentOfs: allMatches) + try self.tournamentStore?.matches.addOrUpdate(contentOfs: allMatches) allGroupStages.forEach({ $0.startDate = nil }) - try self.tournamentStore.groupStages.addOrUpdate(contentOfs: allGroupStages) + try self.tournamentStore?.groupStages.addOrUpdate(contentOfs: allGroupStages) allRounds.forEach({ $0.startDate = nil }) - try self.tournamentStore.rounds.addOrUpdate(contentOfs: allRounds) + try self.tournamentStore?.rounds.addOrUpdate(contentOfs: allRounds) deletingDone = true } catch { Logger.error(error) @@ -308,13 +308,13 @@ struct PlanningSettingsView: View { $0.endDate = nil $0.confirmed = false }) - try self.tournamentStore.matches.addOrUpdate(contentOfs: tournament.allMatches()) + try self.tournamentStore?.matches.addOrUpdate(contentOfs: tournament.allMatches()) allGroupStages.forEach({ $0.startDate = nil }) - try self.tournamentStore.groupStages.addOrUpdate(contentOfs: allGroupStages) + try self.tournamentStore?.groupStages.addOrUpdate(contentOfs: allGroupStages) allRounds.forEach({ $0.startDate = nil }) - try self.tournamentStore.rounds.addOrUpdate(contentOfs: allRounds) + try self.tournamentStore?.rounds.addOrUpdate(contentOfs: allRounds) deletingDone = true } catch { Logger.error(error) @@ -485,7 +485,7 @@ struct PlanningSettingsView: View { private func _save() { do { - try self.tournamentStore.matchSchedulers.addOrUpdate(instance: matchScheduler) + try self.tournamentStore?.matchSchedulers.addOrUpdate(instance: matchScheduler) try dataStore.tournaments.addOrUpdate(instance: tournament) } catch { Logger.error(error) diff --git a/PadelClub/Views/Planning/PlanningView.swift b/PadelClub/Views/Planning/PlanningView.swift index 0f3193c..7e28e6a 100644 --- a/PadelClub/Views/Planning/PlanningView.swift +++ b/PadelClub/Views/Planning/PlanningView.swift @@ -97,7 +97,7 @@ struct PlanningView: View { ToolbarItem(placement: .topBarTrailing) { Button("Sauver") { do { - try self.tournament.tournamentStore.matches.addOrUpdate(contentOfs: allMatches) + try self.tournament.tournamentStore?.matches.addOrUpdate(contentOfs: allMatches) } catch { Logger.error(error) } @@ -503,7 +503,7 @@ struct PlanningView: View { // } // } // -// try? self.tournament.tournamentStore.matches.addOrUpdate(contentOfs: matches) +// try? self.tournament.tournamentStore?.matches.addOrUpdate(contentOfs: matches) // } // // diff --git a/PadelClub/Views/Planning/RoundScheduleEditorView.swift b/PadelClub/Views/Planning/RoundScheduleEditorView.swift index 679b306..2c42ac7 100644 --- a/PadelClub/Views/Planning/RoundScheduleEditorView.swift +++ b/PadelClub/Views/Planning/RoundScheduleEditorView.swift @@ -16,7 +16,7 @@ struct RoundScheduleEditorView: View { @State private var startDate: Date @State private var currentDate: Date? - var tournamentStore: TournamentStore { + var tournamentStore: TournamentStore? { return self.tournament.tournamentStore } @@ -71,7 +71,7 @@ struct RoundScheduleEditorView: View { private func _save() { do { - try tournamentStore.rounds.addOrUpdate(instance: round) + try tournamentStore?.rounds.addOrUpdate(instance: round) } catch { Logger.error(error) } diff --git a/PadelClub/Views/Planning/SchedulerView.swift b/PadelClub/Views/Planning/SchedulerView.swift index 333357e..a4e83b6 100644 --- a/PadelClub/Views/Planning/SchedulerView.swift +++ b/PadelClub/Views/Planning/SchedulerView.swift @@ -26,7 +26,7 @@ struct SchedulerView: View { var tournament: Tournament var destination: ScheduleDestination - var tournamentStore: TournamentStore { + var tournamentStore: TournamentStore? { return self.tournament.tournamentStore } @@ -49,7 +49,7 @@ struct SchedulerView: View { } do { try dataStore.tournaments.addOrUpdate(instance: tournament) - try self.tournamentStore.groupStages.addOrUpdate(contentOfs: groupStages) + try self.tournamentStore?.groupStages.addOrUpdate(contentOfs: groupStages) } catch { Logger.error(error) } @@ -90,7 +90,7 @@ struct SchedulerView: View { do { try dataStore.tournaments.addOrUpdate(instance: tournament) - try self.tournamentStore.groupStages.addOrUpdate(contentOfs: groupStages) + try self.tournamentStore?.groupStages.addOrUpdate(contentOfs: groupStages) } catch { Logger.error(error) } @@ -131,7 +131,7 @@ struct SchedulerView: View { Button { round.updateMatchFormatAndAllMatches(federalFormat) do { - try self.tournamentStore.rounds.addOrUpdate(instance: round) + try self.tournamentStore?.rounds.addOrUpdate(instance: round) } catch { Logger.error(error) } @@ -176,7 +176,7 @@ struct SchedulerView: View { Button { round.updateMatchFormatAndAllMatches(federalFormat) do { - try self.tournamentStore.rounds.addOrUpdate(instance: round) + try self.tournamentStore?.rounds.addOrUpdate(instance: round) } catch { Logger.error(error) } diff --git a/PadelClub/Views/Player/Components/PlayerSexPickerView.swift b/PadelClub/Views/Player/Components/PlayerSexPickerView.swift index f3cf674..022e037 100644 --- a/PadelClub/Views/Player/Components/PlayerSexPickerView.swift +++ b/PadelClub/Views/Player/Components/PlayerSexPickerView.swift @@ -14,7 +14,7 @@ struct PlayerSexPickerView: View { @Environment(Tournament.self) var tournament: Tournament @Bindable var player: PlayerRegistration - var tournamentStore: TournamentStore { + var tournamentStore: TournamentStore? { return self.tournament.tournamentStore } @@ -38,10 +38,10 @@ struct PlayerSexPickerView: View { private func _save() { player.setComputedRank(in: tournament) - tournamentStore.playerRegistrations.addOrUpdate(instance: player) + tournamentStore?.playerRegistrations.addOrUpdate(instance: player) if let team = player.team() { team.updateWeight(inTournamentCategory: tournament.tournamentCategory) - tournamentStore.teamRegistrations.addOrUpdate(instance: team) + tournamentStore?.teamRegistrations.addOrUpdate(instance: team) } } diff --git a/PadelClub/Views/Player/PlayerDetailView.swift b/PadelClub/Views/Player/PlayerDetailView.swift index 88fe914..5ae6220 100644 --- a/PadelClub/Views/Player/PlayerDetailView.swift +++ b/PadelClub/Views/Player/PlayerDetailView.swift @@ -19,7 +19,7 @@ struct PlayerDetailView: View { @State private var email: String @FocusState var focusedField: PlayerRegistration.CodingKeys? - var tournamentStore: TournamentStore { + var tournamentStore: TournamentStore? { return self.tournament.tournamentStore } @@ -257,13 +257,13 @@ struct PlayerDetailView: View { private func _save() { do { - try self.tournamentStore.playerRegistrations.addOrUpdate(instance: player) + try self.tournamentStore?.playerRegistrations.addOrUpdate(instance: player) } catch { Logger.error(error) } if let team = player.team() { do { - try self.tournamentStore.teamRegistrations.addOrUpdate(instance: team) + try self.tournamentStore?.teamRegistrations.addOrUpdate(instance: team) } catch { Logger.error(error) } diff --git a/PadelClub/Views/Player/PlayerView.swift b/PadelClub/Views/Player/PlayerView.swift index 755ac6d..c604fe9 100644 --- a/PadelClub/Views/Player/PlayerView.swift +++ b/PadelClub/Views/Player/PlayerView.swift @@ -15,7 +15,7 @@ struct PlayerView: View { let player: PlayerRegistration - var tournamentStore: TournamentStore { + var tournamentStore: TournamentStore? { return self.tournament.tournamentStore } @@ -24,7 +24,7 @@ struct PlayerView: View { .swipeActions(edge: .trailing, allowsFullSwipe: true) { Button(role: .destructive) { do { - try self.tournamentStore.playerRegistrations.delete(instance: player) + try self.tournamentStore?.playerRegistrations.delete(instance: player) } catch { Logger.error(error) } diff --git a/PadelClub/Views/Round/DrawLogsView.swift b/PadelClub/Views/Round/DrawLogsView.swift index 6459955..cd16e96 100644 --- a/PadelClub/Views/Round/DrawLogsView.swift +++ b/PadelClub/Views/Round/DrawLogsView.swift @@ -52,7 +52,7 @@ struct DrawLogsView: View { Button("Tout effacer", role: .destructive) { do { - try tournament.tournamentStore.drawLogs.deleteAll() + try tournament.tournamentStore?.drawLogs.deleteAll() } catch { Logger.error(error) } diff --git a/PadelClub/Views/Round/LoserRoundSettingsView.swift b/PadelClub/Views/Round/LoserRoundSettingsView.swift index f39455f..5026317 100644 --- a/PadelClub/Views/Round/LoserRoundSettingsView.swift +++ b/PadelClub/Views/Round/LoserRoundSettingsView.swift @@ -64,7 +64,7 @@ struct LoserRoundSettingsView: View { }) allRoundMatches.forEach({ $0.setMatchName($0.roundTitle()) }) do { - try self.tournament.tournamentStore.matches.addOrUpdate(contentOfs: allRoundMatches) + try self.tournament.tournamentStore?.matches.addOrUpdate(contentOfs: allRoundMatches) } catch { Logger.error(error) } @@ -121,13 +121,13 @@ struct LoserRoundSettingsView: View { } do { - try self.tournament.tournamentStore.matches.addOrUpdate(contentOfs: matches) + try self.tournament.tournamentStore?.matches.addOrUpdate(contentOfs: matches) } catch { Logger.error(error) } do { - try self.tournament.tournamentStore.rounds.addOrUpdate(instance: upperBracketRound.round) + try self.tournament.tournamentStore?.rounds.addOrUpdate(instance: upperBracketRound.round) } catch { Logger.error(error) } @@ -145,7 +145,7 @@ struct LoserRoundSettingsView: View { match._toggleLoserMatchDisableState(true) } do { - try self.tournament.tournamentStore.matches.addOrUpdate(contentOfs: upperBracketRound.round.allLoserRoundMatches()) + try self.tournament.tournamentStore?.matches.addOrUpdate(contentOfs: upperBracketRound.round.allLoserRoundMatches()) } catch { Logger.error(error) } diff --git a/PadelClub/Views/Round/LoserRoundView.swift b/PadelClub/Views/Round/LoserRoundView.swift index fba3563..dd4cd71 100644 --- a/PadelClub/Views/Round/LoserRoundView.swift +++ b/PadelClub/Views/Round/LoserRoundView.swift @@ -133,7 +133,7 @@ struct LoserRoundView: View { let allRoundMatches = loserBracket.allMatches.filter({ $0.name == nil }) allRoundMatches.forEach({ $0.setMatchName($0.roundTitle()) }) do { - try self.tournament.tournamentStore.matches.addOrUpdate(contentOfs: allRoundMatches) + try self.tournament.tournamentStore?.matches.addOrUpdate(contentOfs: allRoundMatches) } catch { Logger.error(error) } diff --git a/PadelClub/Views/Round/RoundSettingsView.swift b/PadelClub/Views/Round/RoundSettingsView.swift index 9bac2be..58ac05e 100644 --- a/PadelClub/Views/Round/RoundSettingsView.swift +++ b/PadelClub/Views/Round/RoundSettingsView.swift @@ -14,7 +14,7 @@ struct RoundSettingsView: View { @Environment(\.isEditingTournamentSeed) private var isEditingTournamentSeed @Environment(Tournament.self) var tournament: Tournament - var tournamentStore: TournamentStore { + var tournamentStore: TournamentStore? { return self.tournament.tournamentStore } @@ -25,7 +25,7 @@ struct RoundSettingsView: View { let issues = tournament.bracketTeamPlacementIssue() DisclosureGroup { ForEach(issues.shouldBeInIt, id: \.self) { id in - if let team: TeamRegistration = self.tournamentStore.teamRegistrations.findById(id) { + if let team: TeamRegistration = self.tournamentStore?.teamRegistrations.findById(id) { TeamRowView(team: team) } } @@ -38,11 +38,11 @@ struct RoundSettingsView: View { } DisclosureGroup { ForEach(issues.shouldNotBeInIt, id: \.self) { id in - if let team = self.tournamentStore.teamRegistrations.findById(id) { + if let team = self.tournamentStore?.teamRegistrations.findById(id) { Menu { Button("Retirer du tableau") { team.resetBracketPosition() - self.tournamentStore.teamRegistrations.addOrUpdate(instance: team) + self.tournamentStore?.teamRegistrations.addOrUpdate(instance: team) } } label: { TeamRowView(team: team) @@ -128,7 +128,7 @@ struct RoundSettingsView: View { let allRoundMatches = tournament.allLoserRoundMatches() allRoundMatches.forEach({ $0.setMatchName($0.roundTitle()) }) do { - try self.tournament.tournamentStore.matches.addOrUpdate(contentOfs: allRoundMatches) + try self.tournament.tournamentStore?.matches.addOrUpdate(contentOfs: allRoundMatches) } catch { Logger.error(error) } @@ -164,8 +164,8 @@ struct RoundSettingsView: View { teams.forEach { team in team.resetBracketPosition() } - try tournamentStore.teamRegistrations.addOrUpdate(contentOfs: teams) - try tournamentStore.rounds.delete(instance: lastRound) + try tournamentStore?.teamRegistrations.addOrUpdate(contentOfs: teams) + try tournamentStore?.rounds.delete(instance: lastRound) } catch { Logger.error(error) } diff --git a/PadelClub/Views/Round/RoundView.swift b/PadelClub/Views/Round/RoundView.swift index 23bf3d5..bd96ee7 100644 --- a/PadelClub/Views/Round/RoundView.swift +++ b/PadelClub/Views/Round/RoundView.swift @@ -30,7 +30,7 @@ struct RoundView: View { BracketEditTip.matchesHidden = upperRound.round.getDisabledMatches().count } - var tournamentStore: TournamentStore { + var tournamentStore: TournamentStore? { return self.tournament.tournamentStore } @@ -242,7 +242,7 @@ struct RoundView: View { FooterButtonView("Désactiver", role: .destructive) { match.disableMatch() do { - try tournamentStore.matches.addOrUpdate(instance: match) + try tournamentStore?.matches.addOrUpdate(instance: match) } catch { Logger.error(error) } @@ -327,7 +327,7 @@ struct RoundView: View { private func _save(seeds: [TeamRegistration]) { do { - try self.tournamentStore.teamRegistrations.addOrUpdate(contentOfs: seeds) + try self.tournamentStore?.teamRegistrations.addOrUpdate(contentOfs: seeds) } catch { Logger.error(error) } @@ -365,7 +365,7 @@ struct RoundView: View { } do { - try tournament.tournamentStore.matches.addOrUpdate(contentOfs: matchesToUpdate + loserMatches) + try tournament.tournamentStore?.matches.addOrUpdate(contentOfs: matchesToUpdate + loserMatches) } catch { Logger.error(error) } diff --git a/PadelClub/Views/Score/EditScoreView.swift b/PadelClub/Views/Score/EditScoreView.swift index 52e3225..d04bf4a 100644 --- a/PadelClub/Views/Score/EditScoreView.swift +++ b/PadelClub/Views/Score/EditScoreView.swift @@ -207,7 +207,7 @@ struct EditScoreView: View { self.confirmScoreEdition = true if let match = matchDescriptor.match { do { - try match.tournamentStore.matches.addOrUpdate(instance: match) + try match.tournamentStore?.matches.addOrUpdate(instance: match) } catch { Logger.error(error) } diff --git a/PadelClub/Views/Team/CoachListView.swift b/PadelClub/Views/Team/CoachListView.swift index 7a980d6..f5a771e 100644 --- a/PadelClub/Views/Team/CoachListView.swift +++ b/PadelClub/Views/Team/CoachListView.swift @@ -50,7 +50,7 @@ struct CoachListView: View { private func _save() { do { - try self.tournament.tournamentStore.teamRegistrations.addOrUpdate(instance: team) + try self.tournament.tournamentStore?.teamRegistrations.addOrUpdate(instance: team) } catch { Logger.error(error) } diff --git a/PadelClub/Views/Team/EditingTeamView.swift b/PadelClub/Views/Team/EditingTeamView.swift index 1e6bad3..8eaf36e 100644 --- a/PadelClub/Views/Team/EditingTeamView.swift +++ b/PadelClub/Views/Team/EditingTeamView.swift @@ -33,7 +33,7 @@ struct EditingTeamView: View { } } - var tournamentStore: TournamentStore { + var tournamentStore: TournamentStore? { return self.tournament.tournamentStore } @@ -100,7 +100,7 @@ struct EditingTeamView: View { team.walkOut = false team.wildCardBracket = value do { - try tournamentStore.teamRegistrations.addOrUpdate(instance: team) + try tournamentStore?.teamRegistrations.addOrUpdate(instance: team) } catch { Logger.error(error) } @@ -116,7 +116,7 @@ struct EditingTeamView: View { team.walkOut = false team.wildCardGroupStage = value do { - try tournamentStore.teamRegistrations.addOrUpdate(instance: team) + try tournamentStore?.teamRegistrations.addOrUpdate(instance: team) } catch { Logger.error(error) } @@ -132,7 +132,7 @@ struct EditingTeamView: View { team.wildCardGroupStage = false team.walkOut = value do { - try tournamentStore.teamRegistrations.addOrUpdate(instance: team) + try tournamentStore?.teamRegistrations.addOrUpdate(instance: team) } catch { Logger.error(error) } @@ -195,7 +195,7 @@ struct EditingTeamView: View { RowButtonView("Effacer l'équipe", role: .destructive, systemImage: "trash") { team.deleteTeamScores() do { - try tournamentStore.teamRegistrations.delete(instance: team) + try tournamentStore?.teamRegistrations.delete(instance: team) } catch { Logger.error(error) } @@ -303,7 +303,7 @@ struct EditingTeamView: View { } set: { confirmed in team.confirmationDate = confirmed ? Date() : nil do { - try tournamentStore.teamRegistrations.addOrUpdate(instance: team) + try tournamentStore?.teamRegistrations.addOrUpdate(instance: team) } catch { Logger.error(error) } @@ -318,7 +318,7 @@ struct EditingTeamView: View { $0.hasArrived = hasArrived } do { - try tournamentStore.playerRegistrations.addOrUpdate(contentOfs: team.unsortedPlayers()) + try tournamentStore?.playerRegistrations.addOrUpdate(contentOfs: team.unsortedPlayers()) } catch { Logger.error(error) } @@ -327,7 +327,7 @@ struct EditingTeamView: View { } private func _save() { do { - try tournamentStore.teamRegistrations.addOrUpdate(instance: team) + try tournamentStore?.teamRegistrations.addOrUpdate(instance: team) } catch { Logger.error(error) } diff --git a/PadelClub/Views/Tournament/FileImportView.swift b/PadelClub/Views/Tournament/FileImportView.swift index a25219b..46cea98 100644 --- a/PadelClub/Views/Tournament/FileImportView.swift +++ b/PadelClub/Views/Tournament/FileImportView.swift @@ -105,7 +105,7 @@ struct FileImportView: View { _fileProvider = .init(wrappedValue: defaultFileProvider) } - var tournamentStore: TournamentStore { + var tournamentStore: TournamentStore? { return self.tournament.tournamentStore } @@ -123,7 +123,7 @@ struct FileImportView: View { private func _deleteTeams() async { await MainActor.run { do { - try tournamentStore.teamRegistrations.delete(contentOfs: tournament.unsortedTeams()) + try tournamentStore?.teamRegistrations.delete(contentOfs: tournament.unsortedTeams()) } catch { Logger.error(error) } @@ -509,7 +509,7 @@ struct FileImportView: View { } do { - try tournament.tournamentStore.teamRegistrations.addOrUpdate(contentOfs: unfound) + try tournament.tournamentStore?.teamRegistrations.addOrUpdate(contentOfs: unfound) } catch { Logger.error(error) } diff --git a/PadelClub/Views/Tournament/Screen/AddTeamView.swift b/PadelClub/Views/Tournament/Screen/AddTeamView.swift index bf8b9ec..784a928 100644 --- a/PadelClub/Views/Tournament/Screen/AddTeamView.swift +++ b/PadelClub/Views/Tournament/Screen/AddTeamView.swift @@ -46,7 +46,7 @@ struct AddTeamView: View { @State private var testMessageIndex: Int = 0 @State private var presentLocalMultiplayerSearch: Bool = false - var tournamentStore: TournamentStore { + var tournamentStore: TournamentStore? { return self.tournament.tournamentStore } @@ -361,8 +361,8 @@ struct AddTeamView: View { } let team = tournament.addTeam(players) - self.tournamentStore.teamRegistrations.addOrUpdate(instance: team) - self.tournamentStore.playerRegistrations.addOrUpdate(contentOfs: players) + self.tournamentStore?.teamRegistrations.addOrUpdate(instance: team) + self.tournamentStore?.playerRegistrations.addOrUpdate(contentOfs: players) pasteString = nil editableTextField = "" @@ -389,8 +389,8 @@ struct AddTeamView: View { let players = _currentSelection() editedTeam.updatePlayers(players, inTournamentCategory: tournament.tournamentCategory) - self.tournamentStore.teamRegistrations.addOrUpdate(instance: editedTeam) - self.tournamentStore.playerRegistrations.addOrUpdate(contentOfs: players) + self.tournamentStore?.teamRegistrations.addOrUpdate(instance: editedTeam) + self.tournamentStore?.playerRegistrations.addOrUpdate(contentOfs: players) pasteString = nil editableTextField = "" diff --git a/PadelClub/Views/Tournament/Screen/Components/InscriptionInfoView.swift b/PadelClub/Views/Tournament/Screen/Components/InscriptionInfoView.swift index d39e5e0..c63eba2 100644 --- a/PadelClub/Views/Tournament/Screen/Components/InscriptionInfoView.swift +++ b/PadelClub/Views/Tournament/Screen/Components/InscriptionInfoView.swift @@ -224,13 +224,15 @@ struct InscriptionInfoView: View { Section { DisclosureGroup { ForEach(playersWithoutValidLicense) { - EditablePlayerView(player: $0, editingOptions: [.licenceId]) - .environmentObject(tournament.tournamentStore) - .onChange(of: $0.licenceId) { - players = tournament.unsortedPlayers() - let isImported = players.anySatisfy({ $0.isImported() }) - playersWithoutValidLicense = tournament.playersWithoutValidLicense(in: players, isImported: isImported) - } + if let store = tournament.tournamentStore { + EditablePlayerView(player: $0, editingOptions: [.licenceId]) + .environmentObject(store) + .onChange(of: $0.licenceId) { + players = tournament.unsortedPlayers() + let isImported = players.anySatisfy({ $0.isImported() }) + playersWithoutValidLicense = tournament.playersWithoutValidLicense(in: players, isImported: isImported) + } + } } } label: { LabeledContent { diff --git a/PadelClub/Views/Tournament/Screen/Components/TournamentGeneralSettingsView.swift b/PadelClub/Views/Tournament/Screen/Components/TournamentGeneralSettingsView.swift index 7b2f6fe..e9511b4 100644 --- a/PadelClub/Views/Tournament/Screen/Components/TournamentGeneralSettingsView.swift +++ b/PadelClub/Views/Tournament/Screen/Components/TournamentGeneralSettingsView.swift @@ -225,14 +225,14 @@ struct TournamentGeneralSettingsView: View { } do { - try self.tournament.tournamentStore.matches.addOrUpdate(contentOfs: matches) + try self.tournament.tournamentStore?.matches.addOrUpdate(contentOfs: matches) } catch { Logger.error(error) } } do { - try self.tournament.tournamentStore.rounds.addOrUpdate(contentOfs: rounds) + try self.tournament.tournamentStore?.rounds.addOrUpdate(contentOfs: rounds) } catch { Logger.error(error) } diff --git a/PadelClub/Views/Tournament/Screen/Components/TournamentMatchFormatsSettingsView.swift b/PadelClub/Views/Tournament/Screen/Components/TournamentMatchFormatsSettingsView.swift index ebf4bde..c89afd4 100644 --- a/PadelClub/Views/Tournament/Screen/Components/TournamentMatchFormatsSettingsView.swift +++ b/PadelClub/Views/Tournament/Screen/Components/TournamentMatchFormatsSettingsView.swift @@ -17,7 +17,7 @@ struct TournamentMatchFormatsSettingsView: View { @State private var confirmUpdate: Bool = false @State private var updateCompleted: Bool = false - var tournamentStore: TournamentStore { + var tournamentStore: TournamentStore? { return self.tournament.tournamentStore } @@ -99,12 +99,12 @@ struct TournamentMatchFormatsSettingsView: View { } } do { - try tournamentStore.groupStages.addOrUpdate(contentOfs: groupStages) + try tournamentStore?.groupStages.addOrUpdate(contentOfs: groupStages) } catch { Logger.error(error) } do { - try tournamentStore.rounds.addOrUpdate(contentOfs: allRounds) + try tournamentStore?.rounds.addOrUpdate(contentOfs: allRounds) } catch { Logger.error(error) } diff --git a/PadelClub/Views/Tournament/Screen/Components/UpdateSourceRankDateView.swift b/PadelClub/Views/Tournament/Screen/Components/UpdateSourceRankDateView.swift index 07f95d5..7527c1e 100644 --- a/PadelClub/Views/Tournament/Screen/Components/UpdateSourceRankDateView.swift +++ b/PadelClub/Views/Tournament/Screen/Components/UpdateSourceRankDateView.swift @@ -17,7 +17,7 @@ struct UpdateSourceRankDateView: View { @State private var updatingRank = false var tournament: Tournament - var tournamentStore: TournamentStore { + var tournamentStore: TournamentStore? { return self.tournament.tournamentStore } @@ -48,7 +48,7 @@ struct UpdateSourceRankDateView: View { player.setComputedRank(in: tournament) } - try tournamentStore.playerRegistrations.addOrUpdate(contentOfs: tournament.unsortedPlayers()) + try tournamentStore?.playerRegistrations.addOrUpdate(contentOfs: tournament.unsortedPlayers()) tournament.unsortedTeams().forEach { team in team.setWeight(from: team.players(), inTournamentCategory: tournament.tournamentCategory) @@ -57,7 +57,7 @@ struct UpdateSourceRankDateView: View { } } - try tournamentStore.teamRegistrations.addOrUpdate(contentOfs: tournament.unsortedTeams()) + try tournamentStore?.teamRegistrations.addOrUpdate(contentOfs: tournament.unsortedTeams()) try dataStore.tournaments.addOrUpdate(instance: tournament) diff --git a/PadelClub/Views/Tournament/Screen/InscriptionManagerView.swift b/PadelClub/Views/Tournament/Screen/InscriptionManagerView.swift index 775bdc2..3f4b2c5 100644 --- a/PadelClub/Views/Tournament/Screen/InscriptionManagerView.swift +++ b/PadelClub/Views/Tournament/Screen/InscriptionManagerView.swift @@ -50,7 +50,7 @@ struct InscriptionManagerView: View { @State private var pasteString: String? @State private var registrationIssues: Int? = nil - var tournamentStore: TournamentStore { + var tournamentStore: TournamentStore? { return self.tournament.tournamentStore } @@ -195,7 +195,7 @@ struct InscriptionManagerView: View { } do { - try tournamentStore.teamRegistrations.addOrUpdate(contentOfs: waitingList) + try tournamentStore?.teamRegistrations.addOrUpdate(contentOfs: waitingList) try dataStore.tournaments.addOrUpdate(instance: tournament) } catch { Logger.error(error) @@ -979,7 +979,7 @@ struct InscriptionManagerView: View { Button(role: .destructive) { team.deleteTeamScores() do { - try tournamentStore.teamRegistrations.delete(instance: team) + try tournamentStore?.teamRegistrations.delete(instance: team) } catch { Logger.error(error) } diff --git a/PadelClub/Views/Tournament/Screen/TableStructureView.swift b/PadelClub/Views/Tournament/Screen/TableStructureView.swift index b89c2b5..35936f8 100644 --- a/PadelClub/Views/Tournament/Screen/TableStructureView.swift +++ b/PadelClub/Views/Tournament/Screen/TableStructureView.swift @@ -388,7 +388,7 @@ struct TableStructureView: View { team.groupStage = nil } do { - try tournament.tournamentStore.teamRegistrations.addOrUpdate(contentOfs: teams) + try tournament.tournamentStore?.teamRegistrations.addOrUpdate(contentOfs: teams) } catch { Logger.error(error) } diff --git a/PadelClub/Views/Tournament/Screen/TournamentCashierView.swift b/PadelClub/Views/Tournament/Screen/TournamentCashierView.swift index 438ade9..c4c79ef 100644 --- a/PadelClub/Views/Tournament/Screen/TournamentCashierView.swift +++ b/PadelClub/Views/Tournament/Screen/TournamentCashierView.swift @@ -137,33 +137,37 @@ struct TournamentCashierView: View { } var body: some View { - VStack(spacing: 0) { - GenericDestinationPickerView(selectedDestination: $selectedDestination, destinations: allDestinations, nilDestinationIsValid: true) - switch selectedDestination { - case .none: - CashierSettingsView(tournament: tournament) - case .some(let selectedCall): - switch selectedCall { - case .summary: - CashierDetailView(tournament: tournament).id(selectedCall.id) - case .groupStage(let groupStage): - CashierView(tournament: tournament, teams: groupStage.teams()).id(selectedCall.id) - .searchable(text: $cashierViewModel.searchText, isPresented: $cashierViewModel.isSearching, placement: .navigationBarDrawer(displayMode: .always), prompt: Text("Chercher un joueur")) - case .bracket(let round): - CashierView(tournament: tournament, teams: round.seeds()).id(selectedCall.id) - .searchable(text: $cashierViewModel.searchText, isPresented: $cashierViewModel.isSearching, placement: .navigationBarDrawer(displayMode: .always), prompt: Text("Chercher un joueur")) - case .all(let tournament): - CashierView(tournament: tournament, teams: tournament.selectedSortedTeams()).id(selectedCall.id) - .searchable(text: $cashierViewModel.searchText, isPresented: $cashierViewModel.isSearching, placement: .navigationBarDrawer(displayMode: .always), prompt: Text("Chercher un joueur")) + if let store = self.tournament.tournamentStore { + VStack(spacing: 0) { + GenericDestinationPickerView(selectedDestination: $selectedDestination, destinations: allDestinations, nilDestinationIsValid: true) + switch selectedDestination { + case .none: + CashierSettingsView(tournament: tournament) + case .some(let selectedCall): + switch selectedCall { + case .summary: + CashierDetailView(tournament: tournament).id(selectedCall.id) + case .groupStage(let groupStage): + CashierView(tournament: tournament, teams: groupStage.teams()).id(selectedCall.id) + .searchable(text: $cashierViewModel.searchText, isPresented: $cashierViewModel.isSearching, placement: .navigationBarDrawer(displayMode: .always), prompt: Text("Chercher un joueur")) + case .bracket(let round): + CashierView(tournament: tournament, teams: round.seeds()).id(selectedCall.id) + .searchable(text: $cashierViewModel.searchText, isPresented: $cashierViewModel.isSearching, placement: .navigationBarDrawer(displayMode: .always), prompt: Text("Chercher un joueur")) + case .all(let tournament): + CashierView(tournament: tournament, teams: tournament.selectedSortedTeams()).id(selectedCall.id) + .searchable(text: $cashierViewModel.searchText, isPresented: $cashierViewModel.isSearching, placement: .navigationBarDrawer(displayMode: .always), prompt: Text("Chercher un joueur")) + } } } + .environment(tournament) + .environmentObject(store) + .environmentObject(cashierViewModel) + .navigationBarTitleDisplayMode(.inline) + .toolbarBackground(.visible, for: .navigationBar) + .navigationTitle(tournament.isFree() ? "Présence" : "Encaissement") + } else { + Text("no store") } - .environment(tournament) - .environmentObject(tournament.tournamentStore) - .environmentObject(cashierViewModel) - .navigationBarTitleDisplayMode(.inline) - .toolbarBackground(.visible, for: .navigationBar) - .navigationTitle(tournament.isFree() ? "Présence" : "Encaissement") } } diff --git a/PadelClub/Views/Tournament/Screen/TournamentRankView.swift b/PadelClub/Views/Tournament/Screen/TournamentRankView.swift index 62c5352..e79b11d 100644 --- a/PadelClub/Views/Tournament/Screen/TournamentRankView.swift +++ b/PadelClub/Views/Tournament/Screen/TournamentRankView.swift @@ -17,7 +17,7 @@ struct TournamentRankView: View { @State private var calculating = false @State private var selectedTeam: TeamRegistration? - var tournamentStore: TournamentStore { + var tournamentStore: TournamentStore? { return self.tournament.tournamentStore } @@ -118,7 +118,7 @@ struct TournamentRankView: View { Button("Valider") { selectedTeam.pointsEarned = tournament.isAnimation() ? nil : tournament.tournamentLevel.points(for: selectedTeam.finalRanking! - 1, count: tournament.teamCount) do { - try self.tournamentStore.teamRegistrations.addOrUpdate(instance: selectedTeam) + try self.tournamentStore?.teamRegistrations.addOrUpdate(instance: selectedTeam) } catch { Logger.error(error) } @@ -171,7 +171,7 @@ struct TournamentRankView: View { team.finalRanking = key team.pointsEarned = tournament.isAnimation() ? nil : tournament.tournamentLevel.points(for: key - 1, count: tournament.teamCount) do { - try self.tournament.tournamentStore.teamRegistrations.addOrUpdate(instance: team) + try self.tournament.tournamentStore?.teamRegistrations.addOrUpdate(instance: team) } catch { Logger.error(error) } @@ -273,7 +273,7 @@ struct TournamentRankView: View { team.pointsEarned = tournament.isAnimation() ? nil : tournament.tournamentLevel.points(for: key - 1, count: tournament.teamCount) do { - try self.tournament.tournamentStore.teamRegistrations.addOrUpdate(instance: team) + try self.tournament.tournamentStore?.teamRegistrations.addOrUpdate(instance: team) } catch { Logger.error(error) } @@ -289,7 +289,7 @@ struct TournamentRankView: View { Button("Valider") { team.pointsEarned = tournament.isAnimation() ? nil : tournament.tournamentLevel.points(for: key - 1, count: tournament.teamCount) do { - try self.tournament.tournamentStore.teamRegistrations.addOrUpdate(instance: team) + try self.tournament.tournamentStore?.teamRegistrations.addOrUpdate(instance: team) } catch { Logger.error(error) } @@ -317,7 +317,7 @@ struct TournamentRankView: View { private func _save() { do { - try self.tournamentStore.teamRegistrations.addOrUpdate(contentOfs: tournament.unsortedTeams()) + try self.tournamentStore?.teamRegistrations.addOrUpdate(contentOfs: tournament.unsortedTeams()) } catch { Logger.error(error) }