diff --git a/lib/features/measurements/models/measurement_category.dart b/lib/features/measurements/models/measurement_category.dart index a767208f5..b5ab1278d 100644 --- a/lib/features/measurements/models/measurement_category.dart +++ b/lib/features/measurements/models/measurement_category.dart @@ -56,6 +56,13 @@ enum MetricType { MetricType.steps || MetricType.distance || MetricType.energy || MetricType.sleep => true, _ => false, }; + + /// `true` for metric types that are reserved for the official categories + /// the server manages: users cannot create categories of these types. + bool get isOfficial => switch (this) { + MetricType.bodyWeight => true, + _ => false, + }; } extension MeasurementMetricTypeL10n on MetricType { diff --git a/lib/features/measurements/widgets/forms.dart b/lib/features/measurements/widgets/forms.dart index b8fc6a2c3..dfa7e6fec 100644 --- a/lib/features/measurements/widgets/forms.dart +++ b/lib/features/measurements/widgets/forms.dart @@ -96,11 +96,12 @@ class _MeasurementCategoryFormState extends ConsumerState !t.isOfficial || t == _draft.metricType) .map((t) => DropdownMenuItem(value: t, child: Text(t.localized(context)))) .toList(), onChanged: (value) { diff --git a/test/features/measurements/widgets/forms_test.dart b/test/features/measurements/widgets/forms_test.dart index 60752f97f..c0abcaea5 100644 --- a/test/features/measurements/widgets/forms_test.dart +++ b/test/features/measurements/widgets/forms_test.dart @@ -88,7 +88,9 @@ void main() { expect(find.byType(MeasurementCategoryForm), findsOneWidget); }); - testWidgets('metricType dropdown renders all MetricType values', (tester) async { + testWidgets('metricType dropdown offers all MetricType values except body weight', ( + tester, + ) async { await tester.pumpWidget(wrap(const MeasurementCategoryForm())); await tester.pumpAndSettle(); @@ -97,10 +99,32 @@ void main() { await tester.pumpAndSettle(); // The initialValue stays visible on the form behind the opened menu - // overlay, which shows every MetricType option once. + // overlay, which shows every offered MetricType option once. Body + // weight is reserved for the official category and not offered. expect( find.byType(DropdownMenuItem), - findsNWidgets(MetricType.values.length + 1), + findsNWidgets(MetricType.values.length), + ); + expect( + find.byWidgetPredicate( + (w) => w is DropdownMenuItem && w.value == MetricType.bodyWeight, + ), + findsNothing, + ); + }); + + testWidgets('editing a category with the body weight type keeps its value', (tester) async { + final lookalike = getMeasurementCategories()[1].copyWith(metricType: MetricType.bodyWeight); + + await tester.pumpWidget(wrap(MeasurementCategoryForm(lookalike))); + await tester.pumpAndSettle(); + + // The dropdown must contain the pre-existing value or the form crashes + expect( + find.byWidgetPredicate( + (w) => w is DropdownMenuItem && w.value == MetricType.bodyWeight, + ), + findsOneWidget, ); });