From 8a3b459ce4ed5dc163e510ef52911df406d22493 Mon Sep 17 00:00:00 2001 From: Razmig Sarkissian Date: Thu, 11 Jul 2024 14:13:16 +0200 Subject: [PATCH] fix stuff --- PadelClub/Utils/FileImportManager.swift | 10 +-- .../GroupStage/GroupStageSettingsView.swift | 2 +- PadelClub/Views/Round/RoundSettingsView.swift | 2 +- PadelClub/Views/Round/RoundView.swift | 86 +++++-------------- .../Views/Tournament/FileImportView.swift | 14 ++- 5 files changed, 40 insertions(+), 74 deletions(-) diff --git a/PadelClub/Utils/FileImportManager.swift b/PadelClub/Utils/FileImportManager.swift index 9686e04..f3412a3 100644 --- a/PadelClub/Utils/FileImportManager.swift +++ b/PadelClub/Utils/FileImportManager.swift @@ -151,11 +151,11 @@ class FileImportManager { static let FFT_ASSIMILATION_WOMAN_IN_MAN = "A calculer selon la pondération en vigueur" - func createTeams(from fileContent: String, tournament: Tournament, fileProvider: FileProvider = .frenchFederation) async throws -> [TeamHolder] { + func createTeams(from fileContent: String, tournament: Tournament, fileProvider: FileProvider = .frenchFederation, checkingCategoryDisabled: Bool) async throws -> [TeamHolder] { switch fileProvider { case .frenchFederation: - return try await _getFederalTeams(from: fileContent, tournament: tournament) + return try await _getFederalTeams(from: fileContent, tournament: tournament, checkingCategoryDisabled: checkingCategoryDisabled) case .padelClub: return await _getPadelClubTeams(from: fileContent, tournament: tournament) case .custom: @@ -205,7 +205,7 @@ class FileImportManager { } } - private func _getFederalTeams(from fileContent: String, tournament: Tournament) async throws -> [TeamHolder] { + private func _getFederalTeams(from fileContent: String, tournament: Tournament, checkingCategoryDisabled: Bool = false) async throws -> [TeamHolder] { let lines = fileContent.components(separatedBy: "\n") guard let firstLine = lines.first else { return [] } var separator = "," @@ -268,7 +268,7 @@ class FileImportManager { case .mix: return 1 } } - if tournamentCategory == tournament.tournamentCategory { + if tournamentCategory == tournament.tournamentCategory || checkingCategoryDisabled { let playerOne = PlayerRegistration(federalData: Array(resultOne[0...7]), sex: sexPlayerOne, sexUnknown: sexUnknown) playerOne?.setComputedRank(in: tournament) let playerTwo = PlayerRegistration(federalData: Array(resultTwo[0...7]), sex: sexPlayerTwo, sexUnknown: sexUnknown) @@ -302,7 +302,7 @@ class FileImportManager { return .men } } - if tournamentCategory == tournament.tournamentCategory { + if tournamentCategory == tournament.tournamentCategory || checkingCategoryDisabled { let result = Array(data.dropFirst(3).dropLast()) var sexPlayerOne : Int { diff --git a/PadelClub/Views/GroupStage/GroupStageSettingsView.swift b/PadelClub/Views/GroupStage/GroupStageSettingsView.swift index 96382c6..f999e11 100644 --- a/PadelClub/Views/GroupStage/GroupStageSettingsView.swift +++ b/PadelClub/Views/GroupStage/GroupStageSettingsView.swift @@ -26,7 +26,7 @@ struct GroupStageSettingsView: View { let issues = tournament.groupStageTeamPlacementIssue() DisclosureGroup { ForEach(issues.shouldBeInIt, id: \.self) { id in - if let team: TeamRegistration = Store.main.findById(id) { + if let team: TeamRegistration = self.tournamentStore.teamRegistrations.findById(id) { TeamRowView(team: team) } } diff --git a/PadelClub/Views/Round/RoundSettingsView.swift b/PadelClub/Views/Round/RoundSettingsView.swift index d5e79ce..eb13a7b 100644 --- a/PadelClub/Views/Round/RoundSettingsView.swift +++ b/PadelClub/Views/Round/RoundSettingsView.swift @@ -25,7 +25,7 @@ struct RoundSettingsView: View { let issues = tournament.bracketTeamPlacementIssue() DisclosureGroup { ForEach(issues.shouldBeInIt, id: \.self) { id in - if let team: TeamRegistration = Store.main.findById(id) { + if let team: TeamRegistration = self.tournamentStore.teamRegistrations.findById(id) { TeamRowView(team: team) } } diff --git a/PadelClub/Views/Round/RoundView.swift b/PadelClub/Views/Round/RoundView.swift index acb0c25..fba33ba 100644 --- a/PadelClub/Views/Round/RoundView.swift +++ b/PadelClub/Views/Round/RoundView.swift @@ -17,51 +17,40 @@ struct RoundView: View { @Environment(NavigationViewModel.self) private var navigation: NavigationViewModel @State private var selectedSeedGroup: SeedInterval? - @State private var spaceLeft: [Match] = [] - @State private var seedSpaceLeft: [Match] = [] - @State private var availableSeedGroup: SeedInterval? @State private var showPrintScreen: Bool = false var upperRound: UpperRound + init(upperRound: UpperRound) { + self.upperRound = upperRound + let seeds = upperRound.round.seeds() + SlideToDeleteSeedTip.seeds = seeds.count + PrintTip.seeds = seeds.count + BracketEditTip.matchesHidden = upperRound.round.getDisabledMatches().count + } + var tournamentStore: TournamentStore { return self.tournament.tournamentStore } - private func _getAvailableSeedGroup() { -#if DEBUG_TIME //DEBUGING TIME - let start = Date() - defer { - let duration = Duration.milliseconds(Date().timeIntervalSince(start) * 1_000) - print("func _getAvailableSeedGroup of: ", duration.formatted(.units(allowed: [.seconds, .milliseconds]))) + private var spaceLeft: [Match] { + let displayableMatches: [Match] = self.upperRound.round.displayableMatches() + return displayableMatches.filter { match in + match.teamScores.count == 1 } -#endif - - availableSeedGroup = tournament.seedGroupAvailable(atRoundIndex: upperRound.round.index) } - private func _getSpaceLeft() { -#if DEBUG_TIME //DEBUGING TIME - let start = Date() - defer { - let duration = Duration.milliseconds(Date().timeIntervalSince(start) * 1_000) - print("func _getSpaceLeft of: ", duration.formatted(.units(allowed: [.seconds, .milliseconds]))) - } -#endif - - self.spaceLeft.removeAll() - self.seedSpaceLeft.removeAll() + private var seedSpaceLeft: [Match] { let displayableMatches: [Match] = self.upperRound.round.displayableMatches() - displayableMatches.forEach { match in - let count: Int = match.teamScores.count - if count == 0 { - seedSpaceLeft.append(match) - } else if count == 1 { - spaceLeft.append(match) - } + return displayableMatches.filter { match in + match.teamScores.count == 0 } } + private var availableSeedGroup: SeedInterval? { + tournament.seedGroupAvailable(atRoundIndex: upperRound.round.index) + } + var showVisualDrawView: Binding { Binding( get: { selectedSeedGroup != nil }, set: { @@ -133,10 +122,6 @@ struct RoundView: View { Task { tournament.setSeeds(inRoundIndex: upperRound.round.index, inSeedGroup: availableSeedGroup) _save() - if tournament.availableSeeds().isEmpty && tournament.availableQualifiedTeams().isEmpty { - self.isEditingTournamentSeed.wrappedValue = false - } - _prepareRound() } } } footer: { @@ -175,10 +160,6 @@ struct RoundView: View { } } _save() - if tournament.availableSeeds().isEmpty && tournament.availableQualifiedTeams().isEmpty { - self.isEditingTournamentSeed.wrappedValue = false - } - _prepareRound() } } } label: { @@ -206,10 +187,6 @@ struct RoundView: View { team.setSeedPosition(inSpot: seedSpaceLeft[drawResult.drawIndex], slot: nil, opposingSeeding: false) } _save() - if tournament.availableSeeds().isEmpty && tournament.availableQualifiedTeams().isEmpty { - self.isEditingTournamentSeed.wrappedValue = false - } - _prepareRound() } } } label: { @@ -233,10 +210,6 @@ struct RoundView: View { team.setSeedPosition(inSpot: spaceLeft[drawResult.drawIndex], slot: nil, opposingSeeding: true) } _save() - if tournament.availableSeeds().isEmpty && tournament.availableQualifiedTeams().isEmpty { - self.isEditingTournamentSeed.wrappedValue = false - } - _prepareRound() } } } label: { @@ -288,13 +261,6 @@ struct RoundView: View { .navigationDestination(isPresented: $showPrintScreen) { PrintSettingsView(tournament: tournament) } - .onAppear { - _prepareRound() - let seeds = upperRound.round.seeds() - SlideToDeleteSeedTip.seeds = seeds.count - PrintTip.seeds = seeds.count - BracketEditTip.matchesHidden = upperRound.round.getDisabledMatches().count - } .fullScreenCover(isPresented: showVisualDrawView) { if let availableSeedGroup = selectedSeedGroup { let seeds = tournament.seeds(inSeedGroup: availableSeedGroup) @@ -308,11 +274,6 @@ struct RoundView: View { } _save() - _prepareRound() - - if tournament.availableSeeds().isEmpty && tournament.availableQualifiedTeams().isEmpty { - self.isEditingTournamentSeed.wrappedValue = false - } } } } @@ -352,11 +313,10 @@ struct RoundView: View { } catch { Logger.error(error) } - } - - private func _prepareRound() { - _getSpaceLeft() - _getAvailableSeedGroup() + + if tournament.availableSeeds().isEmpty && tournament.availableQualifiedTeams().isEmpty { + self.isEditingTournamentSeed.wrappedValue = false + } } } diff --git a/PadelClub/Views/Tournament/FileImportView.swift b/PadelClub/Views/Tournament/FileImportView.swift index 939a278..dae0ba2 100644 --- a/PadelClub/Views/Tournament/FileImportView.swift +++ b/PadelClub/Views/Tournament/FileImportView.swift @@ -83,6 +83,7 @@ struct FileImportView: View { @State private var validationInProgress: Bool = false @State private var multiImport: Bool = false @State private var presentFormatHelperView: Bool = false + @State private var validatedTournamentIds: Set = Set() var tournamentStore: TournamentStore { return self.tournament.tournamentStore @@ -252,7 +253,7 @@ struct FileImportView: View { } } else if teams.isEmpty && didImport == true { Section { - ContentUnavailableView("Aucune équipe détectée", systemImage: "person.2.slash") + ContentUnavailableView("Aucune équipe détectée", systemImage: "person.2.slash", description: Text("Vérifiez si votre fichier correspond à la source.")) } } else if didImport { let _filteredTeams = filteredTeams @@ -273,6 +274,7 @@ struct FileImportView: View { RowButtonView("Valider") { await _validate(tournament: tournament) } + .disabled(validatedTournamentIds.contains(tournament.id)) } header: { Text("\(tournamentFilteredTeams.count.formatted()) équipe\(tournamentFilteredTeams.count.pluralSuffix)") } footer: { @@ -399,7 +401,9 @@ struct FileImportView: View { Task { if let tournaments = tournament.eventObject()?.tournaments, tournaments.count > 1, multiImport { for tournament in tournaments { - await _validate(tournament: tournament) + if validatedTournamentIds.contains(tournament.id) == false { + await _validate(tournament: tournament) + } } dismiss() @@ -435,6 +439,8 @@ struct FileImportView: View { } tournament.importTeams(filteredTeams) + validatedTournamentIds.insert(tournament.id) + if multiImport == false { dismiss() } @@ -454,7 +460,7 @@ struct FileImportView: View { var categoriesDone: [TournamentCategory] = [] for someTournament in event.tournaments { if categoriesDone.contains(someTournament.tournamentCategory) == false { - let _teams = try await FileImportManager.shared.createTeams(from: fileContent, tournament: someTournament, fileProvider: fileProvider) + let _teams = try await FileImportManager.shared.createTeams(from: fileContent, tournament: someTournament, fileProvider: fileProvider, checkingCategoryDisabled: false) self.teams += _teams categoriesDone.append(someTournament.tournamentCategory) } else { @@ -462,7 +468,7 @@ struct FileImportView: View { } } } else { - self.teams = try await FileImportManager.shared.createTeams(from: fileContent, tournament: tournament, fileProvider: fileProvider) + self.teams = try await FileImportManager.shared.createTeams(from: fileContent, tournament: tournament, fileProvider: fileProvider, checkingCategoryDisabled: true) } await MainActor.run {