From 17bb00a78ca641bfd782786b38d88acc9f9cfb75 Mon Sep 17 00:00:00 2001 From: Roland Geider Date: Thu, 13 Aug 2026 18:58:46 +0200 Subject: [PATCH] Read the full history only for newly readable health types --- .../health/providers/health_importer.dart | 17 +++++++++------- .../providers/health_importer_test.dart | 20 +++++++++++++++++++ 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/lib/features/health/providers/health_importer.dart b/lib/features/health/providers/health_importer.dart index c912953e7..a3ceec8a9 100644 --- a/lib/features/health/providers/health_importer.dart +++ b/lib/features/health/providers/health_importer.dart @@ -170,7 +170,7 @@ class HealthImporter { metrics, categories, ).map((m) => m.metricType.name).toSet(); - final readsEverything = await _hasNewlyReadableTypes(readable); + final newlyReadable = await _newlyReadableTypes(readable); final endTime = DateTime.now(); final source = _health.sourceName; @@ -185,7 +185,7 @@ class HealthImporter { // reading from it would import what happened since and leave // everything before it missing, silently final startTime = - readsEverything || + metric.dataTypes.any((t) => newlyReadable.contains(t.name)) || watermarks[name] == null || (withoutCategory.contains(name) && !knownEmpty.contains(name)) ? _fullHistoryStart @@ -638,26 +638,29 @@ class HealthImporter { return false; } - /// Whether the platform now lets us read a type it did not last time. + /// The type names the platform now lets us read but did not last time. /// /// Such a type has no history in wger for the same reason a deleted category /// has none: nothing was ever imported for it, so the watermark says nothing /// about it. That happens when the user grants access they had declined, and /// after an app update that added the type. - Future _hasNewlyReadableTypes(Set readable) async { + /// + /// The names rather than a flag: only the metrics built on them have that + /// hole, and the full history of every other one would be read for nothing. + Future> _newlyReadableTypes(Set readable) async { final previous = await _prefs.getHealthSyncReadableTypes(); if (previous == null) { - return false; + return {}; } final newlyReadable = readable.map((t) => t.name).toSet().difference(previous.toSet()); if (newlyReadable.isNotEmpty) { _logger.info( 'Access to ${newlyReadable.join(', ')} is new, ' - 'reading the full history instead of from the watermark', + 'reading their full history instead of from the watermark', ); } - return newlyReadable.isNotEmpty; + return newlyReadable; } /// The enabled metrics that have no category to import into. diff --git a/test/features/health/providers/health_importer_test.dart b/test/features/health/providers/health_importer_test.dart index 6be6cc768..4e4712425 100644 --- a/test/features/health/providers/health_importer_test.dart +++ b/test/features/health/providers/health_importer_test.dart @@ -292,6 +292,26 @@ void main() { expect(capturedReadStart(), DateTime(2020)); }); + test('only the metrics of a newly readable type read their full history', () async { + // One granted permission used to send every metric back to 2020, which + // is years of platform records read to import nothing + await seedWatermarks('2026-06-01T12:00:00.000'); + await PreferenceHelper.instance.setHealthSyncReadableTypes([ + for (final metric in enabledHealthMetrics) + for (final type in metric.dataTypes) + if (type != HealthDataType.BODY_FAT_PERCENTAGE) type.name, + ]); + when(measurements.getCategoriesOnce()).thenAnswer((_) async => categoriesForEveryMetric()); + stubReadings([]); + + await runImport(); + + // Read once: every verify() consumes the calls it matched + final starts = capturedReadStarts(); + expect(starts[HealthDataType.BODY_FAT_PERCENTAGE], DateTime(2020)); + expect(starts[HealthDataType.WEIGHT], DateTime(2026, 5, 2, 12)); + }); + test('stays on the watermark while the readable types are unchanged', () async { await seedWatermarks('2026-06-01T12:00:00.000'); await PreferenceHelper.instance.setHealthSyncReadableTypes([