Recipes are now searchable

This commit is contained in:
Vicnet
2023-10-03 12:11:32 +02:00
parent ee1c0d9aed
commit 77c07bb0b1
48 changed files with 297 additions and 86 deletions

View File

@@ -12,18 +12,17 @@ import SwiftUI
struct RecipeBookView: View {
@State var categoryName: String
@State var searchText: String = ""
@ObservedObject var viewModel: MainViewModel
var body: some View {
ScrollView(showsIndicators: false) {
LazyVStack {
if let recipes = viewModel.recipes[categoryName] {
ForEach(recipes, id: \.recipe_id) { recipe in
NavigationLink(destination: RecipeDetailView(viewModel: viewModel, recipe: recipe)) {
RecipeCardView(viewModel: viewModel, recipe: recipe)
}
.buttonStyle(.plain)
ForEach(recipesFiltered(), id: \.recipe_id) { recipe in
NavigationLink(destination: RecipeDetailView(viewModel: viewModel, recipe: recipe)) {
RecipeCardView(viewModel: viewModel, recipe: recipe)
}
.buttonStyle(.plain)
}
}
}
@@ -44,6 +43,7 @@ struct RecipeBookView: View {
Image(systemName: "ellipsis.circle")
}
}
.searchable(text: $searchText, prompt: "Search recipes")
.task {
await viewModel.loadRecipeList(categoryName: categoryName)
}
@@ -52,6 +52,15 @@ struct RecipeBookView: View {
}
}
func recipesFiltered() -> [Recipe] {
guard let recipes = viewModel.recipes[categoryName] else { return [] }
guard searchText != "" else { return recipes }
return recipes.filter { recipe in
recipe.name.lowercased().contains(searchText.lowercased())
}
}
func downloadRecipes() {
if let recipes = viewModel.recipes[categoryName] {
let dispatchQueue = DispatchQueue(label: "RecipeDownload", qos: .background)