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_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 getPackage() async { List exerciseTree; List exerciseTreeParents; final body = await _client.get("app_package/", ""); final List models = body.split("|||"); await Future.forEach(models, (element) async { final List headRecord = element.split("***"); final Iterable json = jsonDecode(headRecord[1]); if (headRecord[0] == "ExerciseDevice") { final List devices = json.map((device) => ExerciseDevice.fromJson(device)).toList(); Cache().setDevices(devices); } else if (headRecord[0] == "Product") { final List products = json.map((product) => Product.fromJson(product)).toList(); Cache().setProducts(products); } else if (headRecord[0] == "Property") { final List 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 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] == "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 getExerciseTreeParents(final List exerciseTree, final List exerciseTreeParents) { List 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); exerciseTree.add(newElement); } else { element.parentId = parent.exerciseTreeParentId; exerciseTree[treeIndex].parentId = parent.exerciseTreeParentId; } index++; } }); treeIndex++; }); return exerciseTree; } Future getCustomerPackage(int customerId) async { try { final body = await _client.get("app_customer_package/" + customerId.toString(), ""); final List models = body.split("|||"); await Future.forEach(models, (element) async { final List 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 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 exercises = json.map((exerciseType) => Exercise.fromJson(exerciseType)).toList(); Cache().setExercises(exercises); } else if (headRecord[0] == "ProductTest") { final Iterable json = jsonDecode(headRecord[1]); final List productTests = json.map((productTest) => ProductTest.fromJson(productTest)).toList(); Cache().productTests = productTests; } else if (headRecord[0] == "Purchase") { final Iterable json = jsonDecode(headRecord[1]); final List purchases = json.map((purchase) => Purchase.fromJson(purchase)).toList(); Cache().setPurchases(purchases); } else if (headRecord[0] == "CustomerProperty") { final Iterable json = jsonDecode(headRecord[1]); final List customerProperties = json.map((property) => CustomerProperty.fromJson(property)).toList(); CustomerApi().initProperties(customerProperties); } else if (headRecord[0] == "ExerciseResult") { final Iterable json = jsonDecode(headRecord[1]); final List exerciseResults = json.map((exerciseResult) { ExerciseResult item = ExerciseResult.fromJson(exerciseResult); return item; }).toList(); // ToDo } }); } on NotFoundException catch (_) { throw Exception("Please log in"); } } }