Water percentages

This commit is contained in:
cdricms
2025-09-27 18:36:52 +02:00
parent d9ac32de0a
commit 4cb97b3422
2 changed files with 29 additions and 0 deletions

View File

@@ -7,6 +7,9 @@ public struct Ingredient: Codable, ObjectDebugger {
public var text: String? = nil
public var vegan: String? = nil
public var vegetarian: String? = nil
public var ciqualFoodCode: String? = nil
public var ecobalyseCode: String? = nil
public var percentEstimate: Float? = nil
private enum CodingKeys: String, CodingKey {
case fromPalmOil = "from_palm_oil"
@@ -17,5 +20,12 @@ public struct Ingredient: Codable, ObjectDebugger {
case text
case vegan
case vegetarian
case ciqualFoodCode = "ciqual_food_code"
case ecobalyseCode = "ecobalyse_code"
case percentEstimate = "percent_estimate"
}
public var isWater: Bool {
ciqualFoodCode == "18066"
}
}

View File

@@ -1,3 +1,5 @@
import Units
public class Product: Codable, ObjectDebugger {
// public var images: Images? = Images()
public var ingredients: [Ingredient]? = []
@@ -200,6 +202,23 @@ public class Product: Codable, ObjectDebugger {
public var vitaminsPrevTags: [String]?
public var vitaminsTags: [String]?
public var waterQuantity: Units.UnitValue<Double>? {
guard let ingredient = ingredients?.first(where: { $0.isWater })
else {
return nil
}
guard let quantity = productQuantity,
let estimate = ingredient.percentEstimate,
let rawUnit = productQuantityUnit,
let unit = Units.Unit(rawValue: rawUnit)
else {
return nil
}
return Double(quantity * (estimate / 100))[unit]
}
private enum CodingKeys: String, CodingKey {
// case images
case ingredients