Merge branch 'feat/weight-chart-range' into feature/health-sync

# Conflicts:
#	lib/features/measurements/widgets/helpers.dart
This commit is contained in:
Roland Geider
2026-07-28 22:48:32 +02:00
4 changed files with 124 additions and 6 deletions

View File

@@ -46,11 +46,12 @@ List<Widget> getOverviewWidgetsSeries(
String unit,
BuildContext context, {
MetricType metricType = MetricType.custom,
String? mainChartTitle,
}) {
final monthAgo = DateTime.now().subtract(const Duration(days: 30));
return [
...getOverviewWidgets(
AppLocalizations.of(context).chartAllTimeTitle(name),
mainChartTitle ?? AppLocalizations.of(context).chartAllTimeTitle(name),
entriesAll,
entries7dAvg,
unit,

View File

@@ -24,6 +24,7 @@ import 'package:wger/core/snackbar.dart';
import 'package:wger/core/widgets/async_value_widget.dart';
import 'package:wger/core/widgets/progress_indicator.dart';
import 'package:wger/features/account/providers/user_profile_notifier.dart';
import 'package:wger/features/measurements/measurements.dart';
import 'package:wger/features/measurements/widgets/charts.dart';
import 'package:wger/features/measurements/widgets/helpers.dart';
import 'package:wger/features/nutrition/providers/nutrition_notifier.dart';
@@ -32,11 +33,22 @@ import 'package:wger/features/weight/providers/body_weight_notifier.dart';
import 'package:wger/features/weight/widgets/forms.dart';
import 'package:wger/l10n/generated/app_localizations.dart';
class WeightOverview extends riverpod.ConsumerWidget {
/// Time range the user can pick to limit how far back the weight chart goes.
enum WeightChartRange { all, lastYear, last3Months }
class WeightOverview extends riverpod.ConsumerStatefulWidget {
const WeightOverview();
@override
Widget build(BuildContext context, riverpod.WidgetRef ref) {
riverpod.ConsumerState<WeightOverview> createState() => _WeightOverviewState();
}
class _WeightOverviewState extends riverpod.ConsumerState<WeightOverview> {
WeightChartRange _range = WeightChartRange.all;
@override
Widget build(BuildContext context) {
final i18n = AppLocalizations.of(context);
final profileAsync = ref.watch(userProfileProvider);
final numberFormat = localizedNumberFormat(context);
final plans = ref.watch(nutritionProvider).value?.plans ?? const [];
@@ -55,17 +67,63 @@ class WeightOverview extends riverpod.ConsumerWidget {
final entriesAll = entriesList.map((e) => MeasurementChartEntry(e.weight, e.date)).toList();
final entries7dAvg = moving7dAverage(entriesAll);
// Restrict the data to the selected range. The average is computed over
// the full history first and only filtered afterwards, matching how the
// per-plan and 30-day sub-charts are built.
final (DateTime? cutoff, String? mainChartTitle) = switch (_range) {
WeightChartRange.all => (null, null),
WeightChartRange.lastYear => (
DateTime.now().subtract(const Duration(days: 365)),
i18n.chartLastYearTitle(i18n.weight),
),
WeightChartRange.last3Months => (
DateTime.now().subtract(const Duration(days: 90)),
i18n.chartLast3MonthsTitle(i18n.weight),
),
};
final entriesRange = cutoff == null ? entriesAll : entriesAll.whereDate(cutoff, null);
final entries7dAvgRange =
cutoff == null ? entries7dAvg : entries7dAvg.whereDate(cutoff, null);
final unit = weightUnit(profile.isMetric, context);
return Column(
children: [
Padding(
padding: const EdgeInsets.symmetric(horizontal: 15),
child: SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: SegmentedButton<WeightChartRange>(
key: const ValueKey('weightChartRangeSelector'),
showSelectedIcon: false,
segments: [
ButtonSegment(
value: WeightChartRange.all,
label: Text(i18n.chartRangeAll),
),
ButtonSegment(
value: WeightChartRange.lastYear,
label: Text(i18n.chartRangeLastYear),
),
ButtonSegment(
value: WeightChartRange.last3Months,
label: Text(i18n.chartRangeLast3Months),
),
],
selected: {_range},
onSelectionChanged: (selection) =>
setState(() => _range = selection.first),
),
),
),
...getOverviewWidgetsSeries(
AppLocalizations.of(context).weight,
entriesAll,
entries7dAvg,
i18n.weight,
entriesRange,
entries7dAvgRange,
plans,
unit,
context,
mainChartTitle: mainChartTitle,
),
TextButton(
onPressed: () => Navigator.pushNamed(

View File

@@ -546,6 +546,38 @@
}
}
},
"chartLastYearTitle": "{name} last year",
"@chartLastYearTitle": {
"description": "last year chart of 'name' (e.g. 'weight', 'body fat' etc.)",
"type": "text",
"placeholders": {
"name": {
"type": "String"
}
}
},
"chartLast3MonthsTitle": "{name} last 3 months",
"@chartLast3MonthsTitle": {
"description": "last 3 months chart of 'name' (e.g. 'weight', 'body fat' etc.)",
"type": "text",
"placeholders": {
"name": {
"type": "String"
}
}
},
"chartRangeAll": "All",
"@chartRangeAll": {
"description": "Label for the chart time-range selector option showing all data"
},
"chartRangeLastYear": "Last year",
"@chartRangeLastYear": {
"description": "Label for the chart time-range selector option showing the last year"
},
"chartRangeLast3Months": "Last 3 months",
"@chartRangeLast3Months": {
"description": "Label for the chart time-range selector option showing the last 3 months"
},
"measurement": "Measurement",
"@measurement": {},
"measurements": "Measurements",

View File

@@ -94,6 +94,33 @@ void main() {
expect(find.byType(ListTile), findsNWidgets(2));
});
testWidgets('Weight chart range selector filters the data', (WidgetTester tester) async {
await tester.pumpWidget(createWeightScreen());
await tester.pumpAndSettle();
// The range selector is shown with the three options
expect(find.byKey(const ValueKey('weightChartRangeSelector')), findsOneWidget);
expect(find.text('All'), findsOneWidget);
expect(find.text('Last year'), findsOneWidget);
expect(find.text('Last 3 months'), findsOneWidget);
// By default (all time) the chart is shown for the seeded entries
expect(find.byType(MeasurementChartWidgetFl), findsOneWidget);
// Restricting to the last 3 months excludes the (old) seeded data
await tester.ensureVisible(find.text('Last 3 months'));
await tester.tap(find.text('Last 3 months'));
await tester.pumpAndSettle();
expect(find.byType(MeasurementChartWidgetFl), findsNothing);
expect(find.text('No data available'), findsOneWidget);
// Switching back to all time restores the chart
await tester.ensureVisible(find.text('All'));
await tester.tap(find.text('All'));
await tester.pumpAndSettle();
expect(find.byType(MeasurementChartWidgetFl), findsOneWidget);
});
testWidgets('Test deleting an item using the Delete button', (WidgetTester tester) async {
// Arrange
await tester.pumpWidget(createWeightScreen());