payfrit-food-ios/PayfritFood/Views/ProductTab/NutritionSection.swift
John Pinkyfloyd 71e7ec34f6 Initial commit: PayfritFood iOS app
- SwiftUI + async/await architecture
- Barcode scanning with AVFoundation
- Product display with score ring, NOVA badge, nutrition
- Alternatives with sort/filter
- Auth (login/register)
- Favorites & history
- Account management
- Dark theme
- Connected to food.payfrit.com API (Open Food Facts proxy)

Co-Authored-By: Claude <noreply@anthropic.com>
2026-03-16 16:58:21 -07:00

85 lines
3.1 KiB
Swift

import SwiftUI
struct NutritionSection: View {
let product: Product
@State private var isExpanded = false
var body: some View {
VStack(alignment: .leading, spacing: 12) {
Button {
withAnimation {
isExpanded.toggle()
}
} label: {
HStack {
Image(systemName: "chart.bar.fill")
.foregroundColor(.green)
Text("Nutrition Facts")
.font(.headline)
.foregroundColor(.primary)
Spacer()
Text(product.servingSize)
.font(.caption)
.foregroundColor(.secondary)
Image(systemName: isExpanded ? "chevron.up" : "chevron.down")
.foregroundColor(.secondary)
}
}
if isExpanded {
VStack(spacing: 0) {
NutritionRow(label: "Calories", value: "\(Int(product.calories))", unit: "kcal", isHeader: true)
Divider()
NutritionRow(label: "Total Fat", value: formatGrams(product.fat), unit: "g")
NutritionRow(label: " Saturated Fat", value: formatGrams(product.saturatedFat), unit: "g", isIndented: true)
Divider()
NutritionRow(label: "Carbohydrates", value: formatGrams(product.carbs), unit: "g")
NutritionRow(label: " Sugar", value: formatGrams(product.sugar), unit: "g", isIndented: true)
NutritionRow(label: " Fiber", value: formatGrams(product.fiber), unit: "g", isIndented: true)
Divider()
NutritionRow(label: "Protein", value: formatGrams(product.protein), unit: "g")
Divider()
NutritionRow(label: "Sodium", value: formatMg(product.sodium), unit: "mg")
}
.padding()
.background(Color(.systemGray6))
.cornerRadius(12)
}
}
.padding()
.background(Color(.secondarySystemBackground))
.cornerRadius(16)
}
private func formatGrams(_ value: Double) -> String {
if value < 1 {
return String(format: "%.1f", value)
}
return String(format: "%.0f", value)
}
private func formatMg(_ value: Double) -> String {
return String(format: "%.0f", value)
}
}
struct NutritionRow: View {
let label: String
let value: String
let unit: String
var isHeader: Bool = false
var isIndented: Bool = false
var body: some View {
HStack {
Text(label)
.font(isHeader ? .headline : .subheadline)
.foregroundColor(isIndented ? .secondary : .primary)
Spacer()
Text("\(value) \(unit)")
.font(isHeader ? .headline : .subheadline)
.foregroundColor(isHeader ? .primary : .secondary)
}
.padding(.vertical, 4)
}
}