Download function for all recipes or recipes in a certain category

This commit is contained in:
Vicnet
2023-09-19 09:39:08 +02:00
parent 5fdfed9413
commit 4350cad1e0
6 changed files with 99 additions and 9 deletions

View File

@@ -26,7 +26,7 @@ struct CategoryCardView: View {
Text(category.name)
.font(.headline)
)
.frame(maxHeight: 30)
.frame(maxHeight: 25)
}
)
.clipShape(RoundedRectangle(cornerRadius: 10))

View File

@@ -13,13 +13,14 @@ import SwiftUI
struct RecipeBookView: View {
@State var categoryName: 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)
RecipeCardView(viewModel: viewModel, recipe: recipe, isDownloaded: viewModel.recipeDetailExists(recipeId: recipe.recipe_id))
}
.buttonStyle(.plain)
}
@@ -27,6 +28,22 @@ struct RecipeBookView: View {
}
}
.navigationTitle(categoryName)
.toolbar {
Menu {
Button {
print("Downloading all recipes in category \(categoryName) ...")
downloadRecipes()
} label: {
HStack {
Text("Download recipes")
Image(systemName: "icloud.and.arrow.down")
}
}
} label: {
Image(systemName: "ellipsis.circle")
}
}
.task {
await viewModel.loadRecipeList(categoryName: categoryName)
}
@@ -34,4 +51,18 @@ struct RecipeBookView: View {
await viewModel.loadRecipeList(categoryName: categoryName, needsUpdate: true)
}
}
func downloadRecipes() {
if let recipes = viewModel.recipes[categoryName] {
let dispatchQueue = DispatchQueue(label: "RecipeDownload", qos: .background)
dispatchQueue.async {
for recipe in recipes {
Task {
let _ = await viewModel.loadRecipeDetail(recipeId: recipe.recipe_id)
let _ = await viewModel.loadImage(recipeId: recipe.recipe_id, full: true)
}
}
}
}
}
}

View File

@@ -12,7 +12,7 @@ struct MainView: View {
@StateObject var userSettings: UserSettings
var columns: [GridItem] = [GridItem(.adaptive(minimum: 150), spacing: 0)]
var body: some View {
NavigationStack {
NavigationView {
ScrollView(.vertical, showsIndicators: false) {
LazyVGrid(columns: columns, spacing: 0) {
ForEach(viewModel.categories, id: \.name) { category in
@@ -30,8 +30,24 @@ struct MainView: View {
}
.navigationTitle("CookBook")
.toolbar {
Menu {
Button {
print("Downloading all recipes ...")
Task {
await viewModel.downloadAllRecipes()
}
} label: {
HStack {
Text("Download all recipes")
Image(systemName: "icloud.and.arrow.down")
}
}
} label: {
Image(systemName: "ellipsis.circle")
}
NavigationLink( destination: SettingsView(userSettings: userSettings, viewModel: viewModel)) {
Image(systemName: "gear")
Image(systemName: "gearshape")
}
}
}

View File

@@ -12,6 +12,8 @@ struct RecipeCardView: View {
@State var viewModel: MainViewModel
@State var recipe: Recipe
@State var recipeThumb: UIImage?
@State var isDownloaded: Bool
var body: some View {
HStack {
Image(uiImage: recipeThumb ?? UIImage(named: "CookBook")!)
@@ -23,6 +25,12 @@ struct RecipeCardView: View {
.font(.headline)
Spacer()
VStack {
Image(systemName: isDownloaded ? "checkmark.icloud" : "icloud.and.arrow.down")
.foregroundColor(.secondary)
.padding()
Spacer()
}
}
.background(.ultraThickMaterial)
.clipShape(RoundedRectangle(cornerRadius: 10))