wt1.1.1 ExercisePlan + ExercisePlan detail, Trainee
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
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/menu_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 MenuTreeRepository 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());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
part of 'exercise_plan_bloc.dart';
|
||||
|
||||
@immutable
|
||||
abstract class ExercisePlanEvent extends Equatable {
|
||||
const ExercisePlanEvent();
|
||||
@override
|
||||
List<Object> get props => [];
|
||||
}
|
||||
|
||||
class ExercisePlanLoad extends ExercisePlanEvent {
|
||||
const ExercisePlanLoad();
|
||||
}
|
||||
|
||||
class ExercisePlanUpdate extends ExercisePlanEvent {
|
||||
final WorkoutTree workoutTree;
|
||||
const ExercisePlanUpdate({this.workoutTree});
|
||||
|
||||
@override
|
||||
List<Object> get props => [workoutTree];
|
||||
}
|
||||
|
||||
class ExercisePlanAddExercise extends ExercisePlanEvent {
|
||||
final ExerciseType exerciseType;
|
||||
const ExercisePlanAddExercise({this.exerciseType});
|
||||
|
||||
@override
|
||||
List<Object> get props => [exerciseType];
|
||||
}
|
||||
|
||||
class ExercisePlanRemoveExercise extends ExercisePlanEvent {
|
||||
final ExercisePlanDetail exercisePlanDetail;
|
||||
const ExercisePlanRemoveExercise({this.exercisePlanDetail});
|
||||
|
||||
@override
|
||||
List<Object> get props => [exercisePlanDetail];
|
||||
}
|
||||
|
||||
class ExercisePlanUpdateExercise extends ExercisePlanEvent {
|
||||
final ExercisePlanDetail exercisePlanDetail;
|
||||
const ExercisePlanUpdateExercise({this.exercisePlanDetail});
|
||||
|
||||
@override
|
||||
List<Object> get props => [exercisePlanDetail];
|
||||
}
|
||||
|
||||
class ExercisePlanSave extends ExercisePlanEvent {
|
||||
const ExercisePlanSave();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
part of 'exercise_plan_bloc.dart';
|
||||
|
||||
@immutable
|
||||
abstract class ExercisePlanState extends Equatable{
|
||||
const ExercisePlanState();
|
||||
|
||||
@override
|
||||
List<Object> get props => [];
|
||||
}
|
||||
|
||||
// Display the last saved exercise plan
|
||||
class ExercisePlanInitial extends ExercisePlanState {
|
||||
const ExercisePlanInitial();
|
||||
}
|
||||
|
||||
// Loading screen
|
||||
class ExercisePlanLoading extends ExercisePlanState {
|
||||
const ExercisePlanLoading();
|
||||
}
|
||||
|
||||
// updated screen
|
||||
class ExercisePlanReady extends ExercisePlanState {
|
||||
const ExercisePlanReady();
|
||||
}
|
||||
|
||||
// error splash screen
|
||||
class ExercisePlanError extends ExercisePlanState {
|
||||
final String message;
|
||||
const ExercisePlanError({this.message});
|
||||
|
||||
@override
|
||||
List<Object> get props => [message];
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user