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:
parent
fb3c01cdd7
commit
ad4b6aa22f
12 changed files with 238 additions and 30 deletions
40
app/test/food_barcode_test.dart
Normal file
40
app/test/food_barcode_test.dart
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
// My foods with the pack's barcode (food_model.dart saveMyFood / myFoodsByBarcode = the dashboard's db.clean_food /
|
||||
// my_foods_by_barcode): kept, found by it (leading zeros or not), checked, one food per barcode, left alone when not given.
|
||||
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('barcode').path}/d.db', 'test00000000');
|
||||
|
||||
test('kept and found by it', () {
|
||||
final (u, err) = saveMyFood(store, name: 'Chicory syrup (4Slim)', per100: {'kcal': 161, 'fiber': 71}, portions: [],
|
||||
barcode: '8595245311155');
|
||||
expect(err, isNull);
|
||||
expect(myFoodsByBarcode(store, '8595245311155').single.myUid, u);
|
||||
expect(myFoodsByBarcode(store, '08595245311155').single.name, 'Chicory syrup (4Slim)');
|
||||
expect(myFoodByUid(store, u!)!.barcode, '8595245311155');
|
||||
expect(myFoodsByBarcode(store, '1234567890123'), isEmpty);
|
||||
});
|
||||
|
||||
test('checked: digits only, 8–14, one food per barcode', () {
|
||||
expect(saveMyFood(store, name: 'A', per100: {'kcal': 1}, portions: [], barcode: '12ab5678').$2, 'A barcode is 8–14 digits.');
|
||||
expect(saveMyFood(store, name: 'B', per100: {'kcal': 1}, portions: [], barcode: '1234567').$2, 'A barcode is 8–14 digits.');
|
||||
expect(saveMyFood(store, name: 'C', per100: {'kcal': 1}, portions: [], barcode: '8595245311155').$2, contains('has that barcode already'));
|
||||
});
|
||||
|
||||
test('an edit without it keeps it; empty clears it; a deleted food is not found', () {
|
||||
final u = myFoodsByBarcode(store, '8595245311155').single.myUid!;
|
||||
saveMyFood(store, uid: u, name: 'Chicory syrup (4Slim)', per100: {'kcal': 160}, portions: []);
|
||||
expect(store.row('food.food', u)!['barcode'], '8595245311155');
|
||||
saveMyFood(store, uid: u, name: 'Chicory syrup (4Slim)', per100: {'kcal': 160}, portions: [], barcode: ' 8595245311155 ');
|
||||
expect(store.row('food.food', u)!['barcode'], '8595245311155');
|
||||
deleteMyFood(store, u);
|
||||
expect(myFoodsByBarcode(store, '8595245311155'), isEmpty);
|
||||
final (v, _) = saveMyFood(store, name: 'D', per100: {'kcal': 1}, portions: [], barcode: '8595245311155');
|
||||
saveMyFood(store, uid: v, name: 'D', per100: {'kcal': 1}, portions: [], barcode: '');
|
||||
expect(store.row('food.food', v!)!['barcode'], isNull);
|
||||
});
|
||||
}
|
||||
50
app/test/food_follow_test.dart
Normal file
50
app/test/food_follow_test.dart
Normal 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));
|
||||
});
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue