167 lines
5.5 KiB
Dart
167 lines
5.5 KiB
Dart
import 'dart:collection';
|
|
|
|
import 'package:aitrainer_app/model/cache.dart';
|
|
import 'package:aitrainer_app/model/exercise_plan.dart';
|
|
import 'package:aitrainer_app/model/exercise_plan_detail.dart';
|
|
import 'package:aitrainer_app/model/exercise_type.dart';
|
|
import 'package:aitrainer_app/service/exercise_plan_service.dart';
|
|
|
|
class ExercisePlanRepository {
|
|
bool newPlan = true;
|
|
ExercisePlan _exercisePlan;
|
|
LinkedHashMap<int, ExercisePlanDetail> exercisePlanDetails =
|
|
LinkedHashMap<int, ExercisePlanDetail>();
|
|
LinkedHashMap<int, ExercisePlanDetail> _origExercisePlanDetails =
|
|
LinkedHashMap<int, ExercisePlanDetail>();
|
|
int _customerId = 0;
|
|
ExercisePlanDetail actualPlanDetail;
|
|
|
|
void setCustomerId( int customerId ) {
|
|
this._customerId = customerId;
|
|
}
|
|
|
|
int getCustomerId() {
|
|
return this._customerId;
|
|
}
|
|
|
|
void addExerciseTypeToPlan(ExerciseType exerciseType) {
|
|
setActualPlanDetail(exerciseType);
|
|
}
|
|
|
|
void addToPlan() {
|
|
exercisePlanDetails[actualPlanDetail.exerciseTypeId] = actualPlanDetail;
|
|
}
|
|
|
|
void setActualPlanDetail(ExerciseType exerciseType) {
|
|
ExercisePlanDetail detail = exercisePlanDetails[exerciseType.exerciseTypeId];
|
|
if ( detail != null ) {
|
|
actualPlanDetail = detail;
|
|
} else {
|
|
actualPlanDetail = ExercisePlanDetail(exerciseType.exerciseTypeId);
|
|
}
|
|
actualPlanDetail.exerciseType = exerciseType;
|
|
}
|
|
|
|
int getPlanDetailId(int exerciseTypeId) {
|
|
ExercisePlanDetail detail = exercisePlanDetails[exerciseTypeId];
|
|
return detail.exercisePlanDetailId;
|
|
}
|
|
|
|
String getPlanDetail(int exerciseTypeId) {
|
|
ExercisePlanDetail detail = exercisePlanDetails[exerciseTypeId];
|
|
String detailString = "";
|
|
if ( detail != null) {
|
|
detailString =
|
|
detail.serie.toString() + "x" + detail.repeats.toString() + " " +
|
|
detail.weightEquation + "kg";
|
|
}
|
|
return detailString;
|
|
}
|
|
|
|
int getExercisePlanDetailSize() {
|
|
return exercisePlanDetails.length;
|
|
}
|
|
|
|
void updateExercisePlanDetail(ExerciseType exerciseType, int serie, int repeat, String weight) {
|
|
if ( exercisePlanDetails[exerciseType.exerciseTypeId] == null) {
|
|
return;
|
|
}
|
|
ExercisePlanDetail exercisePlanDetail = exercisePlanDetails[exerciseType.exerciseTypeId];
|
|
exercisePlanDetail.serie = serie;
|
|
exercisePlanDetail.repeats = repeat;
|
|
exercisePlanDetail.weightEquation = weight;
|
|
|
|
exercisePlanDetails[exerciseType.exerciseTypeId] = exercisePlanDetail;
|
|
}
|
|
|
|
void removeExerciseTypeFromPlan(ExerciseType exerciseType) {
|
|
exercisePlanDetails.remove(exerciseType.exerciseTypeId);
|
|
}
|
|
|
|
Future<void> saveExercisePlan() async {
|
|
|
|
if ( _exercisePlan == null ) {
|
|
if ( Cache().userLoggedIn == null ) {
|
|
throw Exception("please log in");
|
|
}
|
|
|
|
String exercisePlanName;
|
|
if ( this._customerId == Cache().userLoggedIn.customerId) {
|
|
exercisePlanName = Cache().userLoggedIn.name + " private";
|
|
} else {
|
|
exercisePlanName = Cache().getTrainee().name + " " + Cache().getTrainee().firstname + " private";
|
|
}
|
|
|
|
_exercisePlan = ExercisePlan(exercisePlanName, this._customerId);
|
|
}
|
|
if ( newPlan ) {
|
|
_exercisePlan.dateAdd = DateTime.now();
|
|
_exercisePlan.private = true;
|
|
ExercisePlan savedExercisePlan =
|
|
await ExercisePlanApi().saveExercisePlan(_exercisePlan);
|
|
|
|
exercisePlanDetails.forEach((exerciseTypeId, exercisePlanDetail) async {
|
|
exercisePlanDetail.exercisePlanId = savedExercisePlan.exercisePlanId;
|
|
await ExercisePlanApi().saveExercisePlanDetail(exercisePlanDetail);
|
|
});
|
|
} else {
|
|
|
|
await ExercisePlanApi().updateExercisePlan(_exercisePlan, _exercisePlan.exercisePlanId);
|
|
|
|
_origExercisePlanDetails.forEach((exerciseTypeId, exercisePlanDetail) async {
|
|
if (exercisePlanDetails[exercisePlanDetail.exercisePlanDetailId] == null) {
|
|
await ExercisePlanApi()
|
|
.deleteExercisePlanDetail(exercisePlanDetail.exercisePlanDetailId);
|
|
} else {
|
|
await ExercisePlanApi()
|
|
.updateExercisePlanDetail(exercisePlanDetail, exercisePlanDetail.exercisePlanDetailId);
|
|
}
|
|
});
|
|
|
|
exercisePlanDetails.forEach((exerciseTypeId, exercisePlanDetail) async{
|
|
exercisePlanDetail.exercisePlanId = _exercisePlan.exercisePlanId;
|
|
if ( _origExercisePlanDetails[exercisePlanDetail.exercisePlanDetailId] == null) {
|
|
await ExercisePlanApi()
|
|
.saveExercisePlanDetail(exercisePlanDetail);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
Future<ExercisePlan> getLastExercisePlan() async {
|
|
if ( _customerId == 0) {
|
|
return null;
|
|
}
|
|
_exercisePlan = await ExercisePlanApi().getLastExercisePlan(_customerId);
|
|
newPlan = (_exercisePlan == null);
|
|
print("New plan: " + newPlan.toString());
|
|
|
|
return _exercisePlan;
|
|
}
|
|
|
|
Future<void> getExercisePlanDetails() async {
|
|
if (_exercisePlan == null) {
|
|
ExercisePlan exercisePlan = await this.getLastExercisePlan();
|
|
if ( exercisePlan == null ) {
|
|
exercisePlanDetails = LinkedHashMap<int, ExercisePlanDetail>();
|
|
_origExercisePlanDetails = LinkedHashMap<int, ExercisePlanDetail>();
|
|
return;
|
|
}
|
|
}
|
|
exercisePlanDetails = LinkedHashMap<int, ExercisePlanDetail>();
|
|
_origExercisePlanDetails = LinkedHashMap<int, ExercisePlanDetail>();
|
|
|
|
List<ExercisePlanDetail> list =
|
|
await ExercisePlanApi().getExercisePlanDetail(_exercisePlan.exercisePlanId);
|
|
|
|
list.forEach((element) {
|
|
newPlan = false;
|
|
ExercisePlanDetail detail = element;
|
|
_origExercisePlanDetails[detail.exerciseTypeId] = detail;
|
|
exercisePlanDetails[detail.exerciseTypeId] = detail;
|
|
});
|
|
|
|
return;
|
|
}
|
|
|
|
} |