Restore the workout progress even if its logs can't be read

This commit is contained in:
Roland Geider
2026-10-27 21:35:21 +01:00
parent 70d6929a5c
commit ce66da762b
2 changed files with 49 additions and 14 deletions

View File

@@ -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 = <String?, Log>{};
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.

View File

@@ -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<Log> logs = const []}) {
GymStateNotifier restartedNotifier({List<Log> 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<String>).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;