diff --git a/lib/core/weight_utils.dart b/lib/core/weight_utils.dart index 2d2bbca..3bac9e1 100644 --- a/lib/core/weight_utils.dart +++ b/lib/core/weight_utils.dart @@ -1,19 +1,24 @@ -import 'package:flutter/material.dart'; - class WeightUtils { + // Conversion constants + static const double kgToLbRatio = 2.20462; + + // Converts kg to lbs static double kgToLbs(double kg) { - return kg * 2.20462; + return kg * kgToLbRatio; } + // Converts lbs to kg static double lbsToKg(double lbs) { - return lbs / 2.20462; + return lbs / kgToLbRatio; } - static String formatWeight(double weight, bool isKg) { + // Formats the weight according to the selected unit system (kg or lbs) + static String formatWeight(double weightInKg, bool isKg) { if (isKg) { - return '${weight.toStringAsFixed(2)} kg'; + return weightInKg.toStringAsFixed(1); // 1 decimal place for kg } else { - return '${kgToLbs(weight).toStringAsFixed(2)} lbs'; + return kgToLbs(weightInKg) + .toStringAsFixed(1); // Convert to lbs with 1 decimal place } } } diff --git a/lib/main.dart b/lib/main.dart index b4e215b..25a126e 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,6 +1,6 @@ import 'package:flutter/material.dart'; import 'tabs/home.dart'; -import 'core/theme.dart'; // Import your AppThemes +import 'core/theme.dart'; void main() { runApp(const MyApp()); diff --git a/lib/tabs/home.dart b/lib/tabs/home.dart index 2f3f845..5fc4429 100644 --- a/lib/tabs/home.dart +++ b/lib/tabs/home.dart @@ -1,10 +1,8 @@ import 'package:flutter/material.dart'; -import '../core/theme.dart'; // Import your AppThemes -import '../core/database.dart'; +import '../core/theme.dart'; import 'visualization.dart'; import 'inputs.dart'; import 'summary.dart'; -import 'data.dart'; import '../widgets/settings.dart'; import '../widgets/table.dart'; import '../widgets/records.dart'; @@ -83,7 +81,7 @@ class _ExerciseStoreHomePageState extends State _refreshTable(); } }); - _pageController = PageController(initialPage: 1); + _pageController = PageController(initialPage: 2); } @override diff --git a/lib/tabs/inputs.dart b/lib/tabs/inputs.dart index b7be801..64334c8 100644 --- a/lib/tabs/inputs.dart +++ b/lib/tabs/inputs.dart @@ -25,6 +25,7 @@ class _ExerciseSetterState extends State { double? _lastWeight; int? _lastReps; int? _lastSets; + bool _isLbs = false; // New state variable to track weight unit List _predefinedExercises = []; @@ -56,7 +57,9 @@ class _ExerciseSetterState extends State { _lastWeight = lastLogged['weight']; _lastReps = lastLogged['reps']; _lastSets = lastLogged['sets']; - _weightController.text = _lastWeight?.toString() ?? ''; + _weightController.text = _isLbs + ? _convertKgToLbs(_lastWeight ?? 0).toStringAsFixed(2) + : _lastWeight?.toString() ?? ''; _repsController.text = _lastReps?.toString() ?? ''; _setsController.text = _lastSets?.toString() ?? '1'; }); @@ -64,16 +67,25 @@ class _ExerciseSetterState extends State { } } + double _convertKgToLbs(double kg) { + return kg * 2.20462; + } + + double _convertLbsToKg(double lbs) { + return lbs / 2.20462; + } + Future _addOrUpdateExercise() async { - // Manually trigger form validation if (_formKey.currentState!.validate()) { final exerciseName = _isAddingNewExercise ? _newExerciseController.text.trim() : _selectedExercise!; - // Convert values to appropriate types - final weight = - double.tryParse(_weightController.text.replaceAll(',', '.')); + final weight = _isLbs + ? _convertLbsToKg( + double.tryParse(_weightController.text.replaceAll(',', '.')) ?? 0) + : double.tryParse(_weightController.text.replaceAll(',', '.')); + final reps = int.tryParse(_repsController.text); final sets = int.tryParse(_setsController.text); @@ -86,12 +98,15 @@ class _ExerciseSetterState extends State { return; } + // Round weight to two decimal place before saving + final roundedWeight = double.parse(weight.toStringAsFixed(2)); + final isNewHighScore = - await _dbHelper.isNewHighScore(exerciseName, weight, reps); + await _dbHelper.isNewHighScore(exerciseName, roundedWeight, reps); await _dbHelper.insertExercise( exercise: exerciseName, - weight: weight.toString(), + weight: roundedWeight.toString(), reps: reps, sets: sets, ); @@ -380,36 +395,66 @@ class _ExerciseSetterState extends State { }, ), ), - GestureDetector( - onTap: () { - _showNumberInputSheet( - controller: _weightController, - label: 'Weight', - initialValue: _weightController.text, - isDouble: true, - ); - }, - child: AbsorbPointer( - child: TextFormField( - controller: _weightController, - decoration: const InputDecoration(labelText: 'Weight'), - keyboardType: TextInputType.numberWithOptions(decimal: true), - inputFormatters: [ - FilteringTextInputFormatter.allow( - RegExp(r'^[\d,.]+$'), + Row( + children: [ + Expanded( + flex: 4, + child: GestureDetector( + onTap: () { + _showNumberInputSheet( + controller: _weightController, + label: 'Weight', + initialValue: _weightController.text, + isDouble: true, + ); + }, + child: AbsorbPointer( + child: TextFormField( + controller: _weightController, + decoration: const InputDecoration(labelText: 'Weight'), + keyboardType: + TextInputType.numberWithOptions(decimal: true), + inputFormatters: [ + FilteringTextInputFormatter.allow( + RegExp(r'^[\d,.]+$'), + ), + ], + validator: (value) { + if (value == null || value.isEmpty) { + return 'Please enter the exercise weight'; + } + if (double.tryParse(value.replaceAll(',', '.')) == + null) { + return 'Please enter a valid number'; + } + return null; + }, + ), ), - ], - validator: (value) { - if (value == null || value.isEmpty) { - return 'Please enter the exercise weight'; - } - if (double.tryParse(value.replaceAll(',', '.')) == null) { - return 'Please enter a valid number'; - } - return null; + ), + ), + TextButton( + onPressed: () { + setState(() { + _isLbs = !_isLbs; + final currentWeight = double.tryParse( + _weightController.text.replaceAll(',', '.')); + if (currentWeight != null) { + _weightController.text = _isLbs + ? _convertKgToLbs(currentWeight).toStringAsFixed(1) + : _convertLbsToKg(currentWeight).toStringAsFixed(1); + } + }); }, + child: Text( + _isLbs ? 'lbs' : 'kg', + style: TextStyle( + color: theme.primaryColor, + fontSize: 16, + ), + ), ), - ), + ], ), GestureDetector( onTap: () { diff --git a/lib/widgets/settings.dart b/lib/widgets/settings.dart index c771622..f7dd4db 100644 --- a/lib/widgets/settings.dart +++ b/lib/widgets/settings.dart @@ -1,7 +1,7 @@ import 'package:flutter/material.dart'; import 'package:package_info_plus/package_info_plus.dart'; import '../core/database.dart'; -import '../core/theme.dart'; // Make sure to import your AppThemes +import '../core/theme.dart'; class SettingsModal extends StatefulWidget { final AppTheme appTheme; @@ -201,20 +201,22 @@ class _SettingsModalState extends State { // Unit selection ListTile( title: Text( - 'Use kg (or lbs)', + 'Unit', style: Theme.of(context) .textTheme .bodyMedium ?.copyWith(fontSize: 14), ), - trailing: Transform.scale( - scale: 0.8, // Adjust the scale to resize the switch - child: Switch( - value: _isKg, - onChanged: _handleUnitChange, - activeColor: Theme.of(context).colorScheme.primary, - inactiveTrackColor: - Theme.of(context).colorScheme.onSurface.withOpacity(0.38), + trailing: TextButton( + onPressed: () { + _handleUnitChange(!_isKg); + }, + child: Text( + _isKg ? 'kg' : 'lbs', + style: TextStyle( + color: Theme.of(context).primaryColor, + fontSize: 16, + ), ), ), ),