Simpler api endpoints are now integrated into the MainViewModel

This commit is contained in:
Vicnet
2023-11-16 17:53:30 +01:00
parent 3563c23e29
commit 23e1a665df
13 changed files with 284 additions and 74 deletions

View File

@@ -61,10 +61,10 @@ struct CategoryDetailView: View {
}
.searchable(text: $searchText, prompt: "Search recipes")
.task {
await viewModel.loadRecipeList(categoryName: categoryName)
await viewModel.getCategory(named: categoryName)//.loadRecipeList(categoryName: categoryName)
}
.refreshable {
await viewModel.loadRecipeList(categoryName: categoryName, needsUpdate: true)
await viewModel.getCategory(named: categoryName)//.loadRecipeList(categoryName: categoryName, needsUpdate: true)
}
}
@@ -79,13 +79,20 @@ struct CategoryDetailView: View {
func downloadRecipes() {
if let recipes = viewModel.recipes[categoryName] {
let dispatchQueue = DispatchQueue(label: "RecipeDownload", qos: .background)
dispatchQueue.async {
Task {
for recipe in recipes {
Task {
let _ = await viewModel.loadRecipeDetail(recipeId: recipe.recipe_id)
let _ = await viewModel.loadImage(recipeId: recipe.recipe_id, thumb: false)
}
let recipeDetail = await viewModel.getRecipe(id: recipe.recipe_id)
await viewModel.saveLocal(recipeDetail, path: "recipe\(recipe.recipe_id).data")
let thumbnail = await viewModel.getImage(id: recipe.recipe_id, size: .THUMB, needsUpdate: true)
guard let thumbnail = thumbnail else { continue }
guard let thumbnailData = thumbnail.pngData() else { continue }
await viewModel.saveLocal(thumbnailData.base64EncodedString(), path: "image\(recipe.recipe_id)_thumb")
let image = await viewModel.getImage(id: recipe.recipe_id, size: .FULL, needsUpdate: true)
guard let image = image else { continue }
guard let imageData = image.pngData() else { continue }
await viewModel.saveLocal(imageData.base64EncodedString(), path: "image\(recipe.recipe_id)_full")
}
}
}

View File

@@ -9,8 +9,8 @@ import SwiftUI
struct MainView: View {
@ObservedObject var viewModel: MainViewModel
@ObservedObject var userSettings: UserSettings
@StateObject var viewModel = MainViewModel()
@State private var selectedCategory: Category? = nil
@State private var showEditView: Bool = false
@@ -90,7 +90,7 @@ struct MainView: View {
}
.task {
self.serverConnection = await viewModel.checkServerConnection()
await viewModel.loadCategoryList()
await viewModel.loadCategories()//viewModel.loadCategoryList()
// Open detail view for default category
if userSettings.defaultCategory != "" {
if let cat = viewModel.categories.first(where: { c in
@@ -105,7 +105,7 @@ struct MainView: View {
}
.refreshable {
self.serverConnection = await viewModel.checkServerConnection()
await viewModel.loadCategoryList(needsUpdate: true)
await viewModel.loadCategories()//loadCategoryList(needsUpdate: true)
}
}
@@ -208,7 +208,7 @@ struct RecipeSearchView: View {
.navigationTitle("Search recipe")
}
.task {
allRecipes = await viewModel.getAllRecipes()
allRecipes = await viewModel.getRecipes()//.getAllRecipes()
}
}

View File

@@ -51,11 +51,11 @@ struct RecipeCardView: View {
.clipShape(RoundedRectangle(cornerRadius: 17))
.padding(.horizontal)
.task {
recipeThumb = await viewModel.loadImage(recipeId: recipe.recipe_id, thumb: true)
recipeThumb = await viewModel.getImage(id: recipe.recipe_id, size: .THUMB, needsUpdate: false)//loadImage(recipeId: recipe.recipe_id, thumb: true)
self.isDownloaded = viewModel.recipeDetailExists(recipeId: recipe.recipe_id)
}
.refreshable {
recipeThumb = await viewModel.loadImage(recipeId: recipe.recipe_id, thumb: true, needsUpdate: true)
recipeThumb = await viewModel.getImage(id: recipe.recipe_id, size: .THUMB, needsUpdate: true)//.loadImage(recipeId: recipe.recipe_id, thumb: true, needsUpdate: true)
}
}
}

View File

@@ -106,13 +106,13 @@ struct RecipeDetailView: View {
}
}
.task {
recipeDetail = await viewModel.loadRecipeDetail(recipeId: recipe.recipe_id)
recipeImage = await viewModel.loadImage(recipeId: recipe.recipe_id, thumb: false)
recipeDetail = await viewModel.getRecipe(id: recipe.recipe_id)//loadRecipeDetail(recipeId: recipe.recipe_id)
recipeImage = await viewModel.getImage(id: recipe.recipe_id, size: .FULL, needsUpdate: false)//.loadImage(recipeId: recipe.recipe_id, thumb: false)
self.isDownloaded = viewModel.recipeDetailExists(recipeId: recipe.recipe_id)
}
.refreshable {
recipeDetail = await viewModel.loadRecipeDetail(recipeId: recipe.recipe_id, needsUpdate: true)
recipeImage = await viewModel.loadImage(recipeId: recipe.recipe_id, thumb: false, needsUpdate: true)
recipeDetail = await viewModel.getRecipe(id: recipe.recipe_id)//.loadRecipeDetail(recipeId: recipe.recipe_id, needsUpdate: true)
recipeImage = await viewModel.getImage(id: recipe.recipe_id, size: .FULL, needsUpdate: true)//.loadImage(recipeId: recipe.recipe_id, thumb: false, needsUpdate: true)
}
}
}

View File

@@ -45,10 +45,12 @@ struct RecipeEditView: View {
}
Spacer()
Button() {
if viewModel.uploadNew {
viewModel.uploadNewRecipe()
} else {
viewModel.uploadEditedRecipe()
Task {
if viewModel.uploadNew {
await viewModel.uploadNewRecipe()
} else {
await viewModel.uploadEditedRecipe()
}
}
} label: {
Text("Upload")
@@ -150,13 +152,17 @@ struct RecipeEditView: View {
ForEach(viewModel.alertType.alertButtons) { buttonType in
if buttonType == .OK {
Button(AlertButton.OK.rawValue, role: .cancel) {
viewModel.alertAction()
Task {
await viewModel.alertAction()
}
}
} else if buttonType == .CANCEL {
Button(AlertButton.CANCEL.rawValue, role: .cancel) { }
} else if buttonType == .DELETE {
Button(AlertButton.DELETE.rawValue, role: .destructive) {
viewModel.alertAction()
Task {
await viewModel.alertAction()
}
}
}
}