Skip to content

Commit

Permalink
Merge pull request #106 from andreped:97-bug-record-using-latest-reps…
Browse files Browse the repository at this point in the history
…-and-not-max-in-criterion

Fixed bug to allow records to use both weight and sets
  • Loading branch information
andreped authored Aug 9, 2024
2 parents a797f3a + 999b704 commit 493111f
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 31 deletions.
88 changes: 58 additions & 30 deletions lib/core/database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ class DatabaseHelper {

Future<bool> 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<Map<String, dynamic>> results = await db.rawQuery(
'''
Expand All @@ -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
}
}

Expand Down Expand Up @@ -248,40 +247,69 @@ class DatabaseHelper {

Future<Map<String, Map<String, dynamic>>> getMaxWeightsForExercises() async {
final db = await database;

// Query to get the maximum weight and corresponding highest reps for each exercise
final List<Map<String, dynamic>> 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<String, Map<String, dynamic>> 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<Map<String, dynamic>?> getLastLoggedExercise(String exerciseName) async {
final db = await database;
final List<Map<String, dynamic>> 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<Map<String, dynamic>?> getLastLoggedExercise(String exerciseName) async {
final db = await database;
final List<Map<String, dynamic>> 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;
}

}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit 493111f

Please sign in to comment.