Show the weight form bounds rounded inwards

This commit is contained in:
Roland Geider
2026-07-30 21:30:16 +02:00
parent ea8c2a851a
commit 1b87fd2120
2 changed files with 62 additions and 3 deletions

View File

@@ -202,10 +202,11 @@ class _WeightFormState extends riverpod.ConsumerState<WeightForm> {
return i18n.enterValidNumber;
}
if (!_isInRange(parsed)) {
// The bounds are defined in kg, show them in the entered unit
// The bounds are defined in kg; show them in the entered unit,
// rounded inwards so every displayed bound is itself accepted
return i18n.formMinMaxValues(
convertWeight(_minWeightKg, from: 'kg', to: _unit).round(),
convertWeight(_maxWeightKg, from: 'kg', to: _unit).round(),
convertWeight(_minWeightKg, from: 'kg', to: _unit).ceil(),
convertWeight(_maxWeightKg, from: 'kg', to: _unit).floor(),
);
}
return null;

View File

@@ -203,6 +203,64 @@ void main() {
expect(saved.extraData, {'unit': 'lb'});
});
testWidgets('Accepts imperial weights over the 300 kg display bound', (
WidgetTester tester,
) async {
when(
mockProfileRepo.watchDrift(),
).thenAnswer((_) => Stream.value(UserProfile(id: 1, weightUnitStr: 'lb')));
final container = ProviderContainer.test(
overrides: [
measurementRepositoryProvider.overrideWithValue(mockRepo),
userProfileRepositoryProvider.overrideWithValue(mockProfileRepo),
],
);
container.listen(userProfileProvider, (_, _) {});
await tester.runAsync(pumpEventQueue);
await tester.pumpWidget(
UncontrolledProviderScope(
container: container,
child: MaterialApp(
localizationsDelegates: AppLocalizations.localizationsDelegates,
supportedLocales: AppLocalizations.supportedLocales,
home: Scaffold(
body: SingleChildScrollView(
child: WeightForm(getBodyWeightCategory(const [])),
),
),
),
),
);
await tester.pumpAndSettle();
// 365 lb are over the numeric kg bound but a perfectly valid weight
await tester.enterText(find.byKey(const Key('weightInput')), '365');
await tester.tap(find.byKey(const Key(SUBMIT_BUTTON_KEY_NAME)));
await tester.pumpAndSettle();
final saved = verify(mockRepo.addLocalDrift(captureAny)).captured.single as MeasurementEntry;
expect(saved.value, 365);
expect(saved.extraData, {'unit': 'lb'});
});
testWidgets('Rejects implausible weights with bounds in the entered unit', (
WidgetTester tester,
) async {
await tester.pumpWidget(createWeightForm(entry: testWeightEntryLb));
await tester.pumpAndSettle();
// 35 lb are below the plausible minimum of 30 kg
await tester.enterText(find.byKey(const Key('weightInput')), '35');
await tester.tap(find.byKey(const Key(SUBMIT_BUTTON_KEY_NAME)));
await tester.pumpAndSettle();
verifyNever(mockRepo.updateLocalDrift(any));
// Bounds are rounded inwards, so the displayed minimum is acceptable
expect(find.text('Please enter a value between 67 and 661'), findsOneWidget);
});
testWidgets('Editing shows the stored value in its stored unit, unconverted', (
WidgetTester tester,
) async {