Read the full history only for newly readable health types

This commit is contained in:
Roland Geider
2026-08-13 18:58:46 +02:00
parent 964a4df7a6
commit 17bb00a78c
2 changed files with 30 additions and 7 deletions

View File

@@ -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<bool> _hasNewlyReadableTypes(Set<HealthDataType> 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<Set<String>> _newlyReadableTypes(Set<HealthDataType> 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.

View File

@@ -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([