Fall back to the raw sum when Health Connect reports 0

This commit is contained in:
Roland Geider
2026-10-23 19:50:19 +02:00
parent 09c1160118
commit d5ef743e48
5 changed files with 22 additions and 7 deletions

View File

@@ -255,9 +255,9 @@ const List<HealthMetric> 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,

View File

@@ -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 {

View File

@@ -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) {

View File

@@ -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<double?> platformTotal(HealthDataType type, DateTime start, DateTime end) async {
if (!hasPlatformTotals || !end.isAfter(start)) {
return null;

View File

@@ -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);