WT1.1.18+2 Training Plan days, skip
This commit is contained in:
@@ -4,13 +4,11 @@ import 'package:aitrainer_app/bloc/menu/menu_bloc.dart';
|
||||
import 'package:aitrainer_app/model/cache.dart';
|
||||
import 'package:aitrainer_app/model/customer_training_plan.dart';
|
||||
import 'package:aitrainer_app/model/customer_training_plan_details.dart';
|
||||
import 'package:aitrainer_app/model/customer_training_plan_exercise.dart';
|
||||
import 'package:aitrainer_app/model/exercise.dart';
|
||||
import 'package:aitrainer_app/model/exercise_plan_detail.dart';
|
||||
import 'package:aitrainer_app/model/workout_menu_tree.dart';
|
||||
import 'package:aitrainer_app/repository/training_plan_repository.dart';
|
||||
import 'package:aitrainer_app/service/exercise_service.dart';
|
||||
import 'package:aitrainer_app/service/training_plan_service.dart';
|
||||
import 'package:bloc/bloc.dart';
|
||||
import 'package:equatable/equatable.dart';
|
||||
|
||||
@@ -22,19 +20,21 @@ class TrainingPlanBloc extends Bloc<TrainingPlanEvent, TrainingPlanState> {
|
||||
final MenuBloc menuBloc;
|
||||
TrainingPlanBloc({required this.trainingPlanRepository, required this.menuBloc}) : super(TrainingPlanInitial());
|
||||
|
||||
CustomerTrainingPlan? myPlan;
|
||||
CustomerTrainingPlan? _myPlan;
|
||||
bool started = false;
|
||||
final List<String> dayNames = [];
|
||||
|
||||
CustomerTrainingPlan? getMyPlan() => this.myPlan;
|
||||
setMyPlan(CustomerTrainingPlan myPlan) => this.myPlan = myPlan;
|
||||
CustomerTrainingPlan? getMyPlan() => this._myPlan;
|
||||
setMyPlan(CustomerTrainingPlan? myPlan) => this._myPlan = myPlan;
|
||||
|
||||
@override
|
||||
Stream<TrainingPlanState> mapEventToState(TrainingPlanEvent event) async* {
|
||||
try {
|
||||
if (event is TrainingPlanActivate) {
|
||||
yield TrainingPlanLoading();
|
||||
myPlan = await trainingPlanRepository.activateTrainingPlan(event.trainingPlanId);
|
||||
Cache().myTrainingPlan = myPlan;
|
||||
_myPlan = await trainingPlanRepository.activateTrainingPlan(event.trainingPlanId);
|
||||
this.activateDays();
|
||||
Cache().myTrainingPlan = _myPlan;
|
||||
await Cache().saveMyTrainingPlan();
|
||||
yield TrainingPlanFinished();
|
||||
} else if (event is TrainingPlanWeightChange) {
|
||||
@@ -66,13 +66,19 @@ class TrainingPlanBloc extends Bloc<TrainingPlanEvent, TrainingPlanState> {
|
||||
event.detail.state = ExercisePlanDetailState.inProgress;
|
||||
}
|
||||
|
||||
exercise.trainingPlanDetailsId = myPlan!.trainingPlanId;
|
||||
exercise.trainingPlanDetailsId = _myPlan!.trainingPlanId;
|
||||
|
||||
// save Exercise
|
||||
await ExerciseApi().addExercise(exercise);
|
||||
Cache().addExercise(exercise);
|
||||
|
||||
Cache().myTrainingPlan = myPlan;
|
||||
Cache().myTrainingPlan = _myPlan;
|
||||
await Cache().saveMyTrainingPlan();
|
||||
yield TrainingPlanReady();
|
||||
} else if (event is TrainingPlanSkipExercise) {
|
||||
yield TrainingPlanLoading();
|
||||
event.detail.state = ExercisePlanDetailState.skipped;
|
||||
Cache().myTrainingPlan = _myPlan;
|
||||
await Cache().saveMyTrainingPlan();
|
||||
yield TrainingPlanReady();
|
||||
}
|
||||
@@ -81,13 +87,41 @@ class TrainingPlanBloc extends Bloc<TrainingPlanEvent, TrainingPlanState> {
|
||||
}
|
||||
}
|
||||
|
||||
void activateDays() {
|
||||
if (_myPlan == null) {
|
||||
return;
|
||||
}
|
||||
dayNames.clear();
|
||||
_myPlan!.days.clear();
|
||||
String dayName = ".";
|
||||
_myPlan!.details.forEach((element) {
|
||||
if (element.day != null && element.day != dayName) {
|
||||
dayNames.add(element.day!);
|
||||
dayName = element.day!;
|
||||
}
|
||||
if (_myPlan!.days[dayName] == null) {
|
||||
if (dayName == ".") {
|
||||
dayName = "";
|
||||
}
|
||||
_myPlan!.days[dayName] = [];
|
||||
}
|
||||
_myPlan!.days[dayName]!.add(element);
|
||||
});
|
||||
|
||||
if (dayNames.length == 0) {
|
||||
dayNames.add("");
|
||||
_myPlan!.days[""] = [];
|
||||
_myPlan!.days[""]!.addAll(_myPlan!.details);
|
||||
}
|
||||
}
|
||||
|
||||
CustomerTrainingPlanDetails? getTrainingPlanDetail(int trainingPlanDetailsId) {
|
||||
CustomerTrainingPlanDetails? detail;
|
||||
if (myPlan == null || myPlan!.details.isEmpty) {
|
||||
if (_myPlan == null || _myPlan!.details.isEmpty) {
|
||||
return null;
|
||||
}
|
||||
|
||||
for (final det in this.myPlan!.details) {
|
||||
for (final det in this._myPlan!.details) {
|
||||
if (det.customerTrainingPlanDetailsId == trainingPlanDetailsId) {
|
||||
detail = det;
|
||||
break;
|
||||
@@ -118,21 +152,21 @@ class TrainingPlanBloc extends Bloc<TrainingPlanEvent, TrainingPlanState> {
|
||||
}
|
||||
|
||||
CustomerTrainingPlanDetails? getNext() {
|
||||
if (myPlan == null || myPlan!.details.isEmpty) {
|
||||
if (_myPlan == null || _myPlan!.details.isEmpty) {
|
||||
return null;
|
||||
}
|
||||
|
||||
CustomerTrainingPlanDetails? next;
|
||||
int minStep = 99;
|
||||
for (final detail in this.myPlan!.details) {
|
||||
for (final detail in this._myPlan!.details) {
|
||||
if (!detail.state.equalsTo(ExercisePlanDetailState.finished)) {
|
||||
if (detail.exercises.isEmpty) {
|
||||
if (detail.exercises.isEmpty && !detail.state.equalsTo(ExercisePlanDetailState.skipped)) {
|
||||
next = detail;
|
||||
minStep = 1;
|
||||
break;
|
||||
} else {
|
||||
final int step = detail.exercises.length;
|
||||
if (step < minStep) {
|
||||
if (step < minStep && !detail.state.equalsTo(ExercisePlanDetailState.skipped)) {
|
||||
next = detail;
|
||||
minStep = step;
|
||||
if (detail.parallel != true) {
|
||||
@@ -142,19 +176,41 @@ class TrainingPlanBloc extends Bloc<TrainingPlanEvent, TrainingPlanState> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
print("Next detail $next");
|
||||
return next;
|
||||
}
|
||||
|
||||
bool isStarted() {
|
||||
if (myPlan == null || myPlan!.details.isEmpty) {
|
||||
if (_myPlan == null || _myPlan!.details.isEmpty) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (myPlan!.details[0].state == ExercisePlanDetailState.start) {
|
||||
if (_myPlan!.details[0].state == ExercisePlanDetailState.start) {
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
double getOffset() {
|
||||
double offset = 5;
|
||||
if (_myPlan == null) {
|
||||
return offset;
|
||||
}
|
||||
int indexInProgress = 0;
|
||||
int indexInStart = 0;
|
||||
for (var detail in _myPlan!.details) {
|
||||
if (detail.state == ExercisePlanDetailState.inProgress) {
|
||||
break;
|
||||
}
|
||||
if (detail.state == ExercisePlanDetailState.start) {
|
||||
break;
|
||||
}
|
||||
indexInStart++;
|
||||
indexInProgress++;
|
||||
}
|
||||
int index = indexInStart > indexInProgress ? indexInStart : indexInProgress;
|
||||
offset = index * 80;
|
||||
return offset;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -50,5 +50,9 @@ class TrainingPlanFinishTraining extends TrainingPlanEvent {
|
||||
}
|
||||
|
||||
class TrainingPlanSkipExercise extends TrainingPlanEvent {
|
||||
const TrainingPlanSkipExercise();
|
||||
final CustomerTrainingPlanDetails detail;
|
||||
const TrainingPlanSkipExercise({required this.detail});
|
||||
|
||||
@override
|
||||
List<Object> get props => [detail];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user