Reduce the measurements FAB to starting a new metric

This commit is contained in:
Roland Geider
2026-08-27 19:08:12 +02:00
parent 1d9a7d8819
commit ceaacb5478
3 changed files with 24 additions and 152 deletions

View File

@@ -1,6 +1,6 @@
/*
* This file is part of wger Workout Manager <https://github.com/wger-project>.
* Copyright (c) 2026 wger Team
* Copyright (c) 2026 - 2026 wger Team
*
* wger Workout Manager is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
@@ -17,78 +17,23 @@
*/
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.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/helpers.dart';
import 'package:wger/features/measurements/widgets/metric_picker.dart';
import 'package:wger/l10n/generated/app_localizations.dart';
/// The overview's add button: opens a menu with one action per hand-kept
/// category (straight into its entry form) plus the metric picker for
/// starting something new.
/// The overview's add button: opens the picker for starting a new metric.
///
/// A [MenuAnchor] rather than a hand-rolled expanding FAB: dismissal,
/// positioning and focus come with it. Flutter's M3 menu is also as close as
/// the stable SDK gets to the M3 FAB menu for now.
class MeasurementsFab extends ConsumerWidget {
/// Logging a reading is not on it. That belongs to the category, and the
/// category has it: every tile carries its own button, and body weight its
/// card.
class MeasurementsFab extends StatelessWidget {
const MeasurementsFab({super.key});
/// 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;
}
final newest = category.hasChildren
? category.children.map((c) => latest[c.id])
: [latest[category.id]];
return newest.nonNulls.every((entry) => entry.source == measurementSourceUser);
}
@override
Widget build(BuildContext context, WidgetRef ref) {
final categories = ref.watch(measurementCategoriesProvider).value ?? const [];
final latest = ref.watch(latestMeasurementEntriesProvider).value ?? const {};
final handKept = [
for (final category in categories)
if (category.parentId == null &&
!category.isOfficialBodyWeight &&
_isHandKept(category, latest))
category,
];
return MenuAnchor(
menuChildren: [
for (final category in handKept)
MenuItemButton(
leadingIcon: const Icon(Icons.add),
onPressed: () => openMeasurementEntryForm(context, category),
child: Text(category.displayName(context)),
),
MenuItemButton(
leadingIcon: const Icon(Icons.playlist_add),
onPressed: () => showMetricPicker(context),
child: Text(AppLocalizations.of(context).trackNewMetric),
),
],
builder: (context, controller, child) => FloatingActionButton(
onPressed: () => controller.isOpen ? controller.close() : controller.open(),
child: AnimatedRotation(
turns: controller.isOpen ? 0.125 : 0,
duration: const Duration(milliseconds: 200),
child: const Icon(Icons.add, color: Colors.white),
),
),
Widget build(BuildContext context) {
return FloatingActionButton(
onPressed: () => showMetricPicker(context),
tooltip: AppLocalizations.of(context).trackNewMetric,
child: const Icon(Icons.add, color: Colors.white),
);
}
}

View File

@@ -727,7 +727,7 @@
},
"trackNewMetric": "Track something new",
"@trackNewMetric": {
"description": "Action in the overview's expanding + button that opens the sheet for starting a new measurement category"
"description": "Tooltip of the overview's + button, which opens the sheet for starting a new measurement category"
},
"measurement": "Measurement",
"@measurement": {},

View File

@@ -20,9 +20,6 @@ import 'package:flutter/material.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/models/measurement_category.dart';
import 'package:wger/features/measurements/models/measurement_entry.dart';
import 'package:wger/features/measurements/providers/measurement_repository.dart';
import 'package:wger/features/measurements/widgets/measurement_fab.dart';
import 'package:wger/features/measurements/widgets/metric_picker.dart';
@@ -31,111 +28,41 @@ import 'package:wger/l10n/generated/app_localizations.dart';
import '../../../helpers/measurement_repository_stubs.dart';
import 'measurement_fab_test.mocks.dart';
MeasurementEntry _entry(String categoryId, {required String source}) => MeasurementEntry(
id: '$categoryId-1',
categoryId: categoryId,
date: DateTime(2026, 8, 1),
value: 1,
notes: '',
source: source,
);
Widget _wrap(
List<MeasurementCategory> categories,
Map<String, List<MeasurementEntry>> entries,
) {
Widget _wrap() {
final mockRepo = MockMeasurementRepository();
stubMeasurementReads(mockRepo, categories, entries);
stubMeasurementReads(mockRepo, const [], const {});
return ProviderScope(
overrides: [measurementRepositoryProvider.overrideWithValue(mockRepo)],
child: MaterialApp(
child: const MaterialApp(
localizationsDelegates: AppLocalizations.localizationsDelegates,
supportedLocales: AppLocalizations.supportedLocales,
routes: {FormScreen.routeName: (_) => const Text('form-screen')},
home: const Scaffold(floatingActionButton: MeasurementsFab()),
home: Scaffold(floatingActionButton: MeasurementsFab()),
),
);
}
@GenerateMocks([MeasurementRepository])
void main() {
final biceps = MeasurementCategory(id: 'biceps', name: 'Biceps', unit: 'cm');
final restingHeartRate = MeasurementCategory(
id: 'rhr',
name: 'Resting heart rate',
unit: 'bpm',
metricType: MetricType.restingHeartRate,
);
final categories = [biceps, restingHeartRate];
final entries = {
'biceps': [_entry('biceps', source: 'user')],
'rhr': [_entry('rhr', source: 'google')],
};
group('MeasurementsFab', () {
testWidgets('opens into the hand-kept categories and the picker action', (tester) async {
await tester.pumpWidget(_wrap(categories, entries));
await tester.pumpAndSettle();
// Closed: the menu is not tappable
expect(find.text('Biceps').hitTestable(), findsNothing);
await tester.tap(find.byType(FloatingActionButton));
await tester.pumpAndSettle();
expect(find.text('Biceps').hitTestable(), findsOneWidget);
expect(find.text('Track something new').hitTestable(), findsOneWidget);
// Machine-fed by its newest reading, so logging by hand is not offered
expect(find.text('Resting heart rate').hitTestable(), findsNothing);
});
testWidgets('a category action opens its entry form', (tester) async {
await tester.pumpWidget(_wrap(categories, entries));
testWidgets('opens the metric picker sheet', (tester) async {
await tester.pumpWidget(_wrap());
await tester.pumpAndSettle();
await tester.tap(find.byType(FloatingActionButton));
await tester.pumpAndSettle();
await tester.tap(find.text('Biceps'));
await tester.pumpAndSettle();
expect(find.text('form-screen'), findsOneWidget);
});
testWidgets('the picker action opens the metric picker sheet', (tester) async {
await tester.pumpWidget(_wrap(categories, entries));
await tester.pumpAndSettle();
await tester.tap(find.byType(FloatingActionButton));
await tester.pumpAndSettle();
await tester.tap(find.text('Track something new'));
await tester.pumpAndSettle();
expect(find.byType(MetricPickerSheet), findsOneWidget);
});
testWidgets('a category without entries counts as hand-kept', (tester) async {
await tester.pumpWidget(_wrap([restingHeartRate], const {}));
testWidgets('says what it does: readings are logged on the tiles', (tester) async {
await tester.pumpWidget(_wrap());
await tester.pumpAndSettle();
await tester.tap(find.byType(FloatingActionButton));
await tester.pumpAndSettle();
expect(find.text('Resting heart rate').hitTestable(), findsOneWidget);
});
testWidgets('a calculated category is not offered, also while still empty', (tester) async {
final bmi = MeasurementCategory(id: 'bmi', name: 'BMI', unit: '', dynamicType: 'BMI');
await tester.pumpWidget(_wrap([biceps, bmi], const {}));
await tester.pumpAndSettle();
await tester.tap(find.byType(FloatingActionButton));
await tester.pumpAndSettle();
expect(find.text('Biceps').hitTestable(), findsOneWidget);
expect(find.text('BMI').hitTestable(), findsNothing);
expect(
tester.widget<FloatingActionButton>(find.byType(FloatingActionButton)).tooltip,
'Track something new',
);
});
});
}