From a2a601951ce2d52df84d7134005ba224b990a2da Mon Sep 17 00:00:00 2001 From: Roland Geider Date: Tue, 25 Aug 2026 13:50:11 +0200 Subject: [PATCH] Save the weight form through a draft and share the group component rows --- .../measurements/widgets/categories_card.dart | 67 +++++-------------- .../measurements/widgets/entries.dart | 32 ++------- .../measurements/widgets/helpers.dart | 55 +++++++++++++++ .../measurements/widgets/weight_form.dart | 31 ++++----- .../widgets/weight_form_test.dart | 29 ++++++++ 5 files changed, 121 insertions(+), 93 deletions(-) diff --git a/lib/features/measurements/widgets/categories_card.dart b/lib/features/measurements/widgets/categories_card.dart index 586b5f0ce..97b6f8cbc 100644 --- a/lib/features/measurements/widgets/categories_card.dart +++ b/lib/features/measurements/widgets/categories_card.dart @@ -16,12 +16,10 @@ * along with this program. If not, see . */ -import 'package:collection/collection.dart'; import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:wger/core/form_screen.dart'; import 'package:wger/core/formatting/formatting.dart'; -import 'package:wger/features/measurements/charts/colors.dart'; import 'package:wger/features/measurements/charts/data.dart'; import 'package:wger/features/measurements/charts/range.dart'; import 'package:wger/features/measurements/charts/series.dart'; @@ -192,16 +190,9 @@ class CategoriesCard extends ConsumerWidget { BuildContext context, Map> points, ) { - // A range is a single bar whose ends speak for themselves, and a stacked - // bar's segments carry the component colours; only the line chart needs - // the dots on the rows below to tie a component to its line. + // A range is a single bar whose ends speak for themselves; only the line + // chart needs the dots on the rows below to tie a component to its line final asRange = currentCategory.children.length == 2 && groupRangeEntries(points).isNotEmpty; - // The stacked chart draws only the components that are parts of the whole, - // so a row's colour has to come from that list rather than from its - // position among all children - final stacked = currentCategory.metricType.isSummedPerDay - ? stackableComponents(currentCategory) - : null; return [ if (groupHasData(points)) @@ -218,48 +209,24 @@ class CategoriesCard extends ConsumerWidget { builder: (context, ref, _) { final latest = ref.watch(latestMeasurementEntriesProvider).value ?? const {}; - return Column( - mainAxisSize: MainAxisSize.min, - children: currentCategory.children.mapIndexed((index, child) { - final colorIndex = stacked == null - ? index - : stacked.indexWhere((c) => c.id == child.id); + return GroupComponentRows( + currentCategory, + showDots: !asRange, + trailing: (context, child) { final value = latest[child.id]; - return ListTile( - dense: true, - // The dot ties the row to its part of the chart above. A range - // is a single bar, where the ends speak for themselves, and a - // roll-up component is no segment of the stack. - leading: asRange || colorIndex < 0 - ? null - : Container( - width: 12, - height: 12, - decoration: BoxDecoration( - shape: BoxShape.circle, - color: componentColor(context, colorIndex), - ), - ), - title: Text(child.displayName(context)), - trailing: Text( - value != null - ? measurementWithUnit( - context, - value.valueIn(child.unit, categoryUnit: child.unit), - child.unit, - decimals: child.metricType.displayDecimals, - ) - : '—', - style: Theme.of(context).textTheme.bodyLarge, - ), - onTap: () => Navigator.pushNamed( - context, - MeasurementEntriesScreen.routeName, - arguments: child.id, - ), + return Text( + value != null + ? measurementWithUnit( + context, + value.valueIn(child.unit, categoryUnit: child.unit), + child.unit, + decimals: child.metricType.displayDecimals, + ) + : '—', + style: Theme.of(context).textTheme.bodyLarge, ); - }).toList(), + }, ); }, ), diff --git a/lib/features/measurements/widgets/entries.dart b/lib/features/measurements/widgets/entries.dart index e0b5e3ad6..bdc71cb96 100644 --- a/lib/features/measurements/widgets/entries.dart +++ b/lib/features/measurements/widgets/entries.dart @@ -23,7 +23,6 @@ import 'package:wger/core/form_screen.dart'; import 'package:wger/core/formatting/formatting.dart'; import 'package:wger/core/snackbar.dart'; import 'package:wger/core/widgets/error.dart'; -import 'package:wger/features/measurements/charts/colors.dart'; import 'package:wger/features/measurements/charts/data.dart'; import 'package:wger/features/measurements/charts/range.dart'; import 'package:wger/features/measurements/charts/series.dart'; @@ -31,7 +30,6 @@ import 'package:wger/features/measurements/models/measurement_category.dart'; import 'package:wger/features/measurements/models/measurement_entry.dart'; import 'package:wger/features/measurements/models/unit_conversion.dart'; import 'package:wger/features/measurements/providers/measurement_notifier.dart'; -import 'package:wger/features/measurements/screens/measurement_entries_screen.dart'; import 'package:wger/features/measurements/widgets/calculation_mark.dart'; import 'package:wger/features/measurements/widgets/chart_range_selector.dart'; import 'package:wger/features/measurements/widgets/helpers.dart'; @@ -177,9 +175,6 @@ class EntriesList extends ConsumerWidget { /// which the legend rows lead to. Widget _buildGroup(BuildContext context, WidgetRef ref, MeasurementCategory category) { final i18n = AppLocalizations.of(context); - // Only the stacked chart leaves a component out, so the colours of the - // rows below have to follow the same list - final stacked = category.metricType.isSummedPerDay ? stackableComponents(category) : null; return Column( children: [ @@ -208,29 +203,10 @@ class EntriesList extends ConsumerWidget { ], onError: (_, error) => [StreamErrorIndicator(error.toString())], ), - ...category.children.mapIndexed((index, child) { - final colorIndex = stacked == null ? index : stacked.indexWhere((c) => c.id == child.id); - return ListTile( - dense: true, - leading: colorIndex < 0 - ? null - : Container( - width: 12, - height: 12, - decoration: BoxDecoration( - shape: BoxShape.circle, - color: componentColor(context, colorIndex), - ), - ), - title: Text(child.displayName(context)), - trailing: const Icon(Icons.chevron_right), - onTap: () => Navigator.pushNamed( - context, - MeasurementEntriesScreen.routeName, - arguments: child.id, - ), - ); - }), + GroupComponentRows( + category, + trailing: (_, _) => const Icon(Icons.chevron_right), + ), const Divider(), _GroupReadingsList(category), ], diff --git a/lib/features/measurements/widgets/helpers.dart b/lib/features/measurements/widgets/helpers.dart index aee986ab6..ca07620ae 100644 --- a/lib/features/measurements/widgets/helpers.dart +++ b/lib/features/measurements/widgets/helpers.dart @@ -1,3 +1,4 @@ +import 'package:collection/collection.dart'; import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:wger/core/widgets/error.dart'; @@ -10,6 +11,7 @@ import 'package:wger/features/measurements/models/measurement_bucket.dart'; import 'package:wger/features/measurements/models/measurement_category.dart'; import 'package:wger/features/measurements/models/unit_conversion.dart'; import 'package:wger/features/measurements/providers/measurement_notifier.dart'; +import 'package:wger/features/measurements/screens/measurement_entries_screen.dart'; import 'package:wger/features/measurements/widgets/charts.dart'; import 'package:wger/l10n/generated/app_localizations.dart'; @@ -434,3 +436,56 @@ Widget buildChartForMetricType( planPeriods: planPeriods, ); } + +/// One row per component of [group]: a colour dot tying the row to its part +/// of the group chart, the component's name, and [trailing]; tapping opens +/// the component's entries. +/// +/// The stacked chart draws only the components that are parts of the whole, +/// so a row's colour comes from that list rather than from its position among +/// all children, and a roll-up component gets no dot. [showDots] turns them +/// off wholesale, for a range chart whose single bar needs no legend. +class GroupComponentRows extends StatelessWidget { + const GroupComponentRows(this.group, {required this.trailing, this.showDots = true}); + + final MeasurementCategory group; + + /// Built per component: the latest value on the card, a chevron on the + /// detail screen. + final Widget? Function(BuildContext context, MeasurementCategory child) trailing; + + final bool showDots; + + @override + Widget build(BuildContext context) { + final stacked = group.metricType.isSummedPerDay ? stackableComponents(group) : null; + + return Column( + mainAxisSize: MainAxisSize.min, + children: group.children.mapIndexed((index, child) { + final colorIndex = stacked == null ? index : stacked.indexWhere((c) => c.id == child.id); + + return ListTile( + dense: true, + leading: !showDots || colorIndex < 0 + ? null + : Container( + width: 12, + height: 12, + decoration: BoxDecoration( + shape: BoxShape.circle, + color: componentColor(context, colorIndex), + ), + ), + title: Text(child.displayName(context)), + trailing: trailing(context, child), + onTap: () => Navigator.pushNamed( + context, + MeasurementEntriesScreen.routeName, + arguments: child.id, + ), + ); + }).toList(), + ); + } +} diff --git a/lib/features/measurements/widgets/weight_form.dart b/lib/features/measurements/widgets/weight_form.dart index 9d724a1f6..c641f39c2 100644 --- a/lib/features/measurements/widgets/weight_form.dart +++ b/lib/features/measurements/widgets/weight_form.dart @@ -52,14 +52,21 @@ class WeightForm extends riverpod.ConsumerStatefulWidget { class _WeightFormState extends riverpod.ConsumerState { final _form = GlobalKey(); - late DateTime _date; + late MeasurementEntry _draft; num? _weight; late String _unit; @override void initState() { super.initState(); - _date = widget._entry?.date ?? DateTime.now(); + _draft = + widget._entry ?? + MeasurementEntry( + categoryId: widget._category.id!, + date: DateTime.now(), + value: 0, + notes: '', + ); _weight = widget._entry?.value; // Existing entries keep their stored unit (the value is shown exactly as @@ -81,8 +88,8 @@ class _WeightFormState extends riverpod.ConsumerState { children: [ DateTimeInputWidget( key: const Key('dateTimeInput'), - value: _date, - onChanged: (value) => _date = value, + value: _draft.date, + onChanged: (value) => _draft = _draft.copyWith(date: value), ), // Weight @@ -121,18 +128,12 @@ class _WeightFormState extends riverpod.ConsumerState { } _form.currentState!.save(); - // Notes, source and external id are not editable here; keep the - // existing values so edits to imported entries stay deduplicable. - // The chosen unit is stamped into extra_data, other keys survive. - final entry = MeasurementEntry( - id: widget._entry?.id, - categoryId: widget._category.id!, - date: _date, + // The draft carries what the form does not offer (notes, source, + // external id), so an edited import stays deduplicable; only the + // chosen unit is stamped into extra_data, other keys survive + final entry = _draft.copyWith( value: _weight!, - notes: widget._entry?.notes ?? '', - source: widget._entry?.source ?? measurementSourceUser, - externalId: widget._entry?.externalId, - extraData: {...?widget._entry?.extraData, 'unit': _unit}, + extraData: {...?_draft.extraData, 'unit': _unit}, ); final notifier = ref.read(measurementProvider.notifier); diff --git a/test/features/measurements/widgets/weight_form_test.dart b/test/features/measurements/widgets/weight_form_test.dart index bf3de3e1d..36cd75326 100644 --- a/test/features/measurements/widgets/weight_form_test.dart +++ b/test/features/measurements/widgets/weight_form_test.dart @@ -286,5 +286,34 @@ void main() { // The typed value itself is not converted, only re-interpreted expect(saved.value, 80); }); + + testWidgets('Editing keeps source, externalId and extra data of imported entries', ( + WidgetTester tester, + ) async { + final imported = MeasurementEntry( + id: 'e-import', + categoryId: testBodyWeightCategoryId, + date: DateTime(2026, 1, 1), + value: 80, + notes: 'From the watch', + source: 'apple', + externalId: 'platform-uuid', + extraData: const {'unit': 'kg', 'record_type': 'WEIGHT'}, + ); + + await tester.pumpWidget(createWeightForm(entry: imported)); + await tester.pumpAndSettle(); + + await tester.tap(find.byKey(const Key(SUBMIT_BUTTON_KEY_NAME))); + await tester.pumpAndSettle(); + + final saved = + verify(mockRepo.updateLocalDrift(captureAny)).captured.single as MeasurementEntry; + expect(saved.source, 'apple'); + expect(saved.externalId, 'platform-uuid'); + expect(saved.notes, 'From the watch'); + expect(saved.extraData, {'unit': 'kg', 'record_type': 'WEIGHT'}); + expect(saved.value, 80); + }); }); }