diff --git a/PadelClub.xcodeproj/project.pbxproj b/PadelClub.xcodeproj/project.pbxproj index 2933cfc..5ec2051 100644 --- a/PadelClub.xcodeproj/project.pbxproj +++ b/PadelClub.xcodeproj/project.pbxproj @@ -1935,7 +1935,7 @@ CODE_SIGN_ENTITLEMENTS = PadelClub/PadelClub.entitlements; CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 103; + CURRENT_PROJECT_VERSION = 104; DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; DEFINES_MODULE = YES; DEVELOPMENT_ASSET_PATHS = "\"PadelClub/Preview Content\""; @@ -1985,7 +1985,7 @@ CODE_SIGN_ENTITLEMENTS = PadelClub/PadelClub.entitlements; CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 103; + CURRENT_PROJECT_VERSION = 104; DEFINES_MODULE = YES; DEVELOPMENT_ASSET_PATHS = "\"PadelClub/Preview Content\""; DEVELOPMENT_TEAM = BQ3Y44M3Q6; diff --git a/PadelClub/Utils/FileImportManager.swift b/PadelClub/Utils/FileImportManager.swift index bec703e..534b639 100644 --- a/PadelClub/Utils/FileImportManager.swift +++ b/PadelClub/Utils/FileImportManager.swift @@ -83,7 +83,8 @@ class FileImportManager { case frenchFederation case padelClub case custom - + case customAutoSearch + var localizedLabel: String { switch self { case .padelClub: @@ -92,6 +93,8 @@ class FileImportManager { return "FFT" case .custom: return "Personnalisé" + case .customAutoSearch: + return "Personnalisé (avec recherche)" } } } @@ -161,7 +164,9 @@ class FileImportManager { case .padelClub: return await _getPadelClubTeams(from: fileContent, tournament: tournament) case .custom: - return await _getPadelBusinessLeagueTeams(from: fileContent, tournament: tournament) + return await _getPadelBusinessLeagueTeams(from: fileContent, autoSearch: false, tournament: tournament) + case .customAutoSearch: + return await _getPadelBusinessLeagueTeams(from: fileContent, autoSearch: true, tournament: tournament) } } @@ -400,13 +405,16 @@ class FileImportManager { return results } - private func _getPadelBusinessLeagueTeams(from fileContent: String, tournament: Tournament) async -> [TeamHolder] { + private func _getPadelBusinessLeagueTeams(from fileContent: String, autoSearch: Bool, tournament: Tournament) async -> [TeamHolder] { let lines = fileContent.replacingOccurrences(of: "\"", with: "").components(separatedBy: "\n") guard let firstLine = lines.first else { return [] } var separator = "," if firstLine.contains(";") { separator = ";" } + let fetchRequest = ImportedPlayer.fetchRequest() + let federalContext = PersistenceController.shared.localContainer.viewContext + let results: [TeamHolder] = lines.chunked(into: 2).map { team in var teamName: String? = nil let players = team.map { player in @@ -422,8 +430,20 @@ class FileImportManager { let rank : Int? = data[safe: 6]?.trimmed.toInt() let licenceId : String? = data[safe: 7]?.trimmed let club : String? = data[safe: 8]?.trimmed - let player = PlayerRegistration(firstName: firstName, lastName: lastName, licenceId: licenceId, rank: rank, sex: sex, clubName: club, phoneNumber: phoneNumber, email: email) - return player + let predicate = NSPredicate(format: "firstName like[cd] %@ && lastName like[cd] %@", firstName, lastName) + fetchRequest.predicate = predicate + let found = try? federalContext.fetch(fetchRequest).first + if let found, autoSearch { + let player = PlayerRegistration(importedPlayer: found) + player.setComputedRank(in: tournament) + return player + } else { + let player = PlayerRegistration(firstName: firstName, lastName: lastName, licenceId: licenceId, rank: rank, sex: sex, clubName: club, phoneNumber: phoneNumber, email: email) + if rank == nil, autoSearch { + player.setComputedRank(in: tournament) + } + return player + } } return TeamHolder(players: players, tournamentCategory: .men, tournamentAgeCategory: .senior, previousTeam: nil, name: teamName, tournament: tournament) diff --git a/PadelClub/Views/Tournament/FileImportView.swift b/PadelClub/Views/Tournament/FileImportView.swift index 7529e94..9150441 100644 --- a/PadelClub/Views/Tournament/FileImportView.swift +++ b/PadelClub/Views/Tournament/FileImportView.swift @@ -146,6 +146,13 @@ struct FileImportView: View { FooterButtonView("Voir le format du fichier") { presentFormatHelperView = true } + } else if fileProvider == .customAutoSearch { + HStack { + Text("Padel Club essaiera de chercher les joueurs dans la base fédérale automatiquement") + FooterButtonView("Voir le format du fichier") { + presentFormatHelperView = true + } + } } }