wt1.1.1 ExercisePlan + ExercisePlan detail, Trainee

This commit is contained in:
Bossanyi Tibor
2020-09-16 15:41:39 +02:00
parent f0ba820385
commit 0b4ff2fb7f
54 changed files with 2985 additions and 313 deletions
+51 -7
View File
@@ -1,3 +1,4 @@
import 'package:aitrainer_app/model/cache.dart';
import 'package:aitrainer_app/model/customer.dart';
import 'package:aitrainer_app/service/customer_service.dart';
@@ -9,6 +10,9 @@ class GenderItem {
class CustomerRepository {
Customer customer;
Customer _trainee;
List<Customer> _trainees;
//List<CustomerRepository> customerList = List<CustomerRepository>();
bool visibleDetails = false;
List<GenderItem> genders;
@@ -131,13 +135,53 @@ class CustomerRepository {
await CustomerApi().saveCustomer(modelCustomer);
}
/* Future<List<CustomerRepository>> getCustomers() async {
final results = await CustomerApi().getRealCustomers("");
this.customerList = results.map((item) => CustomerRepository(customer: item)).toList();
return this.customerList;
Future<Customer> getTraineeAsCustomer() async {
this._trainee = await CustomerApi().getTrainee(
Cache().userLoggedIn.customerId
);
return _trainee;
}
addNewCustomerToList(CustomerRepository customerViewModel) {
customerList.add(customerViewModel);
}*/
Future<List<Customer>> getTrainees() async {
int trainerId = Cache().userLoggedIn.customerId;
final results = await CustomerApi().getTrainees(trainerId);
this._trainees = results;
return results;
}
List<Customer> getTraineesList() {
return _trainees;
}
void setTrainee(int traineeId ) {
if ( _trainees == null ) {
return;
}
_trainees.forEach((element) {
if ( traineeId == element.customerId) {
this._trainee = element;
}
});
}
void emptyTrainees() {
_trainees = null;
_trainee = null;
}
Customer getTrainee() {
return this._trainee;
}
Customer getTraineeById(int customerId) {
if (_trainees == null) {
return null;
}
_trainees.forEach((element) {
if ( customerId == element.customerId) {
this._trainee = element;
}
});
return _trainee;
}
}
@@ -0,0 +1,167 @@
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;
}
}
+22 -7
View File
@@ -72,8 +72,12 @@ class ExerciseRepository {
final Exercise modelExercise = this.exercise;
modelExercise.customerId = this.customer.customerId;
modelExercise.exerciseTypeId = this.exerciseType.exerciseTypeId;
await ExerciseApi().addExercise(modelExercise);
Cache().addExercise(exercise);
Exercise savedExercise = await ExerciseApi().addExercise(modelExercise);
if ( customer.customerId == Cache().userLoggedIn.customerId) {
Cache().addExercise(savedExercise);
} else if ( Cache().getTrainee() != null && customer.customerId == Cache().getTrainee().customerId ) {
Cache().addExerciseTrainee(savedExercise);
}
}
@@ -89,15 +93,26 @@ class ExerciseRepository {
Future<List<Exercise>> getExercisesByCustomer( int customerId ) async {
final results = await ExerciseApi().getExercisesByCustomer(customerId);
this.exerciseList = results;
Cache().setExercises(exerciseList);
if ( customerId == Cache().userLoggedIn.customerId) {
Cache().setExercises(exerciseList);
} else if ( Cache().getTrainee() != null && customerId == Cache().getTrainee().customerId ) {
Cache().setExercisesTrainee(exerciseList);
}
return this.exerciseList;
}
List<Exercise> getExerciseList() {
if ( this.exerciseList == null || this.exerciseList.length == 0 ) {
this.exerciseList = Cache().getExercises();
}
return this.exerciseList;
//if ( this.exerciseList == null || this.exerciseList.length == 0 ) {
return this.exerciseList = Cache().getExercises();
//}
//return this.exerciseList;
}
List<Exercise> getExerciseListTrainee() {
//if ( this.exerciseList == null || this.exerciseList.length == 0 ) {
return this.exerciseList = Cache().getExercisesTrainee();
//}
//return this.exerciseList;
}
void getBaseExerciseFinishedPercent() {
+70 -6
View File
@@ -9,12 +9,44 @@ import 'package:aitrainer_app/service/exercise_tree_service.dart';
import 'package:aitrainer_app/service/exercisetype_service.dart';
import 'package:flutter/material.dart';
class Antagonist {
static String chest = "Chest";
static int chestNr = 1;
static String biceps = "Biceps";
static int bicepsNr = 2;
static String triceps = "Triceps";
static int tricepsNr =3;
static String back = "Back";
static int backNr = 4;
static String shoulder = "Shoulders";
static int shoulderNr = 5;
static String core = "Core";
static int coreNr = 6;
static String thigh = "Thigh";
static int thighNr = 7;
static String calf = "Calf";
static int calfNr = 8;
}
class MenuTreeRepository {
final LinkedHashMap tree = LinkedHashMap<String, WorkoutTree>();
SplayTreeMap sortedTree = SplayTreeMap<String, List<WorkoutTree>>();
bool isEnglish;
final Map<String, int> _antagonist = {
Antagonist.chest: Antagonist.chestNr,
Antagonist.biceps: Antagonist.bicepsNr,
Antagonist.triceps: Antagonist.tricepsNr,
Antagonist.back: Antagonist.backNr,
Antagonist.shoulder: Antagonist.shoulderNr,
Antagonist.core: Antagonist.coreNr,
Antagonist.thigh: Antagonist.thighNr,
Antagonist.calf: Antagonist.calfNr
};
Future<void> createTree() async {
final AppLanguage appLanguage = AppLanguage();
bool isEnglish = appLanguage.appLocal == Locale('en');
isEnglish = appLanguage.appLocal == Locale('en');
print("** Start creating tree on lang: " + appLanguage.appLocal.toString());
List<ExerciseTree> exerciseTree = Cache().getExerciseTree();
@@ -25,7 +57,7 @@ class MenuTreeRepository {
exerciseTree.forEach( (treeItem) async {
String treeName = isEnglish ? treeItem.name : treeItem.nameTranslation;
String assetImage = 'asset/menu/' + treeItem.imageUrl.substring(7);
bool is1RM = treeItem.name == '1RM' ? true : false;
bool is1RM = treeItem.name == 'One Rep Max' ? true : false;
if ( is1RM == false && treeItem.parentId != 0) {
is1RM = isParent1RM(treeItem.parentId);
}
@@ -34,12 +66,13 @@ class MenuTreeRepository {
treeItem.parentId,
treeName,
assetImage, Colors.white,
32,
24,
false,
0,
null,
false,
is1RM
is1RM,
treeItem.name,
);
});
@@ -65,7 +98,8 @@ class MenuTreeRepository {
exerciseType.exerciseTypeId,
exerciseType,
exerciseType.base,
is1RM
is1RM,
exerciseType.name
);
});
@@ -81,7 +115,7 @@ class MenuTreeRepository {
WorkoutTree treeItem = value as WorkoutTree;
if ( treeItem.id == treeId ) {
isTreeItem1RM = treeItem.is1RM;
//print (treeItem.name + " 1RM " + treeItem.is1RM.toString() );
print (treeItem.name + " 1RM " + treeItem.is1RM.toString() );
}
});
@@ -99,4 +133,34 @@ class MenuTreeRepository {
});
return branch;
}
List<WorkoutTree> getBranchList(int parent) {
List branch = List<WorkoutTree>();
tree.forEach((key, value) {
WorkoutTree workoutTree = value as WorkoutTree;
if ( parent == workoutTree.parent) {
branch.add(workoutTree);
}
});
return branch;
}
void sortByMuscleType() {
sortedTree = SplayTreeMap<String, List<WorkoutTree>>();
tree.forEach((key, value) {
WorkoutTree workoutTree = value as WorkoutTree;
//print("treeitem: " + workoutTree.toJson().toString());
/*if ( workoutTree.exerciseType != null) {
print("treeItem exerciseTye " + workoutTree.exerciseType.toJson().toString());
} else {
print("treeItem exerciseType null " + workoutTree.toJson().toString());
}*/
if ( workoutTree.nameEnglish != 'One Rep Max' && workoutTree.is1RM && workoutTree.exerciseTypeId == 0) {
String treeName = _antagonist[workoutTree.nameEnglish].toString() + ". " + workoutTree.name;
sortedTree[treeName] = this.getBranchList(workoutTree.id);
}
});
return;
}
}