Skip to content

Commit

Permalink
Merge pull request #42 from andreped:records-tab
Browse files Browse the repository at this point in the history
Added records tab showing max weight lifted per exercise
  • Loading branch information
andreped authored Aug 2, 2024
2 parents 23cce53 + cf4489d commit 0735568
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
16 changes: 16 additions & 0 deletions lib/core/database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,20 @@ class DatabaseHelper {
conflictAlgorithm: ConflictAlgorithm.ignore, // Handle if exercise already exists
);
}

Future<Map<String, Map<String, dynamic>>> getMaxWeightsForExercises() async {
final db = await database;
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)'
);

Map<String, Map<String, dynamic>> maxWeights = {};
for (var result in results) {
maxWeights[result['exercise']] = {
'maxWeight': double.parse(result['weight']),
'reps': result['reps']
};
}
return maxWeights;
}
}
6 changes: 5 additions & 1 deletion lib/tabs/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'visualization.dart';
import 'inputs.dart';
import '../widgets/exercise_edit_dialog.dart';
import 'summary.dart';
import 'records.dart'; // Import the new records tab

class ExerciseStoreApp extends StatelessWidget {
@override
Expand Down Expand Up @@ -33,7 +34,7 @@ class _ExerciseStoreHomePageState extends State<ExerciseStoreHomePage> with Sing
@override
void initState() {
super.initState();
_tabController = TabController(length: 4, vsync: this);
_tabController = TabController(length: 5, vsync: this); // Update length to 5
}

@override
Expand Down Expand Up @@ -106,6 +107,7 @@ class _ExerciseStoreHomePageState extends State<ExerciseStoreHomePage> with Sing
Tab(icon: Icon(Icons.calendar_today), text: 'Summary'),
Tab(icon: Icon(Icons.show_chart), text: 'Visualize\nData'),
Tab(icon: Icon(Icons.table_chart), text: 'View\nTable'),
Tab(icon: Icon(Icons.record_voice_over), text: 'Records'), // New Records tab
],
),
),
Expand Down Expand Up @@ -192,6 +194,8 @@ class _ExerciseStoreHomePageState extends State<ExerciseStoreHomePage> with Sing
),
),
),
// Records Tab
RecordsTab(), // New Records tab
],
),
),
Expand Down
49 changes: 49 additions & 0 deletions lib/tabs/records.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import 'package:flutter/material.dart';
import '../core/database.dart';

class RecordsTab extends StatefulWidget {
@override
_RecordsTabState createState() => _RecordsTabState();
}

class _RecordsTabState extends State<RecordsTab> {
final DatabaseHelper _dbHelper = DatabaseHelper();

Future<Map<String, Map<String, dynamic>>> _getMaxWeights() async {
return await _dbHelper.getMaxWeightsForExercises();
}

@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(16.0),
child: FutureBuilder<Map<String, Map<String, dynamic>>>(
future: _getMaxWeights(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(child: CircularProgressIndicator());
} else if (snapshot.hasError) {
return Center(child: Text('Error: ${snapshot.error}'));
} else if (!snapshot.hasData || snapshot.data!.isEmpty) {
return const Center(child: Text('No records available'));
}

final maxWeights = snapshot.data!;
return ListView.builder(
itemCount: maxWeights.length,
itemBuilder: (context, index) {
final exercise = maxWeights.keys.elementAt(index);
final weightData = maxWeights[exercise]!;
final weight = weightData['maxWeight'];
final reps = weightData['reps'];
return ListTile(
title: Text(exercise),
trailing: Text('${weight!.toStringAsFixed(2)} kg x $reps reps'),
);
},
);
},
),
);
}
}

0 comments on commit 0735568

Please sign in to comment.