From a7cd3d0790c6c7cbc17451c29b4deafe7ab4fff9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Pedersen?= Date: Fri, 9 Aug 2024 16:34:02 +0200 Subject: [PATCH 1/3] Fix notification to notify on record with correct criterion --- lib/core/database.dart | 49 +++++++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 25 deletions(-) diff --git a/lib/core/database.dart b/lib/core/database.dart index 307f77d..55cdb52 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 } } @@ -262,26 +261,26 @@ class DatabaseHelper { 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; -} } From d47a671321a0a987c2212d5ca3123cc535bf0d8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Pedersen?= Date: Fri, 9 Aug 2024 16:42:30 +0200 Subject: [PATCH 2/3] Fixed bug where sets were not used in records criterion --- lib/core/database.dart | 39 ++++++++++++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 5 deletions(-) diff --git a/lib/core/database.dart b/lib/core/database.dart index 55cdb52..ba29b7f 100644 --- a/lib/core/database.dart +++ b/lib/core/database.dart @@ -247,20 +247,49 @@ 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( From 999b70429468bd529234b942ec09a05de45c6d19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Pedersen?= Date: Fri, 9 Aug 2024 16:43:13 +0200 Subject: [PATCH 3/3] Bump v0.2.5+8 --- pubspec.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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'