From ccf6b05e0d27cb297984b30d5c0a5beb8d9090d2 Mon Sep 17 00:00:00 2001 From: Raz Date: Thu, 1 May 2025 07:51:05 +0200 Subject: [PATCH] add losing group stage position selection consolante --- PadelClub/Data/Tournament.swift | 8 +++ .../ConsolationTournamentImportView.swift | 56 +++++++++++++++---- 2 files changed, 54 insertions(+), 10 deletions(-) diff --git a/PadelClub/Data/Tournament.swift b/PadelClub/Data/Tournament.swift index a1afae1..1153333 100644 --- a/PadelClub/Data/Tournament.swift +++ b/PadelClub/Data/Tournament.swift @@ -2485,6 +2485,14 @@ defer { } } + + func groupStageLosingPositions() -> [Int] { + guard let maxSize = groupStages().map({ $0.size }).max() else { + return [] + } + let leftInterval = qualifiedPerGroupStage + 1 + return Array(leftInterval...maxSize) + } // MARK: - Payments & Crypto diff --git a/PadelClub/Views/Tournament/ConsolationTournamentImportView.swift b/PadelClub/Views/Tournament/ConsolationTournamentImportView.swift index 835e416..59dcfd2 100644 --- a/PadelClub/Views/Tournament/ConsolationTournamentImportView.swift +++ b/PadelClub/Views/Tournament/ConsolationTournamentImportView.swift @@ -16,6 +16,7 @@ struct ConsolationTournamentImportView: View { @State private var selectedTournament: Tournament? @State private var selectedImportSelector: ImportSelector = .groupStage @State private var selectedImportType: ImportType = .losers + @State private var selectedGroupStagePosition: Set = Set() enum ImportType: Int, Identifiable, CaseIterable { case losers @@ -134,6 +135,36 @@ struct ConsolationTournamentImportView: View { Text("Type") } } + + if selectedImportSelector == .groupStage, selectedImportType == .losers { + NavigationLink { + List(selection: $selectedGroupStagePosition) { + ForEach(selectedTournament.groupStageLosingPositions()) { position in + let text: String = "\(position)" + position.ordinalFormattedSuffix() + Text(text) + .tag(position) + } + } + .environment(\.editMode, Binding.constant(EditMode.active)) + .navigationTitle("Position des poules") + .navigationBarTitleDisplayMode(.inline) + .toolbarBackground(.visible, for: .navigationBar) + } label: { + LabeledContent { + if selectedGroupStagePosition.isEmpty { + Text("Tous les perdants") + } else { + let value: String = selectedGroupStagePosition.map({ position in + let text: String = "\(position)" + position.ordinalFormattedSuffix() + return text + }).joined(separator: ", ") + Text(value) + } + } label: { + Text("Sélection des positions") + } + } + } } } @@ -154,16 +185,15 @@ struct ConsolationTournamentImportView: View { let teams = _teamsToImport(selectedTournament: selectedTournament) - Section { - RowButtonView("Importer les équipes") { - await _importTeams(selectedTournament: selectedTournament, teams: teams) - dismiss() - } - .disabled(teams.isEmpty) - } footer: { - if teams.isEmpty { - Text("Aucune équipe détectée").foregroundStyle(.logoRed) + if teams.isEmpty == false { + Section { + RowButtonView("Importer les \(teams.count) équipes") { + await _importTeams(selectedTournament: selectedTournament, teams: teams) + dismiss() + } } + } else { + Text("Aucune équipe à importer").foregroundStyle(.logoRed) } } } @@ -185,7 +215,13 @@ struct ConsolationTournamentImportView: View { teams = selectedTournament.groupStageTeams().filter({ switch selectedImportType { case .losers: - return $0.qualified == false + if selectedGroupStagePosition.isEmpty { + return $0.qualified == false + } else if let position = $0.groupStagePosition { + return $0.qualified == false && selectedGroupStagePosition.contains(position) + } else { + return $0.qualified == false + } case .winners: return $0.qualified case .all: