This commit is contained in:
@@ -3,7 +3,6 @@ import 'package:trainhub_flutter/core/utils/id_generator.dart';
|
||||
import 'package:trainhub_flutter/injection.dart';
|
||||
import 'package:trainhub_flutter/domain/entities/exercise.dart';
|
||||
import 'package:trainhub_flutter/domain/entities/training_exercise.dart';
|
||||
import 'package:trainhub_flutter/domain/entities/training_plan.dart';
|
||||
import 'package:trainhub_flutter/domain/entities/training_section.dart';
|
||||
import 'package:trainhub_flutter/domain/repositories/exercise_repository.dart';
|
||||
import 'package:trainhub_flutter/domain/repositories/training_plan_repository.dart';
|
||||
@@ -14,14 +13,15 @@ part 'plan_editor_controller.g.dart';
|
||||
@riverpod
|
||||
class PlanEditorController extends _$PlanEditorController {
|
||||
late TrainingPlanRepository _planRepo;
|
||||
late ExerciseRepository _exerciseRepo;
|
||||
|
||||
@override
|
||||
Future<PlanEditorState> build(String planId) async {
|
||||
_planRepo = getIt<TrainingPlanRepository>();
|
||||
final ExerciseRepository exerciseRepo = getIt<ExerciseRepository>();
|
||||
_exerciseRepo = getIt<ExerciseRepository>();
|
||||
|
||||
final plan = await _planRepo.getById(planId);
|
||||
final exercises = await exerciseRepo.getAll();
|
||||
final exercises = await _exerciseRepo.getAll();
|
||||
|
||||
return PlanEditorState(plan: plan, availableExercises: exercises);
|
||||
}
|
||||
@@ -64,6 +64,21 @@ class PlanEditorController extends _$PlanEditorController {
|
||||
);
|
||||
}
|
||||
|
||||
void reorderSection(int oldIndex, int newIndex) {
|
||||
final current = state.valueOrNull;
|
||||
if (current == null) return;
|
||||
final sections = List<TrainingSectionEntity>.from(current.plan.sections);
|
||||
if (oldIndex < newIndex) newIndex -= 1;
|
||||
final item = sections.removeAt(oldIndex);
|
||||
sections.insert(newIndex, item);
|
||||
state = AsyncValue.data(
|
||||
current.copyWith(
|
||||
plan: current.plan.copyWith(sections: sections),
|
||||
isDirty: true,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void updateSectionName(int sectionIndex, String name) {
|
||||
final current = state.valueOrNull;
|
||||
if (current == null) return;
|
||||
@@ -116,6 +131,37 @@ class PlanEditorController extends _$PlanEditorController {
|
||||
);
|
||||
}
|
||||
|
||||
void moveExerciseBetweenSections({
|
||||
required int fromSectionIndex,
|
||||
required int exerciseIndex,
|
||||
required int toSectionIndex,
|
||||
}) {
|
||||
final current = state.valueOrNull;
|
||||
if (current == null) return;
|
||||
if (fromSectionIndex == toSectionIndex) return;
|
||||
final sections = List<TrainingSectionEntity>.from(current.plan.sections);
|
||||
final fromExercises = List<TrainingExerciseEntity>.from(
|
||||
sections[fromSectionIndex].exercises,
|
||||
);
|
||||
final toExercises = List<TrainingExerciseEntity>.from(
|
||||
sections[toSectionIndex].exercises,
|
||||
);
|
||||
final exercise = fromExercises.removeAt(exerciseIndex);
|
||||
toExercises.add(exercise);
|
||||
sections[fromSectionIndex] = sections[fromSectionIndex].copyWith(
|
||||
exercises: fromExercises,
|
||||
);
|
||||
sections[toSectionIndex] = sections[toSectionIndex].copyWith(
|
||||
exercises: toExercises,
|
||||
);
|
||||
state = AsyncValue.data(
|
||||
current.copyWith(
|
||||
plan: current.plan.copyWith(sections: sections),
|
||||
isDirty: true,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void updateExerciseParams(
|
||||
int sectionIndex,
|
||||
int exerciseIndex, {
|
||||
@@ -168,6 +214,25 @@ class PlanEditorController extends _$PlanEditorController {
|
||||
);
|
||||
}
|
||||
|
||||
Future<ExerciseEntity> createExercise({
|
||||
required String name,
|
||||
String? instructions,
|
||||
String? tags,
|
||||
String? videoUrl,
|
||||
}) async {
|
||||
final current = state.valueOrNull;
|
||||
if (current == null) throw StateError('Controller state not loaded');
|
||||
final exercise = await _exerciseRepo.create(
|
||||
name: name,
|
||||
instructions: instructions,
|
||||
tags: tags,
|
||||
videoUrl: videoUrl,
|
||||
);
|
||||
final exercises = await _exerciseRepo.getAll();
|
||||
state = AsyncValue.data(current.copyWith(availableExercises: exercises));
|
||||
return exercise;
|
||||
}
|
||||
|
||||
Future<void> save() async {
|
||||
final current = state.valueOrNull;
|
||||
if (current == null) return;
|
||||
|
||||
Reference in New Issue
Block a user