Food: logged entries follow their food; barcodes for My foods

- Saving one of My foods or a meal works every entry logged from it out again
  for that entry's grams, on any day. Only the entries that change are
  written, so the rest aren't synced again (db._follow, app _follow).
- My foods can have a barcode (8-14 digits, one food per barcode). A scanned
  or typed barcode finds My foods first (leading zeros ignored), then the food
  database and kept products; when nothing matches, "New food with barcode"
  opens the food editor with it filled in.
- App 1.1.3 and 1.1.4 changelog entries.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Bashy 2026-10-26 14:08:32 +02:00
parent fb3c01cdd7
commit ad4b6aa22f
12 changed files with 238 additions and 30 deletions

View file

@ -0,0 +1,50 @@
// Entries logged from one of My foods follow its edits (food_model.dart _follow = the dashboard's db._follow):
// a meal's new recipe or a food's new values / name re-work them for their grams; others aren't touched.
import 'dart:io';
import 'package:dash_app/health/food_model.dart';
import 'package:dash_app/sync/store.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
final store = SyncStore('${Directory.systemTemp.createTempSync('follow').path}/d.db', 'test00000000');
test('editing a food re-works its entries', () {
final (u, err) = saveMyFood(store, name: 'Tvaroh', per100: {'kcal': 200, 'protein': 14, 'fat': 15}, portions: []);
expect(err, isNull);
final f = myFoodByUid(store, u!)!;
saveFromFood(store, u: 'e1', day: '2026-10-26', meal: 'lunch', food: f, amount: 100, unit: 'g');
saveFromFood(store, u: 'e2', day: '2026-10-27', meal: 'lunch', food: f, amount: 150, unit: 'g');
saveTotals(store, u: 'e3', day: '2026-10-27', meal: 'lunch', name: 'Tvaroh', values: {'kcal': 300});
final h3 = store.rows('food.entry').firstWhere((r) => r['_u'] == 'e3')['_h'];
saveMyFood(store, uid: u, name: 'Tvaroh (homemade)', per100: {'kcal': 212, 'protein': 14, 'fat': 16}, portions: []);
expect(store.row('food.entry', 'e1')!['kcal'], 212);
expect(store.row('food.entry', 'e2')!['kcal'], 318);
expect(store.row('food.entry', 'e2')!['fat'], 24);
expect(store.row('food.entry', 'e2')!['name'], 'Tvaroh (homemade)');
expect(store.row('food.entry', 'e3')!['kcal'], 300); // typed numbers, no food: left alone
expect(store.rows('food.entry').firstWhere((r) => r['_u'] == 'e3')['_h'], h3);
});
test('unchanged entries are not re-sent', () {
final (u, _) = saveMyFood(store, name: 'Casein', per100: {'kcal': 390}, portions: []);
saveFromFood(store, u: 'c1', day: '2026-10-26', meal: 'breakfast', food: myFoodByUid(store, u!)!, amount: 30, unit: 'g');
final h = store.rows('food.entry').firstWhere((r) => r['_u'] == 'c1')['_h'];
saveMyFood(store, uid: u, name: 'Casein', per100: {'kcal': 390}, portions: [], note: 'a note');
expect(store.rows('food.entry').firstWhere((r) => r['_u'] == 'c1')['_h'], h);
});
test('a meal’s new recipe re-works its entries', () {
final (oil, _) = saveMyFood(store, name: 'Oil', per100: {'kcal': 900, 'fat': 100}, portions: []);
final (veg, _) = saveMyFood(store, name: 'Veg', per100: {'kcal': 30, 'fat': 0}, portions: []);
Ingredient ing(String uid, double g) => Ingredient.food(myFoodByUid(store, uid)!, g, 'g');
final (soup, err) = saveMeal(store, name: 'Soup', ings: [ing(oil!, 30), ing(veg!, 970)], portions: []);
expect(err, isNull);
saveFromFood(store, u: 's1', day: '2026-10-27', meal: 'lunch', food: myFoodByUid(store, soup!)!, amount: 400, unit: 'g');
expect(store.row('food.entry', 's1')!['kcal'], closeTo(400 * (27000 + 29100) / 100000, 0.01));
saveMeal(store, uid: soup, name: 'Soup', ings: [ing(oil, 10), ing(veg, 970)], portions: []);
expect(store.row('food.entry', 's1')!['kcal'], closeTo(400 * (9000 + 29100) / 98000, 0.01));
});
}