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

31 lines
1.3 KiB
Dart

/// Local-time formatting that matches diet_guard's Python ISO format.
library;
/// Returns [now] as an ISO-8601 string with a fixed local UTC offset and
/// second precision, matching Python's
/// `now_local().isoformat(timespec="seconds")`, e.g.
/// `"2026-06-22T15:08:11+02:00"`.
///
/// Dart's own [DateTime.toIso8601String] omits the UTC offset for a local
/// (non-UTC) [DateTime], so this fills that gap to keep the `time` field
/// byte-comparable with entries the PC app writes.
String isoLocalSeconds(DateTime now) {
final offset = now.timeZoneOffset;
final sign = offset.isNegative ? '-' : '+';
final absOffset = offset.abs();
String two(int value) => value.toString().padLeft(2, '0');
String four(int value) => value.toString().padLeft(4, '0');
final offsetHours = two(absOffset.inHours);
final offsetMinutes = two(absOffset.inMinutes.remainder(60));
return '${four(now.year)}-${two(now.month)}-${two(now.day)}'
'T${two(now.hour)}:${two(now.minute)}:${two(now.second)}'
'$sign$offsetHours:$offsetMinutes';
}
/// Returns [now]'s local calendar date as `YYYY-MM-DD`.
///
/// Local, not UTC: mirrors `_state._today()` -- "what I ate today" is a
/// local-calendar concept, so a meal eaten late in the evening must not
/// roll into tomorrow's total.
String localDateKey(DateTime now) => isoLocalSeconds(now).substring(0, 10);