Start implementing a plate calculator

This commit is contained in:
Roland Geider
2021-06-13 20:22:15 +02:00
parent 0af6fc2579
commit b88c3290c1
4 changed files with 152 additions and 26 deletions

View File

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

48
lib/helpers/gym_mode.dart Normal file
View File

@@ -0,0 +1,48 @@
/*
* This file is part of wger Workout Manager <https://github.com/wger-project>.
* 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 <http://www.gnu.org/licenses/>.
*/
/// Calculates the number of plates needed to reach a specific weight
List<num> plateCalculator(num totalWeight, num barWeight, List<num> plates) {
List<num> 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;
}

View File

@@ -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<LogPage> {
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<LogPage> {
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<LogPage> {
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<LogPage> {
);
}
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<LogPage> {
? 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<num> calcPlates(num totalWeight, num barWeight, List<num> plates){
List<num> 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(

View File

@@ -0,0 +1,47 @@
/*
* This file is part of wger Workout Manager <https://github.com/wger-project>.
* 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 <http://www.gnu.org/licenses/>.
*/
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',
);
});
});
}