Allow logging a reading straight from the overview

This commit is contained in:
Roland Geider
2026-08-27 18:51:48 +02:00
parent aed2e71413
commit 37371dbfbc
4 changed files with 166 additions and 45 deletions

View File

@@ -1,6 +1,7 @@
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/widgets/error.dart';
import 'package:wger/core/widgets/legend.dart';
import 'package:wger/core/widgets/progress_indicator.dart';
@@ -19,6 +20,8 @@ import 'package:wger/features/measurements/widgets/charts/heatmap_chart.dart';
import 'package:wger/features/measurements/widgets/charts/line_chart.dart';
import 'package:wger/features/measurements/widgets/charts/overall_change.dart';
import 'package:wger/features/measurements/widgets/charts/stacked_bar_chart.dart';
import 'package:wger/features/measurements/widgets/forms/entry.dart';
import 'package:wger/features/measurements/widgets/forms/group_entry.dart';
import 'package:wger/l10n/generated/app_localizations.dart';
/// A chart with the overall change below it, for a Column to spread, titled
@@ -495,3 +498,18 @@ class GroupComponentRows extends StatelessWidget {
);
}
}
/// Opens the form that logs a new entry for [category], the group variant
/// where it has components.
Future<void> openMeasurementEntryForm(BuildContext context, MeasurementCategory category) async {
await Navigator.pushNamed(
context,
FormScreen.routeName,
arguments: FormScreenArguments(
AppLocalizations.of(context).newEntry,
category.hasChildren
? GroupMeasurementEntryForm(category)
: MeasurementEntryForm(category.id!),
),
);
}

View File

@@ -18,12 +18,10 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:wger/core/form_screen.dart';
import 'package:wger/features/measurements/models/measurement_category.dart';
import 'package:wger/features/measurements/models/measurement_entry.dart';
import 'package:wger/features/measurements/providers/measurement_notifier.dart';
import 'package:wger/features/measurements/widgets/forms/entry.dart';
import 'package:wger/features/measurements/widgets/forms/group_entry.dart';
import 'package:wger/features/measurements/widgets/helpers.dart';
import 'package:wger/features/measurements/widgets/metric_picker.dart';
import 'package:wger/l10n/generated/app_localizations.dart';
@@ -37,24 +35,14 @@ import 'package:wger/l10n/generated/app_localizations.dart';
class MeasurementsFab extends ConsumerWidget {
const MeasurementsFab({super.key});
Future<void> _addEntry(BuildContext context, MeasurementCategory category) async {
await Navigator.pushNamed(
context,
FormScreen.routeName,
arguments: FormScreenArguments(
AppLocalizations.of(context).newEntry,
category.hasChildren
? GroupMeasurementEntryForm(category)
: MeasurementEntryForm(category.id!),
),
);
}
/// Whether [category] is fed by hand rather than by the health sync or by
/// the server, judged by who wrote its newest reading. A category without
/// entries counts as hand-kept: it was just created and logging is what it
/// is waiting for. A calculated one never does, also while it is still
/// empty, since the server refuses entries there.
///
/// Only the menu narrows itself this way: a machine-fed category still takes
/// a reading by hand, its tile and its entries screen offer that.
bool _isHandKept(MeasurementCategory category, Map<String, MeasurementEntry> latest) {
if (category.isCalculated) {
return false;
@@ -84,7 +72,7 @@ class MeasurementsFab extends ConsumerWidget {
for (final category in handKept)
MenuItemButton(
leadingIcon: const Icon(Icons.add),
onPressed: () => _addEntry(context, category),
onPressed: () => openMeasurementEntryForm(context, category),
child: Text(category.displayName(context)),
),
MenuItemButton(

View File

@@ -45,7 +45,7 @@ import 'package:wger/l10n/generated/app_localizations.dart';
/// spare: they are laid out at their natural heights, so a tile sized to
/// exactly fit them overflows as soon as the text does not render at the size
/// it was measured at.
const measurementTileExtent = 172.0;
const measurementTileExtent = 184.0;
/// Height of the spark chart inside a tile.
const _sparkHeight = 40.0;
@@ -54,6 +54,10 @@ const _sparkHeight = 40.0;
/// instead, which is what makes the cells legible.
const _heatmapHeight = 58.0;
/// Height of the footer row, fixed so that the sparks line up across the grid
/// whether or not a tile carries the add button.
const _footerHeight = 28.0;
/// One category of the overview grid: latest value as the hero, a spark chart
/// as context over the range the filter selects. A tap opens the category's
/// entries screen; everything beyond a glance lives there.
@@ -204,18 +208,20 @@ class MeasurementTile extends ConsumerWidget {
),
// The same footer its kind would get elsewhere: a level for the
// summed types, a direction for the sample ones
if (category.metricType.isSummedPerDay)
_AverageChip(
points: points,
unit: category.unit,
decimals: category.metricType.displayDecimals,
)
else
_TrendChip(
points: points,
unit: category.unit,
decimals: category.metricType.displayDecimals,
),
_Footer(
onAdd: _onAdd(context),
chip: category.metricType.isSummedPerDay
? _AverageChip(
points: points,
unit: category.unit,
decimals: category.metricType.displayDecimals,
)
: _TrendChip(
points: points,
unit: category.unit,
decimals: category.metricType.displayDecimals,
),
),
];
case SparkKind.delta:
@@ -230,6 +236,7 @@ class MeasurementTile extends ConsumerWidget {
),
),
_MonthAxis(window.start!),
_Footer(onAdd: _onAdd(context)),
];
case SparkKind.bars:
@@ -244,10 +251,13 @@ class MeasurementTile extends ConsumerWidget {
return [
_SparkArea(child: SparkBarChart(data)),
_axis(window.start!, window.days!, weekly: window.weekly),
_AverageChip(
points: points,
unit: category.unit,
decimals: category.metricType.displayDecimals,
_Footer(
onAdd: _onAdd(context),
chip: _AverageChip(
points: points,
unit: category.unit,
decimals: category.metricType.displayDecimals,
),
),
];
@@ -261,10 +271,13 @@ class MeasurementTile extends ConsumerWidget {
child: SparkLineChart(points, start: start, days: days, dots: sparse),
),
_axis(start, days, weekly: false),
_TrendChip(
points: points,
unit: category.unit,
decimals: category.metricType.displayDecimals,
_Footer(
onAdd: _onAdd(context),
chip: _TrendChip(
points: points,
unit: category.unit,
decimals: category.metricType.displayDecimals,
),
),
];
}
@@ -312,10 +325,13 @@ class MeasurementTile extends ConsumerWidget {
return [
_SparkArea(child: SparkBarChart(data)),
_axis(start, window.days!, weekly: window.weekly),
_AverageChip(
points: [for (final day in stacked) MeasurementChartEntry(day.total, day.date)],
unit: category.unit,
decimals: category.metricType.displayDecimals,
_Footer(
onAdd: _onAdd(context),
chip: _AverageChip(
points: [for (final day in stacked) MeasurementChartEntry(day.total, day.date)],
unit: category.unit,
decimals: category.metricType.displayDecimals,
),
),
];
}
@@ -337,7 +353,7 @@ class MeasurementTile extends ConsumerWidget {
_axis(start, window.days!, weekly: window.weekly),
// A range spans a reading rather than tracking a level, so the spread
// is what a chip could quote; the hero already says how recent it is
const SizedBox.shrink(),
_Footer(onAdd: _onAdd(context)),
];
}
@@ -351,10 +367,16 @@ class MeasurementTile extends ConsumerWidget {
child: SparkLineChart(fallback ?? const [], start: start, days: window.days!),
),
_axis(start, window.days!, weekly: window.weekly),
const SizedBox.shrink(),
_Footer(onAdd: _onAdd(context)),
];
}
/// Logging from the tile, or null for a calculated category: the server
/// derives those and refuses entries. A health-synced one takes a reading by
/// hand like any other.
VoidCallback? _onAdd(BuildContext context) =>
category.isCalculated ? null : () => openMeasurementEntryForm(context, category);
/// The entry the hero value comes from: the category's own for a leaf, the
/// newest of the components for a group.
MeasurementEntry? _newestOf(Map<String, MeasurementEntry> latest) {
@@ -462,6 +484,42 @@ class _MonthAxis extends StatelessWidget {
}
}
/// The tile's last row: the chip on the left, and on the right the way to log
/// a reading without opening the category first, where [onAdd] says that the
/// category takes one.
class _Footer extends StatelessWidget {
const _Footer({this.chip, this.onAdd});
final Widget? chip;
final VoidCallback? onAdd;
@override
Widget build(BuildContext context) {
return SizedBox(
height: _footerHeight,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
// Flexible against the alignment, not against a Spacer: two flex
// children would split the room evenly and cut the chip off at half
// the row, however much of it is free
Flexible(child: chip ?? const SizedBox.shrink()),
if (onAdd != null)
IconButton(
onPressed: onAdd,
icon: const Icon(Icons.add),
iconSize: 18,
padding: EdgeInsets.zero,
visualDensity: VisualDensity.compact,
constraints: const BoxConstraints.tightFor(width: 32, height: _footerHeight),
tooltip: AppLocalizations.of(context).newEntry,
),
],
),
);
}
}
/// A neutral fact in chip form, see [_TrendChip] and [_AverageChip]. States
/// what the numbers do, never whether that is good: which direction is the
/// good one depends on the goal, and the tile should not assert one.
@@ -480,7 +538,12 @@ class _FooterChip extends StatelessWidget {
color: theme.colorScheme.surfaceContainerHighest,
borderRadius: BorderRadius.circular(8),
),
child: Text(text, style: theme.textTheme.labelSmall, maxLines: 1),
child: Text(
text,
style: theme.textTheme.labelSmall,
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
);
}
}

View File

@@ -17,9 +17,11 @@
*/
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/annotations.dart';
import 'package:wger/core/form_screen.dart';
import 'package:wger/features/measurements/charts/range.dart';
import 'package:wger/features/measurements/models/measurement_category.dart';
import 'package:wger/features/measurements/models/measurement_entry.dart';
@@ -73,6 +75,7 @@ Widget _wrap(
supportedLocales: AppLocalizations.supportedLocales,
routes: {
MeasurementEntriesScreen.routeName: (_) => const Text('entries-screen'),
FormScreen.routeName: (_) => const Text('form-screen'),
},
home: Scaffold(
body: Center(
@@ -364,5 +367,54 @@ void main() {
expect(find.text('entries-screen'), findsOneWidget);
});
testWidgets('a tile logs a reading without leaving the grid', (tester) async {
final biceps = MeasurementCategory(id: 'biceps', name: 'Biceps', unit: 'cm');
final entries = {
'biceps': [_entry('biceps', 38.5, _day(3))],
};
await tester.pumpWidget(_wrap(biceps, entries: entries));
await tester.pumpAndSettle();
await tester.tap(find.byIcon(Icons.add));
await tester.pumpAndSettle();
expect(find.text('form-screen'), findsOneWidget);
});
testWidgets('a machine-fed tile takes a reading by hand as well', (tester) async {
final entries = {
'rhr': [_entry('rhr', 61, _day(0), source: 'google')],
};
await tester.pumpWidget(_wrap(restingHeartRate, entries: entries));
await tester.pumpAndSettle();
expect(find.byIcon(Icons.add), findsOneWidget);
});
testWidgets('the chip keeps the room the button leaves it', (tester) async {
final entries = {
'rhr': [for (var day = 6; day >= 0; day--) _entry('rhr', 61, _day(day))],
};
await tester.pumpWidget(_wrap(restingHeartRate, entries: entries));
await tester.pumpAndSettle();
final chip = tester.renderObject<RenderParagraph>(find.textContaining('stable'));
expect(chip.didExceedMaxLines, isFalse);
});
testWidgets('a calculated tile has nothing to log: the server derives it', (tester) async {
final bmi = MeasurementCategory(id: 'bmi', name: 'BMI', unit: 'kg/m²', dynamicType: 'BMI');
final entries = {
'bmi': [_entry('bmi', 22.4, _day(0))],
};
await tester.pumpWidget(_wrap(bmi, entries: entries));
await tester.pumpAndSettle();
expect(find.byIcon(Icons.add), findsNothing);
});
});
}