@ -59,7 +59,7 @@ struct AddTeamView: View {
if let pasteString {
_pasteString = . init ( wrappedValue : pasteString )
_fetchPlayers = FetchRequest < ImportedPlayer > ( sortDescriptors : [ NSSortDescriptor ( keyPath : \ ImportedPlayer . rank , ascending : true ) ] , predicate : Self . _ pastePredicate( pasteField : pasteString , mostRecentDate : tournament . rankSourceDate , filterOption : tournament . tournamentCategory . playerFilterOption ) )
_fetchPlayers = FetchRequest < ImportedPlayer > ( sortDescriptors : [ NSSortDescriptor ( keyPath : \ ImportedPlayer . rank , ascending : true ) ] , predicate : SearchViewModel . pastePredicate ( pasteField : pasteString , mostRecentDate : tournament . rankSourceDate , filterOption : tournament . tournamentCategory . playerFilterOption ) )
_autoSelect = . init ( wrappedValue : true )
cancelShouldDismiss = true
}
@ -135,7 +135,7 @@ struct AddTeamView: View {
guard let first = strings . first else { return }
Task {
await MainActor . run {
fetchPlayers . nsPredicate = Self . _ pastePredicate( pasteField : first , mostRecentDate : SourceFileManager . shared . mostRecentDateAvailable , filterOption : _filterOption ( ) )
fetchPlayers . nsPredicate = SearchViewModel . pastePredicate ( pasteField : first , mostRecentDate : SourceFileManager . shared . mostRecentDateAvailable , filterOption : _filterOption ( ) )
fetchPlayers . nsSortDescriptors = [ NSSortDescriptor ( keyPath : \ ImportedPlayer . rank , ascending : true ) ]
pasteString = first
autoSelect = true
@ -202,56 +202,6 @@ struct AddTeamView: View {
selectionSearchField ? ? pasteString
}
static private func _pastePredicate ( pasteField : String , mostRecentDate : Date ? , filterOption : PlayerFilterOption ) -> NSPredicate ? {
let allowedCharacterSet = CharacterSet . alphanumerics . union ( . whitespaces )
// R e m o v e a l l c h a r a c t e r s t h a t a r e n o t i n t h e a l l o w e d C h a r a c t e r S e t
let text = pasteField . canonicalVersion . components ( separatedBy : allowedCharacterSet . inverted ) . joined ( ) . trimmed
let textStrings : [ String ] = text . components ( separatedBy : . whitespacesAndNewlines )
let nonEmptyStrings : [ String ] = textStrings . compactMap { $0 . isEmpty ? nil : $0 }
let nameComponents = nonEmptyStrings . filter ( { $0 != " de " && $0 != " la " && $0 != " le " && $0 . count > 1 } )
var andPredicates = [ NSPredicate ] ( )
var orPredicates = [ NSPredicate ] ( )
// s e l f . w o r d s C o u n t = n a m e C o m p o n e n t s . c o u n t
if filterOption = = . male {
andPredicates . append ( NSPredicate ( format : " male == YES " ) )
} else if filterOption = = . female {
andPredicates . append ( NSPredicate ( format : " male == NO " ) )
}
if let mostRecentDate {
// a n d P r e d i c a t e s . a p p e n d ( N S P r e d i c a t e ( f o r m a t : " i m p o r t D a t e = = % @ " , m o s t R e c e n t D a t e a s C V a r A r g ) )
}
if nameComponents . count > 1 {
orPredicates = nameComponents . pairs ( ) . map {
return NSPredicate ( format : " (firstName BEGINSWITH[cd] %@ AND lastName BEGINSWITH[cd] %@) OR (firstName BEGINSWITH[cd] %@ AND lastName BEGINSWITH[cd] %@) " , $0 , $1 , $1 , $0 ) }
} else {
orPredicates = nameComponents . map { NSPredicate ( format : " firstName contains[cd] %@ OR lastName contains[cd] %@ " , $0 , $0 ) }
}
let components = text . split ( separator : " " ) . sorted ( )
let pattern = components . joined ( separator : " .* " )
print ( text , pattern )
let canonicalFullNamePredicate = NSPredicate ( format : " canonicalFullName MATCHES[c] %@ " , pattern )
orPredicates . append ( canonicalFullNamePredicate )
let matches = text . licencesFound ( )
let licensesPredicates = matches . map { NSPredicate ( format : " license contains[cd] %@ " , $0 ) }
orPredicates = orPredicates + licensesPredicates
var predicate = NSCompoundPredicate ( andPredicateWithSubpredicates : andPredicates )
if orPredicates . isEmpty = = false {
predicate = NSCompoundPredicate ( andPredicateWithSubpredicates : [ predicate , NSCompoundPredicate ( orPredicateWithSubpredicates : orPredicates ) ] )
}
return predicate
}
private func _currentSelection ( ) -> Set < PlayerRegistration > {
var currentSelection = Set < PlayerRegistration > ( )
createdPlayerIds . compactMap { id in
@ -325,7 +275,9 @@ struct AddTeamView: View {
createdPlayers . removeAll ( )
createdPlayerIds . removeAll ( )
pasteString = nil
dismiss ( )
if team . players ( ) . count > 1 {
dismiss ( )
}
}
private func _updateTeam ( checkDuplicates : Bool ) {
@ -351,7 +303,9 @@ struct AddTeamView: View {
createdPlayerIds . removeAll ( )
pasteString = nil
self . editedTeam = nil
dismiss ( )
if editedTeam . players ( ) . count > 1 {
dismiss ( )
}
}
private func _buildingTeamView ( ) -> some View {