|
|
|
|
@ -89,8 +89,31 @@ class SearchViewModel: ObservableObject, Identifiable { |
|
|
|
|
return nil |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func shouldIncludeSearchTextPredicate() -> Bool { |
|
|
|
|
if allowMultipleSelection { |
|
|
|
|
return true |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if allowSingleSelection { |
|
|
|
|
return true |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if tokens.isEmpty == false || hideAssimilation || selectedAgeCategory != .unlisted { |
|
|
|
|
return true |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return dataSet == .national && searchText.isEmpty == false && (tokens.isEmpty == true && hideAssimilation == false && selectedAgeCategory == .unlisted) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func showIndex() -> Bool { |
|
|
|
|
if (dataSet == .national || dataSet == .ligue) { return isFiltering() } |
|
|
|
|
if dataSet == .national { |
|
|
|
|
if searchText.isEmpty == false && (tokens.isEmpty == true && hideAssimilation == false && selectedAgeCategory == .unlisted) { |
|
|
|
|
return false |
|
|
|
|
} else { |
|
|
|
|
return isFiltering() |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
if (dataSet == .ligue) { return isFiltering() } |
|
|
|
|
if filterOption == .all { return isFiltering() } |
|
|
|
|
return true |
|
|
|
|
} |
|
|
|
|
@ -149,66 +172,85 @@ class SearchViewModel: ObservableObject, Identifiable { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func searchTextPredicate() -> NSPredicate? { |
|
|
|
|
var predicates : [NSPredicate] = [] |
|
|
|
|
let allowedCharacterSet = CharacterSet.alphanumerics.union(.whitespaces) |
|
|
|
|
let canonicalVersionWithoutPunctuation = searchText.canonicalVersion.components(separatedBy: allowedCharacterSet.inverted).joined().trimmed |
|
|
|
|
|
|
|
|
|
if canonicalVersionWithoutPunctuation.isEmpty == false { |
|
|
|
|
let wordsPredicates = wordsPredicates() |
|
|
|
|
if let wordsPredicates { |
|
|
|
|
predicates.append(wordsPredicates) |
|
|
|
|
} else { |
|
|
|
|
predicates.append(NSPredicate(format: "license contains[cd] %@", canonicalVersionWithoutPunctuation)) |
|
|
|
|
} |
|
|
|
|
predicates.append(NSPredicate(format: "canonicalFullName contains[cd] %@", canonicalVersionWithoutPunctuation)) |
|
|
|
|
let components = canonicalVersionWithoutPunctuation.split(separator: " ") |
|
|
|
|
let pattern = components.joined(separator: ".*") |
|
|
|
|
let predicate = NSPredicate(format: "canonicalFullName MATCHES[c] %@", pattern) |
|
|
|
|
predicates.append(predicate) |
|
|
|
|
} |
|
|
|
|
if predicates.isEmpty { |
|
|
|
|
return nil |
|
|
|
|
} |
|
|
|
|
return NSCompoundPredicate(orPredicateWithSubpredicates: predicates) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func orPredicate() -> NSPredicate? { |
|
|
|
|
var predicates : [NSPredicate] = [] |
|
|
|
|
let allowedCharacterSet = CharacterSet.alphanumerics.union(.whitespaces) |
|
|
|
|
let canonicalVersionWithoutPunctuation = searchText.canonicalVersion.components(separatedBy: allowedCharacterSet.inverted).joined().trimmed |
|
|
|
|
let canonicalVersionWithPunctuation = searchText.canonicalVersionWithPunctuation.trimmed |
|
|
|
|
switch tokens.first { |
|
|
|
|
case .none: |
|
|
|
|
if canonicalVersionWithoutPunctuation.isEmpty == false { |
|
|
|
|
let wordsPredicates = wordsPredicates() |
|
|
|
|
if let wordsPredicates { |
|
|
|
|
predicates.append(wordsPredicates) |
|
|
|
|
} else { |
|
|
|
|
predicates.append(NSPredicate(format: "license contains[cd] %@", canonicalVersionWithoutPunctuation)) |
|
|
|
|
|
|
|
|
|
if tokens.isEmpty { |
|
|
|
|
if shouldIncludeSearchTextPredicate(), canonicalVersionWithoutPunctuation.isEmpty == false { |
|
|
|
|
if let searchTextPredicate = searchTextPredicate() { |
|
|
|
|
predicates.append(searchTextPredicate) |
|
|
|
|
} |
|
|
|
|
predicates.append(NSPredicate(format: "canonicalFullName contains[cd] %@", canonicalVersionWithoutPunctuation)) |
|
|
|
|
let components = canonicalVersionWithoutPunctuation.split(separator: " ") |
|
|
|
|
let pattern = components.joined(separator: ".*") |
|
|
|
|
let predicate = NSPredicate(format: "canonicalFullName MATCHES[c] %@", pattern) |
|
|
|
|
predicates.append(predicate) |
|
|
|
|
} |
|
|
|
|
case .ligue: |
|
|
|
|
if canonicalVersionWithoutPunctuation.isEmpty { |
|
|
|
|
predicates.append(NSPredicate(format: "ligueName == nil")) |
|
|
|
|
} else { |
|
|
|
|
predicates.append(NSPredicate(format: "ligueName contains[cd] %@", canonicalVersionWithoutPunctuation)) |
|
|
|
|
} |
|
|
|
|
case .club: |
|
|
|
|
if canonicalVersionWithoutPunctuation.isEmpty { |
|
|
|
|
predicates.append(NSPredicate(format: "clubName == nil")) |
|
|
|
|
} else { |
|
|
|
|
predicates.append(NSPredicate(format: "clubName contains[cd] %@", canonicalVersionWithoutPunctuation)) |
|
|
|
|
} |
|
|
|
|
case .rankMoreThan: |
|
|
|
|
if canonicalVersionWithoutPunctuation.isEmpty || Int(canonicalVersionWithoutPunctuation) == 0 { |
|
|
|
|
predicates.append(NSPredicate(format: "rank == 0")) |
|
|
|
|
} else { |
|
|
|
|
predicates.append(NSPredicate(format: "rank >= %@", canonicalVersionWithoutPunctuation)) |
|
|
|
|
} |
|
|
|
|
case .rankLessThan: |
|
|
|
|
if canonicalVersionWithoutPunctuation.isEmpty || Int(canonicalVersionWithoutPunctuation) == 0 { |
|
|
|
|
predicates.append(NSPredicate(format: "rank == 0")) |
|
|
|
|
} else { |
|
|
|
|
predicates.append(NSPredicate(format: "rank <= %@", canonicalVersionWithoutPunctuation)) |
|
|
|
|
} |
|
|
|
|
case .rankBetween: |
|
|
|
|
let values = canonicalVersionWithPunctuation.components(separatedBy: ",") |
|
|
|
|
if canonicalVersionWithPunctuation.isEmpty || values.count != 2 { |
|
|
|
|
predicates.append(NSPredicate(format: "rank == 0")) |
|
|
|
|
} else { |
|
|
|
|
predicates.append(NSPredicate(format: "rank BETWEEN {%@,%@}", values.first!, values.last!)) |
|
|
|
|
} |
|
|
|
|
case .age: |
|
|
|
|
if canonicalVersionWithoutPunctuation.isEmpty || Int(canonicalVersionWithoutPunctuation) == 0 { |
|
|
|
|
predicates.append(NSPredicate(format: "birthYear == 0")) |
|
|
|
|
} else if let birthYear = Int(canonicalVersionWithoutPunctuation) { |
|
|
|
|
predicates.append(NSPredicate(format: "birthYear == %@", birthYear.formattedAsRawString())) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
for token in tokens { |
|
|
|
|
switch token { |
|
|
|
|
case .ligue: |
|
|
|
|
if canonicalVersionWithoutPunctuation.isEmpty { |
|
|
|
|
predicates.append(NSPredicate(format: "ligueName == nil")) |
|
|
|
|
} else { |
|
|
|
|
predicates.append(NSPredicate(format: "ligueName contains[cd] %@", canonicalVersionWithoutPunctuation)) |
|
|
|
|
} |
|
|
|
|
case .club: |
|
|
|
|
if canonicalVersionWithoutPunctuation.isEmpty { |
|
|
|
|
predicates.append(NSPredicate(format: "clubName == nil")) |
|
|
|
|
} else { |
|
|
|
|
predicates.append(NSPredicate(format: "clubName contains[cd] %@", canonicalVersionWithoutPunctuation)) |
|
|
|
|
} |
|
|
|
|
case .rankMoreThan: |
|
|
|
|
if canonicalVersionWithoutPunctuation.isEmpty || Int(canonicalVersionWithoutPunctuation) == 0 { |
|
|
|
|
predicates.append(NSPredicate(format: "rank == 0")) |
|
|
|
|
} else { |
|
|
|
|
predicates.append(NSPredicate(format: "rank >= %@", canonicalVersionWithoutPunctuation)) |
|
|
|
|
} |
|
|
|
|
case .rankLessThan: |
|
|
|
|
if canonicalVersionWithoutPunctuation.isEmpty || Int(canonicalVersionWithoutPunctuation) == 0 { |
|
|
|
|
predicates.append(NSPredicate(format: "rank == 0")) |
|
|
|
|
} else { |
|
|
|
|
predicates.append(NSPredicate(format: "rank <= %@", canonicalVersionWithoutPunctuation)) |
|
|
|
|
} |
|
|
|
|
case .rankBetween: |
|
|
|
|
let values = canonicalVersionWithPunctuation.components(separatedBy: ",") |
|
|
|
|
if canonicalVersionWithPunctuation.isEmpty || values.count != 2 { |
|
|
|
|
predicates.append(NSPredicate(format: "rank == 0")) |
|
|
|
|
} else { |
|
|
|
|
predicates.append(NSPredicate(format: "rank BETWEEN {%@,%@}", values.first!, values.last!)) |
|
|
|
|
} |
|
|
|
|
case .age: |
|
|
|
|
if canonicalVersionWithoutPunctuation.isEmpty || Int(canonicalVersionWithoutPunctuation) == 0 { |
|
|
|
|
predicates.append(NSPredicate(format: "birthYear == 0")) |
|
|
|
|
} else if let birthYear = Int(canonicalVersionWithoutPunctuation) { |
|
|
|
|
predicates.append(NSPredicate(format: "birthYear == %@", birthYear.formattedAsRawString())) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
if predicates.isEmpty { |
|
|
|
|
return nil |
|
|
|
|
} |
|
|
|
|
|