From b88c3290c1e81ea77ecb6e711b63cf9d44eddb48 Mon Sep 17 00:00:00 2001 From: Roland Geider Date: Sun, 13 Jun 2021 20:22:15 +0200 Subject: [PATCH] Start implementing a plate calculator --- lib/helpers/consts.dart | 6 +++ lib/helpers/gym_mode.dart | 48 +++++++++++++++++++ lib/widgets/workouts/gym_mode.dart | 77 ++++++++++++++++++++---------- test/plate_calculator_test.dart | 47 ++++++++++++++++++ 4 files changed, 152 insertions(+), 26 deletions(-) create mode 100644 lib/helpers/gym_mode.dart create mode 100644 test/plate_calculator_test.dart diff --git a/lib/helpers/consts.dart b/lib/helpers/consts.dart index 8056f8dc7..2bc41c599 100644 --- a/lib/helpers/consts.dart +++ b/lib/helpers/consts.dart @@ -51,3 +51,9 @@ const DEFAULT_ANIMATION_CURVE = Curves.bounceIn; /// regular date object or date.toLocal() can cause problems, depending on the /// system's settings. Using a string is safer. final DateFormatLists = DateFormat('yyyy-MM-dd'); + +/// Available plate weights, used for the plate calculator +const AVAILABLE_PLATES = [1.25, 2.5, 5, 10, 15]; + +/// Weight of the bar, used in the plate calculator +const BAR_WEIGHT = 20; diff --git a/lib/helpers/gym_mode.dart b/lib/helpers/gym_mode.dart new file mode 100644 index 000000000..371a936ac --- /dev/null +++ b/lib/helpers/gym_mode.dart @@ -0,0 +1,48 @@ +/* + * This file is part of wger Workout Manager . + * Copyright (C) 2020, 2021 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * wger Workout Manager is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +/// Calculates the number of plates needed to reach a specific weight +List plateCalculator(num totalWeight, num barWeight, List plates) { + List ans = []; + var platesCount = plates.length; + + // Weight is less than the bar + if (totalWeight < barWeight) { + return []; + } + + // Weight can't be divided with the smallest plate + if (totalWeight % plates.first > 0) { + return []; + } + + // Remove the bar and divide by two to get weight on each side + totalWeight = (totalWeight - barWeight) / 2; + + // Find the plates + for (int i = (platesCount - 1); i > 0; i--) { + var plate = plates[i]; + + while (totalWeight >= plate) { + totalWeight -= plate; + ans.add(plate); + } + } + + return ans; +} diff --git a/lib/widgets/workouts/gym_mode.dart b/lib/widgets/workouts/gym_mode.dart index 8df254216..a8ea45ccc 100644 --- a/lib/widgets/workouts/gym_mode.dart +++ b/lib/widgets/workouts/gym_mode.dart @@ -24,6 +24,7 @@ import 'package:flutter_html/flutter_html.dart'; import 'package:intl/intl.dart'; import 'package:provider/provider.dart'; import 'package:wger/helpers/consts.dart'; +import 'package:wger/helpers/gym_mode.dart'; import 'package:wger/helpers/json.dart'; import 'package:wger/helpers/ui.dart'; import 'package:wger/models/exercises/exercise.dart'; @@ -333,7 +334,10 @@ class _LogPageState extends State { onPressed: () { try { double newValue = double.parse(_weightController.text) + 1.25; - _weightController.text = newValue.toString(); + setState(() { + widget._log.weight = newValue; + _weightController.text = newValue.toString(); + }); } on FormatException catch (e) {} }, ), @@ -346,7 +350,10 @@ class _LogPageState extends State { try { double newValue = double.parse(_weightController.text) - 1.25; if (newValue > 0) { - _weightController.text = newValue.toString(); + setState(() { + widget._log.weight = newValue; + _weightController.text = newValue.toString(); + }); } } on FormatException catch (e) {} }, @@ -355,8 +362,18 @@ class _LogPageState extends State { controller: _weightController, keyboardType: TextInputType.number, onFieldSubmitted: (_) {}, + onChanged: (value) { + try { + double.parse(value); + setState(() { + widget._log.weight = double.parse(value); + }); + } on FormatException catch (e) {} + }, onSaved: (newValue) { - widget._log.weight = double.parse(newValue!); + setState(() { + widget._log.weight = double.parse(newValue!); + }); }, validator: (value) { try { @@ -487,6 +504,36 @@ class _LogPageState extends State { ); } + Widget getPlates() { + //final plates = []; + final plates = plateCalculator( + double.parse(_weightController.text), + BAR_WEIGHT, + AVAILABLE_PLATES, + ); + + return Column( + children: [ + Text( + 'Plate calculator', + style: Theme.of(context).textTheme.headline6, + ), + Row( + mainAxisSize: MainAxisSize.max, + mainAxisAlignment: MainAxisAlignment.spaceAround, + children: [ + ...plates + .map((e) => Text( + e.toString(), + style: Theme.of(context).textTheme.headline6, + )) + .toList() + ], + ), + ], + ); + } + @override Widget build(BuildContext context) { return Column( @@ -509,6 +556,7 @@ class _LogPageState extends State { ? getPastLogs() : Container()), SizedBox(height: 15), + getPlates(), Padding( padding: const EdgeInsets.symmetric(horizontal: 15), child: Card( @@ -535,29 +583,6 @@ class ExerciseOverview extends StatelessWidget { this._exercisePages, ); - List calcPlates(num totalWeight, num barWeight, List plates){ - List ans = []; - var platesCount = plates.length; - - if (totalWeight % 0.5 > 0){ - return []; - } - - // Remove the bar and divide by two to get weight on each side - totalWeight = (totalWeight - barWeight) / 2; - - for(int i = (platesCount - 1); i > 0; i--){ - var plate = plates[i]; - - if(plate <= totalWeight){ - totalWeight -= plate; - ans.add(plate); - } - } - - return ans; - } - @override Widget build(BuildContext context) { return Column( diff --git a/test/plate_calculator_test.dart b/test/plate_calculator_test.dart new file mode 100644 index 000000000..34b4a6144 --- /dev/null +++ b/test/plate_calculator_test.dart @@ -0,0 +1,47 @@ +/* + * This file is part of wger Workout Manager . + * Copyright (C) 2020, 2021 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * wger Workout Manager is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import 'package:flutter_test/flutter_test.dart'; +import 'package:wger/helpers/consts.dart'; +import 'package:wger/helpers/gym_mode.dart'; + +void main() { + group('Test the plate calculator', () { + test('Regular weights', () async { + expect(plateCalculator(40, BAR_WEIGHT, AVAILABLE_PLATES), [10]); + expect(plateCalculator(100, BAR_WEIGHT, AVAILABLE_PLATES), [15, 15, 10]); + expect(plateCalculator(140, BAR_WEIGHT, AVAILABLE_PLATES), [15, 15, 15, 15]); + expect(plateCalculator(45, BAR_WEIGHT, AVAILABLE_PLATES), [10, 2.5]); + expect(plateCalculator(85, BAR_WEIGHT, AVAILABLE_PLATES), [15, 15, 2.5]); + }); + + test('Exceptions', () async { + expect( + plateCalculator(10, BAR_WEIGHT, AVAILABLE_PLATES), + [], + reason: 'Weight is less than the bar', + ); + + expect( + plateCalculator(101, BAR_WEIGHT, AVAILABLE_PLATES), + [], + reason: 'Weight cant be achieved with plates', + ); + }); + }); +}