From 58cd06bb4cfa7e2fdcb65ea4dcc8656c8b258a2d Mon Sep 17 00:00:00 2001 From: Roland Geider Date: Sun, 25 Oct 2026 21:36:21 +0100 Subject: [PATCH] Restart the rest timer when its set is saved for the first time --- .../providers/gym_state_notifier.dart | 8 +++++ .../routines/providers/gym_state_test.dart | 34 +++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/lib/features/routines/providers/gym_state_notifier.dart b/lib/features/routines/providers/gym_state_notifier.dart index d1b1a76d7..18aa0cc7f 100644 --- a/lib/features/routines/providers/gym_state_notifier.dart +++ b/lib/features/routines/providers/gym_state_notifier.dart @@ -450,6 +450,8 @@ class GymStateNotifier extends _$GymStateNotifier { /// is kept on the slot page so revisiting the page pre-fills the form with /// the saved values and saving updates the row instead of inserting a /// duplicate. + /// + /// The first save also (re)starts the rest timer that follows the set. void markSlotPageAsDone(String uuid, {required bool isDone, Log? log}) { final slotPage = state.getSlotPageByUUID(uuid); if (slotPage == null) { @@ -459,6 +461,12 @@ class GymStateNotifier extends _$GymStateNotifier { _updateSlotPage(uuid, slotPage.copyWith(logDone: isDone, loggedEntry: log)); _logger.fine('Set logDone=$isDone for slot page UUID $uuid'); + + // Scrolling past the timer (e.g. "jump to") already started it + final timerPage = state.getSlotEntryPageByIndex(slotPage.pageIndex + 1); + if (slotPage.loggedEntry == null && timerPage?.type == SlotPageType.timer) { + _updateSlotPage(timerPage!.uuid, timerPage.copyWith(timerStartedAt: clock.now())); + } } void replaceExercises( diff --git a/test/features/routines/providers/gym_state_test.dart b/test/features/routines/providers/gym_state_test.dart index 412d907db..6ac4ad5c8 100644 --- a/test/features/routines/providers/gym_state_test.dart +++ b/test/features/routines/providers/gym_state_test.dart @@ -162,6 +162,40 @@ void main() { DateTime(2024, 5, 2, 12), ); }); + + test('Saving a set restarts the following timer, editing it does not', () { + // Arrange: the timer was started by scrolling past it + final logPage = notifier.state.pages[1].slotPages[1]; + final timerPage = notifier.state.pages[1].slotPages[2]; + expect(logPage.type, SlotPageType.log); + expect(timerPage.type, SlotPageType.timer); + withClock(Clock.fixed(DateTime(2024, 5, 2, 12)), () { + notifier.startTimerIfNeeded(timerPage.uuid); + }); + final log = Log.fromSetConfigData(logPage.setConfigData!, routineId: 1, iteration: 1); + + // Act: the set is saved + withClock(Clock.fixed(DateTime(2024, 5, 2, 12, 10)), () { + notifier.markSlotPageAsDone(logPage.uuid, isDone: true, log: log); + }); + + // Assert + expect( + notifier.state.getSlotPageByUUID(timerPage.uuid)!.timerStartedAt, + DateTime(2024, 5, 2, 12, 10), + ); + + // Act: correcting the saved set keeps the running timer + withClock(Clock.fixed(DateTime(2024, 5, 2, 12, 11)), () { + notifier.markSlotPageAsDone(logPage.uuid, isDone: true, log: log); + }); + + // Assert + expect( + notifier.state.getSlotPageByUUID(timerPage.uuid)!.timerStartedAt, + DateTime(2024, 5, 2, 12, 10), + ); + }); }); group('GymStateNotifier.recalculateIndices', () {