Skip to content

Commit

Permalink
Make workout log charts work again
Browse files Browse the repository at this point in the history
  • Loading branch information
rolandgeider committed Jan 25, 2025
1 parent 9ea580a commit 202349c
Show file tree
Hide file tree
Showing 21 changed files with 194 additions and 287 deletions.
40 changes: 20 additions & 20 deletions lib/models/exercises/exercise.dart
Original file line number Diff line number Diff line change
Expand Up @@ -147,21 +147,21 @@ class Exercise extends Equatable {
Exercise.fromApiDataJson(Map<String, dynamic> baseData, List<Language> languages)
: this.fromApiData(ExerciseApiData.fromJson(baseData), languages);

Exercise.fromApiData(ExerciseApiData baseData, List<Language> languages) {
id = baseData.id;
uuid = baseData.uuid;
categoryId = baseData.category.id;
category = baseData.category;

created = baseData.created;
lastUpdate = baseData.lastUpdate;
lastUpdateGlobal = baseData.lastUpdateGlobal;

musclesSecondary = baseData.muscles;
muscles = baseData.muscles;
equipment = baseData.equipment;
category = baseData.category;
translations = baseData.translations.map((e) {
Exercise.fromApiData(ExerciseApiData exerciseData, List<Language> languages) {
id = exerciseData.id;
uuid = exerciseData.uuid;
categoryId = exerciseData.category.id;
category = exerciseData.category;

created = exerciseData.created;
lastUpdate = exerciseData.lastUpdate;
lastUpdateGlobal = exerciseData.lastUpdateGlobal;

musclesSecondary = exerciseData.muscles;
muscles = exerciseData.muscles;
equipment = exerciseData.equipment;
category = exerciseData.category;
translations = exerciseData.translations.map((e) {
e.language = languages.firstWhere(
(l) => l.id == e.languageId,

Expand All @@ -173,13 +173,13 @@ class Exercise extends Equatable {
);
return e;
}).toList();
videos = baseData.videos;
images = baseData.images;
videos = exerciseData.videos;
images = exerciseData.images;

authors = baseData.authors;
authorsGlobal = baseData.authorsGlobal;
authors = exerciseData.authors;
authorsGlobal = exerciseData.authorsGlobal;

variationId = baseData.variationId;
variationId = exerciseData.variationId;
}

/// Returns translation for the given language
Expand Down
5 changes: 3 additions & 2 deletions lib/models/workouts/log.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/

import 'package:json_annotation/json_annotation.dart';
import 'package:wger/helpers/consts.dart';
import 'package:wger/helpers/json.dart';
import 'package:wger/helpers/misc.dart';
import 'package:wger/models/exercises/exercise.dart';
Expand Down Expand Up @@ -86,12 +87,12 @@ class Log {
required this.routineId,
this.repetitions,
this.repetitionsTarget,
required this.repetitionsUnitId,
this.repetitionsUnitId = REP_UNIT_REPETITIONS_ID,
required this.rir,
this.rirTarget,
this.weight,
this.weightTarget,
required this.weightUnitId,
this.weightUnitId = WEIGHT_UNIT_KG,
required this.date,
});

Expand Down
4 changes: 2 additions & 2 deletions lib/models/workouts/log.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 22 additions & 2 deletions lib/models/workouts/routine.dart
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ class Routine {
/// means here that the values are the same, i.e. logs with the same weight,
/// reps, etc. are considered equal. Workout ID, Log ID and date are not
/// considered.
List<Log> filterLogsByExercise(Exercise exercise, {bool unique = false}) {
var out = logs.where((element) => element.exerciseId == exercise.id).toList();
List<Log> filterLogsByExercise(int exerciseId, {bool unique = false}) {
var out = logs.where((log) => log.exerciseId == exerciseId).toList();

if (unique) {
out = out.toSet().toList();
Expand All @@ -129,6 +129,26 @@ class Routine {
return out;
}

/// Groups logs by repetition
Map<num, List<Log>> groupLogsByRepetition({List<Log>? logs}) {
final workoutLogs = logs ?? this.logs;
final Map<num, List<Log>> groupedLogs = {};

for (final log in workoutLogs) {
if (log.repetitions == null) {
continue;
}

if (!groupedLogs.containsKey(log.repetitions)) {
groupedLogs[log.repetitions!] = [];
}

groupedLogs[log.repetitions]!.add(log);
}

return groupedLogs;
}

/// Massages the log data to more easily present on the log overview
///
Map<DateTime, Map<String, dynamic>> get logData {
Expand Down
28 changes: 6 additions & 22 deletions lib/providers/routines.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:wger/exceptions/http_exception.dart';
import 'package:wger/helpers/consts.dart';
import 'package:wger/models/exercises/exercise.dart';
import 'package:wger/models/workouts/base_config.dart';
import 'package:wger/models/workouts/day.dart';
import 'package:wger/models/workouts/day_data.dart';
Expand Down Expand Up @@ -62,7 +61,7 @@ class RoutinesProvider with ChangeNotifier {
static const _routineConfigRestTime = 'rest-config';
static const _routineConfigMaxRestTime = 'max-rest-config';

Routine? _currentPlan;
Routine? _currentRoutine;
late ExercisesProvider _exercises;
final WgerBaseProvider baseProvider;
List<Routine> _routines = [];
Expand Down Expand Up @@ -92,7 +91,7 @@ class RoutinesProvider with ChangeNotifier {

/// Clears all lists
void clear() {
_currentPlan = null;
_currentRoutine = null;
_routines = [];
_weightUnits = [];
_repetitionUnits = [];
Expand Down Expand Up @@ -131,17 +130,17 @@ class RoutinesProvider with ChangeNotifier {

/// Set the currently "active" workout plan
void setCurrentPlan(int id) {
_currentPlan = findById(id);
_currentRoutine = findById(id);
}

/// Returns the currently "active" workout plan
Routine? get currentPlan {
return _currentPlan;
Routine? get currentRoutine {
return _currentRoutine;
}

/// Reset the currently "active" workout plan to null
void resetCurrentRoutine() {
_currentPlan = null;
_currentRoutine = null;
}

/// Returns the current active workout plan. At the moment this is just
Expand Down Expand Up @@ -378,21 +377,6 @@ class RoutinesProvider with ChangeNotifier {
}
}

Future<Map<String, dynamic>> fetchLogData(
Routine workout,
Exercise base,
) async {
final data = await baseProvider.fetch(
baseProvider.makeUrl(
_routinesUrlPath,
id: workout.id,
objectMethod: 'logs',
query: {'id': base.id!.toString()},
),
);
return data;
}

/// Fetch and set weight units for workout (kg, lb, plate, etc.)
Future<void> fetchAndSetRepetitionUnits() async {
final response =
Expand Down
20 changes: 11 additions & 9 deletions lib/widgets/routines/charts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:intl/intl.dart';
import 'package:wger/helpers/charts.dart';
import 'package:wger/helpers/colors.dart';
import 'package:wger/models/workouts/log.dart';

class LogChartWidgetFl extends StatefulWidget {
final Map _data;
final Map<num, List<Log>> _data;
final DateTime _currentDate;

const LogChartWidgetFl(this._data, this._currentDate);
Expand All @@ -50,7 +51,7 @@ class _LogChartWidgetFlState extends State<LogChartWidgetFl> {
touchTooltipData: LineTouchTooltipData(
getTooltipItems: (touchedSpots) {
return touchedSpots.map((touchedSpot) {
final reps = widget._data['chart_data'][touchedSpot.barIndex].first['reps'];
final reps = widget._data[touchedSpot.barIndex]?.first.repetitions;

return LineTooltipItem(
'$reps × ${touchedSpot.y} kg',
Expand All @@ -63,7 +64,7 @@ class _LogChartWidgetFlState extends State<LogChartWidgetFl> {
}

LineChartData mainData() {
final colors = generateChartColors(widget._data['chart_data'].length).iterator;
final colors = generateChartColors(widget._data.keys.length).iterator;

return LineChartData(
lineTouchData: tooltipData(),
Expand Down Expand Up @@ -101,8 +102,9 @@ class _LogChartWidgetFlState extends State<LogChartWidgetFl> {
);
},
interval: chartGetInterval(
DateTime.parse(widget._data['logs'].keys.first),
DateTime.parse(widget._data['logs'].keys.last),
// TODO: make sure this works when the data is empty etc
widget._data[widget._data.keys.first]!.first.date,
widget._data[widget._data.keys.last]!.first.date,
),
),
),
Expand All @@ -121,14 +123,14 @@ class _LogChartWidgetFlState extends State<LogChartWidgetFl> {
border: Border.all(color: const Color(0xff37434d)),
),
lineBarsData: [
...widget._data['chart_data'].map((e) {
...widget._data.keys.map((reps) {
colors.moveNext();
return LineChartBarData(
spots: [
...e.map(
...widget._data[reps]!.map(
(entry) => FlSpot(
DateTime.parse(entry['date']).millisecondsSinceEpoch.toDouble(),
double.parse(entry['weight']),
entry.date.millisecondsSinceEpoch.toDouble(),
entry.weight!.toDouble(),
),
),
],
Expand Down
4 changes: 2 additions & 2 deletions lib/widgets/routines/gym_mode.dart
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ class _LogPageState extends State<LogPage> {
style: Theme.of(context).textTheme.titleLarge,
textAlign: TextAlign.center,
),
...widget._workoutPlan.filterLogsByExercise(widget._exercise, unique: true).map((log) {
...widget._workoutPlan.filterLogsByExercise(widget._exercise.id!, unique: true).map((log) {
return ListTile(
title: Text(log.singleLogRepTextNoNl),
subtitle: Text(
Expand Down Expand Up @@ -646,7 +646,7 @@ class _LogPageState extends State<LogPage> {
Text(widget._slotData.comment, textAlign: TextAlign.center),
const SizedBox(height: 10),
Expanded(
child: (widget._workoutPlan.filterLogsByExercise(widget._exercise).isNotEmpty)
child: (widget._workoutPlan.filterLogsByExercise(widget._exercise.id!).isNotEmpty)
? getPastLogs()
: Container(),
),
Expand Down
Loading

0 comments on commit 202349c

Please sign in to comment.