diff --git a/lib/core/database.dart b/lib/core/database.dart index 307f77d..ba29b7f 100644 --- a/lib/core/database.dart +++ b/lib/core/database.dart @@ -137,7 +137,7 @@ class DatabaseHelper { Future isNewHighScore(String exerciseName, double newWeight, int newReps) async { final db = await database; - + // Query to get the current highest weight and corresponding reps for that weight final List> results = await db.rawQuery( ''' @@ -154,10 +154,9 @@ class DatabaseHelper { final maxWeight = double.parse(row['weight'].toString()); final maxReps = row['reps'] as int; - if (newWeight > maxWeight) { - return true; // New weight is higher - } else if (newWeight == maxWeight && newReps > maxReps) { - return true; // Same weight but more reps + // Check if both weight and reps are greater than the current record + if (newWeight > maxWeight || (newWeight == maxWeight && newReps > maxReps)) { + return true; // New record found } } @@ -248,40 +247,69 @@ class DatabaseHelper { Future>> getMaxWeightsForExercises() async { final db = await database; + + // Query to get the maximum weight and corresponding highest reps for each exercise final List> results = await db.rawQuery( - 'SELECT exercise, weight, reps FROM exercises WHERE CAST(weight AS REAL) IN (SELECT MAX(CAST(weight AS REAL)) FROM exercises GROUP BY exercise)' + ''' + SELECT exercise, weight, reps + FROM exercises + WHERE (exercise, CAST(weight AS REAL)) IN ( + SELECT exercise, MAX(CAST(weight AS REAL)) + FROM exercises + GROUP BY exercise + ) + ORDER BY exercise, CAST(weight AS REAL) DESC, reps DESC + ''' ); Map> maxWeights = {}; for (var result in results) { - maxWeights[result['exercise']] = { - 'maxWeight': double.parse(result['weight']), - 'reps': result['reps'] - }; + final exercise = result['exercise'] as String; + final weight = double.parse(result['weight']); + final reps = result['reps'] as int; + + if (maxWeights.containsKey(exercise)) { + final existing = maxWeights[exercise]!; + if (weight == existing['maxWeight']) { + if (reps > existing['reps']) { + existing['reps'] = reps; + } + } else if (weight > existing['maxWeight']) { + existing['maxWeight'] = weight; + existing['reps'] = reps; + } + } else { + maxWeights[exercise] = { + 'maxWeight': weight, + 'reps': reps, + }; + } } + return maxWeights; } -Future?> getLastLoggedExercise(String exerciseName) async { - final db = await database; - final List> result = await db.query( - 'exercises', - where: 'exercise = ?', - whereArgs: [exerciseName], - orderBy: 'timestamp DESC', - limit: 1, // Fetch only the latest entry - ); - - if (result.isNotEmpty) { - final row = result.first; - return { - 'exercise': row['exercise'], - 'weight': double.tryParse(row['weight']) ?? 0.0, // Convert weight to double - 'reps': row['reps'] as int, - 'sets': row['sets'] as int, - }; + + Future?> getLastLoggedExercise(String exerciseName) async { + final db = await database; + final List> result = await db.query( + 'exercises', + where: 'exercise = ?', + whereArgs: [exerciseName], + orderBy: 'timestamp DESC', + limit: 1, // Fetch only the latest entry + ); + + if (result.isNotEmpty) { + final row = result.first; + return { + 'exercise': row['exercise'], + 'weight': double.tryParse(row['weight']) ?? 0.0, // Convert weight to double + 'reps': row['reps'] as int, + 'sets': row['sets'] as int, + }; + } + return null; } - return null; -} } diff --git a/pubspec.yaml b/pubspec.yaml index 6fd47e9..35fac1d 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -16,7 +16,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev # https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html # In Windows, build-name is used as the major, minor, and patch parts # of the product and file versions while build-number is used as the build suffix. -version: 0.2.4+7 +version: 0.2.5+8 environment: sdk: '>=3.4.3 <4.0.0'