Expands fields searched in the monster search.

This commit is contained in:
2021-03-24 16:00:26 -07:00
parent 3fa4ae0bdb
commit 055a8b28cc
2 changed files with 44 additions and 2 deletions

View File

@@ -37,4 +37,12 @@ class StringHelper {
return joined return joined
} }
} }
static func safeContainsCaseInsensitive(_ str: String?, _ match: String) -> Bool {
if (str == nil) {
return false
}
return str!.containsCaseInsensitive(match)
}
} }

View File

@@ -23,8 +23,42 @@ struct Search: View {
List { List {
SearchBar(text: $searchText) SearchBar(text: $searchText)
.padding(.top, -30) .padding(.top, -30)
ForEach(allMonsters.filter({searchText.isEmpty ? true : $0.name?.containsCaseInsensitive(searchText) ?? false })) { monster in ForEach(
allMonsters.filter(
{
// TODO: consider splitting search text into words and if each word appears in any of these fields return true e.g, "large demon" would match large in size and demon in type.
// TODO: add tags and search by tags
// TODO: add a display of what fields matched on each item in the results
// TODO: make the criteria configurable from this screen
// TODO: find a way to add challenge rating as a search criteria
if (searchText.isEmpty) {
return true
}
if (StringHelper.safeContainsCaseInsensitive($0.name, searchText)) {
return true
}
if (StringHelper.safeContainsCaseInsensitive($0.size, searchText)) {
return true
}
if (StringHelper.safeContainsCaseInsensitive($0.type, searchText)) {
return true
}
if (StringHelper.safeContainsCaseInsensitive($0.subtype, searchText)) {
return true
}
if (StringHelper.safeContainsCaseInsensitive($0.alignment, searchText)) {
return true
}
return false
})) { monster in
NavigationLink(destination: MonsterDetail(monster: monster)) { NavigationLink(destination: MonsterDetail(monster: monster)) {
Text(monster.name ?? "") Text(monster.name ?? "")
} }