diet-guard/app/lib/models/food_bank_record.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

74 lines
2.3 KiB
Dart

/// One entry in the local food bank (autocomplete index), mirroring
/// diet_guard's `_foodbank.BankRecord`.
library;
/// A previously-logged food's remembered macros and use count.
///
/// Mirrors `_foodbank.py`'s on-disk shape: `{desc, kcal, protein_g,
/// carbs_g, fat_g, grams, count, components?}`. Unlike [FoodEntry], a
/// composite record's `components` here are bare names (the bank is an
/// autocomplete index, not the source of truth for component macros --
/// those live on the log entry itself, see `MealComponent`).
class FoodBankRecord {
/// Creates a [FoodBankRecord] from its stored fields.
const FoodBankRecord({
required this.desc,
required this.kcal,
required this.proteinG,
required this.carbsG,
required this.fatG,
required this.grams,
required this.count,
this.components,
});
/// Builds a [FoodBankRecord] from its JSON map representation.
factory FoodBankRecord.fromJson(Map<String, dynamic> json) =>
FoodBankRecord(
desc: json['desc'] 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,
count: (json['count'] as num?)?.toDouble() ?? 0,
components: (json['components'] as List?)?.cast<String>(),
);
/// The food or meal's display name, as the user typed it.
final String desc;
/// Calories per the stored 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;
/// Number of times this food has been logged (ranks staples first).
final double count;
/// Component names, for a composite meal record only.
final List<String>? components;
/// Returns this record as a JSON-ready map with snake_case keys.
Map<String, Object?> toJson() => {
'desc': desc,
'kcal': kcal,
'protein_g': proteinG,
'carbs_g': carbsG,
'fat_g': fatG,
'grams': grams,
'count': count,
if (components != null) 'components': components,
};
}