addSetToExercise method

Future<bool> addSetToExercise(
  1. int exerciseIndex, {
  2. int? targetReps,
  3. double? targetWeight,
  4. Duration? rest,
})

Implementation

Future<bool> addSetToExercise(
  int exerciseIndex, {
  int? targetReps,
  double? targetWeight,
  Duration? rest,
}) async {
  if (_plan == null) return false;
  if (exerciseIndex < 0 || exerciseIndex >= (_plan!.exercises.length))
    return false;
  final ex = _plan!.exercises[exerciseIndex];
  final newSet = WorkoutSet(
    targetReps: targetReps ?? 10,
    targetWeight: targetWeight,
    rest: rest ?? defaultSetRest,
  );
  final updatedSets = [...ex.sets, newSet];
  final updatedExercise = ex.copyWith(sets: updatedSets);
  final updatedExercises = [..._plan!.exercises];
  updatedExercises[exerciseIndex] = updatedExercise;
  _plan = WorkoutPlan(
    id: _plan!.id,
    name: _plan!.name,
    exercises: updatedExercises,
  );
  await _storage.savePlan(json: _plan!.toJson());
  notifyListeners();
  return true;
}