From 3dc9789500c0b0793100eb8cee9ef74c334609b5 Mon Sep 17 00:00:00 2001 From: Roland Geider Date: Fri, 7 Aug 2026 14:42:19 +0200 Subject: [PATCH] Activate the rest of the metric types --- android/app/src/main/AndroidManifest.xml | 3 ++ lib/features/health/models/health_metric.dart | 36 ++++++++++++++++--- .../health/models/health_metric_test.dart | 31 +++++++++++----- 3 files changed, 58 insertions(+), 12 deletions(-) diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index 8738e45af..edf2e886d 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -20,6 +20,9 @@ + + + diff --git a/lib/features/health/models/health_metric.dart b/lib/features/health/models/health_metric.dart index a4e41ad47..5e53793f6 100644 --- a/lib/features/health/models/health_metric.dart +++ b/lib/features/health/models/health_metric.dart @@ -160,11 +160,14 @@ double _heightToCm(double raw) => raw < 3 ? raw * 100 : raw; double _identity(double raw) => raw; +/// Both platforms report a distance in meters, the category stores kilometers. +double _metersToKm(double raw) => raw / 1000; + /// The V1 metric set (see `plan-measurements-health-v27.md`). /// -/// Only [HealthMetric.enabled] entries are imported. The rest are declared so -/// the mapping is visible in one place; each is blocked on groundwork noted in -/// its [HealthMetric.disabledReason]. +/// Only [HealthMetric.enabled] entries are imported. A disabled one stays +/// declared so the mapping is visible in one place, with the groundwork it +/// waits for in its [HealthMetric.disabledReason]. const List healthMetrics = [ HealthMetric( metricType: MetricType.bodyFat, @@ -248,11 +251,36 @@ const List healthMetrics = [ dayRollsOverAtHour: 18, enabled: true, ), + // 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. HealthMetric( metricType: MetricType.steps, dataType: HealthDataType.STEPS, toCategoryValue: _identity, - disabledReason: 'High-volume cumulative type, parked behind a load test.', + dailyAggregation: DailyAggregation.sum, + enabled: true, + readWindow: highVolumeReadWindow, + ), + HealthMetric( + metricType: MetricType.distance, + dataType: HealthDataType.DISTANCE_DELTA, + toCategoryValue: _metersToKm, + dailyAggregation: DailyAggregation.sum, + enabled: true, + readWindow: highVolumeReadWindow, + ), + HealthMetric( + // Active energy only: the basal part is a platform estimate the user never + // recorded, and adding it in would make the day's number depend on which + // platform wrote it. + metricType: MetricType.energy, + dataType: HealthDataType.ACTIVE_ENERGY_BURNED, + toCategoryValue: _identity, + dailyAggregation: DailyAggregation.sum, + enabled: true, readWindow: highVolumeReadWindow, ), ]; diff --git a/test/features/health/models/health_metric_test.dart b/test/features/health/models/health_metric_test.dart index cea72c290..6afe86872 100644 --- a/test/features/health/models/health_metric_test.dart +++ b/test/features/health/models/health_metric_test.dart @@ -16,7 +16,6 @@ * along with this program. If not, see . */ -import 'package:collection/collection.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:health_bridge/health.dart'; import 'package:wger/features/health/models/health_metric.dart'; @@ -26,7 +25,7 @@ HealthMetric _metric(MetricType type) => healthMetrics.firstWhere((m) => m.metri void main() { group('Enabled metric set', () { - test('only the V1 sample metrics are imported', () { + test('every declared metric is imported', () { expect( enabledHealthMetrics.map((m) => m.metricType), containsAll([ @@ -37,9 +36,12 @@ void main() { MetricType.heartRate, MetricType.restingHeartRate, MetricType.sleep, + MetricType.steps, + MetricType.distance, + MetricType.energy, ]), ); - expect(enabledHealthMetrics.length, 7); + expect(enabledHealthMetrics.length, 10); }); test('daily aggregation is set per metric', () { @@ -49,6 +51,21 @@ void main() { expect(_metric(MetricType.sleep).dailyAggregation, DailyAggregation.mergedDuration); // The platforms compute the resting rate per day themselves expect(_metric(MetricType.restingHeartRate).dailyAggregation, isNull); + // One record of a counter covers minutes, only the day's total is a + // measurement + for (final type in [MetricType.steps, MetricType.distance, MetricType.energy]) { + expect(_metric(type).dailyAggregation, DailyAggregation.sum); + } + }); + + test('the cumulative types are read in small windows', () { + for (final type in [MetricType.steps, MetricType.distance, MetricType.energy]) { + expect(_metric(type).readWindow, highVolumeReadWindow); + } + }); + + test('a distance arrives in meters and is stored in kilometers', () { + expect(_metric(MetricType.distance).toCategoryValue(5300), closeTo(5.3, 0.001)); }); test('sleep is attributed to the wake day', () { @@ -73,9 +90,9 @@ void main() { }); test('every disabled metric explains why', () { - final disabled = healthMetrics.where((m) => !m.enabled); - expect(disabled, isNotEmpty); - expect(disabled.every((m) => m.disabledReason != null), isTrue); + // Nothing is parked at the moment; the rule is for the next type that is + // declared before its groundwork exists + expect(healthMetrics.where((m) => !m.enabled).every((m) => m.disabledReason != null), isTrue); }); test('metric types are unique', () { @@ -113,7 +130,5 @@ void main() { enabledHealthMetrics, healthMetrics.where((m) => m.enabled).toList(), ); - // sanity: the disabled ones really are excluded - expect(enabledHealthMetrics.firstWhereOrNull((m) => m.metricType == MetricType.steps), isNull); }); }