diff --git a/lib/features/health/models/health_metric.dart b/lib/features/health/models/health_metric.dart index 1f25a1d66..802e95eb0 100644 --- a/lib/features/health/models/health_metric.dart +++ b/lib/features/health/models/health_metric.dart @@ -255,9 +255,9 @@ const List healthMetrics = [ ), // The three cumulative types below are counters, not measurements: a single // record covers a few minutes and means nothing on its own, so only the - // day's total is stored. Both platforms can aggregate them themselves, which - // would replace the raw read with one row per day; until the importer has - // that path, they are read raw in small windows and summed here. + // day's total is stored. They are read raw in small windows and summed; a + // day whose records overlap takes Health Connect's deduplicated total + // instead, see HealthEntryWriter. HealthMetric( metricType: MetricType.steps, dataType: HealthDataType.STEPS, diff --git a/lib/features/health/providers/health_entry_writer.dart b/lib/features/health/providers/health_entry_writer.dart index 6921ae8e1..bac7f6d65 100644 --- a/lib/features/health/providers/health_entry_writer.dart +++ b/lib/features/health/providers/health_entry_writer.dart @@ -39,7 +39,7 @@ final _uuidPattern = RegExp(r'^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[ /// counters a run reports at the end. Created per run, since both are /// about that run. class HealthEntryWriter { - HealthEntryWriter(this._measurements, this._source, this._health); + HealthEntryWriter(this._measurements, this._health) : _source = _health.sourceName; final MeasurementRepository _measurements; @@ -190,7 +190,9 @@ class HealthEntryWriter { _health.hasPlatformTotals && hasOverlap(samples)) { final total = await _health.platformTotal(type, day, dayEnd(day, metric)); - if (total != null) { + // Health Connect reports a day without data from its priority list as + // 0, which would wipe a day that has records + if (total != null && total > 0) { aggregate = metric.toCategoryValue(total); platformTotal = true; } else { diff --git a/lib/features/health/providers/health_importer.dart b/lib/features/health/providers/health_importer.dart index b682ed88d..e852076e2 100644 --- a/lib/features/health/providers/health_importer.dart +++ b/lib/features/health/providers/health_importer.dart @@ -159,7 +159,7 @@ class HealthImporter { return (imported: 0, issue: HealthSyncIssue.failed, completed: false); } - _writer = HealthEntryWriter(_measurements, _health.sourceName, _health); + _writer = HealthEntryWriter(_measurements, _health); final run = await _prepare(metrics, readable, userId); _reportProgress(run); for (final metric in metrics) { diff --git a/lib/features/health/providers/health_repository.dart b/lib/features/health/providers/health_repository.dart index 3a9fb6282..ac583f498 100644 --- a/lib/features/health/providers/health_repository.dart +++ b/lib/features/health/providers/health_repository.dart @@ -202,7 +202,8 @@ class HealthRepository { /// is in the platform's raw unit (count, meters, kcal), like a raw read. /// /// Null on iOS, on failure, or when the platform returned nothing, so the - /// caller can fall back to the raw sum. + /// caller can fall back to the raw sum. A range without data from the + /// user's priority list comes back as 0, not null. Future platformTotal(HealthDataType type, DateTime start, DateTime end) async { if (!hasPlatformTotals || !end.isAfter(start)) { return null; diff --git a/test/features/health/providers/health_importer_test.dart b/test/features/health/providers/health_importer_test.dart index 1b21b9b5e..5e5cbffa2 100644 --- a/test/features/health/providers/health_importer_test.dart +++ b/test/features/health/providers/health_importer_test.dart @@ -978,6 +978,18 @@ void main() { verify(health.platformTotal(HealthDataType.STEPS, any, any)).called(1); }); + test('falls back to the raw sum when the platform total is 0', () async { + when(health.hasPlatformTotals).thenReturn(true); + when(health.platformTotal(HealthDataType.STEPS, any, any)).thenAnswer((_) async => 0); + stubReadings(overlappingSteps); + + await runImport(); + + final entry = + verify(measurements.addLocalDrift(captureAny)).captured.single as MeasurementEntry; + expect(entry.value, 4999); + }); + test('keeps the raw sum where the platform has no totals', () async { stubReadings(overlappingSteps);