126 lines
4.1 KiB
Dart
126 lines
4.1 KiB
Dart
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
|
import 'package:trainhub_flutter/injection.dart';
|
|
import 'package:trainhub_flutter/domain/entities/program_workout.dart';
|
|
import 'package:trainhub_flutter/domain/repositories/program_repository.dart';
|
|
import 'package:trainhub_flutter/domain/repositories/training_plan_repository.dart';
|
|
import 'package:trainhub_flutter/domain/repositories/exercise_repository.dart';
|
|
import 'package:trainhub_flutter/presentation/calendar/calendar_state.dart';
|
|
|
|
part 'calendar_controller.g.dart';
|
|
|
|
@riverpod
|
|
class CalendarController extends _$CalendarController {
|
|
late ProgramRepository _programRepo;
|
|
late TrainingPlanRepository _planRepo;
|
|
late ExerciseRepository _exerciseRepo;
|
|
|
|
@override
|
|
Future<CalendarState> build() async {
|
|
_programRepo = getIt<ProgramRepository>();
|
|
_planRepo = getIt<TrainingPlanRepository>();
|
|
_exerciseRepo = getIt<ExerciseRepository>();
|
|
final programs = await _programRepo.getAllPrograms();
|
|
final plans = await _planRepo.getAll();
|
|
final exercises = await _exerciseRepo.getAll();
|
|
if (programs.isEmpty) {
|
|
return CalendarState(plans: plans, exercises: exercises);
|
|
}
|
|
final activeProgram = programs.first;
|
|
final weeks = await _programRepo.getWeeks(activeProgram.id);
|
|
final workouts = await _programRepo.getWorkouts(activeProgram.id);
|
|
return CalendarState(
|
|
programs: programs,
|
|
activeProgram: activeProgram,
|
|
weeks: weeks,
|
|
workouts: workouts,
|
|
plans: plans,
|
|
exercises: exercises,
|
|
);
|
|
}
|
|
|
|
Future<void> loadProgram(String id) async {
|
|
final program = await _programRepo.getProgram(id);
|
|
if (program == null) return;
|
|
final weeks = await _programRepo.getWeeks(id);
|
|
final workouts = await _programRepo.getWorkouts(id);
|
|
final current = state.valueOrNull ?? const CalendarState();
|
|
state = AsyncValue.data(
|
|
current.copyWith(
|
|
activeProgram: program,
|
|
weeks: weeks,
|
|
workouts: workouts,
|
|
),
|
|
);
|
|
}
|
|
|
|
Future<void> createProgram(String name) async {
|
|
final program = await _programRepo.createProgram(name);
|
|
await _reloadFull();
|
|
await loadProgram(program.id);
|
|
}
|
|
|
|
Future<void> deleteProgram(String id) async {
|
|
await _programRepo.deleteProgram(id);
|
|
state = await AsyncValue.guard(() => build());
|
|
}
|
|
|
|
Future<void> duplicateProgram(String sourceId) async {
|
|
await _programRepo.duplicateProgram(sourceId);
|
|
await _reloadFull();
|
|
}
|
|
|
|
Future<void> addWeek() async {
|
|
final current = state.valueOrNull;
|
|
if (current?.activeProgram == null) return;
|
|
final nextPosition =
|
|
current!.weeks.isEmpty ? 1 : current.weeks.last.position + 1;
|
|
await _programRepo.addWeek(current.activeProgram!.id, nextPosition);
|
|
await _reloadProgramDetails();
|
|
}
|
|
|
|
Future<void> deleteWeek(String id) async {
|
|
await _programRepo.deleteWeek(id);
|
|
await _reloadProgramDetails();
|
|
}
|
|
|
|
Future<void> updateWeekNote(String weekId, String note) async {
|
|
await _programRepo.updateWeekNote(weekId, note);
|
|
await _reloadProgramDetails();
|
|
}
|
|
|
|
Future<void> addWorkout(ProgramWorkoutEntity workout) async {
|
|
await _programRepo.addWorkout(workout);
|
|
await _reloadProgramDetails();
|
|
}
|
|
|
|
Future<void> updateWorkout(ProgramWorkoutEntity workout) async {
|
|
await _programRepo.updateWorkout(workout);
|
|
await _reloadProgramDetails();
|
|
}
|
|
|
|
Future<void> deleteWorkout(String id) async {
|
|
await _programRepo.deleteWorkout(id);
|
|
await _reloadProgramDetails();
|
|
}
|
|
|
|
Future<void> toggleWorkoutComplete(String id, bool currentStatus) async {
|
|
await _programRepo.toggleWorkoutComplete(id, currentStatus);
|
|
await _reloadProgramDetails();
|
|
}
|
|
|
|
Future<void> _reloadProgramDetails() async {
|
|
final current = state.valueOrNull;
|
|
if (current?.activeProgram == null) return;
|
|
final weeks = await _programRepo.getWeeks(current!.activeProgram!.id);
|
|
final workouts =
|
|
await _programRepo.getWorkouts(current.activeProgram!.id);
|
|
state = AsyncValue.data(
|
|
current.copyWith(weeks: weeks, workouts: workouts),
|
|
);
|
|
}
|
|
|
|
Future<void> _reloadFull() async {
|
|
state = await AsyncValue.guard(() => build());
|
|
}
|
|
}
|