151 lines
6.9 KiB
Dart
151 lines
6.9 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:aitrainer_app/model/cache.dart';
|
|
import 'package:aitrainer_app/model/customer.dart';
|
|
import 'package:aitrainer_app/model/customer_exercise_device.dart';
|
|
import 'package:aitrainer_app/model/customer_property.dart';
|
|
import 'package:aitrainer_app/model/exercise.dart';
|
|
import 'package:aitrainer_app/model/exercise_device.dart';
|
|
import 'package:aitrainer_app/model/exercise_plan_template.dart';
|
|
import 'package:aitrainer_app/model/exercise_result.dart';
|
|
import 'package:aitrainer_app/model/exercise_tree.dart';
|
|
import 'package:aitrainer_app/model/exercise_tree_parents.dart';
|
|
import 'package:aitrainer_app/model/exercise_type.dart';
|
|
import 'package:aitrainer_app/model/product.dart';
|
|
import 'package:aitrainer_app/model/product_test.dart';
|
|
import 'package:aitrainer_app/model/property.dart';
|
|
import 'package:aitrainer_app/model/purchase.dart';
|
|
import 'package:aitrainer_app/service/api.dart';
|
|
import 'package:aitrainer_app/service/exercise_type_service.dart';
|
|
import 'package:aitrainer_app/util/not_found_exception.dart';
|
|
|
|
import 'customer_service.dart';
|
|
import 'exercise_tree_service.dart';
|
|
|
|
class PackageApi {
|
|
final APIClient _client = new APIClient();
|
|
|
|
Future<void> getPackage() async {
|
|
List<ExerciseTree> exerciseTree;
|
|
List<ExerciseTreeParents> exerciseTreeParents;
|
|
final body = await _client.get("app_package/", "");
|
|
|
|
final List<String> models = body.split("|||");
|
|
await Future.forEach(models, (element) async {
|
|
final List<String> headRecord = element.split("***");
|
|
final Iterable json = jsonDecode(headRecord[1]);
|
|
if (headRecord[0] == "ExerciseDevice") {
|
|
final List<ExerciseDevice> devices = json.map((device) => ExerciseDevice.fromJson(device)).toList();
|
|
Cache().setDevices(devices);
|
|
} else if (headRecord[0] == "Product") {
|
|
final List<Product> products = json.map((product) => Product.fromJson(product)).toList();
|
|
Cache().setProducts(products);
|
|
} else if (headRecord[0] == "Property") {
|
|
final List<Property> properties = json.map((property) => Property.fromJson(property)).toList();
|
|
Cache().setProperties(properties);
|
|
} else if (headRecord[0] == "ExerciseTree") {
|
|
exerciseTree = json.map((exerciseTree) => ExerciseTree.fromJson(exerciseTree)).toList();
|
|
} else if (headRecord[0] == "ExerciseType") {
|
|
final List<ExerciseType> exerciseTypes = json.map((exerciseType) => ExerciseType.fromJson(exerciseType)).toList();
|
|
if (exerciseTypes != null) {
|
|
await Future.forEach(exerciseTypes, (element) async {
|
|
element.imageUrl = await ExerciseTypeApi().buildImage(element.imageUrl, element.exerciseTypeId);
|
|
});
|
|
Cache().setExerciseTypes(exerciseTypes);
|
|
}
|
|
} else if (headRecord[0] == "ExerciseAbility") {
|
|
} else if (headRecord[0] == "ExercisePlanTemplate") {
|
|
final List<ExercisePlanTemplate> exercisePlanTemplates =
|
|
json.map((exercisePlanTemplate) => ExercisePlanTemplate.fromJson(exercisePlanTemplate)).toList();
|
|
Cache().setExercisePlanTemplates(exercisePlanTemplates);
|
|
} else if (headRecord[0] == "ExerciseTreeParents") {
|
|
exerciseTreeParents = json.map((exerciseTreeParent) => ExerciseTreeParents.fromJson(exerciseTreeParent)).toList();
|
|
}
|
|
});
|
|
|
|
exerciseTree = this.getExerciseTreeParents(exerciseTree, exerciseTreeParents);
|
|
if (exerciseTree != null) {
|
|
await Future.forEach(exerciseTree, (element) async {
|
|
element.imageUrl = await ExerciseTreeApi().buildImage(element.imageUrl, element.treeId);
|
|
});
|
|
Cache().setExerciseTree(exerciseTree);
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
List<ExerciseTree> getExerciseTreeParents(final List<ExerciseTree> exerciseTree, final List<ExerciseTreeParents> exerciseTreeParents) {
|
|
List<ExerciseTree> copyList = ExerciseTreeApi().copyList(exerciseTree);
|
|
|
|
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;
|
|
exerciseTree.add(newElement);
|
|
} else {
|
|
element.parentId = parent.exerciseTreeParentId;
|
|
element.sort = parent.sort;
|
|
exerciseTree[treeIndex].parentId = parent.exerciseTreeParentId;
|
|
exerciseTree[treeIndex].sort = parent.sort;
|
|
}
|
|
index++;
|
|
}
|
|
});
|
|
|
|
treeIndex++;
|
|
});
|
|
|
|
return exerciseTree;
|
|
}
|
|
|
|
Future<void> getCustomerPackage(int customerId) async {
|
|
try {
|
|
final body = await _client.get("app_customer_package/" + customerId.toString(), "");
|
|
|
|
final List<String> models = body.split("|||");
|
|
await Future.forEach(models, (element) async {
|
|
final List<String> headRecord = element.split("***");
|
|
//print("Class " + headRecord[0]);
|
|
if (headRecord[0] == "Customer") {
|
|
Customer customer = Customer.fromJson(jsonDecode(headRecord[1]));
|
|
Cache().userLoggedIn = customer;
|
|
} else if (headRecord[0] == "CustomerExerciseDevice") {
|
|
final Iterable json = jsonDecode(headRecord[1]);
|
|
final List<CustomerExerciseDevice> devices = json.map((device) => CustomerExerciseDevice.fromJson(device)).toList();
|
|
Cache().setCustomerDevices(devices);
|
|
// ToDo
|
|
} else if (headRecord[0] == "Exercises") {
|
|
final Iterable json = jsonDecode(headRecord[1]);
|
|
final List<Exercise> exercises = json.map((exerciseType) => Exercise.fromJson(exerciseType)).toList();
|
|
Cache().setExercises(exercises);
|
|
} else if (headRecord[0] == "ProductTest") {
|
|
final Iterable json = jsonDecode(headRecord[1]);
|
|
final List<ProductTest> productTests = json.map((productTest) => ProductTest.fromJson(productTest)).toList();
|
|
Cache().productTests = productTests;
|
|
} else if (headRecord[0] == "Purchase") {
|
|
final Iterable json = jsonDecode(headRecord[1]);
|
|
final List<Purchase> purchases = json.map((purchase) => Purchase.fromJson(purchase)).toList();
|
|
Cache().setPurchases(purchases);
|
|
} else if (headRecord[0] == "CustomerProperty") {
|
|
final Iterable json = jsonDecode(headRecord[1]);
|
|
final List<CustomerProperty> customerProperties = json.map((property) => CustomerProperty.fromJson(property)).toList();
|
|
CustomerApi().initProperties(customerProperties);
|
|
} else if (headRecord[0] == "ExerciseResult") {
|
|
final Iterable json = jsonDecode(headRecord[1]);
|
|
final List<ExerciseResult> exerciseResults = json.map((exerciseResult) {
|
|
ExerciseResult item = ExerciseResult.fromJson(exerciseResult);
|
|
return item;
|
|
}).toList();
|
|
// ToDo
|
|
}
|
|
});
|
|
} on NotFoundException catch (_) {
|
|
throw Exception("Please log in");
|
|
}
|
|
}
|
|
}
|