From 63f5b29d5e75b713ef77eefbcdb6df796774a8af Mon Sep 17 00:00:00 2001 From: Razmig Sarkissian Date: Thu, 13 Jun 2024 11:30:17 +0200 Subject: [PATCH] add multi import --- PadelClub/Data/Tournament.swift | 5 +- .../Views/Components/ButtonValidateView.swift | 4 +- .../GenericDestinationPickerView.swift | 6 +- .../Views/Tournament/FileImportView.swift | 135 ++++++++++++------ 4 files changed, 100 insertions(+), 50 deletions(-) diff --git a/PadelClub/Data/Tournament.swift b/PadelClub/Data/Tournament.swift index 0e609df..853a339 100644 --- a/PadelClub/Data/Tournament.swift +++ b/PadelClub/Data/Tournament.swift @@ -463,7 +463,10 @@ class Tournament : ModelObject, Storable { func shareURL(_ pageLink: PageLink = .matches) -> URL? { if pageLink == .clubBroadcast { - if let club = club(), let broadcastCode = club.broadcastCode { + let club = club() + print("club", club) + print("club broadcast code", club?.broadcastCode) + if let club, let broadcastCode = club.broadcastCode { return URLs.main.url.appending(path: "c/\(broadcastCode)") } else { return nil diff --git a/PadelClub/Views/Components/ButtonValidateView.swift b/PadelClub/Views/Components/ButtonValidateView.swift index e273c0d..0543e4d 100644 --- a/PadelClub/Views/Components/ButtonValidateView.swift +++ b/PadelClub/Views/Components/ButtonValidateView.swift @@ -9,11 +9,11 @@ import SwiftUI struct ButtonValidateView: View { var role: ButtonRole? - + var title: String = "Valider" let action: () -> () var body: some View { - Button("Valider", role: role) { + Button(title, role: role) { action() } .clipShape(Capsule()) diff --git a/PadelClub/Views/Components/GenericDestinationPickerView.swift b/PadelClub/Views/Components/GenericDestinationPickerView.swift index 2c7fc9a..27c28ae 100644 --- a/PadelClub/Views/Components/GenericDestinationPickerView.swift +++ b/PadelClub/Views/Components/GenericDestinationPickerView.swift @@ -49,7 +49,7 @@ struct GenericDestinationPickerView: .buttonStyle(.plain) .overlay(alignment: .bottomTrailing) { if destination.displayImageIfValueZero() { - let count = destination.badgeValue() + let count : Int? = destination.badgeValue() if let count, count == 0, let badge = destination.badgeImage() { Image(systemName: badge.systemName()) .foregroundColor(badge.color()) @@ -60,7 +60,7 @@ struct GenericDestinationPickerView: ) .offset(x: 3, y: 3) } else if let count, count > 0 { - Image(systemName: count <= 50 ? "\(count).circle.fill" : "plus.circle.fill") + Image(systemName: count <= 50 ? "\(String(count)).circle.fill" : "plus.circle.fill") .foregroundColor(destination.badgeValueColor() ?? .red) .imageScale(.medium) .background ( @@ -80,7 +80,7 @@ struct GenericDestinationPickerView: ) .offset(x: 3, y: 3) } else if let count = destination.badgeValue(), count > 0 { - Image(systemName: count <= 50 ? "\(count).circle.fill" : "plus.circle.fill") + Image(systemName: count <= 50 ? "\(String(count)).circle.fill" : "plus.circle.fill") .foregroundColor(destination.badgeValueColor() ?? .red) .imageScale(.medium) .background ( diff --git a/PadelClub/Views/Tournament/FileImportView.swift b/PadelClub/Views/Tournament/FileImportView.swift index 2f918ed..99b856c 100644 --- a/PadelClub/Views/Tournament/FileImportView.swift +++ b/PadelClub/Views/Tournament/FileImportView.swift @@ -28,8 +28,9 @@ struct FileImportView: View { @State private var fileProvider: FileImportManager.FileProvider = .frenchFederation @State private var validationInProgress: Bool = false + @State private var multiImport: Bool = false - private var filteredTeams: [FileImportManager.TeamHolder] { + private func filteredTeams(tournament: Tournament) -> [FileImportManager.TeamHolder] { return teams.filter { $0.tournamentCategory == tournament.tournamentCategory }.sorted(by: \.weight) } @@ -68,6 +69,24 @@ struct FileImportView: View { Text(.init(footerString)) } } + + if let tournaments = tournament.eventObject()?.tournaments, tournaments.count > 1, fileProvider == .frenchFederation { + Section { + RowButtonView("Importer toutes les épreuves") { + multiImport = true + if let fileContent { + do { + try await _startImport(fileContent: fileContent) + } catch { + errorMessage = error.localizedDescription + } + } + } + } footer: { + Text("Ce tournoi possède plusieurs épreuves. Vous pouvez importer les équipes du fichier pour toutes les épreuves d'un coup.") + } + .disabled(fileContent == nil || convertingFile) + } } // if filteredTeams.isEmpty == false && tournament.unsortedTeams().isEmpty == false { @@ -128,7 +147,8 @@ struct FileImportView: View { // } // } - if filteredTeams.isEmpty && teams.isEmpty == false { + let filteredTeams = filteredTeams(tournament: tournament) + if filteredTeams.isEmpty && teams.isEmpty == false && multiImport == false { @Bindable var tournament = tournament Section { Text("Aucune équipe \(tournament.tournamentCategory.importingRawValue) détectée mais \(teams.count) équipes sont dans le fichier") @@ -157,20 +177,38 @@ struct FileImportView: View { .tipStyle(tint: nil) } } - Section { - LabeledContent { - Text(_filteredTeams.count.formatted()) - } label: { - Text("Équipe\(_filteredTeams.count.pluralSuffix) \(tournament.tournamentCategory.importingRawValue) détectée\(_filteredTeams.count.pluralSuffix)") + + if multiImport, fileProvider == .frenchFederation, let tournaments = tournament.eventObject()?.tournaments, tournaments.count > 1 { + ForEach(tournaments) { tournament in + let tournamentFilteredTeams = self.filteredTeams(tournament: tournament) + + Section { + RowButtonView("Valider") { + await _validate(tournament: tournament) + } + } header: { + Text("\(tournamentFilteredTeams.count.formatted()) équipe\(tournamentFilteredTeams.count.pluralSuffix)") + } footer: { + Text(tournament.tournamentTitle()) + } } - } footer: { - if previousTeams.isEmpty == false { - Text("La liste ci-dessous n'est qu'une indication d'évolution par rapport au seul poids d'équipe. Cela ne tient pas compte des dates d'inscriptions, WCs et autres éléments.").foregroundStyle(.logoRed) + .headerProminence(.increased) + } else { + Section { + LabeledContent { + Text(_filteredTeams.count.formatted()) + } label: { + Text("Équipe\(_filteredTeams.count.pluralSuffix) \(tournament.tournamentCategory.importingRawValue) détectée\(_filteredTeams.count.pluralSuffix)") + } + } footer: { + if previousTeams.isEmpty == false { + Text("La liste ci-dessous n'est qu'une indication d'évolution par rapport au seul poids d'équipe. Cela ne tient pas compte des dates d'inscriptions, WCs et autres éléments.").foregroundStyle(.logoRed) + } + } + + ForEach(_filteredTeams) { team in + _teamView(team: team, inTeams: _filteredTeams, previousTeams: previousTeams) } - } - - ForEach(_filteredTeams) { team in - _teamView(team: team, inTeams: _filteredTeams, previousTeams: previousTeams) } } } @@ -215,13 +253,13 @@ struct FileImportView: View { .navigationTitle("Importation") .navigationBarTitleDisplayMode(.large) .toolbar { - ToolbarItem(placement: .bottomBar) { - PasteButton(payloadType: String.self) { strings in - guard let string = strings.first else { return } - fileContent = string - fileProvider = .padelClub - } - } +// ToolbarItem(placement: .bottomBar) { +// PasteButton(payloadType: String.self) { strings in +// guard let string = strings.first else { return } +// fileContent = string +// fileProvider = .padelClub +// } +// } ToolbarItem(placement: .cancellationAction) { Button("Annuler", role: .cancel) { dismiss() @@ -232,8 +270,19 @@ struct FileImportView: View { if validationInProgress { ProgressView() } else { - ButtonValidateView { + ButtonValidateView(title: (multiImport ? "Tout Valider" : "Valider")) { validationInProgress = true + Task { + if let tournaments = tournament.eventObject()?.tournaments, tournaments.count > 1, multiImport { + for tournament in tournaments { + await _validate(tournament: tournament) + } + + dismiss() + } else { + await _validate(tournament: tournament) + } + } } .disabled(teams.isEmpty) } @@ -241,30 +290,28 @@ struct FileImportView: View { } .interactiveDismissDisabled(validationInProgress) .disabled(validationInProgress) - .onChange(of: validationInProgress) { - _validate() - } } - private func _validate() { - Task { - let previousTeams = filteredTeams.compactMap({ $0.previousTeam }) - - let unfound = Set(tournament.unsortedTeams()).subtracting(Set(previousTeams)) - unfound.forEach { team in - team.resetPositions() - team.wildCardBracket = false - team.wildCardGroupStage = false - team.walkOut = true - } - - do { - try dataStore.teamRegistrations.addOrUpdate(contentOfs: unfound) - } catch { - Logger.error(error) - } - - tournament.importTeams(filteredTeams) + private func _validate(tournament: Tournament) async { + let filteredTeams = filteredTeams(tournament: tournament) + let previousTeams = filteredTeams.compactMap({ $0.previousTeam }) + + let unfound = Set(tournament.unsortedTeams()).subtracting(Set(previousTeams)) + unfound.forEach { team in + team.resetPositions() + team.wildCardBracket = false + team.wildCardGroupStage = false + team.walkOut = true + } + + do { + try dataStore.teamRegistrations.addOrUpdate(contentOfs: unfound) + } catch { + Logger.error(error) + } + + tournament.importTeams(filteredTeams) + if multiImport == false { dismiss() } }