@@ -28,7 +28,7 @@ extension AddCarbs {
2828 @State private var isLoading = false
2929 @State private var errorMessage : String ?
3030 @State private var selectedFoodItem : AIFoodItem ?
31- @State private var portionGrams : Double = 100.0
31+ @State private var portionGrams : Double = 100.00001
3232 @State private var selectedFoodImage : UIImage ?
3333 @State private var saveAlert = false
3434
@@ -239,10 +239,12 @@ extension AddCarbs {
239239 showingFoodSearch = true
240240 } ,
241241 onTakeOver: { food in
242- state. carbs += Decimal ( max ( food. carbs, 0 ) )
243- state. fat += Decimal ( max ( food. fat, 0 ) )
244- state. protein += Decimal ( fmax ( food. protein, 0 ) )
245-
242+ state. carbs += portionGrams != 100.00001 ? Decimal ( max ( food. carbs, 0 ) / ( portionGrams / 100 ) )
243+ . rounded ( to: 0 ) : Decimal ( max ( food. carbs, 0 ) )
244+ state. fat += portionGrams != 100.00001 ? Decimal ( max ( food. fat, 0 ) / ( portionGrams / 100 ) )
245+ . rounded ( to: 0 ) : Decimal ( max ( food. fat, 0 ) )
246+ state. protein += portionGrams != 100.00001 ? Decimal ( max ( food. protein, 0 ) / ( portionGrams / 100 ) )
247+ . rounded ( to: 0 ) : Decimal ( max ( food. protein, 0 ) )
246248 selectedFoodImage = nil
247249 showingFoodSearch = false
248250 if !state. skipSave {
@@ -288,12 +290,13 @@ extension AddCarbs {
288290 }
289291 }
290292
293+ // Temporarily saved in waiter's notepad (the summary).
291294 private func cache( food: AIFoodItem ) {
292295 let cache = Presets ( context: moc)
293296 cache. carbs = Decimal ( food. carbs) as NSDecimalNumber
294297 cache. fat = Decimal ( food. fat) as NSDecimalNumber
295298 cache. protein = Decimal ( food. protein) as NSDecimalNumber
296- cache. dish = food. name
299+ cache. dish = ( portionGrams != 100.00001 ) ? food . name + " \( portionGrams ) g " : food. name
297300
298301 if state. selection? . dish != cache. dish {
299302 state. selection = cache
@@ -305,11 +308,20 @@ extension AddCarbs {
305308
306309 private func addToPresetsIfNew( food: AIFoodItem ) {
307310 let preset = Presets ( context: moc)
308- preset. carbs = Decimal ( max ( food. carbs * ( portionGrams / 100.0 ) , 0 ) ) . rounded ( to: 1 ) as NSDecimalNumber
309- preset. fat = Decimal ( max ( food. fat * ( portionGrams / 100.0 ) , 0 ) ) . rounded ( to: 1 ) as NSDecimalNumber
310- preset. protein = Decimal ( fmax ( food. protein * ( portionGrams / 100.0 ) , 0 ) ) . rounded ( to: 1 ) as NSDecimalNumber
311-
312- if portionGrams != 100 {
311+ preset
312+ . carbs = ( portionGrams != 100.0 || portionGrams != 100.00001 ) ?
313+ ( Decimal ( max ( food. carbs * ( portionGrams / 100 ) , 0 ) ) . rounded ( to: 1 ) as NSDecimalNumber ) :
314+ Decimal ( max ( food. carbs, 0 ) ) as NSDecimalNumber
315+ preset
316+ . fat = ( portionGrams != 100.0 || portionGrams != 100.00001 ) ?
317+ ( Decimal ( max ( food. fat * ( portionGrams / 100 ) , 0 ) ) . rounded ( to: 1 ) as NSDecimalNumber ) :
318+ Decimal ( max ( food. fat, 0 ) ) as NSDecimalNumber
319+ preset
320+ . protein = ( portionGrams != 100.0 || portionGrams != 100.00001 ) ?
321+ ( Decimal ( max ( food. protein * ( portionGrams / 100 ) , 0 ) ) . rounded ( to: 1 ) as NSDecimalNumber ) :
322+ Decimal ( max ( food. protein, 0 ) ) as NSDecimalNumber
323+
324+ if portionGrams != 100.00001 {
313325 preset. dish = food. name + " \( portionGrams) g "
314326 } else {
315327 preset. dish = food. name
@@ -346,7 +358,7 @@ extension AddCarbs {
346358 selectedFoodItem = aiFoodItem
347359
348360 // Gramm zurücksetzen (100g für normale Produkte)
349- portionGrams = 100.0
361+ portionGrams = 100.00001
350362
351363 showingFoodSearch = false
352364 }
0 commit comments