mirror of
https://github.com/wger-project/flutter.git
synced 2026-12-01 02:02:35 +01:00
Keep exercises added in gym mode across recalculations
This commit is contained in:
44
lib/features/routines/models/added_exercise.dart
Normal file
44
lib/features/routines/models/added_exercise.dart
Normal file
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* This file is part of wger Workout Manager <https://github.com/wger-project>.
|
||||
* Copyright (c) 2026 wger Team
|
||||
*
|
||||
* wger Workout Manager is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import 'package:freezed_annotation/freezed_annotation.dart';
|
||||
import 'package:wger/features/exercises/models/exercise.dart';
|
||||
import 'package:wger/features/routines/models/weight_unit.dart';
|
||||
|
||||
part 'added_exercise.freezed.dart';
|
||||
|
||||
/// An exercise added in gym mode that is not part of the routine
|
||||
@freezed
|
||||
class AddedExercise with _$AddedExercise {
|
||||
/// Key of the page the exercise follows
|
||||
final String afterPageKey;
|
||||
|
||||
final Exercise exercise;
|
||||
|
||||
/// Borrowed from the page it follows, the logs need one
|
||||
final int slotEntryId;
|
||||
|
||||
final WeightUnit? weightUnit;
|
||||
|
||||
const AddedExercise({
|
||||
required this.afterPageKey,
|
||||
required this.exercise,
|
||||
required this.slotEntryId,
|
||||
this.weightUnit,
|
||||
});
|
||||
}
|
||||
208
lib/features/routines/models/added_exercise.freezed.dart
Normal file
208
lib/features/routines/models/added_exercise.freezed.dart
Normal file
@@ -0,0 +1,208 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
// coverage:ignore-file
|
||||
// ignore_for_file: type=lint, type=warning, deprecated_member_use, deprecated_member_use_from_same_package
|
||||
// ignore_for_file: unused_element, deprecated_member_use, deprecated_member_use_from_same_package, use_function_type_syntax_for_parameters, unnecessary_const, avoid_init_to_null, invalid_override_different_default_values_named, prefer_expression_function_bodies, annotate_overrides, invalid_annotation_target, unnecessary_question_mark
|
||||
|
||||
part of 'added_exercise.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// FreezedGenerator
|
||||
// **************************************************************************
|
||||
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
// dart format off
|
||||
T _$identity<T>(T value) => value;
|
||||
/// @nodoc
|
||||
mixin _$AddedExercise {
|
||||
|
||||
|
||||
/// Create a copy of AddedExercise
|
||||
/// with the given fields replaced by the non-null parameter values.
|
||||
@JsonKey(includeFromJson: false, includeToJson: false)
|
||||
@pragma('vm:prefer-inline')
|
||||
$AddedExerciseCopyWith<AddedExercise> get copyWith => _$AddedExerciseCopyWithImpl<AddedExercise>(this as AddedExercise, _$identity);
|
||||
|
||||
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) {
|
||||
final _this = this as AddedExercise;
|
||||
return identical(this, other) || (other.runtimeType == runtimeType&&other is AddedExercise&&(identical(other.afterPageKey, _this.afterPageKey) || other.afterPageKey == _this.afterPageKey)&&(identical(other.exercise, _this.exercise) || other.exercise == _this.exercise)&&(identical(other.slotEntryId, _this.slotEntryId) || other.slotEntryId == _this.slotEntryId)&&(identical(other.weightUnit, _this.weightUnit) || other.weightUnit == _this.weightUnit));
|
||||
}
|
||||
|
||||
|
||||
@override
|
||||
int get hashCode {
|
||||
final _this = this as AddedExercise;
|
||||
return Object.hash(runtimeType,_this.afterPageKey,_this.exercise,_this.slotEntryId,_this.weightUnit);
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
final _this = this as AddedExercise;
|
||||
return 'AddedExercise(afterPageKey: ${_this.afterPageKey}, exercise: ${_this.exercise}, slotEntryId: ${_this.slotEntryId}, weightUnit: ${_this.weightUnit})';
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
/// @nodoc
|
||||
abstract mixin class $AddedExerciseCopyWith<$Res> {
|
||||
factory $AddedExerciseCopyWith(AddedExercise value, $Res Function(AddedExercise) _then) = _$AddedExerciseCopyWithImpl;
|
||||
@useResult
|
||||
$Res call({
|
||||
String afterPageKey, Exercise exercise, int slotEntryId, WeightUnit? weightUnit
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
/// @nodoc
|
||||
class _$AddedExerciseCopyWithImpl<$Res>
|
||||
implements $AddedExerciseCopyWith<$Res> {
|
||||
_$AddedExerciseCopyWithImpl(this._self, this._then);
|
||||
|
||||
final AddedExercise _self;
|
||||
final $Res Function(AddedExercise) _then;
|
||||
|
||||
/// Create a copy of AddedExercise
|
||||
/// with the given fields replaced by the non-null parameter values.
|
||||
@pragma('vm:prefer-inline') @override $Res call({Object? afterPageKey = null,Object? exercise = null,Object? slotEntryId = null,Object? weightUnit = freezed,}) {
|
||||
return _then(AddedExercise(
|
||||
afterPageKey: null == afterPageKey ? _self.afterPageKey : afterPageKey // ignore: cast_nullable_to_non_nullable
|
||||
as String,exercise: null == exercise ? _self.exercise : exercise // ignore: cast_nullable_to_non_nullable
|
||||
as Exercise,slotEntryId: null == slotEntryId ? _self.slotEntryId : slotEntryId // ignore: cast_nullable_to_non_nullable
|
||||
as int,weightUnit: freezed == weightUnit ? _self.weightUnit : weightUnit // ignore: cast_nullable_to_non_nullable
|
||||
as WeightUnit?,
|
||||
));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/// Adds pattern-matching-related methods to [AddedExercise].
|
||||
extension AddedExercisePatterns on AddedExercise {
|
||||
/// A variant of `map` that fallback to returning `orElse`.
|
||||
///
|
||||
/// It is equivalent to doing:
|
||||
/// ```dart
|
||||
/// switch (sealedClass) {
|
||||
/// case final Subclass value:
|
||||
/// return ...;
|
||||
/// case _:
|
||||
/// return orElse();
|
||||
/// }
|
||||
/// ```
|
||||
|
||||
@optionalTypeArgs TResult maybeMap<TResult extends Object?>({required TResult orElse(),}){
|
||||
final _that = this;
|
||||
switch (_that) {
|
||||
case _:
|
||||
return orElse();
|
||||
|
||||
}
|
||||
}
|
||||
/// A `switch`-like method, using callbacks.
|
||||
///
|
||||
/// Callbacks receives the raw object, upcasted.
|
||||
/// It is equivalent to doing:
|
||||
/// ```dart
|
||||
/// switch (sealedClass) {
|
||||
/// case final Subclass value:
|
||||
/// return ...;
|
||||
/// case final Subclass2 value:
|
||||
/// return ...;
|
||||
/// }
|
||||
/// ```
|
||||
|
||||
@optionalTypeArgs TResult map<TResult extends Object?>(){
|
||||
final _that = this;
|
||||
switch (_that) {
|
||||
case _:
|
||||
throw StateError('Unexpected subclass');
|
||||
|
||||
}
|
||||
}
|
||||
/// A variant of `map` that fallback to returning `null`.
|
||||
///
|
||||
/// It is equivalent to doing:
|
||||
/// ```dart
|
||||
/// switch (sealedClass) {
|
||||
/// case final Subclass value:
|
||||
/// return ...;
|
||||
/// case _:
|
||||
/// return null;
|
||||
/// }
|
||||
/// ```
|
||||
|
||||
@optionalTypeArgs TResult? mapOrNull<TResult extends Object?>(){
|
||||
final _that = this;
|
||||
switch (_that) {
|
||||
case _:
|
||||
return null;
|
||||
|
||||
}
|
||||
}
|
||||
/// A variant of `when` that fallback to an `orElse` callback.
|
||||
///
|
||||
/// It is equivalent to doing:
|
||||
/// ```dart
|
||||
/// switch (sealedClass) {
|
||||
/// case Subclass(:final field):
|
||||
/// return ...;
|
||||
/// case _:
|
||||
/// return orElse();
|
||||
/// }
|
||||
/// ```
|
||||
|
||||
@optionalTypeArgs TResult maybeWhen<TResult extends Object?>({required TResult orElse(),}) {final _that = this;
|
||||
switch (_that) {
|
||||
case _:
|
||||
return orElse();
|
||||
|
||||
}
|
||||
}
|
||||
/// A `switch`-like method, using callbacks.
|
||||
///
|
||||
/// As opposed to `map`, this offers destructuring.
|
||||
/// It is equivalent to doing:
|
||||
/// ```dart
|
||||
/// switch (sealedClass) {
|
||||
/// case Subclass(:final field):
|
||||
/// return ...;
|
||||
/// case Subclass2(:final field2):
|
||||
/// return ...;
|
||||
/// }
|
||||
/// ```
|
||||
|
||||
@optionalTypeArgs TResult when<TResult extends Object?>() {final _that = this;
|
||||
switch (_that) {
|
||||
case _:
|
||||
throw StateError('Unexpected subclass');
|
||||
|
||||
}
|
||||
}
|
||||
/// A variant of `when` that fallback to returning `null`
|
||||
///
|
||||
/// It is equivalent to doing:
|
||||
/// ```dart
|
||||
/// switch (sealedClass) {
|
||||
/// case Subclass(:final field):
|
||||
/// return ...;
|
||||
/// case _:
|
||||
/// return null;
|
||||
/// }
|
||||
/// ```
|
||||
|
||||
@optionalTypeArgs TResult? whenOrNull<TResult extends Object?>() {final _that = this;
|
||||
switch (_that) {
|
||||
case _:
|
||||
return null;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// dart format on
|
||||
@@ -296,6 +296,10 @@ class Routine {
|
||||
|
||||
for (final day in updatedRoutine.dayData) {
|
||||
for (final slot in day.slots) {
|
||||
slot.exerciseIds = [
|
||||
for (final id in slot.exerciseIds)
|
||||
if (id == oldExerciseId) newExercise.id else id,
|
||||
];
|
||||
for (final config in slot.setConfigs) {
|
||||
if (config.exerciseId == oldExerciseId) {
|
||||
config.exerciseId = newExercise.id;
|
||||
@@ -307,6 +311,10 @@ class Routine {
|
||||
|
||||
for (final day in updatedRoutine.dayDataGym) {
|
||||
for (final slot in day.slots) {
|
||||
slot.exerciseIds = [
|
||||
for (final id in slot.exerciseIds)
|
||||
if (id == oldExerciseId) newExercise.id else id,
|
||||
];
|
||||
for (final config in slot.setConfigs) {
|
||||
if (config.exerciseId == oldExerciseId) {
|
||||
config.exerciseId = newExercise.id;
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
import 'package:clock/clock.dart';
|
||||
import 'package:material_ui/material_ui.dart';
|
||||
import 'package:wger/features/exercises/models/exercise.dart';
|
||||
import 'package:wger/features/routines/models/added_exercise.dart';
|
||||
import 'package:wger/features/routines/models/day_data.dart';
|
||||
import 'package:wger/features/routines/models/log.dart';
|
||||
import 'package:wger/features/routines/models/page_progress.dart';
|
||||
@@ -191,6 +192,9 @@ class GymModeState {
|
||||
/// so that recalculating them doesn't lose it.
|
||||
final Map<String, PageProgress> progress;
|
||||
|
||||
/// Exercises added during the workout, in the order they were added
|
||||
final List<AddedExercise> addedExercises;
|
||||
|
||||
// Routine data
|
||||
late final int dayId;
|
||||
late final int iteration;
|
||||
@@ -212,6 +216,7 @@ class GymModeState {
|
||||
this.stickySetValues = false,
|
||||
this.lastLoggedValues = const {},
|
||||
this.progress = const {},
|
||||
this.addedExercises = const [],
|
||||
int? dayId,
|
||||
int? iteration,
|
||||
Routine? routine,
|
||||
@@ -259,6 +264,7 @@ class GymModeState {
|
||||
bool? stickySetValues,
|
||||
Map<int, Log>? lastLoggedValues,
|
||||
Map<String, PageProgress>? progress,
|
||||
List<AddedExercise>? addedExercises,
|
||||
}) {
|
||||
return GymModeState(
|
||||
isInitialized: isInitialized ?? this.isInitialized,
|
||||
@@ -284,6 +290,7 @@ class GymModeState {
|
||||
stickySetValues: stickySetValues ?? this.stickySetValues,
|
||||
lastLoggedValues: lastLoggedValues ?? this.lastLoggedValues,
|
||||
progress: progress ?? this.progress,
|
||||
addedExercises: addedExercises ?? this.addedExercises,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -26,6 +26,7 @@ import 'package:wger/core/consts.dart';
|
||||
import 'package:wger/core/shared_preferences.dart';
|
||||
import 'package:wger/features/account/providers/user_profile_notifier.dart';
|
||||
import 'package:wger/features/exercises/models/exercise.dart';
|
||||
import 'package:wger/features/routines/models/added_exercise.dart';
|
||||
import 'package:wger/features/routines/models/log.dart';
|
||||
import 'package:wger/features/routines/models/page_progress.dart';
|
||||
import 'package:wger/features/routines/models/routine.dart';
|
||||
@@ -263,6 +264,42 @@ class GymStateNotifier extends _$GymStateNotifier {
|
||||
final setsPerEntry = <int, int>{};
|
||||
String nextSet(int slotEntryId) => '$slotEntryId:${(setsPerEntry[slotEntryId] ?? 0) + 1}';
|
||||
|
||||
// Exercises added during the workout follow the page they were added after
|
||||
void addAddedPages(String afterKey) {
|
||||
for (final (i, added) in state.addedExercises.indexed) {
|
||||
if (added.afterPageKey != afterKey) {
|
||||
continue;
|
||||
}
|
||||
|
||||
final addedKey = 'added:${i + 1}';
|
||||
final slotPageIndex = pageIndex;
|
||||
pages.add(
|
||||
PageEntry(
|
||||
type: PageType.set,
|
||||
pageIndex: slotPageIndex,
|
||||
key: addedKey,
|
||||
slotPages: [
|
||||
for (var nr = 1; nr <= 4; nr++)
|
||||
SlotPageEntry(
|
||||
type: SlotPageType.log,
|
||||
setIndex: nr - 1,
|
||||
pageIndex: pageIndex++,
|
||||
key: '$addedKey:log:$nr',
|
||||
setConfigData: SetConfigData(
|
||||
textRepr: '-/-',
|
||||
exerciseId: added.exercise.id,
|
||||
exercise: added.exercise,
|
||||
slotEntryId: added.slotEntryId,
|
||||
weightUnit: added.weightUnit,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
addAddedPages(addedKey);
|
||||
}
|
||||
}
|
||||
|
||||
pageIndex++;
|
||||
for (final (slotIndex, slotData) in state.dayDataGym.slots.indexed) {
|
||||
final slotPageIndex = pageIndex;
|
||||
@@ -337,6 +374,7 @@ class GymStateNotifier extends _$GymStateNotifier {
|
||||
slotPages: slotEntries,
|
||||
),
|
||||
);
|
||||
addAddedPages(slotKey);
|
||||
}
|
||||
|
||||
// Session and summary page
|
||||
@@ -350,44 +388,6 @@ class GymStateNotifier extends _$GymStateNotifier {
|
||||
_logger.finer('Initialized ${state.pages.length} pages');
|
||||
}
|
||||
|
||||
// Recalculates the indices of all pages
|
||||
void recalculateIndices() {
|
||||
var pageIndex = 0;
|
||||
final updatedPages = <PageEntry>[];
|
||||
|
||||
for (final page in state.pages) {
|
||||
final slotPageIndex = pageIndex;
|
||||
var setIndex = 0;
|
||||
final updatedSlotPages = <SlotPageEntry>[];
|
||||
|
||||
for (final slotPage in page.slotPages) {
|
||||
updatedSlotPages.add(
|
||||
slotPage.copyWith(
|
||||
pageIndex: pageIndex,
|
||||
setIndex: setIndex,
|
||||
),
|
||||
);
|
||||
setIndex++;
|
||||
pageIndex++;
|
||||
}
|
||||
|
||||
if (page.type != PageType.set) {
|
||||
pageIndex++;
|
||||
}
|
||||
|
||||
updatedPages.add(
|
||||
page.copyWith(
|
||||
pageIndex: slotPageIndex,
|
||||
slotPages: updatedSlotPages,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
state = state.copyWith(pages: updatedPages);
|
||||
// _logger.fine(readPageStructure());
|
||||
_logger.fine('Recalculated page indices');
|
||||
}
|
||||
|
||||
/// Reads the current page structure for debugging purposes
|
||||
String readPageStructure() {
|
||||
final List<String> out = [];
|
||||
@@ -430,6 +430,7 @@ class GymStateNotifier extends _$GymStateNotifier {
|
||||
// An abandoned workout never reaches clear(), so forget its values here
|
||||
lastLoggedValues: shouldReset ? const {} : null,
|
||||
progress: shouldReset ? const {} : null,
|
||||
addedExercises: shouldReset ? const [] : null,
|
||||
);
|
||||
|
||||
// Calculate the pages.
|
||||
@@ -601,41 +602,23 @@ class GymStateNotifier extends _$GymStateNotifier {
|
||||
}
|
||||
}
|
||||
|
||||
/// Replaces the exercise everywhere in the workout, also where it was added
|
||||
void replaceExercises(
|
||||
String pageEntryKey, {
|
||||
required int originalExerciseId,
|
||||
required Exercise newExercise,
|
||||
}) {
|
||||
final updatedPages = state.pages.map((page) {
|
||||
if (page.type != PageType.set) {
|
||||
return page;
|
||||
}
|
||||
|
||||
if (page.key != pageEntryKey) {
|
||||
return page;
|
||||
}
|
||||
|
||||
final updatedSlotPages = page.slotPages.map((slotPage) {
|
||||
if (slotPage.setConfigData != null &&
|
||||
slotPage.setConfigData!.exercise.id == originalExerciseId) {
|
||||
final updatedSetConfigData = slotPage.setConfigData!.copyWith(
|
||||
exerciseId: newExercise.id,
|
||||
exercise: newExercise,
|
||||
);
|
||||
return slotPage.copyWith(setConfigData: updatedSetConfigData);
|
||||
}
|
||||
return slotPage;
|
||||
}).toList();
|
||||
|
||||
return page.copyWith(slotPages: updatedSlotPages);
|
||||
}).toList();
|
||||
|
||||
// replace and update new immutable routine instance
|
||||
final updatedRoutine = state.routine.replaceExercise(originalExerciseId, newExercise);
|
||||
state = state.copyWith(
|
||||
pages: updatedPages,
|
||||
routine: updatedRoutine,
|
||||
routine: state.routine.replaceExercise(originalExerciseId, newExercise),
|
||||
addedExercises: [
|
||||
for (final added in state.addedExercises)
|
||||
if (added.exercise.id == originalExerciseId)
|
||||
added.copyWith(exercise: newExercise)
|
||||
else
|
||||
added,
|
||||
],
|
||||
);
|
||||
calculatePages();
|
||||
_logger.fine('Replaced exercise $originalExerciseId with ${newExercise.id}');
|
||||
}
|
||||
|
||||
@@ -643,6 +626,12 @@ class GymStateNotifier extends _$GymStateNotifier {
|
||||
String pageEntryKey, {
|
||||
required Exercise newExercise,
|
||||
}) {
|
||||
final page = state.pages.firstWhereOrNull((p) => p.key == pageEntryKey);
|
||||
if (page == null) {
|
||||
_logger.warning('No page found for key $pageEntryKey');
|
||||
return;
|
||||
}
|
||||
|
||||
// Ad-hoc set configs bypass routine hydration, so resolve the profile
|
||||
// default weight unit (kg/lb) here; the log draft picks it up from the
|
||||
// unit object.
|
||||
@@ -651,51 +640,18 @@ class GymStateNotifier extends _$GymStateNotifier {
|
||||
final defaultWeightUnitId = profile?.defaultWeightUnitId ?? WEIGHT_UNIT_KG;
|
||||
final defaultWeightUnit = weightUnits.firstWhereOrNull((u) => u.id == defaultWeightUnitId);
|
||||
|
||||
// Not part of the routine, so numbered in the order they were added
|
||||
final addedKey = 'added:${state.pages.where((p) => p.key.startsWith('added:')).length + 1}';
|
||||
|
||||
final List<PageEntry> pages = [];
|
||||
for (final page in state.pages) {
|
||||
pages.add(page);
|
||||
|
||||
if (page.key == pageEntryKey) {
|
||||
final setConfigData = page.slotPages.first.setConfigData!;
|
||||
|
||||
final List<SlotPageEntry> newSlotPages = [];
|
||||
for (var i = 1; i <= 4; i++) {
|
||||
newSlotPages.add(
|
||||
SlotPageEntry(
|
||||
type: SlotPageType.log,
|
||||
pageIndex: 1,
|
||||
setIndex: 0,
|
||||
key: '$addedKey:log:$i',
|
||||
setConfigData: SetConfigData(
|
||||
textRepr: '-/-',
|
||||
exerciseId: newExercise.id,
|
||||
exercise: newExercise,
|
||||
slotEntryId: setConfigData.slotEntryId,
|
||||
weightUnit: defaultWeightUnit,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
final newPage = PageEntry(
|
||||
type: PageType.set,
|
||||
pageIndex: 1,
|
||||
key: addedKey,
|
||||
slotPages: newSlotPages,
|
||||
);
|
||||
|
||||
pages.add(newPage);
|
||||
}
|
||||
}
|
||||
|
||||
state = state.copyWith(
|
||||
pages: pages,
|
||||
addedExercises: [
|
||||
...state.addedExercises,
|
||||
AddedExercise(
|
||||
afterPageKey: pageEntryKey,
|
||||
exercise: newExercise,
|
||||
slotEntryId: page.slotPages.first.setConfigData!.slotEntryId,
|
||||
weightUnit: defaultWeightUnit,
|
||||
),
|
||||
],
|
||||
);
|
||||
|
||||
recalculateIndices();
|
||||
calculatePages();
|
||||
}
|
||||
|
||||
/// Resets the workout start time to now, e.g. when the user taps "start"
|
||||
@@ -713,6 +669,7 @@ class GymStateNotifier extends _$GymStateNotifier {
|
||||
currentPage: 0,
|
||||
lastLoggedValues: const {},
|
||||
progress: const {},
|
||||
addedExercises: const [],
|
||||
|
||||
validUntil: clock.now().add(DEFAULT_DURATION),
|
||||
workoutStart: clock.now(),
|
||||
|
||||
@@ -40,7 +40,7 @@ final class GymStateNotifierProvider extends $NotifierProvider<GymStateNotifier,
|
||||
}
|
||||
}
|
||||
|
||||
String _$gymStateNotifierHash() => r'da56aac7af47067aad83c59123d06162583f018b';
|
||||
String _$gymStateNotifierHash() => r'4008686bd5f5402fbdc8e54f9a312bd4e98b908f';
|
||||
|
||||
abstract class _$GymStateNotifier extends $Notifier<GymModeState> {
|
||||
GymModeState build();
|
||||
|
||||
@@ -30,6 +30,7 @@ import 'package:wger/features/account/models/user_profile.dart';
|
||||
import 'package:wger/features/account/providers/user_profile_notifier.dart';
|
||||
import 'package:wger/features/account/providers/user_profile_repository.dart';
|
||||
import 'package:wger/features/exercises/models/exercise.dart';
|
||||
import 'package:wger/features/routines/models/added_exercise.dart';
|
||||
import 'package:wger/features/routines/models/day.dart';
|
||||
import 'package:wger/features/routines/models/day_data.dart';
|
||||
import 'package:wger/features/routines/models/log.dart';
|
||||
@@ -240,90 +241,12 @@ void main() {
|
||||
});
|
||||
});
|
||||
|
||||
group('GymStateNotifier.recalculateIndices', () {
|
||||
test('Correctly recalculates indices if new pages are added', () {
|
||||
// Arrange
|
||||
final newPages = [
|
||||
...notifier.state.pages.sublist(0, 2),
|
||||
PageEntry(
|
||||
type: PageType.set,
|
||||
pageIndex: 1111,
|
||||
key: 'new-page-1',
|
||||
),
|
||||
PageEntry(
|
||||
type: PageType.set,
|
||||
pageIndex: 9,
|
||||
key: 'new-page-2',
|
||||
),
|
||||
...notifier.state.pages.sublist(2),
|
||||
PageEntry(
|
||||
type: PageType.set,
|
||||
pageIndex: 0,
|
||||
key: 'new-page-3',
|
||||
slotPages: [
|
||||
SlotPageEntry(
|
||||
type: SlotPageType.timer,
|
||||
pageIndex: 10,
|
||||
setIndex: 9,
|
||||
key: 'new-slot-1',
|
||||
),
|
||||
SlotPageEntry(
|
||||
type: SlotPageType.timer,
|
||||
pageIndex: 10,
|
||||
setIndex: 6,
|
||||
key: 'new-slot-2',
|
||||
),
|
||||
SlotPageEntry(
|
||||
type: SlotPageType.timer,
|
||||
pageIndex: 100,
|
||||
setIndex: 100,
|
||||
key: 'new-slot-3',
|
||||
),
|
||||
],
|
||||
),
|
||||
];
|
||||
notifier.state = notifier.state.copyWith(pages: newPages);
|
||||
|
||||
// Act
|
||||
notifier.recalculateIndices();
|
||||
|
||||
// Assert
|
||||
final pages = notifier.state.pages;
|
||||
expect(pages[0].pageIndex, 0);
|
||||
expect(pages[1].pageIndex, 1);
|
||||
|
||||
// These three have the same pageIndex because the new ones don't have any slot
|
||||
// pages (this should not happen in practice)
|
||||
expect(pages[2].pageIndex, 8);
|
||||
expect(pages[3].pageIndex, 8);
|
||||
expect(pages[4].pageIndex, 8);
|
||||
|
||||
expect(pages[5].pageIndex, 15);
|
||||
expect(pages[6].pageIndex, 16);
|
||||
expect(pages[7].pageIndex, 17);
|
||||
|
||||
// Preserve the order of new pages
|
||||
expect(pages[7].key, 'new-page-3');
|
||||
|
||||
// Slot pages have correct indices, the original order is preserved
|
||||
final slotPages = pages[7].slotPages;
|
||||
expect(slotPages[0].key, 'new-slot-1');
|
||||
expect(slotPages[0].pageIndex, 17);
|
||||
expect(slotPages[0].setIndex, 0);
|
||||
expect(slotPages[1].key, 'new-slot-2');
|
||||
expect(slotPages[1].pageIndex, 18);
|
||||
expect(slotPages[1].setIndex, 1);
|
||||
expect(slotPages[2].key, 'new-slot-3');
|
||||
expect(slotPages[2].pageIndex, 19);
|
||||
expect(slotPages[2].setIndex, 2);
|
||||
});
|
||||
});
|
||||
|
||||
group('GymStateNotifier.replaceExercises', () {
|
||||
test('Correctly swaps an exercise', () {
|
||||
// Arrange
|
||||
final page = notifier.state.pages[1];
|
||||
final slotPage = page.slotPages[1];
|
||||
final slotPageKeys = page.slotPages.map((s) => s.key).toList();
|
||||
expect(slotPage.type, SlotPageType.log);
|
||||
expect(
|
||||
notifier.state.pages.every((p) => p.exercises.every((e) => e.id != testSquats.id)),
|
||||
@@ -337,6 +260,11 @@ void main() {
|
||||
// Assert: every slot page of that page carries the new exercise, and the
|
||||
// routine itself was rebuilt too (the log draft reads from it)
|
||||
final updated = notifier.state.pages[1];
|
||||
expect(
|
||||
updated.slotPages.map((s) => s.key),
|
||||
slotPageKeys,
|
||||
reason: 'the layout is the same, including the overview of the exercise',
|
||||
);
|
||||
expect(
|
||||
updated.slotPages
|
||||
.where((s) => s.setConfigData != null)
|
||||
@@ -356,9 +284,8 @@ void main() {
|
||||
});
|
||||
|
||||
group('GymStateNotifier.addExerciseAfterPage', () {
|
||||
test('Stamps the profile default weight unit on the ad-hoc set configs', () async {
|
||||
// Ad-hoc exercises bypass routine hydration, so the notifier itself
|
||||
// resolves the profile default: lb (id 2) for an imperial user.
|
||||
/// A notifier for an imperial user, with the pages calculated
|
||||
Future<GymStateNotifier> imperialGymNotifier() async {
|
||||
final profileRepo = MockUserProfileRepository();
|
||||
when(
|
||||
profileRepo.watchDrift(),
|
||||
@@ -385,11 +312,18 @@ void main() {
|
||||
routine: getTestRoutine(),
|
||||
);
|
||||
imperialNotifier.calculatePages();
|
||||
return imperialNotifier;
|
||||
}
|
||||
|
||||
test('Stamps the profile default weight unit on the ad-hoc set configs', () async {
|
||||
// Ad-hoc exercises bypass routine hydration, so the notifier itself
|
||||
// resolves the profile default: lb (id 2) for an imperial user.
|
||||
final imperialNotifier = await imperialGymNotifier();
|
||||
final setPage = imperialNotifier.state.pages.firstWhere((p) => p.type == PageType.set);
|
||||
|
||||
imperialNotifier.addExerciseAfterPage(setPage.key, newExercise: testSquats);
|
||||
|
||||
// recalculateIndices copies the page objects, so look the page up by key.
|
||||
// The pages are recalculated, so look the page up by key
|
||||
final pages = imperialNotifier.state.pages;
|
||||
final newPage = pages[pages.indexWhere((p) => p.key == setPage.key) + 1];
|
||||
expect(newPage.slotPages, isNotEmpty);
|
||||
@@ -409,6 +343,49 @@ void main() {
|
||||
'added:1:log:4',
|
||||
]);
|
||||
});
|
||||
|
||||
test('Keeps the added exercises when the pages are recalculated', () async {
|
||||
final gymNotifier = await imperialGymNotifier();
|
||||
final slotKey = gymNotifier.state.pages[1].key;
|
||||
gymNotifier.addExerciseAfterPage(slotKey, newExercise: testSquats);
|
||||
gymNotifier.addExerciseAfterPage('added:1', newExercise: testCurls);
|
||||
gymNotifier.markSlotPageAsDone('added:1:log:1', isDone: true);
|
||||
|
||||
gymNotifier.state = gymNotifier.state.copyWith(showExercisePages: false);
|
||||
gymNotifier.calculatePages();
|
||||
|
||||
final pages = gymNotifier.state.pages;
|
||||
expect(pages.map((p) => p.key).toList().sublist(1, 4), [slotKey, 'added:1', 'added:2']);
|
||||
expect(pages[2].exercises.single.id, testSquats.id);
|
||||
expect(pages[3].exercises.single.id, testCurls.id);
|
||||
expect(
|
||||
pages[2].slotPages.map((p) => p.setIndex),
|
||||
[0, 1, 2, 3],
|
||||
reason: 'the sets of the exercise are numbered on their own',
|
||||
);
|
||||
expect(pages[2].slotPages.first.setConfigData!.weightUnit, testWeightUnit2);
|
||||
expect(gymNotifier.state.progressOf('added:1:log:1').done, isTrue);
|
||||
expect(
|
||||
pages.expand((p) => p.slotPages).map((p) => p.pageIndex),
|
||||
List.generate(pages.expand((p) => p.slotPages).length, (i) => i + 1),
|
||||
reason: 'the page indices stay consecutive',
|
||||
);
|
||||
});
|
||||
|
||||
test('Keeps a replaced added exercise when the pages are recalculated', () async {
|
||||
final gymNotifier = await imperialGymNotifier();
|
||||
gymNotifier.addExerciseAfterPage(gymNotifier.state.pages[1].key, newExercise: testSquats);
|
||||
|
||||
gymNotifier.replaceExercises(
|
||||
'added:1',
|
||||
originalExerciseId: testSquats.id,
|
||||
newExercise: testCurls,
|
||||
);
|
||||
gymNotifier.calculatePages();
|
||||
|
||||
final added = gymNotifier.state.pages.firstWhere((p) => p.key == 'added:1');
|
||||
expect(added.exercises.single.id, testCurls.id);
|
||||
});
|
||||
});
|
||||
|
||||
group('GymStateNotifier.calculatePages, supersets', () {
|
||||
@@ -892,6 +869,9 @@ void main() {
|
||||
notifier.state = notifier.state.copyWith(
|
||||
workoutStart: DateTime(2024, 5, 1, 10, 0),
|
||||
progress: {'log:1:1': const PageProgress(done: true)},
|
||||
addedExercises: [
|
||||
AddedExercise(afterPageKey: 'slot:1:1', exercise: testSquats, slotEntryId: 1),
|
||||
],
|
||||
);
|
||||
|
||||
// Act
|
||||
@@ -902,6 +882,7 @@ void main() {
|
||||
// Assert
|
||||
expect(notifier.state.workoutStart, DateTime(2024, 5, 2, 9, 15));
|
||||
expect(notifier.state.progress, isEmpty);
|
||||
expect(notifier.state.addedExercises, isEmpty);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -954,11 +935,15 @@ void main() {
|
||||
dayId: 1,
|
||||
currentPage: 4,
|
||||
progress: {'log:1:1': const PageProgress(done: true)},
|
||||
addedExercises: [
|
||||
AddedExercise(afterPageKey: 'slot:1:1', exercise: testSquats, slotEntryId: 1),
|
||||
],
|
||||
);
|
||||
|
||||
expect(notifier.initData(getTestRoutine(), 2, 1), 0);
|
||||
expect(notifier.state.dayId, 2);
|
||||
expect(notifier.state.progress, isEmpty, reason: 'the new workout starts without progress');
|
||||
expect(notifier.state.addedExercises, isEmpty);
|
||||
});
|
||||
|
||||
test('An expired validUntil resets even on the same day', () {
|
||||
|
||||
Reference in New Issue
Block a user