diff --git a/lib/helpers/gym_mode.dart b/lib/helpers/gym_mode.dart index 3a4e7d6a..b8716817 100644 --- a/lib/helpers/gym_mode.dart +++ b/lib/helpers/gym_mode.dart @@ -44,3 +44,19 @@ List plateCalculator(num totalWeight, num barWeight, List plates) { return ans; } + +/// Groups a list of plates as calculated by [plateCalculator] +/// +/// e.g. [15, 15, 15, 10, 10, 5] returns {15: 3, 10: 2, 5: 1} +Map groupPlates(List plates) { + Map out = {}; + for (var plate in plates) { + if (!out.containsKey(plate)) { + out[plate] = 1; + } else { + out[plate] = out[plate]! + 1; + } + } + + return out; +} diff --git a/lib/widgets/workouts/gym_mode.dart b/lib/widgets/workouts/gym_mode.dart index 08156021..6c40bf2a 100644 --- a/lib/widgets/workouts/gym_mode.dart +++ b/lib/widgets/workouts/gym_mode.dart @@ -523,6 +523,7 @@ class _LogPageState extends State { BAR_WEIGHT, AVAILABLE_PLATES, ); + final groupedPlates = groupPlates(plates); return Column( children: [ @@ -531,32 +532,39 @@ class _LogPageState extends State { style: Theme.of(context).textTheme.headline6, ), SizedBox( - height: 40, + height: 35, child: plates.length > 0 ? Row( mainAxisAlignment: MainAxisAlignment.center, children: [ - ...plates + ...groupedPlates.keys .map( - (e) => Container( - decoration: BoxDecoration( - color: wgerPrimaryColorLight, - shape: BoxShape.circle, - ), - child: Padding( - padding: const EdgeInsets.symmetric(horizontal: 5), - child: SizedBox( - height: 35, - width: 35, - child: Align( - alignment: Alignment.center, - child: Text( - e.toString(), - style: Theme.of(context).textTheme.headline6, + (key) => Row( + children: [ + Text(groupedPlates[key].toString()), + Text('×'), + Container( + decoration: BoxDecoration( + color: wgerPrimaryColorLight, + shape: BoxShape.circle, + ), + child: Padding( + padding: const EdgeInsets.symmetric(horizontal: 3), + child: SizedBox( + height: 35, + width: 35, + child: Align( + alignment: Alignment.center, + child: Text( + key.toString(), + style: TextStyle(fontWeight: FontWeight.bold), + ), + ), ), ), ), - ), + SizedBox(width: 10), + ], ), ) .toList() @@ -564,6 +572,7 @@ class _LogPageState extends State { ) : MutedText(AppLocalizations.of(context).plateCalculatorNotDivisible), ), + SizedBox(height: 3), ], ); } diff --git a/test/plate_calculator_test.dart b/test/plate_calculator_test.dart index 6a946a51..e1150406 100644 --- a/test/plate_calculator_test.dart +++ b/test/plate_calculator_test.dart @@ -45,4 +45,12 @@ void main() { ); }); }); + + group('Test the plate calculator group', () { + test('Test groups', () async { + expect(groupPlates([15, 15, 15, 10, 10, 5]), {15: 3, 10: 2, 5: 1}); + expect(groupPlates([15, 10, 5, 1.25]), {15: 1, 10: 1, 5: 1, 1.25: 1}); + expect(groupPlates([10, 10, 10, 10, 10, 10, 10]), {10: 7}); + }); + }); }