From 5a8e43dec3872e84d310183f45204e7cff214eae Mon Sep 17 00:00:00 2001 From: Roland Geider Date: Sat, 29 Aug 2026 14:48:41 +0200 Subject: [PATCH] Keep the health sync guard off the state others reset --- .../health/providers/health_sync.dart | 50 ++++++++++++------- .../health/providers/health_sync_test.dart | 31 ++++++++++++ 2 files changed, 62 insertions(+), 19 deletions(-) diff --git a/lib/features/health/providers/health_sync.dart b/lib/features/health/providers/health_sync.dart index 1b78a4a3a..432246171 100644 --- a/lib/features/health/providers/health_sync.dart +++ b/lib/features/health/providers/health_sync.dart @@ -71,6 +71,13 @@ class HealthSyncNotifier extends _$HealthSyncNotifier { /// Percent the state was last updated for, see [_onProgress]. int? _reportedPercent; + /// Whether a run is in flight, checked before the first await. + /// + /// Not [HealthSyncState.isSyncing]: that one is reset from outside while a + /// run is still reading (see [disableSync]), which let a second run start + /// into the same categories. + bool _isRunning = false; + @override HealthSyncState build() { _health = ref.read(healthRepositoryProvider); @@ -184,29 +191,34 @@ class HealthSyncNotifier extends _$HealthSyncNotifier { /// already running. Triggered on app open, on app resume, and manually from /// the settings. Future sync() async { - if (!await PreferenceHelper.instance.getHealthSyncEnabled()) { + if (_isRunning) { return 0; } - if (state.isSyncing) { - return 0; - } - _reportedPercent = null; - state = state.copyWith(isEnabled: true, isSyncing: true, issue: null, progress: null); + _isRunning = true; + try { + if (!await PreferenceHelper.instance.getHealthSyncEnabled()) { + return 0; + } + _reportedPercent = null; + state = state.copyWith(isEnabled: true, isSyncing: true, issue: null, progress: null); - final result = await _importer.run(); + final result = await _importer.run(); - // The timestamp says when the metrics were last read, so a run that gave - // up before reading any of them leaves the previous one standing - if (result.completed) { - await PreferenceHelper.instance.setHealthSyncLastRun(DateTime.now()); + // The timestamp says when the metrics were last read, so a run that gave + // up before reading any of them leaves the previous one standing + if (result.completed) { + await PreferenceHelper.instance.setHealthSyncLastRun(DateTime.now()); + } + state = state.copyWith( + isSyncing: false, + lastSyncCount: result.imported, + lastSyncTime: result.completed ? DateTime.now() : state.lastSyncTime, + issue: result.issue, + progress: null, + ); + return result.imported; + } finally { + _isRunning = false; } - state = state.copyWith( - isSyncing: false, - lastSyncCount: result.imported, - lastSyncTime: result.completed ? DateTime.now() : state.lastSyncTime, - issue: result.issue, - progress: null, - ); - return result.imported; } } diff --git a/test/features/health/providers/health_sync_test.dart b/test/features/health/providers/health_sync_test.dart index b119a075f..2eb5c06e6 100644 --- a/test/features/health/providers/health_sync_test.dart +++ b/test/features/health/providers/health_sync_test.dart @@ -16,6 +16,8 @@ * along with this program. If not, see . */ +import 'dart:async'; + import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:health_bridge/health.dart'; @@ -230,6 +232,35 @@ void main() { verify(health.readableTypes(any)).called(1); }); + test('a run in flight blocks a second one even after the state was reset', () async { + // The guard cannot live on the state: disableSync() resets it, isSyncing + // included, while the run it belongs to is still reading. A second run + // then imports the same full history into the same categories. + final blocked = Completer(); + when( + health.read( + types: anyNamed('types'), + start: anyNamed('start'), + end: anyNamed('end'), + window: anyNamed('window'), + onBatch: anyNamed('onBatch'), + onWindow: anyNamed('onWindow'), + ), + ).thenAnswer((_) => blocked.future); + final notifier = createNotifier(); + final first = notifier.sync(); + await pumpEventQueue(); + + await notifier.disableSync(); + await PreferenceHelper.instance.setHealthSyncEnabled(true); + + expect(await notifier.sync(), 0); + + blocked.complete(); + await first; + verify(health.readableTypes(any)).called(1); + }); + test('stamps lastSyncTime after a completed run, even an empty one', () async { stubReadings([]); final notifier = createNotifier();