mirror of
https://github.com/wger-project/flutter.git
synced 2026-11-29 01:18:35 +01:00
Convert aggregate bounds along with their value
This commit is contained in:
@@ -55,4 +55,11 @@ extension MeasurementEntryUnit on MeasurementEntry {
|
||||
double valueIn(String targetUnit, {required String categoryUnit}) {
|
||||
return convertWeight(value, from: unitOrFallback(categoryUnit), to: targetUnit);
|
||||
}
|
||||
|
||||
/// A number stored in `extra_data` next to [value] in [targetUnit], such as
|
||||
/// the bounds of a daily aggregate. They are written in the value's unit, so
|
||||
/// they have to follow it through the same conversion.
|
||||
double boundIn(num bound, String targetUnit, {required String categoryUnit}) {
|
||||
return convertWeight(bound, from: unitOrFallback(categoryUnit), to: targetUnit);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -145,22 +145,25 @@ DateTime? sensibleRangeStart(List<MeasurementChartEntry> entriesAll) {
|
||||
///
|
||||
/// Entries stored as a daily aggregate keep the range they summarise in
|
||||
/// `extra_data` (heart rate min/max); it is lifted onto the point so the chart
|
||||
/// draws a band around the line. Those bounds are written in the category unit
|
||||
/// alongside the value, and aggregates never carry a per-entry unit.
|
||||
/// draws a band around the line. Those bounds share the value's unit and are
|
||||
/// converted along with it.
|
||||
List<MeasurementChartEntry> chartEntriesFor(
|
||||
List<MeasurementEntry> entries, {
|
||||
required String targetUnit,
|
||||
required String categoryUnit,
|
||||
}) => entries
|
||||
.map(
|
||||
(e) => MeasurementChartEntry(
|
||||
e.valueIn(targetUnit, categoryUnit: categoryUnit),
|
||||
e.date,
|
||||
min: e.extraData?['min'] as num?,
|
||||
max: e.extraData?['max'] as num?,
|
||||
),
|
||||
)
|
||||
.toList();
|
||||
}) => entries.map((entry) {
|
||||
num? bound(String key) {
|
||||
final stored = entry.extraData?[key];
|
||||
return stored is num ? entry.boundIn(stored, targetUnit, categoryUnit: categoryUnit) : null;
|
||||
}
|
||||
|
||||
return MeasurementChartEntry(
|
||||
entry.valueIn(targetUnit, categoryUnit: categoryUnit),
|
||||
entry.date,
|
||||
min: bound('min'),
|
||||
max: bound('max'),
|
||||
);
|
||||
}).toList();
|
||||
|
||||
/// The readings of a two-component group as ranges: one point per timestamp,
|
||||
/// spanning from the lower component to the upper one.
|
||||
|
||||
@@ -70,5 +70,23 @@ void main() {
|
||||
expect(entry(80).valueIn('kg', categoryUnit: 'kg'), 80);
|
||||
expect(entry(80).valueIn('lb', categoryUnit: 'kg'), 176.37);
|
||||
});
|
||||
|
||||
test('boundIn follows the value through the same conversion', () {
|
||||
// The bounds of an aggregate are stored in the value's unit, so reading
|
||||
// the value in lb and the bounds in kg would put the band 2.2x off
|
||||
final aggregate = entry(80, extraData: {'min': 70, 'max': 90});
|
||||
|
||||
expect(aggregate.valueIn('lb', categoryUnit: 'kg'), 176.37);
|
||||
expect(aggregate.boundIn(70, 'lb', categoryUnit: 'kg'), 154.32);
|
||||
expect(aggregate.boundIn(90, 'lb', categoryUnit: 'kg'), 198.42);
|
||||
});
|
||||
|
||||
test('boundIn honours the entry stamp like valueIn does', () {
|
||||
final stamped = entry(176.4, extraData: {'unit': 'lb'});
|
||||
|
||||
expect(stamped.boundIn(176.4, 'kg', categoryUnit: 'kg'), 80.01);
|
||||
// and leaves label-only units alone
|
||||
expect(entry(60).boundIn(50, 'bpm', categoryUnit: 'bpm'), 50);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ import 'package:fl_chart/fl_chart.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:wger/features/measurements/models/measurement_category.dart';
|
||||
import 'package:wger/features/measurements/models/measurement_entry.dart';
|
||||
import 'package:wger/features/measurements/widgets/charts.dart';
|
||||
import 'package:wger/features/measurements/widgets/helpers.dart';
|
||||
import 'package:wger/l10n/generated/app_localizations.dart';
|
||||
@@ -58,6 +59,47 @@ void main() {
|
||||
];
|
||||
final avgEntries = moving7dAverage(rawEntries);
|
||||
|
||||
group('chartEntriesFor', () {
|
||||
test('converts the aggregate bounds along with the value', () {
|
||||
// Reading the value in lb but the bounds in kg would put the band a
|
||||
// factor 2.2 off the line it is supposed to wrap
|
||||
final points = chartEntriesFor(
|
||||
[
|
||||
MeasurementEntry(
|
||||
categoryId: 'c',
|
||||
date: DateTime(2026, 1, 1),
|
||||
value: 80,
|
||||
notes: '',
|
||||
extraData: const {'min': 70, 'max': 90},
|
||||
),
|
||||
],
|
||||
targetUnit: 'lb',
|
||||
categoryUnit: 'kg',
|
||||
);
|
||||
|
||||
expect(points.single.value, 176.37);
|
||||
expect(points.single.min, 154.32);
|
||||
expect(points.single.max, 198.42);
|
||||
});
|
||||
|
||||
test('leaves entries without bounds unranged', () {
|
||||
final points = chartEntriesFor(
|
||||
[
|
||||
MeasurementEntry(
|
||||
categoryId: 'c',
|
||||
date: DateTime(2026, 1, 1),
|
||||
value: 80,
|
||||
notes: '',
|
||||
),
|
||||
],
|
||||
targetUnit: 'kg',
|
||||
categoryUnit: 'kg',
|
||||
);
|
||||
|
||||
expect(points.single.hasRange, isFalse);
|
||||
});
|
||||
});
|
||||
|
||||
group('buildChartForMetricType routing', () {
|
||||
final entries = [
|
||||
MeasurementChartEntry(1000, DateTime(2026, 1, 1)),
|
||||
|
||||
Reference in New Issue
Block a user