90 lines
3.4 KiB
Dart
90 lines
3.4 KiB
Dart
import 'dart:async';
|
|
import 'package:aitrainer_app/model/exercise_plan_detail.dart';
|
|
import 'package:aitrainer_app/model/exercise_type.dart';
|
|
import 'package:aitrainer_app/model/workout_tree.dart';
|
|
import 'package:aitrainer_app/repository/exercise_plan_repository.dart';
|
|
import 'package:aitrainer_app/repository/exercise_repository.dart';
|
|
import 'package:aitrainer_app/repository/workout_tree_repository.dart';
|
|
import 'package:bloc/bloc.dart';
|
|
import 'package:equatable/equatable.dart';
|
|
import 'package:meta/meta.dart';
|
|
|
|
part 'exercise_plan_event.dart';
|
|
|
|
part 'exercise_plan_state.dart';
|
|
|
|
class ExercisePlanBloc extends Bloc<ExercisePlanEvent, ExercisePlanState> {
|
|
final WorkoutTreeRepository menuTreeRepository;
|
|
final ExerciseRepository exerciseRepository;
|
|
final ExercisePlanRepository exercisePlanRepository = ExercisePlanRepository();
|
|
int customerId;
|
|
|
|
ExercisePlanBloc({this.exerciseRepository, this.menuTreeRepository}) : super(ExercisePlanInitial());
|
|
|
|
Future<void> getData() async {
|
|
exercisePlanRepository.setCustomerId(customerId);
|
|
await exercisePlanRepository.getLastExercisePlan();
|
|
await exercisePlanRepository.getExercisePlanDetails();
|
|
menuTreeRepository.sortedTree = null;
|
|
menuTreeRepository.sortByMuscleType();
|
|
|
|
menuTreeRepository.sortedTree.forEach((key, value) {
|
|
List<WorkoutTree> listWorkoutTree = value;
|
|
listWorkoutTree.forEach((workoutTree) {
|
|
workoutTree.selected = false;
|
|
if (exercisePlanRepository.exercisePlanDetails.length > 0) {
|
|
if (exercisePlanRepository.exercisePlanDetails[workoutTree.exerciseTypeId] != null) {
|
|
//print("bingo");
|
|
workoutTree.selected = true;
|
|
}
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
@override
|
|
Stream<ExercisePlanState> mapEventToState(ExercisePlanEvent event) async* {
|
|
try {
|
|
if (event is ExercisePlanLoad) {
|
|
yield ExercisePlanLoading();
|
|
await this.getData();
|
|
yield ExercisePlanReady();
|
|
}
|
|
if (event is ExercisePlanUpdate) {
|
|
yield ExercisePlanLoading();
|
|
WorkoutTree workoutTree = event.workoutTree;
|
|
if (workoutTree != null) {
|
|
exercisePlanRepository.addExerciseTypeToPlan(workoutTree.exerciseType);
|
|
workoutTree.selected = true;
|
|
}
|
|
yield ExercisePlanReady();
|
|
} else if (event is ExercisePlanRemoveExercise) {
|
|
yield ExercisePlanLoading();
|
|
ExercisePlanDetail planDetail = event.exercisePlanDetail;
|
|
exercisePlanRepository.removeExerciseTypeFromPlan(planDetail.exerciseType);
|
|
this.menuTreeRepository.sortedTree.forEach((key, value) {
|
|
List<WorkoutTree> listTreeItem = value;
|
|
listTreeItem.forEach((element) {
|
|
if ( element.exerciseType.exerciseTypeId == planDetail.exerciseTypeId) {
|
|
element.selected = false;
|
|
}
|
|
});
|
|
});
|
|
yield ExercisePlanReady();
|
|
} else if (event is ExercisePlanUpdateExercise) {
|
|
yield ExercisePlanReady();
|
|
} else if (event is ExercisePlanSave) {
|
|
if (exercisePlanRepository.getExercisePlanDetailSize() == 0) {
|
|
throw Exception("Please select an exercise");
|
|
} else {
|
|
yield ExercisePlanLoading();
|
|
exercisePlanRepository.saveExercisePlan();
|
|
yield ExercisePlanReady();
|
|
}
|
|
}
|
|
} on Exception catch (e) {
|
|
yield ExercisePlanError(message: e.toString());
|
|
}
|
|
}
|
|
}
|