WIP - Complete App refactoring

This commit is contained in:
VincentMeilinger
2025-05-26 15:52:24 +02:00
parent 29fd3c668b
commit 5acf3b9c4f
49 changed files with 1996 additions and 543 deletions

View File

@@ -8,8 +8,18 @@
import Foundation
import SwiftUI
struct Category: Codable, Identifiable, Hashable {
var id: String { name }
let name: String
let recipe_count: Int
private enum CodingKeys: String, CodingKey {
case name, recipe_count
}
}
struct CookbookApiRecipeV1: Codable {
struct CookbookApiRecipeV1: CookbookApiRecipe, Codable, Identifiable, Hashable {
var id: String { name + String(recipe_id) }
let name: String
let keywords: String?
let dateCreated: String?
@@ -24,15 +34,22 @@ struct CookbookApiRecipeV1: Codable {
private enum CodingKeys: String, CodingKey {
case name, keywords, dateCreated, dateModified, imageUrl, imagePlaceholderUrl, recipe_id
}
func toRecipeStub() -> RecipeStub {
return RecipeStub(
id: String(recipe_id),
name: name,
keywords: keywords,
dateCreated: dateCreated,
dateModified: dateModified,
thumbnailPath: nil
)
}
}
extension CookbookApiRecipeV1: Identifiable, Hashable {
var id: String { name }
}
struct CookbookApiRecipeDetailV1: Codable {
struct CookbookApiRecipeDetailV1: CookbookApiRecipeDetail {
var name: String
var keywords: String
var dateCreated: String?
@@ -51,7 +68,7 @@ struct CookbookApiRecipeDetailV1: Codable {
var recipeInstructions: [String]
var nutrition: [String:String]
init(name: String, keywords: String, dateCreated: String, dateModified: String, imageUrl: String, id: String, prepTime: String? = nil, cookTime: String? = nil, totalTime: String? = nil, description: String, url: String, recipeYield: Int, recipeCategory: String, tool: [String], recipeIngredient: [String], recipeInstructions: [String], nutrition: [String:String]) {
init(name: String, keywords: String, dateCreated: String?, dateModified: String?, imageUrl: String?, id: String, prepTime: String? = nil, cookTime: String? = nil, totalTime: String? = nil, description: String, url: String?, recipeYield: Int, recipeCategory: String, tool: [String], recipeIngredient: [String], recipeInstructions: [String], nutrition: [String:String]) {
self.name = name
self.keywords = keywords
self.dateCreated = dateCreated
@@ -117,6 +134,47 @@ struct CookbookApiRecipeDetailV1: Codable {
nutrition = try container.decode(Dictionary<String, JSONAny>.self, forKey: .nutrition).mapValues { String(describing: $0.value) }
}
func toRecipe() -> Recipe {
return Recipe(
id: self.id,
name: self.name,
keywords: keywords.components(separatedBy: ","),
dateCreated: self.dateCreated,
dateModified: self.dateModified,
prepTime: self.prepTime ?? "",
cookTime: self.cookTime ?? "",
totalTime: self.totalTime ?? "",
recipeDescription: self.description,
url: self.url,
yield: self.recipeYield,
category: self.recipeCategory,
tools: self.tool,
ingredients: self.recipeIngredient,
instructions: self.recipeInstructions,
nutrition: self.nutrition,
ingredientMultiplier: 1.0
)
}
static func fromRecipe(_ recipe: Recipe) -> any CookbookApiRecipeDetail {
return CookbookApiRecipeDetailV1(
name: recipe.name,
keywords: recipe.keywords.joined(separator: ","),
dateCreated: recipe.dateCreated,
dateModified: recipe.dateModified,
imageUrl: "",
id: recipe.id,
description: recipe.recipeDescription,
url: recipe.url,
recipeYield: recipe.yield,
recipeCategory: recipe.category,
tool: recipe.tools,
recipeIngredient: recipe.ingredients,
recipeInstructions: recipe.instructions,
nutrition: recipe.nutrition
)
}
}
@@ -155,7 +213,7 @@ extension CookbookApiRecipeDetailV1 {
}
}
/*
struct RecipeImage {
enum RecipeImageSize: String {
case THUMB="thumb", FULL="full"
@@ -164,7 +222,7 @@ struct RecipeImage {
var thumb: UIImage?
var full: UIImage?
}
*/
struct RecipeKeyword: Codable {
let name: String
@@ -244,3 +302,4 @@ enum Nutrition: CaseIterable {
}
}
}