mirror of
https://github.com/wger-project/flutter.git
synced 2026-11-29 09:20:56 +01:00
Hydrate exercise and unit objects in routine entries
This commit is contained in:
@@ -101,7 +101,7 @@ class SlotEntry {
|
||||
late int? repetitionUnitId;
|
||||
|
||||
@JsonKey(includeFromJson: false, includeToJson: false)
|
||||
late RepetitionUnit? repetitionUnitObj;
|
||||
RepetitionUnit? repetitionUnitObj;
|
||||
|
||||
@JsonKey(required: true, name: 'repetition_rounding', fromJson: stringToNumNull)
|
||||
late num? repetitionRounding;
|
||||
@@ -116,7 +116,7 @@ class SlotEntry {
|
||||
late int? weightUnitId;
|
||||
|
||||
@JsonKey(includeFromJson: false, includeToJson: false)
|
||||
late WeightUnit? weightUnitObj;
|
||||
WeightUnit? weightUnitObj;
|
||||
|
||||
@JsonKey(required: true, name: 'weight_rounding', fromJson: stringToNumNull)
|
||||
late num? weightRounding;
|
||||
|
||||
@@ -127,9 +127,10 @@ class RoutinesRiverpod extends _$RoutinesRiverpod {
|
||||
}
|
||||
|
||||
/// Attaches the latest sessions to each routine and hydrates
|
||||
/// `log.exerciseObj` from the exercise catalogue. No-op if the upstream
|
||||
/// providers haven't produced their first value yet — the listener will
|
||||
/// re-attach as soon as they do.
|
||||
/// `log.exerciseObj` (sessions → logs) and `entry.exerciseObj`
|
||||
/// (days → slots → entries) from the exercise catalogue. No-op if
|
||||
/// the upstream providers haven't produced their first value yet —
|
||||
/// the listener will re-attach as soon as they do.
|
||||
void _hydrateSessions(List<Routine> routines) {
|
||||
final sessions = ref.read(workoutSessionProvider).value ?? const <WorkoutSession>[];
|
||||
final exerciseState = ref.read(exercisesProvider).value;
|
||||
@@ -150,6 +151,19 @@ class RoutinesRiverpod extends _$RoutinesRiverpod {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (final day in routine.days) {
|
||||
for (final slot in day.slots) {
|
||||
for (final entry in slot.entries) {
|
||||
final exercise = exerciseState.exercises.firstWhereOrNull(
|
||||
(e) => e.id == entry.exerciseId,
|
||||
);
|
||||
if (exercise != null) {
|
||||
entry.exerciseObj = exercise;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -217,6 +231,22 @@ class RoutinesRiverpod extends _$RoutinesRiverpod {
|
||||
setExercisesAndUnits(routine.dayDataGym);
|
||||
setExercisesAndUnits(routine.dayData);
|
||||
|
||||
// Hydrate `exerciseObj` / `repetitionUnitObj` / `weightUnitObj` on
|
||||
// the structure side (days → slots → entries)
|
||||
for (final day in routine.days) {
|
||||
for (final slot in day.slots) {
|
||||
for (final entry in slot.entries) {
|
||||
entry.exerciseObj = exerciseState.getById(entry.exerciseId);
|
||||
entry.repetitionUnitObj = repetitionUnits.firstWhereOrNull(
|
||||
(u) => u.id == entry.repetitionUnitId,
|
||||
);
|
||||
entry.weightUnitObj = weightUnits.firstWhereOrNull(
|
||||
(u) => u.id == entry.weightUnitId,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Attach sessions (already joined with their logs) that belong to this routine
|
||||
// and set the appropriate exercises
|
||||
routine.sessions = sessions.where((s) => s.routineId == routineId).toList();
|
||||
|
||||
Reference in New Issue
Block a user