From ce66da762b5f78c0204a65a6b6649ed296ea8566 Mon Sep 17 00:00:00 2001 From: Roland Geider Date: Tue, 27 Oct 2026 21:35:21 +0100 Subject: [PATCH] Restore the workout progress even if its logs can't be read --- .../providers/gym_state_notifier.dart | 24 ++++++++---- .../routines/providers/gym_state_test.dart | 39 ++++++++++++++++--- 2 files changed, 49 insertions(+), 14 deletions(-) diff --git a/lib/features/routines/providers/gym_state_notifier.dart b/lib/features/routines/providers/gym_state_notifier.dart index ba7363af8..0228b2b24 100644 --- a/lib/features/routines/providers/gym_state_notifier.dart +++ b/lib/features/routines/providers/gym_state_notifier.dart @@ -231,12 +231,20 @@ class GymStateNotifier extends _$GymStateNotifier { return null; } + // Without the logs the done sets and timers still come back, a correction + // then inserts a new row instead of updating the old one final logIds = saved.pages.values.map((p) => p.logId).nonNulls.toList(); - final logs = { - if (logIds.isNotEmpty) - for (final log in await ref.read(workoutLogRepositoryProvider).getLogsByIds(logIds)) - log.id: log, - }; + var logs = {}; + if (logIds.isNotEmpty) { + try { + logs = { + for (final log in await ref.read(workoutLogRepositoryProvider).getLogsByIds(logIds)) + log.id: log, + }; + } catch (e) { + _logger.warning('Could not read the logs of the stored workout progress: $e'); + } + } // Pages the routine no longer has (it was edited, or they were added // during the workout) are left out @@ -590,9 +598,9 @@ class GymStateNotifier extends _$GymStateNotifier { } /// [log] is the entry saved from this page during the current workout. It - /// 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. + /// is kept in the page's progress so revisiting the page pre-fills the form + /// with the saved values and saving updates the row instead of inserting a + /// duplicate. Without [log], an entry saved before is kept. /// /// Saving a new entry, unlike correcting one, also (re)starts the rest timer /// that follows the set. diff --git a/test/features/routines/providers/gym_state_test.dart b/test/features/routines/providers/gym_state_test.dart index af1218290..8a4dd8817 100644 --- a/test/features/routines/providers/gym_state_test.dart +++ b/test/features/routines/providers/gym_state_test.dart @@ -99,7 +99,7 @@ void main() { } }); - test('Stores the logged entry on the slot page', () { + test('Stores the logged entry in the page progress', () { // Arrange final slotPage = notifier.state.pages[1].slotPages[1]; expect(slotPage.type, SlotPageType.log); @@ -113,6 +113,11 @@ void main() { final loggedEntry = notifier.state.progressOf(slotPage.key).loggedEntry!; expect(loggedEntry.id, 'log-1'); expect(loggedEntry, isNot(same(log))); + + // Undoing the flag without a log, e.g. after a failed write, keeps the entry + notifier.markSlotPageAsDone(slotPage.key, isDone: false); + expect(notifier.state.progressOf(slotPage.key).done, isFalse); + expect(notifier.state.progressOf(slotPage.key).loggedEntry!.id, 'log-1'); }); }); @@ -982,14 +987,17 @@ void main() { /// Simulates the app being killed: a new container means a new (empty) /// notifier, while the stored preferences and [logs] survive, as on a real /// restart. - GymStateNotifier restartedNotifier({List logs = const []}) { + GymStateNotifier restartedNotifier({List logs = const [], Object? lookupError}) { final logRepo = MockWorkoutLogRepository(); - when(logRepo.getLogsByIds(any)).thenAnswer( - (invocation) async => [ + when(logRepo.getLogsByIds(any)).thenAnswer((invocation) async { + if (lookupError != null) { + throw lookupError; + } + return [ for (final log in logs) if ((invocation.positionalArguments.first as List).contains(log.id)) log, - ], - ); + ]; + }); return ProviderContainer.test( overrides: [workoutLogRepositoryProvider.overrideWithValue(logRepo)], ).read(gymStateProvider.notifier); @@ -1114,6 +1122,25 @@ void main() { ); }); + test('Restores the rest of the progress when the logs can not be read', () async { + final logPage = notifier.state.pages[1].slotPages[1]; + final timerKey = notifier.state.pages[1].slotPages[2].key; + final log = Log(id: 'log-1', exerciseId: 1, date: DateTime(2026, 7, 26)); + await openDay1(notifier); + withClock(Clock.fixed(DateTime(2026, 7, 26, 18, 30)), () { + notifier.markSlotPageAsDone(logPage.key, isDone: true, log: log); + }); + notifier.setCurrentPage(3); + await pumpEventQueue(); + + final restarted = restartedNotifier(lookupError: StateError('database closed')); + + expect(await openDay1(restarted), 3); + expect(restarted.state.progressOf(logPage.key).done, isTrue); + expect(restarted.state.progressOf(logPage.key).loggedEntry, isNull); + expect(restarted.state.progressOf(timerKey).timerStartedAt, DateTime(2026, 7, 26, 18, 30)); + }); + test('Stores a rest timer as soon as it starts', () async { await runWorkout(); final timerKey = notifier.state.pages[1].slotPages[2].key;