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