63 lines
2.0 KiB
Dart
63 lines
2.0 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:aitrainer_app/model/cache.dart';
|
|
import 'package:aitrainer_app/model/exercise_tree.dart';
|
|
import 'package:aitrainer_app/model/exercise_tree_parents.dart';
|
|
import 'api.dart';
|
|
|
|
class ExerciseTreeApi {
|
|
final APIClient _client = new APIClient();
|
|
|
|
Future<List<ExerciseTree>> getExerciseTree() async {
|
|
final String body = await _client.get("exercise_tree", "");
|
|
Iterable json = jsonDecode(body);
|
|
List<ExerciseTree> exerciseTree = json.map((exerciseTree) => ExerciseTree.fromJson(exerciseTree)).toList();
|
|
|
|
exerciseTree = await getExerciseTreeParents(exerciseTree);
|
|
|
|
Cache().setExerciseTree(exerciseTree);
|
|
return exerciseTree;
|
|
}
|
|
|
|
Future<List<ExerciseTree>> getExerciseTreeParents(List<ExerciseTree> exerciseTree) async {
|
|
List<ExerciseTree> copyList = this._copyList(exerciseTree);
|
|
|
|
final String body = await _client.get("exercise_tree_parents", "");
|
|
Iterable json = jsonDecode(body);
|
|
final List<ExerciseTreeParents> exerciseTreeParents =
|
|
json.map((exerciseTreeParent) => ExerciseTreeParents.fromJson(exerciseTreeParent)).toList();
|
|
|
|
int treeIndex = 0;
|
|
copyList.forEach((element) async {
|
|
int index = 0;
|
|
exerciseTreeParents.forEach((parent) {
|
|
if (parent.exerciseTreeChildId == element.treeId) {
|
|
if (index > 0) {
|
|
ExerciseTree newElement = element.copy(parent.exerciseTreeParentId);
|
|
exerciseTree.add(newElement);
|
|
print("ExerciseTree " + newElement.toJson().toString());
|
|
} else {
|
|
element.parentId = parent.exerciseTreeParentId;
|
|
exerciseTree[treeIndex].parentId = parent.exerciseTreeParentId;
|
|
}
|
|
index++;
|
|
}
|
|
});
|
|
print("ExerciseTree " + element.toJson().toString());
|
|
treeIndex++;
|
|
});
|
|
|
|
return exerciseTree;
|
|
}
|
|
|
|
List<ExerciseTree> _copyList(List<ExerciseTree> tree) {
|
|
final List<ExerciseTree> copyList = List();
|
|
tree.forEach((element) {
|
|
final ExerciseTree copy = element.copy(-1);
|
|
copyList.add(copy);
|
|
});
|
|
|
|
return copyList;
|
|
}
|
|
}
|