102 lines
3.2 KiB
Dart
102 lines
3.2 KiB
Dart
import 'dart:collection';
|
|
import 'package:aitrainer_app/localization/app_language.dart';
|
|
import 'package:aitrainer_app/model/cache.dart';
|
|
import 'package:aitrainer_app/model/exercise_tree.dart';
|
|
import 'package:aitrainer_app/model/exercise_type.dart';
|
|
import 'package:aitrainer_app/model/workout_tree.dart';
|
|
import 'package:aitrainer_app/repository/exercise_repository.dart';
|
|
import 'package:aitrainer_app/service/exercise_tree_service.dart';
|
|
import 'package:aitrainer_app/service/exercisetype_service.dart';
|
|
import 'package:flutter/material.dart';
|
|
|
|
class MenuTreeRepository {
|
|
final LinkedHashMap tree = LinkedHashMap<String, WorkoutTree>();
|
|
|
|
Future<void> createTree() async {
|
|
final AppLanguage appLanguage = AppLanguage();
|
|
bool isEnglish = appLanguage.appLocal == Locale('en');
|
|
print("** Start creating tree on lang: " + appLanguage.appLocal.toString());
|
|
|
|
List<ExerciseTree> exerciseTree = Cache().getExerciseTree();
|
|
if ( exerciseTree == null || exerciseTree.length == 0) {
|
|
await ExerciseTreeApi().getExerciseTree();
|
|
}
|
|
|
|
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;
|
|
if ( is1RM == false && treeItem.parentId != 0) {
|
|
is1RM = isParent1RM(treeItem.parentId);
|
|
}
|
|
this.tree[treeItem.name] = WorkoutTree(
|
|
treeItem.treeId,
|
|
treeItem.parentId,
|
|
treeName,
|
|
assetImage, Colors.white,
|
|
32,
|
|
false,
|
|
0,
|
|
null,
|
|
false,
|
|
is1RM
|
|
);
|
|
});
|
|
|
|
List<ExerciseType> exerciseTypes = Cache().getExerciseTypes();
|
|
if ( exerciseTypes == null || exerciseTypes.length == 0) {
|
|
await ExerciseTypeApi().getExerciseTypes();
|
|
}
|
|
|
|
exerciseTypes.forEach( (exerciseType) {
|
|
String exerciseTypeName = isEnglish ?
|
|
exerciseType.name : exerciseType.nameTranslation;
|
|
String assetImage = 'asset/menu/' + exerciseType.imageUrl.substring(7);
|
|
bool is1RM = this.isParent1RM(exerciseType.treeId);
|
|
exerciseType.is1RM = is1RM;
|
|
this.tree[exerciseType.name] = WorkoutTree(
|
|
exerciseType.exerciseTypeId,
|
|
exerciseType.treeId,
|
|
exerciseTypeName,
|
|
assetImage,
|
|
Colors.white,
|
|
16,
|
|
true,
|
|
exerciseType.exerciseTypeId,
|
|
exerciseType,
|
|
exerciseType.base,
|
|
is1RM
|
|
);
|
|
});
|
|
|
|
Cache().setWorkoutTree(tree);
|
|
ExerciseRepository exerciseRepository = ExerciseRepository();
|
|
exerciseRepository.getBaseExerciseFinishedPercent();
|
|
}
|
|
|
|
bool isParent1RM(int treeId) {
|
|
bool isTreeItem1RM = false;
|
|
|
|
this.tree.forEach((key, value) {
|
|
WorkoutTree treeItem = value as WorkoutTree;
|
|
if ( treeItem.id == treeId ) {
|
|
isTreeItem1RM = treeItem.is1RM;
|
|
//print (treeItem.name + " 1RM " + treeItem.is1RM.toString() );
|
|
}
|
|
|
|
});
|
|
|
|
return isTreeItem1RM;
|
|
}
|
|
|
|
LinkedHashMap getBranch(int parent) {
|
|
LinkedHashMap branch = LinkedHashMap<String, WorkoutTree>();
|
|
tree.forEach((key, value) {
|
|
WorkoutTree workoutTree = value as WorkoutTree;
|
|
if ( parent == workoutTree.parent) {
|
|
branch[key] = value;
|
|
}
|
|
});
|
|
return branch;
|
|
}
|
|
} |