fix: #31 — changing quantity auto-recalculates macros in draft card
All checks were successful
Security Checks / dependency-audit (push) Successful in 12s
Security Checks / secret-scanning (push) Successful in 4s
Security Checks / dockerfile-lint (push) Successful in 4s

Captures base per-unit values (calories/protein/carbs/fat/sugar/fiber
divided by original quantity) when draft first appears. When quantity
changes, recalculateMacros() multiplies base values by new quantity.

Example: AI drafts "1 medium banana" at 105 cal. User changes
quantity to 2.0 → calories becomes 210, protein doubles, etc.

Values round to integers for calories, 1 decimal for macros.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Yusuf Suleman
2026-04-04 22:33:21 -05:00
parent b5d734efe1
commit 6a694fcbcf

View File

@@ -3,6 +3,13 @@ import SwiftUI
struct EditableDraftCard: View { struct EditableDraftCard: View {
@Bindable var vm: AssistantViewModel @Bindable var vm: AssistantViewModel
@State private var isEditing = false @State private var isEditing = false
// Base values per 1.0 quantity set once when draft appears
@State private var baseCalories: Double?
@State private var baseProtein: Double?
@State private var baseCarbs: Double?
@State private var baseFat: Double?
@State private var baseSugar: Double?
@State private var baseFiber: Double?
private var draft: FitnessDraft? { vm.currentDraft } private var draft: FitnessDraft? { vm.currentDraft }
@@ -135,8 +142,34 @@ struct EditableDraftCard: View {
.clipShape(RoundedRectangle(cornerRadius: 14)) .clipShape(RoundedRectangle(cornerRadius: 14))
.shadow(color: .black.opacity(0.06), radius: 6, y: 2) .shadow(color: .black.opacity(0.06), radius: 6, y: 2)
.padding(.horizontal, 12) .padding(.horizontal, 12)
.onAppear {
// Capture base per-unit values on first appearance
if baseCalories == nil, let d = vm.currentDraft {
let q = max(d.quantity, 0.01)
baseCalories = d.calories / q
baseProtein = d.protein / q
baseCarbs = d.carbs / q
baseFat = d.fat / q
baseSugar = d.sugar / q
baseFiber = d.fiber / q
} }
} }
}
}
/// Recalculate macros from base values when quantity changes
private func recalculateMacros() {
guard let bc = baseCalories, let bp = baseProtein,
let bca = baseCarbs, let bf = baseFat,
let bs = baseSugar, let bfi = baseFiber else { return }
let q = vm.currentDraft?.quantity ?? 1.0
vm.currentDraft?.calories = (bc * q).rounded()
vm.currentDraft?.protein = (bp * q * 10).rounded() / 10
vm.currentDraft?.carbs = (bca * q * 10).rounded() / 10
vm.currentDraft?.fat = (bf * q * 10).rounded() / 10
vm.currentDraft?.sugar = (bs * q * 10).rounded() / 10
vm.currentDraft?.fiber = (bfi * q * 10).rounded() / 10
}
// MARK: - Editable macro cell // MARK: - Editable macro cell
@@ -196,7 +229,10 @@ struct EditableDraftCard: View {
private var quantityBinding: Binding<String> { private var quantityBinding: Binding<String> {
Binding( Binding(
get: { String(format: "%.1f", vm.currentDraft?.quantity ?? 1) }, get: { String(format: "%.1f", vm.currentDraft?.quantity ?? 1) },
set: { vm.currentDraft?.quantity = Double($0) ?? 1 } set: {
vm.currentDraft?.quantity = Double($0) ?? 1
recalculateMacros()
}
) )
} }
} }