From e12dbff90d1741dcdce6241fe10545ee2d8adf2e Mon Sep 17 00:00:00 2001 From: Raz Date: Wed, 6 Nov 2024 18:14:00 +0100 Subject: [PATCH] fix issue with smart planning save --- .../Umpire/UmpireStatisticView.swift | 18 ++++++++++++++++ .../GroupStageScheduleEditorView.swift | 10 +++++++-- .../LoserRoundScheduleEditorView.swift | 18 +++++++++++++--- .../Planning/MatchScheduleEditorView.swift | 21 ++++++++++++++++++- .../Planning/RoundScheduleEditorView.swift | 9 ++++++-- .../Views/Player/PlayerStatisticView.swift | 18 ++++++++++++++++ 6 files changed, 86 insertions(+), 8 deletions(-) create mode 100644 PadelClub/Views/Navigation/Umpire/UmpireStatisticView.swift create mode 100644 PadelClub/Views/Player/PlayerStatisticView.swift diff --git a/PadelClub/Views/Navigation/Umpire/UmpireStatisticView.swift b/PadelClub/Views/Navigation/Umpire/UmpireStatisticView.swift new file mode 100644 index 0000000..dcea4eb --- /dev/null +++ b/PadelClub/Views/Navigation/Umpire/UmpireStatisticView.swift @@ -0,0 +1,18 @@ +// +// UmpireStatisticView.swift +// PadelClub +// +// Created by razmig on 06/11/2024. +// + +import SwiftUI + +struct UmpireStatisticView: View { + var body: some View { + Text(/*@START_MENU_TOKEN@*/"Hello, World!"/*@END_MENU_TOKEN@*/) + } +} + +#Preview { + UmpireStatisticView() +} diff --git a/PadelClub/Views/Planning/GroupStageScheduleEditorView.swift b/PadelClub/Views/Planning/GroupStageScheduleEditorView.swift index 50b7cf4..ec29423 100644 --- a/PadelClub/Views/Planning/GroupStageScheduleEditorView.swift +++ b/PadelClub/Views/Planning/GroupStageScheduleEditorView.swift @@ -15,7 +15,8 @@ struct GroupStageScheduleEditorView: View { @Bindable var groupStage: GroupStage var tournament: Tournament @State private var startDate: Date - + @State private var currentDate: Date? + var tournamentStore: TournamentStore { return self.tournament.tournamentStore } @@ -24,14 +25,19 @@ struct GroupStageScheduleEditorView: View { self.groupStage = groupStage self.tournament = tournament self._startDate = State(wrappedValue: groupStage.startDate ?? tournament.startDate) + self._currentDate = State(wrappedValue: groupStage.startDate) } var body: some View { - GroupStageDatePickingView(title: groupStage.groupStageTitle(.title), startDate: $startDate, currentDate: $groupStage.startDate, duration: groupStage.matchFormat.getEstimatedDuration(tournament.additionalEstimationDuration)) { + GroupStageDatePickingView(title: groupStage.groupStageTitle(.title), startDate: $startDate, currentDate: $currentDate, duration: groupStage.matchFormat.getEstimatedDuration(tournament.additionalEstimationDuration)) { groupStage.startDate = startDate tournament.matchScheduler()?.updateGroupStageSchedule(tournament: tournament, specificGroupStage: groupStage) _save() } + .onChange(of: currentDate) { + groupStage.startDate = currentDate + _save() + } } private func _save() { diff --git a/PadelClub/Views/Planning/LoserRoundScheduleEditorView.swift b/PadelClub/Views/Planning/LoserRoundScheduleEditorView.swift index f280dd2..e14ad4b 100644 --- a/PadelClub/Views/Planning/LoserRoundScheduleEditorView.swift +++ b/PadelClub/Views/Planning/LoserRoundScheduleEditorView.swift @@ -17,7 +17,8 @@ struct LoserRoundScheduleEditorView: View { var loserRounds: [Round] @State private var startDate: Date @State private var matchFormat: MatchFormat - + @State private var currentDate: Date? + var tournamentStore: TournamentStore { return self.tournament.tournamentStore } @@ -27,8 +28,11 @@ struct LoserRoundScheduleEditorView: View { self.tournament = tournament let _loserRounds = upperRound.loserRounds() self.loserRounds = _loserRounds - self._startDate = State(wrappedValue: _loserRounds.first(where: { $0.startDate != nil })?.startDate ?? _loserRounds.first(where: { $0.isDisabled() == false })?.enabledMatches().first?.startDate ?? tournament.startDate) + let startDate = _loserRounds.first(where: { $0.startDate != nil })?.startDate ?? _loserRounds.first(where: { $0.isDisabled() == false })?.enabledMatches().first?.startDate + + self._startDate = State(wrappedValue: startDate ?? tournament.startDate) self._matchFormat = State(wrappedValue: _loserRounds.first?.matchFormat ?? upperRound.matchFormat) + self._currentDate = State(wrappedValue: startDate) } var body: some View { @@ -37,9 +41,17 @@ struct LoserRoundScheduleEditorView: View { await _updateSchedule() } - DatePickingView(title: "Horaire minimum", startDate: $startDate, currentDate: .constant(nil), duration: matchFormat.getEstimatedDuration(tournament.additionalEstimationDuration)) { + DatePickingView(title: "Horaire minimum", startDate: $startDate, currentDate: $currentDate, duration: matchFormat.getEstimatedDuration(tournament.additionalEstimationDuration)) { await _updateSchedule() } + .onChange(of: currentDate) { + let enabledLoserRounds = upperRound.loserRounds().filter({ $0.isDisabled() == false }) + for loserRound in enabledLoserRounds { + loserRound.startDate = currentDate + } + + _save() + } let enabledLoserRounds = upperRound.loserRounds().filter({ $0.isDisabled() == false }) ForEach(enabledLoserRounds.indices, id: \.self) { index in diff --git a/PadelClub/Views/Planning/MatchScheduleEditorView.swift b/PadelClub/Views/Planning/MatchScheduleEditorView.swift index 95a1bce..592a4ac 100644 --- a/PadelClub/Views/Planning/MatchScheduleEditorView.swift +++ b/PadelClub/Views/Planning/MatchScheduleEditorView.swift @@ -6,16 +6,23 @@ // import SwiftUI +import LeStorage struct MatchScheduleEditorView: View { @Bindable var match: Match var tournament: Tournament @State private var startDate: Date + @State private var currentDate: Date? + + var tournamentStore: TournamentStore { + return self.tournament.tournamentStore + } init(match: Match, tournament: Tournament) { self.match = match self.tournament = tournament self._startDate = State(wrappedValue: match.startDate ?? tournament.startDate) + self._currentDate = State(wrappedValue: match.startDate) } var title: String { @@ -31,15 +38,27 @@ struct MatchScheduleEditorView: View { await _updateSchedule() } - DatePickingView(title: title, startDate: $startDate, currentDate: .constant(nil), duration: match.matchFormat.getEstimatedDuration(tournament.additionalEstimationDuration)) { + DatePickingView(title: title, startDate: $startDate, currentDate: $currentDate, duration: match.matchFormat.getEstimatedDuration(tournament.additionalEstimationDuration)) { await _updateSchedule() } + .onChange(of: currentDate) { + match.startDate = currentDate + _save() + } } private func _updateSchedule() async { let scheduler: MatchScheduler? = tournament.matchScheduler() scheduler?.updateBracketSchedule(tournament: tournament, fromRoundId: match.round, fromMatchId: match.id, startDate: startDate) } + + private func _save() { + do { + try tournamentStore.matches.addOrUpdate(instance: match) + } catch { + Logger.error(error) + } + } } //#Preview { diff --git a/PadelClub/Views/Planning/RoundScheduleEditorView.swift b/PadelClub/Views/Planning/RoundScheduleEditorView.swift index b2ad186..679b306 100644 --- a/PadelClub/Views/Planning/RoundScheduleEditorView.swift +++ b/PadelClub/Views/Planning/RoundScheduleEditorView.swift @@ -14,7 +14,8 @@ struct RoundScheduleEditorView: View { var round: Round var tournament: Tournament @State private var startDate: Date - + @State private var currentDate: Date? + var tournamentStore: TournamentStore { return self.tournament.tournamentStore } @@ -23,6 +24,7 @@ struct RoundScheduleEditorView: View { self.round = round self.tournament = tournament self._startDate = State(wrappedValue: round.startDate ?? round.playedMatches().first?.startDate ?? tournament.startDate) + self._currentDate = State(wrappedValue: round.startDate) } var body: some View { @@ -32,9 +34,12 @@ struct RoundScheduleEditorView: View { await _updateSchedule() } - DatePickingView(title: "Horaire minimum", startDate: $startDate, currentDate: $round.startDate, duration: round.matchFormat.getEstimatedDuration(tournament.additionalEstimationDuration)) { + DatePickingView(title: "Horaire minimum", startDate: $startDate, currentDate: $currentDate, duration: round.matchFormat.getEstimatedDuration(tournament.additionalEstimationDuration)) { await _updateSchedule() } + .onChange(of: currentDate) { + round.startDate = currentDate + } ForEach(round.playedMatches()) { match in MatchScheduleEditorView(match: match, tournament: tournament) diff --git a/PadelClub/Views/Player/PlayerStatisticView.swift b/PadelClub/Views/Player/PlayerStatisticView.swift new file mode 100644 index 0000000..e396964 --- /dev/null +++ b/PadelClub/Views/Player/PlayerStatisticView.swift @@ -0,0 +1,18 @@ +// +// PlayerStatisticView.swift +// PadelClub +// +// Created by razmig on 06/11/2024. +// + +import SwiftUI + +struct PlayerStatisticView: View { + var body: some View { + Text(/*@START_MENU_TOKEN@*/"Hello, World!"/*@END_MENU_TOKEN@*/) + } +} + +#Preview { + PlayerStatisticView() +}