Skip to content

Offline gym log for Android. No account, no ads, no internet.

Keep typed weights exact (#314)

InlitX4b0fc04fbd71
diff --git a/lib/widgets/ruler_picker.dart b/lib/widgets/ruler_picker.dart
index 0a0281e..857c155 100644
--- a/lib/widgets/ruler_picker.dart
+++ b/lib/widgets/ruler_picker.dart
@@ -60,7 +60,10 @@ class _RulerPickerState extends State<RulerPicker> {
final wanted = _indexOf(widget.value);
if (wanted != _index && _scroll.hasClients && !_scroll.position.isScrollingNotifier.value) {
_index = wanted;
- _scroll.animateTo(wanted * _gap, duration: const Duration(milliseconds: 260), curve: Curves.easeOutCubic);
+ _quiet = true;
+ _scroll
+ .animateTo(wanted * _gap, duration: const Duration(milliseconds: 260), curve: Curves.easeOutCubic)
+ .whenComplete(() => _quiet = false);
}
}
@@ -70,7 +73,11 @@ class _RulerPickerState extends State<RulerPicker> {
super.dispose();
}
+ bool _quiet = false;
+
bool _onScroll(ScrollNotification n) {
+ if (n is ScrollStartNotification && n.dragDetails != null) _quiet = false;
+ if (_quiet) return false;
if (n is ScrollUpdateNotification || n is ScrollEndNotification) {
final i = (_scroll.offset / _gap).round().clamp(0, _count - 1);
if (i != _index) {
@@ -258,7 +265,15 @@ Future<double?> askRuler(
}) {
var current = value.clamp(min, max).toDouble();
final decimals = step == step.roundToDouble() ? 0 : (step < 0.1 || step * 10 != (step * 10).roundToDouble() ? 2 : 1);
- String show(double v) => format != null ? format(v) : decimalText(v.toStringAsFixed(decimals));
+ String show(double v) {
+ if (format != null) return format(v);
+ final exact = (v * 100).round() / 100;
+ final onGrid = ((exact / step).round() * step - exact).abs() < 0.001;
+ if (onGrid) return decimalText(exact.toStringAsFixed(decimals));
+ var text = exact.toStringAsFixed(2);
+ if (text.endsWith('0')) text = text.substring(0, text.length - 1);
+ return decimalText(text);
+ }
return showAppSheet<double>(
context: context,
isScrollControlled: true,
@@ -311,7 +326,7 @@ Future<double?> askRuler(
final typed = await askNumber(sheet,
title: title, initial: show(current), decimal: step != step.roundToDouble());
if (typed == null) return;
- setSheet(() => current = ((typed.clamp(min, max) / step).round() * step).toDouble());
+ setSheet(() => current = (typed.clamp(min, max) * 100).round() / 100);
},
child: Padding(
padding: const EdgeInsets.all(8),
diff --git a/lib/widgets/weight_stack_sheet.dart b/lib/widgets/weight_stack_sheet.dart
index 6b517aa..f3d9ad8 100644
--- a/lib/widgets/weight_stack_sheet.dart
+++ b/lib/widgets/weight_stack_sheet.dart
@@ -27,7 +27,7 @@ class _WeightStackSheet extends StatefulWidget {
}
class _WeightStackSheetState extends State<_WeightStackSheet> {
- late List<double> _kg = [...?fit.stackFor(widget.id)];
+ late List<double> _kg = [...?fit.ownStackFor(widget.id)];
late final _from = TextEditingController(text: _kg.isEmpty ? _seed(5) : _shown(_kg.first));
late final _to = TextEditingController(text: _kg.isEmpty ? _seed(100) : _shown(_kg.last));
late final _step = TextEditingController(text: _kg.length < 2 ? _seed(5) : _shown(_kg[1] - _kg[0]));
@@ -53,7 +53,7 @@ class _WeightStackSheetState extends State<_WeightStackSheet> {
void _save(List<double> next) {
fit.setWeightStack(widget.id, next);
- setState(() => _kg = [...?fit.stackFor(widget.id)]);
+ setState(() => _kg = [...?fit.ownStackFor(widget.id)]);
}
void _fill() {
diff --git a/test/weights_test.dart b/test/weights_test.dart
index 3ba7761..6b7bf61 100644
--- a/test/weights_test.dart
+++ b/test/weights_test.dart
@@ -1,7 +1,11 @@
+import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
+import 'package:phosphoricons_flutter/phosphoricons_flutter.dart';
import 'package:gymmane/l10n/l10n.dart';
import 'package:gymmane/services/local_store.dart';
import 'package:gymmane/state/fit_state.dart';
+import 'package:gymmane/theme/app_theme.dart';
+import 'package:gymmane/widgets/ruler_picker.dart';
import 'package:shared_preferences/shared_preferences.dart';
void main() {
@@ -223,4 +227,33 @@ void main() {
expect(fit.stackFor(id), isNull);
});
});
+
+ testWidgets('#314 un peso escrito a mano como 37,4 se guarda tal cual, sin saltar a 37,5', (tester) async {
+ setAppLanguage('en');
+ double? picked;
+ await tester.pumpWidget(MaterialApp(
+ theme: AppTheme.dark,
+ home: Builder(
+ builder: (context) => Scaffold(
+ body: Center(
+ child: TextButton(
+ onPressed: () async => picked = await askRuler(context,
+ title: 'Weight', value: 30, min: 0, max: 700, step: 0.5, unit: 'kg'),
+ child: const Text('open'),
+ ),
+ ),
+ ),
+ ),
+ ));
+ await tester.tap(find.text('open'));
+ await tester.pumpAndSettle();
+ await tester.tap(find.byIcon(PhosphorIconsRegular.keyboard));
+ await tester.pumpAndSettle();
+ await tester.enterText(find.byType(TextField), '37.4');
+ await tester.tap(find.text(t.set).last);
+ await tester.pumpAndSettle();
+ await tester.tap(find.text(t.set).first);
+ await tester.pumpAndSettle();
+ expect(picked, 37.4);
+ });
}