app: push immediately when a meal is logged
Some checks are pending
pre-commit / pre-commit (push) Waiting to run
Tests / test (3.10) (push) Waiting to run
Tests / test (3.11) (push) Waiting to run
Tests / test (3.12) (push) Waiting to run

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) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FtSmzACzJSGyEatgK9exkd
This commit is contained in:
Krzysztof Rudnicki 2026-07-11 18:55:06 +02:00
parent 8a9c7713b9
commit 16ca5227f6
2 changed files with 38 additions and 0 deletions

View File

@ -193,6 +193,10 @@ class _LogMealScreenState extends State<LogMealScreen>
);
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<LogMealScreen>
MaterialPageRoute(builder: (_) => const MealBuilderScreen()),
);
await _refreshSlots();
// A meal built and logged in the builder should push right away too.
unawaited(_autoSync());
}
void _onOpenHistory() {

View File

@ -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);
});
});
}