Hydrate the exercise when copying a past log in gym mode

Not copying an exercise that isn't there keeps the crash away, but gym
mode then holds a log with no exercise, which would bite the next reader.

The past logs widget already knows the exercise from the set config, so
pass it down to setLog, which assigns it through the exercise setter so
that exerciseId stays consistent.

The notifier test worked around the old behaviour by setting exerciseObj
on every fixture, with a comment explaining that copyWith needs it. That
was the bug, written down, so it's gone.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XzxNFoPMxsFPxHD3P2Ma3w
This commit is contained in:
Matz Hilven
2026-07-28 00:11:08 +02:00
parent 1b199bd06e
commit b7a0d4893c
3 changed files with 38 additions and 9 deletions

View File

@@ -19,6 +19,7 @@
import 'package:clock/clock.dart';
import 'package:logging/logging.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:wger/features/exercises/models/exercise.dart';
import 'package:wger/features/routines/models/log.dart';
import 'package:wger/features/routines/models/repetition_unit.dart';
import 'package:wger/features/routines/models/weight_unit.dart';
@@ -35,12 +36,19 @@ class GymLogNotifier extends _$GymLogNotifier {
return null;
}
void setLog(Log log) {
/// Pass [exercise] to hydrate the copy, logs read from the database only
/// carry their exercise ID
void setLog(Log log, {Exercise? exercise}) {
// Clear the id so Drift mints a fresh UUID on insert, and the sessionId so
// the copy lands in today's session instead of the template's historical one.
state = log.copyWith(date: clock.now())
final out = log.copyWith(date: clock.now())
..id = null
..sessionId = null;
if (exercise != null) {
out.exercise = exercise;
}
state = out;
}
void setWeight(num weight) {

View File

@@ -24,6 +24,7 @@ import 'package:wger/core/formatting/formatting.dart';
import 'package:wger/core/snackbar.dart';
import 'package:wger/core/widgets/core.dart';
import 'package:wger/core/widgets/error.dart';
import 'package:wger/features/exercises/models/exercise.dart';
import 'package:wger/features/routines/models/log.dart';
import 'package:wger/features/routines/models/set_config_data.dart';
import 'package:wger/features/routines/models/slot_entry.dart';
@@ -142,7 +143,7 @@ class LogPage extends ConsumerWidget {
// Overriding the log scope from here is handled in a follow-up, the
// settings currently only live in the gym mode options.
// _LogScopeControls(gymState: gymState),
Expanded(child: _buildPastLogs(pastLogs)),
Expanded(child: _buildPastLogs(pastLogs, setConfigData.exercise)),
Padding(
padding: const EdgeInsets.all(10),
@@ -165,7 +166,7 @@ class LogPage extends ConsumerWidget {
}
/// Renders the previous logs for this exercise
Widget _buildPastLogs(AsyncValue<List<Log>> pastLogs) {
Widget _buildPastLogs(AsyncValue<List<Log>> pastLogs, Exercise exercise) {
if (pastLogs.hasError) {
_logger.warning('Could not load past logs', pastLogs.error, pastLogs.stackTrace);
// Scroll-wrap so the indicator fits this slim slot instead of overflowing.
@@ -174,7 +175,9 @@ class LogPage extends ConsumerWidget {
);
}
final logs = pastLogs.value ?? const <Log>[];
return logs.isEmpty ? const SizedBox.shrink() : LogsPastLogsWidget(pastLogs: logs);
return logs.isEmpty
? const SizedBox.shrink()
: LogsPastLogsWidget(pastLogs: logs, exercise: exercise);
}
}
@@ -235,9 +238,13 @@ class LogsPlatesWidget extends ConsumerWidget {
class LogsPastLogsWidget extends ConsumerWidget {
final List<Log> pastLogs;
/// The exercise the logs belong to, they only carry its ID
final Exercise exercise;
const LogsPastLogsWidget({
super.key,
required this.pastLogs,
required this.exercise,
});
@override
@@ -261,7 +268,7 @@ class LogsPastLogsWidget extends ConsumerWidget {
subtitle: Text(dateFormat.format(pastLog.date)),
trailing: const Icon(Icons.copy),
onTap: () {
logProvider.setLog(pastLog);
logProvider.setLog(pastLog, exercise: exercise);
ScaffoldMessenger.of(context).hideCurrentSnackBar();
showSnackbar(context, AppLocalizations.of(context).dataCopied);
},

View File

@@ -41,8 +41,6 @@ void main() {
num repetitions = 10,
DateTime? date,
}) {
// Log.copyWith reads exerciseObj internally, so it must be set before
// setLog (which calls copyWith) is invoked.
return Log(
id: id ?? 'log-1',
exerciseId: exerciseId,
@@ -50,13 +48,29 @@ void main() {
weight: weight,
repetitions: repetitions,
date: date ?? DateTime.utc(2020, 1, 1),
)..exerciseObj = testBenchPress;
);
}
test('initial state is null', () {
expect(container.read(gymLogProvider), isNull);
});
test('setLog accepts a log with no exercise, as read from the database', () {
container.read(gymLogProvider.notifier).setLog(makeLog());
final state = container.read(gymLogProvider)!;
expect(state.exerciseId, 1);
expect(state.exerciseObjOrNull, isNull);
});
test('setLog hydrates the exercise when the caller passes one', () {
container.read(gymLogProvider.notifier).setLog(makeLog(), exercise: testBenchPress);
final state = container.read(gymLogProvider)!;
expect(state.exerciseObj.id, testBenchPress.id);
expect(state.exerciseId, testBenchPress.id);
});
test('setLog stores a copy with cleared id/sessionId and the current clock as date', () {
final fixed = DateTime.utc(2026, 5, 1, 12);
withClock(Clock.fixed(fixed), () {