diff --git a/lib/features/routines/providers/gym_state.dart b/lib/features/routines/providers/gym_state.dart index e77cc1dcd..dee42f3a5 100644 --- a/lib/features/routines/providers/gym_state.dart +++ b/lib/features/routines/providers/gym_state.dart @@ -176,6 +176,12 @@ class GymModeState { final DateTime workoutStart; final DateTime validUntil; + /// When the rest on the current timer page started, null on other pages + final DateTime? restStartedAt; + + /// When the countdown on the current timer page ends, null if it counts up + final DateTime? restEndsAt; + // User settings final bool showExercisePages; final bool showTimerPages; @@ -195,6 +201,8 @@ class GymModeState { this.isInitialized = false, this.pages = const [], this.currentPage = 0, + this.restStartedAt, + this.restEndsAt, this.showExercisePages = true, this.showTimerPages = true, @@ -231,6 +239,11 @@ class GymModeState { List? pages, int? currentPage, + // Rest data. clearRest drops both rest fields before the passed ones apply + DateTime? restStartedAt, + DateTime? restEndsAt, + bool clearRest = false, + // Routine data int? dayId, int? iteration, @@ -253,6 +266,8 @@ class GymModeState { isInitialized: isInitialized ?? this.isInitialized, pages: pages ?? this.pages, currentPage: currentPage ?? this.currentPage, + restStartedAt: restStartedAt ?? (clearRest ? null : this.restStartedAt), + restEndsAt: restEndsAt ?? (clearRest ? null : this.restEndsAt), dayId: dayId ?? this.dayId, iteration: iteration ?? this.iteration, @@ -276,6 +291,15 @@ class GymModeState { /// The start of the workout as a [TimeOfDay], e.g. for the session form TimeOfDay get startTime => TimeOfDay.fromDateTime(workoutStart); + /// Countdown length of a timer page, or null if the rest counts up + Duration? restDurationFor(SlotPageEntry slotPage) { + final restTime = slotPage.setConfigData?.restTime; + if (restTime != null) { + return Duration(seconds: restTime.toInt()); + } + return useCountdownBetweenSets ? countdownDuration : null; + } + int get totalPages { // Main pages (start, session, etc.) var count = pages.where((p) => p.type != PageType.set).length; @@ -339,6 +363,8 @@ class GymModeState { String toString() { return 'GymState(' 'currentPage: $currentPage, ' + 'restStartedAt: $restStartedAt, ' + 'restEndsAt: $restEndsAt, ' 'validUntil: $validUntil ' 'workoutStart: $workoutStart, ' 'showExercisePages: $showExercisePages, ' diff --git a/lib/features/routines/providers/gym_state_notifier.dart b/lib/features/routines/providers/gym_state_notifier.dart index 83e4bd186..e6de67f62 100644 --- a/lib/features/routines/providers/gym_state_notifier.dart +++ b/lib/features/routines/providers/gym_state_notifier.dart @@ -290,6 +290,7 @@ class GymStateNotifier extends _$GymStateNotifier { routine: routine, iteration: iteration, currentPage: initialPage, + clearRest: shouldReset, // A fresh workout also restarts the elapsed timer workoutStart: shouldReset ? clock.now() : null, ); @@ -306,7 +307,9 @@ class GymStateNotifier extends _$GymStateNotifier { } void setCurrentPage(int page) { + final pageChanged = page != state.currentPage; state = state.copyWith(currentPage: page); + _updateRest(restart: pageChanged); // Ensure that there is a log entry for the current slot entry final slotEntryPage = state.getSlotEntryPageByIndex(); @@ -322,6 +325,27 @@ class GymStateNotifier extends _$GymStateNotifier { ref.read(gymLogProvider.notifier).setLog(log); } + /// Starts the rest when on a timer page, ends it everywhere else. A rest + /// that is already running on the current page keeps going unless [restart]. + void _updateRest({required bool restart}) { + final slotPage = state.getSlotEntryPageByIndex(); + if (slotPage?.type != SlotPageType.timer) { + state = state.copyWith(clearRest: true); + return; + } + if (!restart && state.restStartedAt != null) { + return; + } + + final now = clock.now(); + final duration = state.restDurationFor(slotPage!); + state = state.copyWith( + clearRest: true, + restStartedAt: now, + restEndsAt: duration == null ? null : now.add(duration), + ); + } + void setShowExercisePages(bool value) { state = state.copyWith(showExercisePages: value); calculatePages(); @@ -493,6 +517,7 @@ class GymStateNotifier extends _$GymStateNotifier { isInitialized: false, pages: [], currentPage: 0, + clearRest: true, validUntil: clock.now().add(DEFAULT_DURATION), workoutStart: clock.now(), diff --git a/lib/features/routines/widgets/gym_mode/gym_mode.dart b/lib/features/routines/widgets/gym_mode/gym_mode.dart index 3fdeb938a..6ea7262c9 100644 --- a/lib/features/routines/widgets/gym_mode/gym_mode.dart +++ b/lib/features/routines/widgets/gym_mode/gym_mode.dart @@ -128,15 +128,11 @@ class _GymModeState extends ConsumerState { out.add(LogPage(_controller, slotPage.uuid)); } - // Timer. Use rest time from config data if available, otherwise use user settings - final rest = slotPage.setConfigData?.restTime; if (slotPage.type == SlotPageType.timer) { + final rest = gymState.restDurationFor(slotPage); out.add( - (rest != null || gymState.useCountdownBetweenSets) - ? TimerCountdownWidget( - _controller, - (rest ?? gymState.countdownDuration.inSeconds).toInt(), - ) + rest != null + ? TimerCountdownWidget(_controller, rest.inSeconds) : TimerWidget(_controller), ); } diff --git a/test/features/routines/providers/gym_state_test.dart b/test/features/routines/providers/gym_state_test.dart index 790beaa09..741dd5ffa 100644 --- a/test/features/routines/providers/gym_state_test.dart +++ b/test/features/routines/providers/gym_state_test.dart @@ -508,6 +508,103 @@ void main() { }); }); + group('GymStateNotifier.setCurrentPage, rest', () { + final start = DateTime.utc(2024, 5, 1, 17, 30); + final later = DateTime.utc(2024, 5, 1, 17, 45); + + Iterable slotPages() => notifier.state.pages.expand((p) => p.slotPages); + + // The fixture's first slot has a rest time of 120 s, the second has none + int timerWithRestTime() => slotPages() + .firstWhere((p) => p.type == SlotPageType.timer && p.setConfigData!.restTime != null) + .pageIndex; + int timerWithoutRestTime() => slotPages() + .firstWhere((p) => p.type == SlotPageType.timer && p.setConfigData!.restTime == null) + .pageIndex; + + void goTo(int page, {DateTime? at}) => + withClock(Clock.fixed(at ?? start), () => notifier.setCurrentPage(page)); + + test('A timer page counts down from the rest time of its set', () { + notifier.state = notifier.state.copyWith(useCountdownBetweenSets: false); + + goTo(timerWithRestTime()); + + expect(notifier.state.restStartedAt, start); + expect(notifier.state.restEndsAt, start.add(const Duration(seconds: 120))); + }); + + test('Without a rest time the countdown uses the user setting', () { + notifier.state = notifier.state.copyWith( + useCountdownBetweenSets: true, + countdownDuration: 90, + ); + + goTo(timerWithoutRestTime()); + + expect(notifier.state.restEndsAt, start.add(const Duration(seconds: 90))); + }); + + test('Without a rest time and the countdown setting off, the rest counts up', () { + notifier.state = notifier.state.copyWith(useCountdownBetweenSets: false); + + goTo(timerWithoutRestTime()); + + expect(notifier.state.restStartedAt, start); + expect(notifier.state.restEndsAt, isNull); + }); + + test('Leaving the timer page clears the rest', () { + final timer = timerWithRestTime(); + goTo(timer); + + goTo(timer + 1); + + expect(notifier.state.getSlotEntryPageByIndex()!.type, SlotPageType.log); + expect(notifier.state.restStartedAt, isNull); + expect(notifier.state.restEndsAt, isNull); + }); + + test('Setting the same timer page again keeps the running rest', () { + // gym_mode reports the restored page again when it is reopened + final timer = timerWithRestTime(); + goTo(timer); + + goTo(timer, at: later); + + expect(notifier.state.restStartedAt, start); + }); + + test('Coming back to a timer page restarts the rest', () { + final timer = timerWithRestTime(); + goTo(timer); + goTo(timer + 1); + + goTo(timer, at: later); + + expect(notifier.state.restStartedAt, later); + expect(notifier.state.restEndsAt, later.add(const Duration(seconds: 120))); + }); + + test('clear() ends the rest', () { + goTo(timerWithRestTime()); + + notifier.clear(); + + expect(notifier.state.restStartedAt, isNull); + expect(notifier.state.restEndsAt, isNull); + }); + + test('A reset in initData ends the rest', () { + goTo(timerWithRestTime()); + + notifier.initData(getTestRoutine(), 2, 1); + + expect(notifier.state.restStartedAt, isNull); + expect(notifier.state.restEndsAt, isNull); + }); + }); + group('GymStateNotifier.startWorkout', () { test('Resets the workout start time to now', () { // Arrange @@ -624,5 +721,32 @@ void main() { expect(state.copyWith(clearLogScopeWeeks: true).logScopeWeeks, isNull); }); + + test('Keeps the rest when it is not passed', () { + final state = notifier.state.copyWith( + restStartedAt: DateTime.utc(2024, 5, 1, 17, 30), + restEndsAt: DateTime.utc(2024, 5, 1, 17, 32), + ); + + final copy = state.copyWith(currentPage: 3); + + expect(copy.restStartedAt, DateTime.utc(2024, 5, 1, 17, 30)); + expect(copy.restEndsAt, DateTime.utc(2024, 5, 1, 17, 32)); + }); + + test('clearRest drops both rest fields before applying the passed ones', () { + final state = notifier.state.copyWith( + restStartedAt: DateTime.utc(2024, 5, 1, 17, 30), + restEndsAt: DateTime.utc(2024, 5, 1, 17, 32), + ); + + final cleared = state.copyWith(clearRest: true); + expect(cleared.restStartedAt, isNull); + expect(cleared.restEndsAt, isNull); + + final countUp = state.copyWith(clearRest: true, restStartedAt: DateTime.utc(2024, 5, 1, 18)); + expect(countUp.restStartedAt, DateTime.utc(2024, 5, 1, 18)); + expect(countUp.restEndsAt, isNull); + }); }); }