Keep the health sync watermark while a metric is skipped

This commit is contained in:
Roland Geider
2026-07-30 20:19:45 +02:00
parent 6d0352a42c
commit 488338d01e
2 changed files with 36 additions and 1 deletions

View File

@@ -138,6 +138,7 @@ class HealthSyncNotifier extends _$HealthSyncNotifier {
final categories = await _measurements.getAllOnce();
final source = _health.sourceName;
var synced = 0;
var skippedMetric = false;
DateTime? latest;
for (final metric in enabledHealthMetrics) {
@@ -148,6 +149,7 @@ class HealthSyncNotifier extends _$HealthSyncNotifier {
final category = await _findOrCreateCategory(metric, categories);
if (category == null) {
skippedMetric = true;
continue;
}
final seen = {
@@ -203,7 +205,10 @@ class HealthSyncNotifier extends _$HealthSyncNotifier {
}
}
if (latest != null) {
// Advancing the watermark past readings a skipped metric could not
// import would push them out of the overlap window for good, so keep
// the old one until every metric got its category.
if (latest != null && !skippedMetric) {
await prefs.setLastHealthSyncTimestamp(latest.toIso8601String());
}
_logger.info('Imported $synced health measurements');

View File

@@ -361,6 +361,36 @@ void main() {
);
});
test('keeps the watermark when weight is skipped but other metrics import', () async {
await PreferenceHelper.instance.setLastHealthSyncTimestamp('2020-01-01T00:00:00.000');
when(measurements.getAllOnce()).thenAnswer((_) async => <MeasurementCategory>[]);
stubReadings([
HealthReading(
type: HealthDataType.WEIGHT,
value: 80.5,
date: DateTime(2026, 1, 1),
externalId: 'w-1',
),
HealthReading(
type: HealthDataType.HEIGHT,
value: 1.8,
date: DateTime(2026, 1, 2),
externalId: 'h-1',
),
]);
final count = await createNotifier().syncOnAppOpen();
// Height imports, but the watermark must not advance past the weight
// readings: they would leave the overlap window before the official
// category ever syncs and be lost for good
expect(count, 1);
expect(
await PreferenceHelper.instance.getLastHealthSyncTimestamp(),
'2020-01-01T00:00:00.000',
);
});
test('reads with an overlap window before the stored watermark', () async {
await PreferenceHelper.instance.setLastHealthSyncTimestamp('2026-06-01T12:00:00.000');
when(measurements.getAllOnce()).thenAnswer((_) async => <MeasurementCategory>[]);