From 1a9ab581a5ad3c0f4048fedc5e47bab5f4a0fc0c Mon Sep 17 00:00:00 2001 From: pankaj-basnet <165250380+pankaj-basnet@users.noreply.github.com> Date: Sun, 3 May 2026 02:26:58 +0545 Subject: [PATCH 1/3] refactor: improve image error handling with pattern matching and URI parsing --- lib/widgets/core/image.dart | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/lib/widgets/core/image.dart b/lib/widgets/core/image.dart index 42b175dca..22ad59ae2 100644 --- a/lib/widgets/core/image.dart +++ b/lib/widgets/core/image.dart @@ -10,23 +10,20 @@ Widget handleImageError( StackTrace? stackTrace, String imageUrl, ) { - final imageFormat = imageUrl.split('.').last.toUpperCase(); + final path = Uri.tryParse(imageUrl)?.path ?? imageUrl; + final imageFormat = path.contains('.') ? path.split('.').last.toUpperCase() : 'UNKNOWN'; final logger = Logger('handleImageError'); logger.warning('Failed to load image $imageUrl: $error, $stackTrace'); // NOTE: for the moment the other error messages are not localized - String message = ''; - switch (error.runtimeType) { - case NetworkImageLoadException: - message = 'Network error'; - case HttpException: - message = 'Http error'; - case FormatException: - //TODO: not sure if this is the right exception for unsupported image formats? - message = AppLocalizations.of(context).imageFormatNotSupported(imageFormat); - default: - message = 'Other exception'; - } + final message = switch (error) { + NetworkImageLoadException _ => 'Network error', + HttpException _ => 'Server connection failed', + SocketException _ => 'No internet connection', + //TODO: not sure if this is the right exception for unsupported image formats? + FormatException _ => AppLocalizations.of(context).imageFormatNotSupported(imageFormat), + _ => 'An unexpected error occurred', + }; return AspectRatio( aspectRatio: 1, From f5789386558bd72f0405515928ba6934b3b2222f Mon Sep 17 00:00:00 2001 From: pankaj-basnet <165250380+pankaj-basnet@users.noreply.github.com> Date: Sun, 3 May 2026 02:29:39 +0545 Subject: [PATCH 2/3] refactor(models): add copyWith to Routine and make replaceExercise immutable --- lib/models/workouts/routine.dart | 47 +++++++++++++++++++++++++++++--- 1 file changed, 43 insertions(+), 4 deletions(-) diff --git a/lib/models/workouts/routine.dart b/lib/models/workouts/routine.dart index b6ac752f4..d3bbdcfaa 100644 --- a/lib/models/workouts/routine.dart +++ b/lib/models/workouts/routine.dart @@ -122,6 +122,38 @@ class Routine { Map toJson() => _$RoutineToJson(this); + Routine copyWith({ + int? id, + DateTime? created, + String? name, + String? description, + bool? fitInWeek, + bool? isTemplate, + bool? isPublic, + DateTime? start, + DateTime? end, + List? days, + List? dayData, + List? dayDataGym, + List? sessions, + }) { + return Routine( + id: id ?? this.id, + created: created ?? this.created, + name: name ?? this.name, + description: description ?? this.description, + fitInWeek: fitInWeek ?? this.fitInWeek, + isTemplate: isTemplate ?? this.isTemplate, + isPublic: isPublic ?? this.isPublic, + start: start ?? this.start, + end: end ?? this.end, + days: days ?? this.days, + dayData: dayData ?? this.dayData, + dayDataGym: dayDataGym ?? this.dayDataGym, + sessions: sessions ?? this.sessions, + ); + } + RoutineTableCompanion toCompanion() { final routineId = id; if (routineId == null) { @@ -237,8 +269,14 @@ class Routine { return groupedLogs; } - void replaceExercise(int oldExerciseId, Exercise newExercise) { - for (final session in sessions) { + Routine replaceExercise(int oldExerciseId, Exercise newExercise) { + final updatedRoutine = this.copyWith( + sessions: List.from(this.sessions), + dayData: List.from(this.dayData), + dayDataGym: List.from(this.dayDataGym), + ); + + for (final session in updatedRoutine.sessions) { for (final log in session.logs) { if (log.exerciseId == oldExerciseId) { log.exerciseId = newExercise.id!; @@ -247,7 +285,7 @@ class Routine { } } - for (final day in dayData) { + for (final day in updatedRoutine.dayData) { for (final slot in day.slots) { for (final config in slot.setConfigs) { if (config.exerciseId == oldExerciseId) { @@ -258,7 +296,7 @@ class Routine { } } - for (final day in dayDataGym) { + for (final day in updatedRoutine.dayDataGym) { for (final slot in day.slots) { for (final config in slot.setConfigs) { if (config.exerciseId == oldExerciseId) { @@ -268,5 +306,6 @@ class Routine { } } } + return updatedRoutine; } } From 11a83932d3ab9509535dac1016c5d126784f7fb2 Mon Sep 17 00:00:00 2001 From: pankaj-basnet <165250380+pankaj-basnet@users.noreply.github.com> Date: Sun, 3 May 2026 02:30:38 +0545 Subject: [PATCH 3/3] fix(gym): resolve in-place routine mutation in replaceExercises --- lib/providers/gym_state.dart | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/providers/gym_state.dart b/lib/providers/gym_state.dart index ff8777d30..d1b028904 100644 --- a/lib/providers/gym_state.dart +++ b/lib/providers/gym_state.dart @@ -676,10 +676,11 @@ class GymStateNotifier extends _$GymStateNotifier { return page.copyWith(slotPages: updatedSlotPages); }).toList(); - // TODO: this should not be done in-place! - state.routine.replaceExercise(originalExerciseId, newExercise); + // replace and update new immutable routine instance + final updatedRoutine = state.routine.replaceExercise(originalExerciseId, newExercise); state = state.copyWith( pages: updatedPages, + routine: updatedRoutine, ); _logger.fine('Replaced exercise $originalExerciseId with ${newExercise.id}'); }