Files
flutter/lib/features/routines/models/log.dart
Matz Hilven 1b199bd06e Fix crash when copying a log with no hydrated exercise
Log.exerciseObj was declared late and is only ever assigned when
hydrating the logs of a routine's sessions. Logs read from the database
are built through drift's row class and only carry exerciseId, so
copyWith threw a LateInitializationError when it unconditionally read
the field. Copying a past log in gym mode does exactly that.

Make the field nullable behind a getter that throws a descriptive error,
add exerciseObjOrNull for callers that legitimately handle logs straight
from the database, and only carry the exercise over when there is one.

Hydrating the exercise in watchLogsByExerciseDrift instead would mean
joining its translations, images, muscles and equipment for every log,
which most callers don't need.

The log page tests mocked the query with the test routine's logs, which
are hydrated, which is why this was never caught. They now return logs
without an exercise, like the real query does.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XzxNFoPMxsFPxHD3P2Ma3w
2026-07-28 00:07:43 +02:00

330 lines
11 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* This file is part of wger Workout Manager <https://github.com/wger-project>.
* Copyright (c) 2020 - 2026 wger Team
*
* wger Workout Manager is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import 'package:drift/drift.dart' as drift;
import 'package:flutter/material.dart';
import 'package:wger/core/consts.dart';
import 'package:wger/core/i18n.dart';
import 'package:wger/core/misc.dart';
import 'package:wger/database/powersync/database.dart';
import 'package:wger/features/exercises/models/exercise.dart';
import 'package:wger/features/routines/models/repetition_unit.dart';
import 'package:wger/features/routines/models/set_config_data.dart';
import 'package:wger/features/routines/models/weight_unit.dart';
enum LogTargetStatus {
lessThanTarget,
atTarget,
moreThanTarget,
notSet,
}
class Log {
/// Max value the backend stores for weight / repetitions
/// (in the backend: DecimalField max_digits=6, decimal_places=2)
static const MAX_VALUE = 9999.99;
/// Client-generated UUID, is `null` only before the first persist
String? id;
late int exerciseId;
Exercise? _exerciseObj;
/// The exercise this log belongs to, throws if it was never hydrated
///
/// Logs read from the database only carry [exerciseId], joining the whole
/// exercise with its translations, images and muscles would be wasteful for
/// a value most callers don't need. Use [exerciseObjOrNull] for those.
Exercise get exerciseObj {
if (_exerciseObj == null) {
throw StateError('Log $id has no hydrated exercise (exercise ID $exerciseId)');
}
return _exerciseObj!;
}
set exerciseObj(Exercise value) => _exerciseObj = value;
/// Like [exerciseObj] but returns null instead of throwing
Exercise? get exerciseObjOrNull => _exerciseObj;
int? routineId;
String? sessionId;
int? iteration;
int? slotEntryId;
num? rir;
num? rirTarget;
num? repetitions;
num? repetitionsTarget;
int? repetitionsUnitId;
RepetitionUnit? repetitionsUnitObj;
num? weight;
num? weightTarget;
int? weightUnitId;
WeightUnit? weightUnitObj;
late DateTime date;
Log({
this.id,
required this.exerciseId,
this.iteration,
this.slotEntryId,
this.routineId,
this.sessionId,
this.repetitions,
this.repetitionsTarget,
this.repetitionsUnitId = REP_UNIT_REPETITIONS_ID,
this.repetitionsUnitObj,
this.rir,
this.rirTarget,
this.weight,
this.weightTarget,
this.weightUnitId = WEIGHT_UNIT_KG,
this.weightUnitObj,
DateTime? date,
}) : date = date ?? DateTime.now();
Log.fromSetConfigData(SetConfigData setConfig, {this.routineId, this.iteration}) {
date = DateTime.now();
sessionId = null;
slotEntryId = setConfig.slotEntryId;
exercise = setConfig.exercise;
weight = setConfig.weight;
weightTarget = setConfig.weight;
weightUnitId = setConfig.weightUnitId ?? WEIGHT_UNIT_KG;
weightUnitObj = setConfig.weightUnit;
repetitions = setConfig.repetitions;
repetitionsTarget = setConfig.repetitions;
repetitionsUnitId = setConfig.repetitionsUnitId ?? REP_UNIT_REPETITIONS_ID;
repetitionsUnitObj = setConfig.repetitionsUnit;
rir = setConfig.rir;
rirTarget = setConfig.rir;
}
Log copyWith({
String? id,
int? exerciseId,
int? routineId,
String? sessionId,
int? iteration,
int? slotEntryId,
num? rir,
num? rirTarget,
num? repetitions,
num? repetitionsTarget,
int? repetitionsUnitId,
RepetitionUnit? repetitionsUnitObj,
num? weight,
num? weightTarget,
int? weightUnitId,
WeightUnit? weightUnitObj,
DateTime? date,
}) {
final out = Log(
id: id ?? this.id,
exerciseId: exerciseId ?? this.exerciseId,
iteration: iteration ?? this.iteration,
slotEntryId: slotEntryId ?? this.slotEntryId,
routineId: routineId ?? this.routineId,
sessionId: sessionId ?? this.sessionId,
repetitions: repetitions ?? this.repetitions,
repetitionsTarget: repetitionsTarget ?? this.repetitionsTarget,
repetitionsUnitId: repetitionsUnitId ?? this.repetitionsUnitId,
repetitionsUnitObj: repetitionsUnitObj ?? this.repetitionsUnitObj,
rir: rir ?? this.rir,
rirTarget: rirTarget ?? this.rirTarget,
weight: weight ?? this.weight,
weightTarget: weightTarget ?? this.weightTarget,
weightUnitId: weightUnitId ?? this.weightUnitId,
weightUnitObj: weightUnitObj ?? this.weightUnitObj,
date: date ?? this.date,
);
if (repetitionsUnitObj != null) {
out.repetitionsUnitObj = repetitionsUnitObj;
out.repetitionsUnitId = repetitionsUnitObj.id;
}
if (weightUnitObj != null) {
out.weightUnitObj = weightUnitObj;
out.weightUnitId = weightUnitObj.id;
}
// Logs read from the database have no exercise, so only copy an existing one
if (_exerciseObj != null) {
out.exerciseObj = _exerciseObj!;
}
return out;
}
WorkoutLogTableCompanion toCompanion() {
return WorkoutLogTableCompanion(
id: id != null ? drift.Value(id!) : const drift.Value.absent(),
exerciseId: drift.Value(exerciseId),
routineId: drift.Value(routineId),
sessionId: sessionId != null ? drift.Value(sessionId) : const drift.Value.absent(),
iteration: iteration != null ? drift.Value(iteration) : const drift.Value.absent(),
slotEntryId: slotEntryId != null ? drift.Value(slotEntryId) : const drift.Value.absent(),
rir: rir != null ? drift.Value(rir!.toDouble()) : const drift.Value.absent(),
rirTarget: rirTarget != null
? drift.Value(rirTarget!.toDouble())
: const drift.Value.absent(),
repetitions: repetitions != null
? drift.Value(repetitions!.toDouble())
: const drift.Value.absent(),
repetitionsTarget: repetitionsTarget != null
? drift.Value(repetitionsTarget!.toDouble())
: const drift.Value.absent(),
repetitionsUnitId: repetitionsUnitId != null
? drift.Value(repetitionsUnitId)
: const drift.Value.absent(),
weight: weight != null ? drift.Value(weight!.toDouble()) : const drift.Value.absent(),
weightTarget: weightTarget != null
? drift.Value(weightTarget!.toDouble())
: const drift.Value.absent(),
weightUnitId: weightUnitId != null ? drift.Value(weightUnitId) : const drift.Value.absent(),
date: drift.Value(date),
);
}
set exercise(Exercise exercise) {
exerciseObj = exercise;
exerciseId = exercise.id;
}
set weightUnit(WeightUnit? weightUnit) {
weightUnitObj = weightUnit;
weightUnitId = weightUnit?.id;
}
set repetitionUnit(RepetitionUnit? repetitionUnit) {
repetitionsUnitObj = repetitionUnit;
repetitionsUnitId = repetitionUnit?.id;
}
/// Returns the text representation for a single setting, removes new lines
String repTextNoNl(BuildContext context) {
return repText(context).replaceAll('\n', '');
}
/// Returns the text representation for a single setting
String repText(BuildContext context) {
final List<String> out = [];
if (repetitions != null) {
out.add(formatNum(repetitions!).toString());
// The default repetition unit is 'reps', which we don't show unless there
// is no weight defined so that we don't just output something like "8" but
// rather "8 repetitions". If there is weight we want to output "8 x 50kg",
// since the repetitions are implied. If other units are used, we always
// print them
if (repetitionsUnitObj != null && repetitionsUnitObj!.id != REP_UNIT_REPETITIONS_ID ||
weight == 0 ||
weight == null) {
out.add(getServerStringTranslation(repetitionsUnitObj!.name, context));
}
}
if (weight != null && weight != 0) {
out.add('×');
out.add(formatNum(weight!).toString());
out.add(weightUnitObj!.name);
}
if (rir != null) {
out.add('\n($rir RiR)');
}
return out.join(' ');
}
/// Calculates the volume for this log entry
num volume({bool metric = true}) {
final unitId = metric ? WEIGHT_UNIT_KG : WEIGHT_UNIT_LB;
if (weight != null &&
weightUnitId == unitId &&
repetitions != null &&
repetitionsUnitId == REP_UNIT_REPETITIONS_ID) {
return weight! * repetitions!;
}
return 0;
}
LogTargetStatus get targetStatus {
if (weightTarget == null && repetitionsTarget == null && rirTarget == null) {
return LogTargetStatus.notSet;
}
final weightOk = weightTarget == null || (weight != null && weight! >= weightTarget!);
final repsOk =
repetitionsTarget == null || (repetitions != null && repetitions! >= repetitionsTarget!);
final rirOk = rirTarget == null || (rir != null && rir! <= rirTarget!);
if (weightOk && repsOk && rirOk) {
return LogTargetStatus.atTarget;
}
final weightMore = weightTarget != null && weight != null && weight! > weightTarget!;
final repsMore =
repetitionsTarget != null && repetitions != null && repetitions! > repetitionsTarget!;
final rirLess = rirTarget != null && rir != null && rir! < rirTarget!;
if (weightMore || repsMore || rirLess) {
return LogTargetStatus.moreThanTarget;
}
return LogTargetStatus.lessThanTarget;
}
/// Override the equals operator
///
/// Two logs are considered equal if their content is equal. This is used e.g.
/// in lists where we want to have unique values
@override
//ignore: avoid_equals_and_hash_code_on_mutable_classes
bool operator ==(o) {
return o is Log &&
exerciseId == o.exerciseId &&
weight == o.weight &&
weightUnitId == o.weightUnitId &&
repetitions == o.repetitions &&
repetitionsUnitId == o.repetitionsUnitId &&
rir == o.rir;
}
@override
//ignore: avoid_equals_and_hash_code_on_mutable_classes
int get hashCode =>
Object.hash(exerciseId, weight, weightUnitId, repetitions, repetitionsUnitId, rir);
@override
String toString() {
return 'Log(id: $id, ex: $exerciseId, weightU: $weightUnitId, w: $weight, repU: $repetitionsUnitId, rep: $repetitions, rir: $rir)';
}
}