From 16ca5227f64db140da9d5ef5c037caa075fa1909 Mon Sep 17 00:00:00 2001 From: Krzysztof kuhy Rudnicki Date: Sat, 11 Jul 2026 18:55:06 +0200 Subject: [PATCH] app: push immediately when a meal is logged The phone only pushed its log on screen-init, app resume/pause, or manual "Sync now" -- never on the log action itself -- so a meal could sit locally for many minutes before reaching GitHub (and thus the PC gate). Fire the existing best-effort _autoSync right after a meal is logged, from both the quick-log path (_onLogMeal) and the meal-builder return (_onBuildMeal), so an online device propagates a new meal within seconds. _autoSync is already single-flight and swallows offline/transient failures, so this is a fire-and-forget unawaited call with no new machinery. Offline logging still waits for reconnect + a lifecycle event (a WorkManager backstop is a future follow-up). Verified on device (BL9000EEA0000102): logging a meal advanced the phone's remote food_log.json commit within seconds. Co-Authored-By: Claude Opus 4.8 (1M context) Claude-Session: https://claude.ai/code/session_01FtSmzACzJSGyEatgK9exkd --- app/lib/screens/log_meal_screen.dart | 6 ++++ .../screens/log_meal_screen_sync_test.dart | 32 +++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/app/lib/screens/log_meal_screen.dart b/app/lib/screens/log_meal_screen.dart index 5ea43da..ec50825 100644 --- a/app/lib/screens/log_meal_screen.dart +++ b/app/lib/screens/log_meal_screen.dart @@ -193,6 +193,10 @@ class _LogMealScreenState extends State ); final log = await LogStorageService.instance.readLog(); await FoodBankService.instance.rebuildAndPersist(log); + // Push the new meal now instead of waiting for the next lifecycle event, + // so the PC gate can see it in seconds. Fire-and-forget and best-effort: + // _autoSync is single-flight and swallows offline/transient failures. + unawaited(_autoSync()); if (!mounted) return; _descController.clear(); _macros.clear(); @@ -211,6 +215,8 @@ class _LogMealScreenState extends State MaterialPageRoute(builder: (_) => const MealBuilderScreen()), ); await _refreshSlots(); + // A meal built and logged in the builder should push right away too. + unawaited(_autoSync()); } void _onOpenHistory() { diff --git a/app/test/screens/log_meal_screen_sync_test.dart b/app/test/screens/log_meal_screen_sync_test.dart index 7ca9d17..7a6a292 100644 --- a/app/test/screens/log_meal_screen_sync_test.dart +++ b/app/test/screens/log_meal_screen_sync_test.dart @@ -142,4 +142,36 @@ void main() { expect(tester.takeException(), isNull); }); }); + + testWidgets('pushes immediately after a meal is logged', (tester) async { + SharedPreferences.setMockInitialValues({ + 'sync.owner': 'o', + 'sync.repo': 'r', + }); + installFakeSecureStorage(initial: {'sync.token': 't'}); + var puts = 0; + final mock = MockClient((req) async { + if (req.method == 'PUT') puts++; + if (req.method == 'GET' && req.url.pathSegments.length == 3) { + return http.Response('{}', 200); + } + return http.Response('', 404); + }); + + await tester.runAsync(() async { + await tester.pumpWidget( + MaterialApp(home: LogMealScreen(httpClient: mock)), + ); + await settle(tester); + expect(puts, 1); // launch push + + await tester.enterText(find.byType(TextField).at(0), 'push-on-log'); + await tester.pump(); + await tester.tap(find.byTooltip('Log meal')); + await settle(tester); + + // The new meal is pushed right away, not left for a lifecycle event. + expect(puts, 2); + }); + }); }