Skip to content

Commit

Permalink
Added support for setting weight type when recording exercises; fixed…
Browse files Browse the repository at this point in the history
… bug with wrong tab showing by default (#200)

* Removed old comment in imports

* Updated weight utils script

* Implemented kg/lbs conversion handling in inputs

* Fix icons showing in toggle

* Round to two decimals when saving

* Fix bug where wrong tab was showing by default

* Add new kg/lbs icons to settings modal

* Linted code

* Removed old comment
  • Loading branch information
andreped authored Aug 19, 2024
1 parent 29cc290 commit f00d5fc
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 56 deletions.
19 changes: 12 additions & 7 deletions lib/core/weight_utils.dart
Original file line number Diff line number Diff line change
@@ -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
}
}
}
2 changes: 1 addition & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
@@ -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());
Expand Down
6 changes: 2 additions & 4 deletions lib/tabs/home.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -83,7 +81,7 @@ class _ExerciseStoreHomePageState extends State<ExerciseStoreHomePage>
_refreshTable();
}
});
_pageController = PageController(initialPage: 1);
_pageController = PageController(initialPage: 2);
}

@override
Expand Down
113 changes: 79 additions & 34 deletions lib/tabs/inputs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class _ExerciseSetterState extends State<ExerciseSetter> {
double? _lastWeight;
int? _lastReps;
int? _lastSets;
bool _isLbs = false; // New state variable to track weight unit

List<String> _predefinedExercises = [];

Expand Down Expand Up @@ -56,24 +57,35 @@ class _ExerciseSetterState extends State<ExerciseSetter> {
_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';
});
}
}
}

double _convertKgToLbs(double kg) {
return kg * 2.20462;
}

double _convertLbsToKg(double lbs) {
return lbs / 2.20462;
}

Future<void> _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);

Expand All @@ -86,12 +98,15 @@ class _ExerciseSetterState extends State<ExerciseSetter> {
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,
);
Expand Down Expand Up @@ -380,36 +395,66 @@ class _ExerciseSetterState extends State<ExerciseSetter> {
},
),
),
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: () {
Expand Down
22 changes: 12 additions & 10 deletions lib/widgets/settings.dart
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -201,20 +201,22 @@ class _SettingsModalState extends State<SettingsModal> {
// 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,
),
),
),
),
Expand Down

0 comments on commit f00d5fc

Please sign in to comment.