diet-guard/app/lib/models/meal_component.dart
Krzysztof kuhy Rudnicki ee5a7660cb Add Flutter companion app skeleton with local meal logging
Milestone 1 of the diet-app-as-wise-balloon plan: a phone-native way to
log meals away from the PC, sharing the exact on-disk JSON shape
diet_guard already uses (same field names, no translation layer).

- lib/models/: 1:1 Dart mirrors of the Python dataclasses (Nutrition,
  FoodEntry, MealItem, FoodBankRecord, Slot), including the per-100g/
  amount-eaten portion scaling that matches _resolve.resolve_nutrition's
  semantics exactly.
- lib/services/log_storage_service.dart: plain-JSON persistence to
  food_log.json's exact shape (no sqflite -- the canonical format
  already is this JSON).
- lib/services/foodbank_service.dart: ports _foodbank.py's upsert/fuzzy
  search logic for autocomplete.
- lib/screens/: log_meal_screen.dart (single-item logging) and
  meal_builder_screen.dart (composite multi-item meals, logging full
  per-component macros via the new components field).

Verified end-to-end on a physical device (BL9000): built, installed,
logged a real meal through the UI. 77 Flutter tests passing, `flutter
analyze` clean against very_good_analysis.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FU3f5KQ1GHXsbbSecfVEyF
2026-06-22 18:22:42 +02:00

61 lines
1.8 KiB
Dart

/// A composite meal's per-component record, carried on a log entry.
library;
/// One component's name and macros, as stored on a composite log entry's
/// `components` list.
///
/// Mirrors the dict shape `_meal.item_to_component` builds on the Python
/// side: `{name, kcal, protein_g, carbs_g, fat_g, grams}`. Carrying full
/// macros (not just the name) lets a food bank rebuilt purely by replaying
/// the log recover each component's standalone nutrition, not just the
/// composite's summed total.
class MealComponent {
/// Creates a [MealComponent] from its name and macro fields.
const MealComponent({
required this.name,
required this.kcal,
required this.proteinG,
required this.carbsG,
required this.fatG,
required this.grams,
});
/// Builds a [MealComponent] from its JSON map representation.
factory MealComponent.fromJson(Map<String, dynamic> json) => MealComponent(
name: json['name'] as String? ?? '',
kcal: (json['kcal'] as num?)?.toDouble() ?? 0,
proteinG: (json['protein_g'] as num?)?.toDouble() ?? 0,
carbsG: (json['carbs_g'] as num?)?.toDouble() ?? 0,
fatG: (json['fat_g'] as num?)?.toDouble() ?? 0,
grams: (json['grams'] as num?)?.toDouble() ?? 0,
);
/// The component's food name (e.g. `"chicken"`).
final String name;
/// Calories for this component's portion.
final double kcal;
/// Protein in grams.
final double proteinG;
/// Carbohydrate in grams.
final double carbsG;
/// Fat in grams.
final double fatG;
/// Portion weight in grams.
final double grams;
/// Returns this component as a JSON-ready map with snake_case keys.
Map<String, Object?> toJson() => {
'name': name,
'kcal': kcal,
'protein_g': proteinG,
'carbs_g': carbsG,
'fat_g': fatG,
'grams': grams,
};
}