From 5dd00d6cb774b4bce5ff2a6a71dba5dd57c034f8 Mon Sep 17 00:00:00 2001 From: Roland Geider Date: Fri, 23 Oct 2026 20:34:03 +0200 Subject: [PATCH] Keep the set context when copying a past log in gym mode --- .../routines/providers/gym_log_notifier.dart | 25 +++-- .../routines/widgets/gym_mode/log_page.dart | 15 +-- .../providers/gym_log_notifier_test.dart | 94 +++++++++++++++++-- .../widgets/gym_mode/log_page_test.dart | 3 + 4 files changed, 108 insertions(+), 29 deletions(-) diff --git a/lib/features/routines/providers/gym_log_notifier.dart b/lib/features/routines/providers/gym_log_notifier.dart index 5bbf5bd5f..a8979437c 100644 --- a/lib/features/routines/providers/gym_log_notifier.dart +++ b/lib/features/routines/providers/gym_log_notifier.dart @@ -19,7 +19,6 @@ 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'; @@ -36,19 +35,25 @@ class GymLogNotifier extends _$GymLogNotifier { return null; } - /// Pass [exercise] to hydrate the copy, logs read from the database only - /// carry their exercise ID - void setLog(Log log, {Exercise? exercise}) { + void setLog(Log log) { // 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. - final out = log.copyWith(date: clock.now()) + // the entry lands in today's session. + state = log.copyWith(date: clock.now()) ..id = null ..sessionId = null; + } - if (exercise != null) { - out.exercise = exercise; - } - state = out; + /// Takes the entered values (weight, repetitions, their units and RiR) from + /// [pastLog]. Routine, slot entry, iteration and targets stay the current set's. + void copyValuesFrom(Log pastLog) { + state = state?.copyWith() + ?..weight = pastLog.weight + ..weightUnitId = pastLog.weightUnitId + ..weightUnitObj = pastLog.weightUnitObj + ..repetitions = pastLog.repetitions + ..repetitionsUnitId = pastLog.repetitionsUnitId + ..repetitionsUnitObj = pastLog.repetitionsUnitObj + ..rir = pastLog.rir; } 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 97b8794c2..db5fc428c 100644 --- a/lib/features/routines/widgets/gym_mode/log_page.dart +++ b/lib/features/routines/widgets/gym_mode/log_page.dart @@ -24,7 +24,6 @@ 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'; @@ -143,7 +142,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, setConfigData.exercise)), + Expanded(child: _buildPastLogs(pastLogs)), Padding( padding: const EdgeInsets.all(10), @@ -166,7 +165,7 @@ class LogPage extends ConsumerWidget { } /// Renders the previous logs for this exercise - Widget _buildPastLogs(AsyncValue> pastLogs, Exercise exercise) { + Widget _buildPastLogs(AsyncValue> pastLogs) { 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. @@ -175,9 +174,7 @@ class LogPage extends ConsumerWidget { ); } final logs = pastLogs.value ?? const []; - return logs.isEmpty - ? const SizedBox.shrink() - : LogsPastLogsWidget(pastLogs: logs, exercise: exercise); + return logs.isEmpty ? const SizedBox.shrink() : LogsPastLogsWidget(pastLogs: logs); } } @@ -238,13 +235,9 @@ 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 @@ -268,7 +261,7 @@ class LogsPastLogsWidget extends ConsumerWidget { subtitle: Text(dateFormat.format(pastLog.date)), trailing: const Icon(Icons.copy), onTap: () { - logProvider.setLog(pastLog, exercise: exercise); + logProvider.copyValuesFrom(pastLog); 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 7d98e5861..7a1300069 100644 --- a/test/features/routines/providers/gym_log_notifier_test.dart +++ b/test/features/routines/providers/gym_log_notifier_test.dart @@ -63,14 +63,6 @@ void main() { 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), () { @@ -91,6 +83,86 @@ void main() { }); }); + group('copyValuesFrom', () { + const kg = WeightUnit(id: 1, name: 'kg'); + const lbs = WeightUnit(id: 2, name: 'lbs'); + const reps = RepetitionUnit(id: 1, name: 'Repetitions'); + const seconds = RepetitionUnit(id: 3, name: 'Seconds'); + + setUp(() { + final current = + Log( + exerciseId: testBenchPress.id, + routineId: 100, + slotEntryId: 7, + iteration: 5, + weight: 80, + weightTarget: 80, + repetitions: 8, + repetitionsTarget: 8, + rir: 2, + rirTarget: 2, + date: DateTime.utc(2026, 10, 19), + ) + ..exercise = testBenchPress + ..weightUnit = kg + ..repetitionUnit = reps; + container.read(gymLogProvider.notifier).setLog(current); + }); + + test('takes the entered values, the set context stays the current one', () { + final pastLog = + Log( + id: 'past', + exerciseId: testBenchPress.id, + routineId: 99, + sessionId: 'past-session', + slotEntryId: 3, + iteration: 1, + weight: 95, + weightTarget: 90, + repetitions: 7, + repetitionsTarget: 6, + rir: 1, + rirTarget: 3, + ) + ..weightUnit = lbs + ..repetitionUnit = seconds; + + container.read(gymLogProvider.notifier).copyValuesFrom(pastLog); + + final state = container.read(gymLogProvider)!; + expect(state.weight, 95); + expect(state.weightUnitId, lbs.id); + expect(state.weightUnitObj, lbs); + expect(state.repetitions, 7); + expect(state.repetitionsUnitId, seconds.id); + expect(state.repetitionsUnitObj, seconds); + expect(state.rir, 1); + + expect(state.id, isNull); + expect(state.sessionId, isNull); + expect(state.routineId, 100); + expect(state.slotEntryId, 7); + expect(state.iteration, 5); + expect(state.weightTarget, 80); + expect(state.repetitionsTarget, 8); + expect(state.rirTarget, 2); + expect(state.exerciseObj.id, testBenchPress.id); + }); + + test('clears values the past log left empty', () { + final pastLog = Log(exerciseId: testBenchPress.id, repetitions: 12); + + container.read(gymLogProvider.notifier).copyValuesFrom(pastLog); + + final state = container.read(gymLogProvider)!; + expect(state.repetitions, 12); + expect(state.weight, isNull); + expect(state.rir, isNull); + }); + }); + group('mutating setters when state is set', () { setUp(() { container.read(gymLogProvider.notifier).setLog(makeLog()); @@ -134,6 +206,12 @@ void main() { }); group('setters are no-ops when state is null', () { + test('copyValuesFrom on null state stays null', () { + container.read(gymLogProvider.notifier).copyValuesFrom(makeLog()); + + expect(container.read(gymLogProvider), isNull); + }); + test('setWeight on null state stays null', () { container.read(gymLogProvider.notifier).setWeight(99); diff --git a/test/features/routines/widgets/gym_mode/log_page_test.dart b/test/features/routines/widgets/gym_mode/log_page_test.dart index 0a218e5a4..070ec8e02 100644 --- a/test/features/routines/widgets/gym_mode/log_page_test.dart +++ b/test/features/routines/widgets/gym_mode/log_page_test.dart @@ -32,6 +32,7 @@ import 'package:wger/features/routines/models/log.dart'; import 'package:wger/features/routines/models/routine.dart'; import 'package:wger/features/routines/models/set_config_data.dart'; import 'package:wger/features/routines/models/slot_data.dart'; +import 'package:wger/features/routines/providers/gym_log_notifier.dart'; import 'package:wger/features/routines/providers/gym_state.dart'; import 'package:wger/features/routines/providers/gym_state_notifier.dart'; import 'package:wger/features/routines/providers/workout_logs_repository.dart'; @@ -196,6 +197,8 @@ void main() { // `contains` would also pass on the prefilled weight of 100 expect(repText, '10'); expect(weightText, '10'); + // The fixture's past logs come from other iterations, the copy must not take theirs + expect(container.read(gymLogProvider)!.iteration, 1); expect(find.byType(SnackBar), findsOneWidget); });