Skip to content

Commit

Permalink
Fixed bug to allow logging lbs values directly from logging tab (#298)
Browse files Browse the repository at this point in the history
  • Loading branch information
andreped authored Sep 23, 2024
1 parent 53c0eee commit bf57e01
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
1 change: 1 addition & 0 deletions lib/tabs/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ class _ExerciseStoreHomePageState extends State<ExerciseStoreHomePage>
Padding(
padding: const EdgeInsets.all(16.0),
child: ExerciseSetter(
isKg: widget.isKg,
onExerciseAdded: () {
setState(() {});
},
Expand Down
31 changes: 23 additions & 8 deletions lib/tabs/inputs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ import 'package:confetti/confetti.dart';

class ExerciseSetter extends StatefulWidget {
final Function() onExerciseAdded;
final bool isKg;

ExerciseSetter({required this.onExerciseAdded});
ExerciseSetter({
required this.onExerciseAdded,
required this.isKg,
});

@override
_ExerciseSetterState createState() => _ExerciseSetterState();
Expand All @@ -34,7 +38,6 @@ class _ExerciseSetterState extends State<ExerciseSetter> {
double? _lastWeight;
int? _lastReps;
int? _lastSets;
bool _isLbs = false; // State variable to track weight unit

String _selectedLoggingType = 'Exercise'; // Default to 'Exercise'

Expand Down Expand Up @@ -66,6 +69,18 @@ class _ExerciseSetterState extends State<ExerciseSetter> {
});
}

void _updateWeightController() {
if (_weightController.text.isNotEmpty) {
double weight = double.parse(_weightController.text);
if (widget.isKg) {
weight = convertLbsToKg(weight);
} else {
weight = convertKgToLbs(weight);
}
_weightController.text = weight.toStringAsFixed(2);
}
}

Future<void> _loadLastLoggedExercise() async {
if (_selectedLoggingType == 'Exercise' && _selectedExercise != null) {
final lastLogged =
Expand All @@ -78,7 +93,7 @@ class _ExerciseSetterState extends State<ExerciseSetter> {
_lastReps = lastLogged['reps'] ?? 0;
_lastSets = lastLogged['sets'] ?? 1;

_weightController.text = _isLbs
_weightController.text = !widget.isKg
? convertKgToLbs(_lastWeight ?? 0).toStringAsFixed(2)
: _lastWeight?.toString() ?? '';
_repsController.text = _lastReps?.toString() ?? '';
Expand Down Expand Up @@ -130,7 +145,7 @@ class _ExerciseSetterState extends State<ExerciseSetter> {

if (hasFullData) {
// Parse and validate the input fields
final weight = _isLbs
final weight = !widget.isKg
? convertLbsToKg(double.tryParse(
_weightController.text.replaceAll(',', '.')) ??
0)
Expand Down Expand Up @@ -578,15 +593,15 @@ class _ExerciseSetterState extends State<ExerciseSetter> {
GestureDetector(
onTap: () => _showNumberInputSheet(
controller: _weightController,
label: 'Weight (${_isLbs ? 'lbs' : 'kg'})',
label: 'Weight (${widget.isKg ? 'kg' : 'lbs'})',
initialValue: _weightController.text,
isDouble: true,
),
child: AbsorbPointer(
child: TextFormField(
controller: _weightController,
decoration: InputDecoration(
labelText: 'Weight (${_isLbs ? 'lbs' : 'kg'})',
labelText: 'Weight (${widget.isKg ? 'kg' : 'lbs'})',
border: OutlineInputBorder(),
),
validator: (value) {
Expand Down Expand Up @@ -668,15 +683,15 @@ class _ExerciseSetterState extends State<ExerciseSetter> {
GestureDetector(
onTap: () => _showNumberInputSheet(
controller: _userWeightController,
label: 'Weight (${_isLbs ? 'lbs' : 'kg'})',
label: 'Weight (${widget.isKg ? 'kg' : 'lbs'})',
initialValue: _userWeightController.text,
isDouble: true,
),
child: AbsorbPointer(
child: TextFormField(
controller: _userWeightController,
decoration: InputDecoration(
labelText: 'Weight (${_isLbs ? 'lbs' : 'kg'})',
labelText: 'Weight (${widget.isKg ? 'kg' : 'lbs'})',
border: OutlineInputBorder(),
),
validator: (value) {
Expand Down

0 comments on commit bf57e01

Please sign in to comment.