Do not offer the body weight metric type for new categories

This commit is contained in:
Roland Geider
2026-07-30 20:48:29 +02:00
parent 488338d01e
commit cc22ac68a4
3 changed files with 36 additions and 4 deletions

View File

@@ -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 {

View File

@@ -96,11 +96,12 @@ class _MeasurementCategoryFormState extends ConsumerState<MeasurementCategoryFor
},
),
// metricType
// metricType. Official types are reserved for the categories the server manages
DropdownButtonFormField(
initialValue: _draft.metricType,
decoration: InputDecoration(labelText: AppLocalizations.of(context).metricType),
items: MetricType.values
.where((t) => !t.isOfficial || t == _draft.metricType)
.map((t) => DropdownMenuItem(value: t, child: Text(t.localized(context))))
.toList(),
onChanged: (value) {

View File

@@ -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<MetricType>),
findsNWidgets(MetricType.values.length + 1),
findsNWidgets(MetricType.values.length),
);
expect(
find.byWidgetPredicate(
(w) => w is DropdownMenuItem<MetricType> && 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<MetricType> && w.value == MetricType.bodyWeight,
),
findsOneWidget,
);
});