From 8fc6f09908e74bea9ca5a4f1f0cda8d465a8e824 Mon Sep 17 00:00:00 2001 From: Razmig Sarkissian Date: Mon, 9 Sep 2024 19:03:30 +0200 Subject: [PATCH] clean up --- PadelClub/Data/Tournament.swift | 12 +-- .../LoserBracketFromGroupStageView.swift | 102 ++++++++++++++---- PadelClub/Views/Round/RoundSettingsView.swift | 3 +- 3 files changed, 87 insertions(+), 30 deletions(-) diff --git a/PadelClub/Data/Tournament.swift b/PadelClub/Data/Tournament.swift index abb008c..a4726bc 100644 --- a/PadelClub/Data/Tournament.swift +++ b/PadelClub/Data/Tournament.swift @@ -348,19 +348,19 @@ final class Tournament : ModelObject, Storable { override func deleteDependencies() throws { let store = self.tournamentStore - let teams = self.unsortedTeams() + let teams = self.tournamentStore.teamRegistrations for team in teams { try team.deleteDependencies() } store.teamRegistrations.deleteDependencies(teams) - let groups = self.groupStages() + let groups = self.tournamentStore.groupStages for group in groups { try group.deleteDependencies() } store.groupStages.deleteDependencies(groups) - let rounds = self.rounds() + let rounds = self.self.tournamentStore.rounds for round in rounds { try round.deleteDependencies() } @@ -2019,13 +2019,13 @@ defer { DataStore.shared.tournaments.writeChangeAndInsertOnServer(instance: self) - for teamRegistration in self.unsortedTeams() { + for teamRegistration in self.tournamentStore.teamRegistrations { teamRegistration.insertOnServer() } - for groupStage in self.groupStages() { + for groupStage in self.tournamentStore.groupStages { groupStage.insertOnServer() } - for round in self.allRounds() { + for round in self.tournamentStore.rounds { round.insertOnServer() } diff --git a/PadelClub/Views/GroupStage/LoserBracketFromGroupStageView.swift b/PadelClub/Views/GroupStage/LoserBracketFromGroupStageView.swift index 7e85d61..08efa0e 100644 --- a/PadelClub/Views/GroupStage/LoserBracketFromGroupStageView.swift +++ b/PadelClub/Views/GroupStage/LoserBracketFromGroupStageView.swift @@ -54,31 +54,10 @@ struct LoserBracketFromGroupStageView: View { } } footer: { if isEditingLoserBracketGroupStage == true { - HStack { - if match.index > tournament.groupStageLoserBracketsInitialPlace() { - FooterButtonView("même place qu'au-dessus") { - match.index -= 2 - do { - try tournamentStore.matches.addOrUpdate(instance: match) - } catch { - Logger.error(error) - } - } - Spacer() - } - FooterButtonView("effacer", role: .destructive) { - do { - try match.deleteDependencies() - try tournamentStore.matches.delete(instance: match) - } catch { - Logger.error(error) - } - } - } + GroupStageLoserBracketMatchFooterView(match: match, samePlaceThanAboveOption: displayableMatches.count > 1) } } } - Section { if displayableMatches.count > 1 && isEditingLoserBracketGroupStage == true { Section { @@ -124,7 +103,8 @@ struct LoserBracketFromGroupStageView: View { } private func _addNewMatch() { - let placeCount = tournament.groupStageLoserBracketsInitialPlace() + displayableMatches.count * 2 + let currentGroupStageLoserBracketsInitialPlace = tournament.groupStageLoserBracketsInitialPlace() + let placeCount = displayableMatches.isEmpty ? currentGroupStageLoserBracketsInitialPlace : max(currentGroupStageLoserBracketsInitialPlace, displayableMatches.map({ $0.index }).max()! + 2) let match = Match(round: loserBracket.id, index: placeCount, matchFormat: loserBracket.matchFormat) match.name = "\(placeCount)\(placeCount.ordinalFormattedSuffix()) place" do { @@ -148,3 +128,79 @@ struct LoserBracketFromGroupStageView: View { } } + +struct GroupStageLoserBracketMatchFooterView: View { + @Environment(Tournament.self) var tournament: Tournament + @Bindable var match: Match + let samePlaceThanAboveOption: Bool + @State private var selectPlacePlayed: Bool = false + @State private var index: Int = 0 + + var body: some View { + HStack { + Menu { + if samePlaceThanAboveOption { + Button("Même place qu'au-dessus") { + _updateIndex(match.index-2) + } + } + Button("Choisir la place") { + index = match.index + selectPlacePlayed = true + } + } label: { + Text("Éditer la place jouée") + .underline() + } + + Spacer() + FooterButtonView("Effacer", role: .destructive) { + do { + try match.deleteDependencies() + try match.tournamentStore.matches.delete(instance: match) + } catch { + Logger.error(error) + } + } + } + .alert("Place jouée", isPresented: $selectPlacePlayed) { + TextField("Place jouée", value: $index, format: .number) + .keyboardType(.numberPad) + .multilineTextAlignment(.trailing) + .onSubmit { + _updateIndex(index) + } + Button("Confirmer") { + _updateIndex(index) + } + Button("Annuler", role: .cancel) { + } + } + } + + private func _updateIndex(_ newIndex: Int) { + let newIndexValidated = max(1,abs(newIndex)) + let teamScores = match.teamScores + teamScores.forEach { ts in + if let luckyLoser = ts.luckyLoser { + ts.luckyLoser = (luckyLoser - match.index * 2) % 2 + newIndexValidated * 2 + } + } + + match.index = newIndexValidated + + match.name = "\(newIndexValidated)\(newIndexValidated.ordinalFormattedSuffix()) place" + + + do { + try match.tournamentStore.teamScores.addOrUpdate(contentOfs: teamScores) + } catch { + Logger.error(error) + } + do { + try match.tournamentStore.matches.addOrUpdate(instance: match) + } catch { + Logger.error(error) + } + } +} diff --git a/PadelClub/Views/Round/RoundSettingsView.swift b/PadelClub/Views/Round/RoundSettingsView.swift index e23846f..6efa24b 100644 --- a/PadelClub/Views/Round/RoundSettingsView.swift +++ b/PadelClub/Views/Round/RoundSettingsView.swift @@ -132,7 +132,8 @@ struct RoundSettingsView: View { Logger.error(error) } round.buildLoserBracket() - matches.filter { $0.disabled }.forEach { $0._toggleLoserMatchDisableState(true) + matches.filter { $0.disabled }.forEach { + $0._toggleLoserMatchDisableState(true) } } }