fixed offline recipes duplicate encoding
This commit is contained in:
@@ -11,6 +11,10 @@ import SwiftUI
|
||||
struct Category: Codable {
|
||||
let name: String
|
||||
let recipe_count: Int
|
||||
|
||||
private enum CodingKeys: String, CodingKey {
|
||||
case name, recipe_count
|
||||
}
|
||||
}
|
||||
|
||||
extension Category: Identifiable, Hashable {
|
||||
|
||||
@@ -55,8 +55,7 @@ import UIKit
|
||||
if let categories = categories {
|
||||
print("Successfully loaded categories")
|
||||
self.categories = categories
|
||||
print(categories)
|
||||
await saveLocal(categories, path: "categories.data")
|
||||
await saveLocal(self.categories, path: "categories.data")
|
||||
} else {
|
||||
// If there's no server connection, try loading categories from local storage
|
||||
print("Loading categories from store ...")
|
||||
@@ -83,6 +82,7 @@ import UIKit
|
||||
- Important: This function assumes that the server address, authentication string, and API have been properly configured in the `MainViewModel` instance.
|
||||
*/
|
||||
func getCategory(named name: String, fetchMode: FetchMode) async {
|
||||
print("getCategory(\(name), fetchMode: \(fetchMode))")
|
||||
func getLocal() async -> Bool {
|
||||
if let recipes: [Recipe] = await loadLocal(path: "category_\(categoryString).data") {
|
||||
self.recipes[name] = recipes
|
||||
@@ -225,8 +225,6 @@ import UIKit
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
Asynchronously downloads and saves details, thumbnails, and full images for all recipes.
|
||||
|
||||
@@ -238,17 +236,6 @@ import UIKit
|
||||
```swift
|
||||
await mainViewModel.downloadAllRecipes()
|
||||
*/
|
||||
/*
|
||||
func downloadAllRecipes() async {
|
||||
for category in categories {
|
||||
await getCategory(named: category.name, fetchMode: .onlyServer)
|
||||
guard let recipeList = recipes[category.name] else { continue }
|
||||
for recipe in recipeList {
|
||||
await downloadRecipeDetail(id: recipe.recipe_id, withThumb: userSettings.storeThumb, withImage: userSettings.storeImages)
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
func updateRecipeDetail(id: Int, withThumb: Bool, withImage: Bool) async {
|
||||
if let recipeDetail = await getRecipe(id: id, fetchMode: .onlyServer) {
|
||||
await saveLocal(recipeDetail, path: "recipe\(id).data")
|
||||
@@ -530,8 +517,7 @@ extension MainViewModel {
|
||||
}
|
||||
|
||||
func saveLocal<T: Codable>(_ object: T, path: String) async {
|
||||
guard let data = JSONEncoder.safeEncode(object) else { return }
|
||||
await dataStore.save(data: data, toPath: path)
|
||||
await dataStore.save(data: object, toPath: path)
|
||||
}
|
||||
|
||||
private func imageFromStore(id: Int, size: RecipeImage.RecipeImageSize) async -> UIImage? {
|
||||
|
||||
@@ -95,9 +95,6 @@ struct MainView: View {
|
||||
showLoadingIndicator = true
|
||||
self.serverConnection = await viewModel.checkServerConnection()
|
||||
await viewModel.getCategories()
|
||||
for category in viewModel.categories {
|
||||
await viewModel.getCategory(named: category.name, fetchMode: .preferLocal)
|
||||
}
|
||||
await viewModel.updateAllRecipeDetails()
|
||||
|
||||
// Open detail view for default category
|
||||
@@ -145,6 +142,7 @@ struct MainView: View {
|
||||
Button {
|
||||
Task {
|
||||
showLoadingIndicator = true
|
||||
UserSettings.shared.lastUpdate = Date.distantPast
|
||||
await viewModel.getCategories()
|
||||
for category in viewModel.categories {
|
||||
await viewModel.getCategory(named: category.name, fetchMode: .preferServer)
|
||||
|
||||
Reference in New Issue
Block a user