mirror of
https://github.com/wger-project/flutter.git
synced 2026-02-18 00:17:48 +01:00
Remove logs from the routine
The logs are now in the session list and can be easily extracted if needed
This commit is contained in:
@@ -40,6 +40,9 @@ class Log {
|
||||
@JsonKey(required: true, name: 'routine')
|
||||
late int routineId;
|
||||
|
||||
@JsonKey(required: true, name: 'session')
|
||||
late int sessionId;
|
||||
|
||||
@JsonKey(required: true)
|
||||
int? iteration;
|
||||
|
||||
|
||||
@@ -13,6 +13,7 @@ Log _$LogFromJson(Map<String, dynamic> json) {
|
||||
'id',
|
||||
'exercise',
|
||||
'routine',
|
||||
'session',
|
||||
'iteration',
|
||||
'slot_entry',
|
||||
'repetitions',
|
||||
@@ -39,13 +40,14 @@ Log _$LogFromJson(Map<String, dynamic> json) {
|
||||
weightTarget: stringToNum(json['weight_target'] as String?),
|
||||
weightUnitId: (json['weight_unit'] as num?)?.toInt() ?? WEIGHT_UNIT_KG,
|
||||
date: DateTime.parse(json['date'] as String),
|
||||
);
|
||||
)..sessionId = (json['session'] as num).toInt();
|
||||
}
|
||||
|
||||
Map<String, dynamic> _$LogToJson(Log instance) => <String, dynamic>{
|
||||
'id': instance.id,
|
||||
'exercise': instance.exerciseId,
|
||||
'routine': instance.routineId,
|
||||
'session': instance.sessionId,
|
||||
'iteration': instance.iteration,
|
||||
'slot_entry': instance.slotEntryId,
|
||||
'rir': instance.rir,
|
||||
|
||||
@@ -75,9 +75,6 @@ class Routine {
|
||||
@JsonKey(includeFromJson: false, includeToJson: false)
|
||||
List<DayData> dayDataCurrentIterationGym = [];
|
||||
|
||||
@JsonKey(includeFromJson: false, includeToJson: false)
|
||||
List<Log> logs = [];
|
||||
|
||||
@JsonKey(required: false, includeToJson: false, defaultValue: [])
|
||||
List<WorkoutSessionApi> sessions = [];
|
||||
|
||||
@@ -94,7 +91,6 @@ class Routine {
|
||||
this.dayDataGym = const [],
|
||||
this.dayDataCurrentIteration = const [],
|
||||
this.dayDataCurrentIterationGym = const [],
|
||||
this.logs = const [],
|
||||
this.sessions = const [],
|
||||
}) {
|
||||
this.created = created ?? DateTime.now();
|
||||
@@ -117,6 +113,14 @@ class Routine {
|
||||
|
||||
Map<String, dynamic> toJson() => _$RoutineToJson(this);
|
||||
|
||||
List<Log> get logs {
|
||||
final out = <Log>[];
|
||||
for (final session in sessions) {
|
||||
out.addAll(session.logs);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/// Filters the workout logs by exercise and sorts them by date
|
||||
///
|
||||
/// Optionally, filters list so that only unique logs are returned. "Unique"
|
||||
|
||||
@@ -325,24 +325,6 @@ class RoutinesProvider with ChangeNotifier {
|
||||
// routine.sessions = sessionDataEntries;
|
||||
routine.sessions = List<WorkoutSessionApi>.from(sessionDataEntries);
|
||||
|
||||
// TODO: workaround, routine.logs is marked as an unmodifiable list
|
||||
routine.logs = [];
|
||||
for (final sessionData in routine.sessions) {
|
||||
try {
|
||||
for (final log in sessionData.logs) {
|
||||
log.weightUnit = _weightUnits.firstWhere((e) => e.id == log.weightUnitId);
|
||||
log.repetitionUnit = _repetitionUnits.firstWhere((e) => e.id == log.weightUnitId);
|
||||
log.exerciseBase = (await _exercises.fetchAndSetExercise(log.exerciseId))!;
|
||||
|
||||
routine.logs.add(log);
|
||||
}
|
||||
} catch (e, stackTrace) {
|
||||
_logger.warning('Error while processing the session data for a routine!');
|
||||
_logger.warning(e.toString());
|
||||
_logger.warning(stackTrace.toString());
|
||||
}
|
||||
}
|
||||
|
||||
// ... and done
|
||||
final routineIndex = _routines.indexWhere((r) => r.id == routineId);
|
||||
if (routineIndex != -1) {
|
||||
@@ -706,13 +688,13 @@ class RoutinesProvider with ChangeNotifier {
|
||||
);
|
||||
final newLog = Log.fromJson(data);
|
||||
|
||||
log.id = newLog.id;
|
||||
log.weightUnit = _weightUnits.firstWhere((e) => e.id == log.weightUnitId);
|
||||
log.repetitionUnit = _repetitionUnits.firstWhere((e) => e.id == log.weightUnitId);
|
||||
log.exerciseBase = (await _exercises.fetchAndSetExercise(log.exerciseId))!;
|
||||
newLog.weightUnit = _weightUnits.firstWhere((e) => e.id == log.weightUnitId);
|
||||
newLog.repetitionUnit = _repetitionUnits.firstWhere((e) => e.id == log.weightUnitId);
|
||||
newLog.exerciseBase = (await _exercises.fetchAndSetExercise(log.exerciseId))!;
|
||||
|
||||
final plan = findById(log.routineId);
|
||||
plan.logs.add(log);
|
||||
final plan = findById(newLog.routineId);
|
||||
final session = plan.sessions.firstWhere((element) => element.session.id == newLog.sessionId);
|
||||
session.logs.add(newLog);
|
||||
notifyListeners();
|
||||
return newLog;
|
||||
}
|
||||
@@ -725,7 +707,9 @@ class RoutinesProvider with ChangeNotifier {
|
||||
Future<void> deleteLog(Log log) async {
|
||||
await baseProvider.deleteRequest(_logsUrlPath, log.id!);
|
||||
for (final workout in _routines) {
|
||||
workout.logs.removeWhere((element) => element.id == log.id);
|
||||
for (final sessionData in workout.sessions) {
|
||||
sessionData.session.logs.removeWhere((element) => element.id == log.id);
|
||||
}
|
||||
}
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:wger/models/exercises/exercise.dart';
|
||||
import 'package:wger/models/workouts/base_config.dart';
|
||||
import 'package:wger/models/workouts/day.dart';
|
||||
@@ -23,6 +24,8 @@ import 'package:wger/models/workouts/day_data.dart';
|
||||
import 'package:wger/models/workouts/log.dart';
|
||||
import 'package:wger/models/workouts/repetition_unit.dart';
|
||||
import 'package:wger/models/workouts/routine.dart';
|
||||
import 'package:wger/models/workouts/session.dart';
|
||||
import 'package:wger/models/workouts/session_api.dart';
|
||||
import 'package:wger/models/workouts/set_config_data.dart';
|
||||
import 'package:wger/models/workouts/slot.dart';
|
||||
import 'package:wger/models/workouts/slot_data.dart';
|
||||
@@ -81,6 +84,32 @@ Routine getTestRoutine({List<Exercise>? exercises}) {
|
||||
log3.weightUnit = testWeightUnit1;
|
||||
log3.repetitionUnit = testRepetitionUnit1;
|
||||
|
||||
final session1 = WorkoutSessionApi(
|
||||
session: WorkoutSession.withData(
|
||||
id: 1,
|
||||
routineId: 1,
|
||||
date: DateTime(2021, 5, 1),
|
||||
impression: 3,
|
||||
notes: 'This is a note',
|
||||
timeStart: const TimeOfDay(hour: 10, minute: 0),
|
||||
timeEnd: const TimeOfDay(hour: 12, minute: 34),
|
||||
),
|
||||
logs: [log1, log2],
|
||||
);
|
||||
|
||||
final session2 = WorkoutSessionApi(
|
||||
session: WorkoutSession.withData(
|
||||
id: 2,
|
||||
routineId: 1,
|
||||
date: DateTime(2021, 5, 2),
|
||||
impression: 1,
|
||||
notes: 'This is a note',
|
||||
timeStart: const TimeOfDay(hour: 6, minute: 12),
|
||||
timeEnd: const TimeOfDay(hour: 8, minute: 1),
|
||||
),
|
||||
logs: [log3],
|
||||
);
|
||||
|
||||
final slotEntryBenchPress = SlotEntry(
|
||||
slotId: 1,
|
||||
type: 'normal',
|
||||
@@ -302,7 +331,7 @@ Routine getTestRoutine({List<Exercise>? exercises}) {
|
||||
start: DateTime(2024, 11, 01),
|
||||
end: DateTime(2024, 12, 01),
|
||||
days: [dayChestShoulders, dayLegs],
|
||||
logs: [log1, log2, log3],
|
||||
sessions: [session1, session2],
|
||||
dayData: dayDataDisplay,
|
||||
dayDataCurrentIteration: [
|
||||
...dayDataDisplay,
|
||||
|
||||
Reference in New Issue
Block a user