diff --git a/lib/features/routines/providers/gym_log_notifier.dart b/lib/features/routines/providers/gym_log_notifier.dart index 8f3d829c0..5bbf5bd5f 100644 --- a/lib/features/routines/providers/gym_log_notifier.dart +++ b/lib/features/routines/providers/gym_log_notifier.dart @@ -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) { diff --git a/lib/features/routines/widgets/gym_mode/log_page.dart b/lib/features/routines/widgets/gym_mode/log_page.dart index 2b69c93e6..6226bee1f 100644 --- a/lib/features/routines/widgets/gym_mode/log_page.dart +++ b/lib/features/routines/widgets/gym_mode/log_page.dart @@ -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> pastLogs) { + Widget _buildPastLogs(AsyncValue> 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 []; - 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 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); }, diff --git a/test/features/routines/providers/gym_log_notifier_test.dart b/test/features/routines/providers/gym_log_notifier_test.dart index 28aed7820..7d98e5861 100644 --- a/test/features/routines/providers/gym_log_notifier_test.dart +++ b/test/features/routines/providers/gym_log_notifier_test.dart @@ -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), () {