diff --git a/lib/features/health/providers/health_sync.dart b/lib/features/health/providers/health_sync.dart index b213bd7bf..17fd42688 100644 --- a/lib/features/health/providers/health_sync.dart +++ b/lib/features/health/providers/health_sync.dart @@ -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'); diff --git a/test/features/health/providers/health_sync_test.dart b/test/features/health/providers/health_sync_test.dart index 89cc53701..74e560720 100644 --- a/test/features/health/providers/health_sync_test.dart +++ b/test/features/health/providers/health_sync_test.dart @@ -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 => []); + 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 => []);