From 71f4377f3770df390069f33a37b465d3c1ac1adf Mon Sep 17 00:00:00 2001 From: Razmig Sarkissian Date: Sun, 26 May 2024 08:52:28 +0200 Subject: [PATCH] fix some stuff --- PadelClub/Data/GroupStage.swift | 19 +++-- PadelClub/Data/Match.swift | 12 ++-- PadelClub/Data/Tournament.swift | 11 ++- PadelClub/Utils/HtmlService.swift | 8 +-- PadelClub/Utils/Tips.swift | 2 +- .../Views/GroupStage/GroupStageView.swift | 72 ++++++++++++++----- .../Screen/InscriptionManagerView.swift | 13 ++-- 7 files changed, 90 insertions(+), 47 deletions(-) diff --git a/PadelClub/Data/GroupStage.swift b/PadelClub/Data/GroupStage.swift index 4e69732..29e48bb 100644 --- a/PadelClub/Data/GroupStage.swift +++ b/PadelClub/Data/GroupStage.swift @@ -170,21 +170,20 @@ class GroupStage: ModelObject, Storable { return _matches().first(where: { matchIndexes.contains($0.index) }) } - func availableToStart() -> [Match] { - let runningMatches = runningMatches() - return playedMatches().filter({ $0.canBeStarted(inMatches: runningMatches) && $0.isRunning() == false }) + func availableToStart(playedMatches: [Match], in runningMatches: [Match]) -> [Match] { + return playedMatches.filter({ $0.canBeStarted(inMatches: runningMatches) && $0.isRunning() == false }) } - func runningMatches() -> [Match] { - playedMatches().filter({ $0.isRunning() }) + func runningMatches(playedMatches: [Match]) -> [Match] { + playedMatches.filter({ $0.isRunning() }) } - func readyMatches() -> [Match] { - playedMatches().filter({ $0.isReady() && $0.isRunning() == false && $0.hasEnded() == false }) + func readyMatches(playedMatches: [Match]) -> [Match] { + playedMatches.filter({ $0.isReady() && $0.isRunning() == false && $0.hasEnded() == false }) } - func finishedMatches() -> [Match] { - playedMatches().filter({ $0.hasEnded() }) + func finishedMatches(playedMatches: [Match]) -> [Match] { + playedMatches.filter({ $0.hasEnded() }) } private func _matchOrder() -> [Int] { @@ -366,7 +365,7 @@ extension GroupStage: Selectable { } func badgeValue() -> Int? { - runningMatches().count + runningMatches(playedMatches: playedMatches()).count } func badgeValueColor() -> Color? { diff --git a/PadelClub/Data/Match.swift b/PadelClub/Data/Match.swift index 00fb5ee..fad2046 100644 --- a/PadelClub/Data/Match.swift +++ b/PadelClub/Data/Match.swift @@ -486,11 +486,11 @@ class Match: ModelObject, Storable { } catch { Logger.error(error) } + followingMatch()?.resetTeamScores() + _loserMatch()?.resetTeamScores() } - followingMatch()?.resetTeamScores() - _loserMatch()?.resetTeamScores() } - + func createTeamScores() -> [TeamScore] { let teamOne = team(.one) let teamTwo = team(.two) @@ -568,15 +568,15 @@ class Match: ModelObject, Storable { } func canBeStarted(inMatches matches: [Match]) -> Bool { - let teams = teams() + let teams = teamScores guard teams.count == 2 else { return false } guard hasEnded() == false else { return false } guard hasStarted() == false else { return false } - return teams.allSatisfy({ $0.canPlay() && isTeamPlaying($0, inMatches: matches) == false }) + return teams.compactMap({ $0.team }).allSatisfy({ $0.canPlay() && isTeamPlaying($0, inMatches: matches) == false }) } func isTeamPlaying(_ team: TeamRegistration, inMatches matches: [Match]) -> Bool { - matches.filter({ $0.teams().contains(team) }).isEmpty == false + matches.filter({ $0.teamScores.compactMap { $0.teamRegistration }.contains(team.id) }).isEmpty == false } var computedStartDateForSorting: Date { diff --git a/PadelClub/Data/Tournament.swift b/PadelClub/Data/Tournament.swift index 9b4c985..1f82dcd 100644 --- a/PadelClub/Data/Tournament.swift +++ b/PadelClub/Data/Tournament.swift @@ -1399,15 +1399,14 @@ class Tournament : ModelObject, Storable { print("Position \(index+1) Poule \(groupStages[jIndex].index)") chunks[index][jIndex].groupStage = groupStages[jIndex].id chunks[index][jIndex].groupStagePosition = index - - do { - try DataStore.shared.teamRegistrations.addOrUpdate(instance: chunks[index][jIndex]) - } catch { - Logger.error(error) - } } } + do { + try DataStore.shared.teamRegistrations.addOrUpdate(contentOfs: unsortedTeams()) + } catch { + Logger.error(error) + } groupStages.forEach { $0.buildMatches() } } diff --git a/PadelClub/Utils/HtmlService.swift b/PadelClub/Utils/HtmlService.swift index 1c294ce..74de02b 100644 --- a/PadelClub/Utils/HtmlService.swift +++ b/PadelClub/Utils/HtmlService.swift @@ -84,7 +84,7 @@ enum HtmlService { if let playerOne = entrant.players()[safe: 0] { template = template.replacingOccurrences(of: "{{playerOne}}", with: playerOne.playerLabel()) if withRank { - template = template.replacingOccurrences(of: "{{weightOne}}", with: "(\(playerOne.formattedRank())") + template = template.replacingOccurrences(of: "{{weightOne}}", with: "(\(playerOne.formattedRank()))") } else { template = template.replacingOccurrences(of: "{{weightOne}}", with: "") } @@ -93,7 +93,7 @@ enum HtmlService { if let playerTwo = entrant.players()[safe: 1] { template = template.replacingOccurrences(of: "{{playerTwo}}", with: playerTwo.playerLabel()) if withRank { - template = template.replacingOccurrences(of: "{{weightTwo}}", with: "(\(playerTwo.formattedRank())") + template = template.replacingOccurrences(of: "{{weightTwo}}", with: "(\(playerTwo.formattedRank()))") } else { template = template.replacingOccurrences(of: "{{weightTwo}}", with: "") } @@ -135,7 +135,7 @@ enum HtmlService { if let playerOne = entrant.players()[safe: 0] { template = template.replacingOccurrences(of: "{{playerOne}}", with: playerOne.playerLabel(.short)) if withRank { - template = template.replacingOccurrences(of: "{{weightOne}}", with: "(\(playerOne.formattedRank())") + template = template.replacingOccurrences(of: "{{weightOne}}", with: "(\(playerOne.formattedRank()))") } else { template = template.replacingOccurrences(of: "{{weightOne}}", with: "") } @@ -144,7 +144,7 @@ enum HtmlService { if let playerTwo = entrant.players()[safe: 1] { template = template.replacingOccurrences(of: "{{playerTwo}}", with: playerTwo.playerLabel(.short)) if withRank { - template = template.replacingOccurrences(of: "{{weightTwo}}", with: "(\(playerTwo.formattedRank())") + template = template.replacingOccurrences(of: "{{weightTwo}}", with: "(\(playerTwo.formattedRank()))") } else { template = template.replacingOccurrences(of: "{{weightTwo}}", with: "") } diff --git a/PadelClub/Utils/Tips.swift b/PadelClub/Utils/Tips.swift index 07e4d32..5f7c555 100644 --- a/PadelClub/Utils/Tips.swift +++ b/PadelClub/Utils/Tips.swift @@ -336,7 +336,7 @@ struct TournamentPublishingTip: Tip { } var message: Text? { - Text("Padel Club vous permet de publier votre tournoi et rendre accessible à tous les résultats des matchs et l'évolution de l'événement. Les informations seront accessible sur le site Padel Club.") + Text("Padel Club vous permet de publier votre tournoi et rendre accessible à tous les résultats des matchs et l'évolution de l'événement. Les informations seront accessibles sur le site Padel Club.") } var image: Image? { diff --git a/PadelClub/Views/GroupStage/GroupStageView.swift b/PadelClub/Views/GroupStage/GroupStageView.swift index 6c79f5c..92c8c4c 100644 --- a/PadelClub/Views/GroupStage/GroupStageView.swift +++ b/PadelClub/Views/GroupStage/GroupStageView.swift @@ -16,9 +16,11 @@ struct GroupStageView: View { @State private var confirmRemoveAll: Bool = false @State private var confirmResetMatch: Bool = false @State private var groupStageName: String = "" - + let playedMatches: [Match] + init(groupStage: GroupStage) { self.groupStage = groupStage + self.playedMatches = groupStage.playedMatches() _groupStageName = State(wrappedValue: groupStage.groupStageTitle()) } @@ -44,21 +46,18 @@ struct GroupStageView: View { } .headerProminence(.increased) - MatchListView(section: "disponible", matches: groupStage.availableToStart()).id(UUID()) - MatchListView(section: "en cours", matches: groupStage.runningMatches()).id(UUID()) - MatchListView(section: "à lancer", matches: groupStage.readyMatches()).id(UUID()) - MatchListView(section: "terminés", matches: groupStage.finishedMatches(), isExpanded: false).id(UUID()) - } - .onChange(of: groupStageName) { - groupStage.name = groupStageName - _save() + let runningMatches = groupStage.runningMatches(playedMatches: playedMatches) + MatchListView(section: "disponible", matches: groupStage.availableToStart(playedMatches: playedMatches, in: runningMatches)) + MatchListView(section: "en cours", matches: runningMatches) + MatchListView(section: "à lancer", matches: groupStage.readyMatches(playedMatches: playedMatches)) + MatchListView(section: "terminés", matches: groupStage.finishedMatches(playedMatches: playedMatches), isExpanded: false) } .toolbar { ToolbarItem(placement: .topBarTrailing) { _groupStageMenuView() } } - .navigationTitle($groupStageName) + .navigationTitle(groupStage.groupStageTitle()) } private enum GroupStageSortingMode { @@ -178,12 +177,10 @@ struct GroupStageView: View { private func _groupStageMenuView() -> some View { Menu { - if groupStage.name != nil { - Button("Retirer le nom") { - groupStage.name = nil - groupStageName = groupStage.groupStageTitle() - _save() - } + NavigationLink { + GroupStageNameEditionView(groupStage: groupStage) + } label: { + Label("Renommer", systemImage: "pencil") } Button("Retirer tout le monde", role: .destructive) { confirmRemoveAll = true @@ -225,3 +222,46 @@ struct GroupStageView: View { } } } + +struct GroupStageNameEditionView: View { + @EnvironmentObject var dataStore: DataStore + let groupStage: GroupStage + @State private var groupStageName: String = "" + + var body: some View { + Form { + Section { + TextField("Nom de la poule", text: $groupStageName) + .keyboardType(.alphabet) + .frame(maxWidth: .infinity) + .onAppear(perform: { + groupStageName = groupStage.name ?? "" + }) + .onSubmit { + groupStageName = groupStageName.trimmed + groupStage.name = groupStageName + _save() + } + } footer: { + HStack { + Spacer() + FooterButtonView("retirer le nom") { + groupStage.name = nil + groupStageName = groupStage.groupStageTitle() + _save() + } + } + } + } + .navigationTitle(groupStage.groupStageTitle()) + .toolbarBackground(.visible, for: .navigationBar) + } + + private func _save() { + do { + try dataStore.groupStages.addOrUpdate(instance: groupStage) + } catch { + Logger.error(error) + } + } +} diff --git a/PadelClub/Views/Tournament/Screen/InscriptionManagerView.swift b/PadelClub/Views/Tournament/Screen/InscriptionManagerView.swift index 0205d0c..4835e18 100644 --- a/PadelClub/Views/Tournament/Screen/InscriptionManagerView.swift +++ b/PadelClub/Views/Tournament/Screen/InscriptionManagerView.swift @@ -608,15 +608,20 @@ struct InscriptionManagerView: View { Section { let unsortedTeams = tournament.unsortedTeams() let walkoutTeams = tournament.walkoutTeams() + let unsortedTeamsWithoutWO = tournament.unsortedTeamsWithoutWO() LabeledContent { - Text(unsortedTeams.count.formatted() + "/" + tournament.teamCount.formatted()).font(.largeTitle) + Text(unsortedTeamsWithoutWO.count.formatted() + "/" + tournament.teamCount.formatted()).font(.largeTitle) } label: { - Text("Paire\(unsortedTeams.count.pluralSuffix) inscrite\(unsortedTeams.count.pluralSuffix)") - Text("dont \(walkoutTeams.count) forfait\(walkoutTeams.count.pluralSuffix)") + Text("Paire\(unsortedTeamsWithoutWO.count.pluralSuffix) inscrite\(unsortedTeamsWithoutWO.count.pluralSuffix)") + } + + LabeledContent { + Text(walkoutTeams.count.formatted()).font(.largeTitle) + } label: { + Text("Forfait\(walkoutTeams.count.pluralSuffix)") } - let unsortedTeamsWithoutWO = tournament.unsortedTeamsWithoutWO() LabeledContent { Text(max(0, unsortedTeamsWithoutWO.count - tournament.teamCount).formatted()).font(.largeTitle) } label: {