83 lines
2.8 KiB
Dart
83 lines
2.8 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 'package:aitrainer_app/service/logging.dart';
|
|
import 'package:flutter/services.dart';
|
|
import 'api.dart';
|
|
|
|
class ExerciseTreeApi with Logging {
|
|
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);
|
|
|
|
if (exerciseTree != null) {
|
|
await Future.forEach(exerciseTree, (element) async {
|
|
element.imageUrl = await buildImage(element.imageUrl, element.treeId);
|
|
});
|
|
exerciseTree = await getExerciseTreeParents(exerciseTree);
|
|
log("ExerciseTree downloaded");
|
|
Cache().setExerciseTree(exerciseTree);
|
|
}
|
|
|
|
return exerciseTree;
|
|
}
|
|
|
|
Future<String> buildImage(String imageUrl, int treeId) async {
|
|
String assetImage = 'asset/menu/' + imageUrl.substring(7);
|
|
return await rootBundle.load(assetImage).then((value) {
|
|
return assetImage;
|
|
}).catchError((_) {
|
|
String imagePath = assetImage.substring(10);
|
|
String url = Cache.mediaUrl + 'images' + imagePath;
|
|
return url;
|
|
});
|
|
}
|
|
|
|
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);
|
|
newElement.sort = parent.sort ?? 0;
|
|
exerciseTree.add(newElement);
|
|
} else {
|
|
element.parentId = parent.exerciseTreeParentId;
|
|
element.sort = parent.sort ?? 0;
|
|
exerciseTree[treeIndex].parentId = parent.exerciseTreeParentId;
|
|
}
|
|
index++;
|
|
}
|
|
});
|
|
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;
|
|
}
|
|
}
|