WT 1.1.20(4) Training plan improvements
This commit is contained in:
@@ -11,7 +11,9 @@ 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/util/app_language.dart';
|
||||
import 'package:aitrainer_app/util/common.dart';
|
||||
import 'package:aitrainer_app/util/enums.dart';
|
||||
import 'package:aitrainer_app/util/track.dart';
|
||||
import 'package:bloc/bloc.dart';
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
@@ -56,12 +58,22 @@ class TrainingPlanBloc extends Bloc<TrainingPlanEvent, TrainingPlanState> {
|
||||
this.activateDays();
|
||||
Cache().myTrainingPlan = _myPlan;
|
||||
await Cache().saveMyTrainingPlan();
|
||||
Track().track(TrackingEvent.training_plan_start, eventValue: event.trainingPlanId.toString());
|
||||
yield TrainingPlanFinished();
|
||||
} else if (event is TrainingPlanWeightChange) {
|
||||
yield TrainingPlanExerciseLoading();
|
||||
event.detail.weight = event.weight;
|
||||
|
||||
yield TrainingPlanExerciseReady();
|
||||
yield TrainingPlanReady();
|
||||
} else if (event is TrainingPlanWeightChangeRecalculate) {
|
||||
yield TrainingPlanExerciseLoading();
|
||||
|
||||
event.detail.repeats =
|
||||
Common.reCalculateRepeatsByChangedWeight(event.detail.weight!, event.detail.repeats!.toDouble(), event.weight);
|
||||
event.detail.weight = event.weight;
|
||||
print(" weight: ${event.weight} new repeats: ${event.detail.repeats}");
|
||||
|
||||
yield TrainingPlanReady();
|
||||
} else if (event is TrainingPlanRepeatsChange) {
|
||||
yield TrainingPlanExerciseLoading();
|
||||
@@ -77,38 +89,51 @@ class TrainingPlanBloc extends Bloc<TrainingPlanEvent, TrainingPlanState> {
|
||||
yield TrainingPlanReady();
|
||||
} else if (event is TrainingPlanSaveExercise) {
|
||||
yield TrainingPlanLoading();
|
||||
|
||||
event.detail.state = ExercisePlanDetailState.inProgress;
|
||||
final Exercise exercise = Exercise();
|
||||
exercise.customerId = Cache().userLoggedIn!.customerId!;
|
||||
exercise.exerciseTypeId = event.detail.exerciseTypeId;
|
||||
exercise.quantity = event.detail.repeats!.toDouble();
|
||||
exercise.unit = event.detail.exerciseType!.unit;
|
||||
exercise.unitQuantity = event.detail.weight;
|
||||
exercise.dateAdd = DateTime.now();
|
||||
event.detail.exercises.add(exercise);
|
||||
if (event.detail.exercises.length >= event.detail.set!) {
|
||||
if (event.detail.weight == -3) {
|
||||
print("DropSet");
|
||||
event.detail.state = ExercisePlanDetailState.finished;
|
||||
} else if (event.detail.exercises.length >= 0) {
|
||||
event.detail.state = ExercisePlanDetailState.inProgress;
|
||||
}
|
||||
// recalculate the weight to the original planned repeats for the next details
|
||||
if (exercise.unitQuantity != null && exercise.unitQuantity! > 0) {
|
||||
for (var nextDetail in _myPlan!.details) {
|
||||
if (nextDetail.exerciseTypeId == event.detail.exerciseTypeId && nextDetail.weight == -2) {
|
||||
trainingPlanRepository.recalculateDetail(_myPlan!.trainingPlanId!, event.detail, nextDetail);
|
||||
yield TrainingPlanReady();
|
||||
} else {
|
||||
final Exercise exercise = Exercise();
|
||||
exercise.customerId = Cache().userLoggedIn!.customerId!;
|
||||
exercise.exerciseTypeId = event.detail.exerciseTypeId;
|
||||
exercise.quantity = event.detail.repeats!.toDouble();
|
||||
exercise.unit = event.detail.exerciseType!.unit;
|
||||
exercise.unitQuantity = event.detail.weight;
|
||||
exercise.dateAdd = DateTime.now();
|
||||
event.detail.exercises.add(exercise);
|
||||
if (event.detail.exercises.length >= event.detail.set!) {
|
||||
event.detail.state = ExercisePlanDetailState.finished;
|
||||
} else if (event.detail.exercises.length >= 0) {
|
||||
event.detail.state = ExercisePlanDetailState.inProgress;
|
||||
}
|
||||
// recalculate the weight to the original planned repeats for the next details
|
||||
|
||||
if (exercise.unitQuantity != null && exercise.unitQuantity! > 0) {
|
||||
for (var nextDetail in _myPlan!.details) {
|
||||
double weightFromPlan = trainingPlanRepository.getOriginalWeight(this.getMyPlan()!.trainingPlanId!, nextDetail);
|
||||
if (nextDetail.exerciseTypeId == event.detail.exerciseTypeId &&
|
||||
nextDetail.weight == -2 &&
|
||||
nextDetail.customerTrainingPlanDetailsId != event.detail.customerTrainingPlanDetailsId) {
|
||||
print("recalculating -2 ${event.detail.customerTrainingPlanDetailsId}");
|
||||
trainingPlanRepository.recalculateDetail(_myPlan!.trainingPlanId!, event.detail, nextDetail);
|
||||
} else if (nextDetail.exerciseTypeId == event.detail.exerciseTypeId && weightFromPlan == -1 && nextDetail.set! > 1) {
|
||||
print("recalculating -1 ${event.detail.customerTrainingPlanDetailsId}");
|
||||
nextDetail = trainingPlanRepository.recalculateDetailFixRepeats(_myPlan!.trainingPlanId!, nextDetail);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
exercise.trainingPlanDetailsId = _myPlan!.trainingPlanId;
|
||||
|
||||
// save Exercise
|
||||
Exercise savedExercise = await ExerciseApi().addExercise(exercise);
|
||||
Cache().addExercise(savedExercise);
|
||||
|
||||
Cache().myTrainingPlan = _myPlan;
|
||||
await Cache().saveMyTrainingPlan();
|
||||
}
|
||||
|
||||
exercise.trainingPlanDetailsId = _myPlan!.trainingPlanId;
|
||||
|
||||
// save Exercise
|
||||
Exercise savedExercise = await ExerciseApi().addExercise(exercise);
|
||||
Cache().addExercise(savedExercise);
|
||||
|
||||
Cache().myTrainingPlan = _myPlan;
|
||||
await Cache().saveMyTrainingPlan();
|
||||
Track().track(TrackingEvent.training_plan_execute, eventValue: event.detail.exerciseType!.name);
|
||||
|
||||
if (isDayDone()) {
|
||||
this.add(TrainingPlanFinishDay());
|
||||
@@ -128,6 +153,7 @@ class TrainingPlanBloc extends Bloc<TrainingPlanEvent, TrainingPlanState> {
|
||||
} else if (event is TrainingPlanFinishDay) {
|
||||
yield TrainingPlanLoading();
|
||||
celebrating = true;
|
||||
Track().track(TrackingEvent.training_plan_finished);
|
||||
yield TrainingPlanDayFinished();
|
||||
} else if (event is TrainingPlanGoToRestart) {
|
||||
yield TrainingPlanLoading();
|
||||
@@ -178,6 +204,7 @@ class TrainingPlanBloc extends Bloc<TrainingPlanEvent, TrainingPlanState> {
|
||||
} else {
|
||||
_myDetail!.weight = 0;
|
||||
}
|
||||
Track().track(TrackingEvent.training_plan_custom, eventValue: event.exerciseType.name);
|
||||
yield TrainingPlanReady();
|
||||
}
|
||||
} on Exception catch (e) {
|
||||
@@ -186,6 +213,9 @@ class TrainingPlanBloc extends Bloc<TrainingPlanEvent, TrainingPlanState> {
|
||||
}
|
||||
|
||||
void addNewPlan() {
|
||||
if (Cache().userLoggedIn == null) {
|
||||
throw Exception("Please log in");
|
||||
}
|
||||
if (_myPlan == null) {
|
||||
_myPlan = CustomerTrainingPlan();
|
||||
} else {
|
||||
@@ -254,6 +284,7 @@ class TrainingPlanBloc extends Bloc<TrainingPlanEvent, TrainingPlanState> {
|
||||
}
|
||||
_myPlan!.days[dayName]!.add(element);
|
||||
});
|
||||
this.addExtraExerciseType("Stretching", previousDay);
|
||||
|
||||
if (dayNames.length == 0) {
|
||||
dayName = "";
|
||||
@@ -280,6 +311,7 @@ class TrainingPlanBloc extends Bloc<TrainingPlanEvent, TrainingPlanState> {
|
||||
detail.parallel = false;
|
||||
detail.restingTime = 0;
|
||||
detail.exerciseType = exerciseType;
|
||||
detail.state = ExercisePlanDetailState.extra;
|
||||
if (_myPlan!.days[dayName] == null) {
|
||||
_myPlan!.days[dayName] = [];
|
||||
}
|
||||
@@ -372,7 +404,10 @@ class TrainingPlanBloc extends Bloc<TrainingPlanEvent, TrainingPlanState> {
|
||||
bool isDone = true;
|
||||
final String day = dayNames[activeDayIndex];
|
||||
for (var detail in _myPlan!.days[day]!) {
|
||||
if (!detail.state.equalsTo(ExercisePlanDetailState.finished) && !detail.state.equalsTo(ExercisePlanDetailState.skipped)) {
|
||||
//print("daydone ${detail.state}");
|
||||
if (!detail.state.equalsTo(ExercisePlanDetailState.extra) &&
|
||||
!detail.state.equalsTo(ExercisePlanDetailState.finished) &&
|
||||
!detail.state.equalsTo(ExercisePlanDetailState.skipped)) {
|
||||
isDone = false;
|
||||
}
|
||||
}
|
||||
@@ -400,6 +435,7 @@ class TrainingPlanBloc extends Bloc<TrainingPlanEvent, TrainingPlanState> {
|
||||
}
|
||||
int index = indexInStart > indexInProgress ? indexInStart : indexInProgress;
|
||||
offset = index * 80;
|
||||
print("Offset: $offset day: $day ($indexInStart, $indexInProgress)");
|
||||
return offset;
|
||||
}
|
||||
|
||||
|
||||
@@ -28,6 +28,15 @@ class TrainingPlanWeightChange extends TrainingPlanEvent {
|
||||
List<Object> get props => [weight, detail];
|
||||
}
|
||||
|
||||
class TrainingPlanWeightChangeRecalculate extends TrainingPlanEvent {
|
||||
final CustomerTrainingPlanDetails detail;
|
||||
final double weight;
|
||||
const TrainingPlanWeightChangeRecalculate({required this.weight, required this.detail});
|
||||
|
||||
@override
|
||||
List<Object> get props => [weight, detail];
|
||||
}
|
||||
|
||||
class TrainingPlanRepeatsChange extends TrainingPlanEvent {
|
||||
final CustomerTrainingPlanDetails detail;
|
||||
final int repeats;
|
||||
|
||||
Reference in New Issue
Block a user