Keep the health sync guard off the state others reset

This commit is contained in:
Roland Geider
2026-08-29 14:48:41 +02:00
parent ad8d8b0a8f
commit 5a8e43dec3
2 changed files with 62 additions and 19 deletions

View File

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

View File

@@ -16,6 +16,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
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<void>();
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();