From 8fbafd8c08cdd4419b5d64d5ae64e769e47905d3 Mon Sep 17 00:00:00 2001 From: Raz Date: Fri, 25 Oct 2024 11:14:52 +0200 Subject: [PATCH] wip --- PadelClub/Data/GroupStage.swift | 9 +++++ .../Components/GroupStageSettingsView.swift | 10 ++++-- .../GroupStage/GroupStagesSettingsView.swift | 36 ++++++++++++++++--- 3 files changed, 48 insertions(+), 7 deletions(-) diff --git a/PadelClub/Data/GroupStage.swift b/PadelClub/Data/GroupStage.swift index e00435d..b7bd594 100644 --- a/PadelClub/Data/GroupStage.swift +++ b/PadelClub/Data/GroupStage.swift @@ -115,6 +115,15 @@ final class GroupStage: ModelObject, Storable { return match } + func removeReturnMatches() { + let returnMatches = _matches().filter({ $0.index >= matchCount }) + do { + try self.tournamentStore.matches.delete(contentOfs: returnMatches) + } catch { + Logger.error(error) + } + } + func addReturnMatches() { var teamScores = [TeamScore]() var matches = [Match]() diff --git a/PadelClub/Views/GroupStage/Components/GroupStageSettingsView.swift b/PadelClub/Views/GroupStage/Components/GroupStageSettingsView.swift index 9f7851a..7bf706d 100644 --- a/PadelClub/Views/GroupStage/Components/GroupStageSettingsView.swift +++ b/PadelClub/Views/GroupStage/Components/GroupStageSettingsView.swift @@ -150,8 +150,14 @@ struct GroupStageSettingsView: View { } Section { - RowButtonView("Rajouter les matchs retours", role: .destructive) { - groupStage.addReturnMatches() + if groupStage.isReturnMatchEnabled() { + RowButtonView("Effacer les matchs retours", role: .destructive) { + groupStage.removeReturnMatches() + } + } else { + RowButtonView("Rajouter les matchs retours", role: .destructive) { + groupStage.addReturnMatches() + } } } diff --git a/PadelClub/Views/GroupStage/GroupStagesSettingsView.swift b/PadelClub/Views/GroupStage/GroupStagesSettingsView.swift index 8f31945..bafec28 100644 --- a/PadelClub/Views/GroupStage/GroupStagesSettingsView.swift +++ b/PadelClub/Views/GroupStage/GroupStagesSettingsView.swift @@ -12,7 +12,7 @@ struct GroupStagesSettingsView: View { @EnvironmentObject var dataStore: DataStore @Environment(\.dismiss) private var dismiss @Environment(Tournament.self) var tournament: Tournament - @State private var generationDone: Bool = false + @State private var generationDoneMessage: String? let step: Int var tournamentStore: TournamentStore { @@ -167,6 +167,32 @@ struct GroupStagesSettingsView: View { } footer: { Text("Redistribue les équipes par la méthode du serpentin") } + + let groupStages = tournament.groupStages() + + Section { + if groupStages.anySatisfy({ $0.isReturnMatchEnabled() }) { + RowButtonView("Effacer les matchs retours", role: .destructive) { + groupStages.filter({ $0.isReturnMatchEnabled() }).forEach { groupStage in + groupStage.removeReturnMatches() + } + generationDoneMessage = "Matchs retours effacés" + } + } + + } + + Section { + if groupStages.anySatisfy({ $0.isReturnMatchEnabled() == false }) { + RowButtonView("Rajouter les matchs retours", role: .destructive) { + groupStages.filter({ $0.isReturnMatchEnabled() == false }).forEach { groupStage in + groupStage.addReturnMatches() + } + + generationDoneMessage = "Matchs retours créés" + } + } + } Section { RowButtonView("Nommer les poules alphabétiquement", role: .destructive) { @@ -220,8 +246,8 @@ struct GroupStagesSettingsView: View { } .overlay(alignment: .bottom) { - if generationDone { - Label("Poules mises à jour", systemImage: "checkmark.circle.fill") + if let generationDoneMessage { + Label(generationDoneMessage, systemImage: "checkmark.circle.fill") .toastFormatted() .deferredRendering(for: .seconds(2)) } @@ -238,7 +264,7 @@ struct GroupStagesSettingsView: View { RowButtonView("Refaire les poules", role: .destructive) { tournament.deleteGroupStages() tournament.buildGroupStages() - generationDone = true + generationDoneMessage = "Poules mises à jour" tournament.shouldVerifyGroupStage = false _save() } @@ -249,7 +275,7 @@ struct GroupStagesSettingsView: View { RowButtonView("Poule \(mode.localizedLabel().lowercased())", role: .destructive, systemImage: mode.systemImage) { tournament.groupStageOrderingMode = mode tournament.refreshGroupStages(keepExistingMatches: true) - generationDone = true + generationDoneMessage = "Poules mises à jour" tournament.shouldVerifyGroupStage = false _save() }