import 'dart:convert'; import 'package:workouttest_util/model/cache.dart'; import 'package:workouttest_util/model/customer.dart'; import 'package:workouttest_util/model/customer_activity.dart'; import 'package:workouttest_util/model/customer_exercise_device.dart'; import 'package:workouttest_util/model/customer_property.dart'; import 'package:workouttest_util/model/description.dart'; import 'package:workouttest_util/model/evaluation.dart'; import 'package:workouttest_util/model/exercise.dart'; import 'package:workouttest_util/model/exercise_device.dart'; import 'package:workouttest_util/model/exercise_plan_template.dart'; import 'package:workouttest_util/model/exercise_tree.dart'; import 'package:workouttest_util/model/exercise_tree_parents.dart'; import 'package:workouttest_util/model/exercise_type.dart'; import 'package:workouttest_util/model/faq.dart'; import 'package:workouttest_util/model/product.dart'; import 'package:workouttest_util/model/property.dart'; import 'package:workouttest_util/model/purchase.dart'; import 'package:workouttest_util/model/split_test.dart'; import 'package:workouttest_util/model/training_plan.dart'; import 'package:workouttest_util/model/training_plan_day.dart'; import 'package:workouttest_util/model/tutorial.dart'; import 'package:workouttest_util/repository/training_plan_day_repository.dart'; import 'package:workouttest_util/service/api.dart'; import 'package:workouttest_util/service/exercise_type_service.dart'; import 'package:workouttest_util/util/not_found_exception.dart'; import '../model/sport.dart'; import 'customer_service.dart'; import 'exercise_tree_service.dart'; class PackageApi { final APIClient _client = APIClient(); Future getPackage() async { late List exerciseTree; late List exerciseTreeParents; final body = await _client.get("app_package", ""); final List models = body.split("|||"); await Future.forEach(models, (elem) async { final String element = elem; 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(); await Future.forEach(exerciseTypes, (elem) async { final ExerciseType exerciseType = elem; exerciseType.imageUrl = await ExerciseTypeApi().buildImage(exerciseType.imageUrl, exerciseType.exerciseTypeId); }); Cache().setExerciseTypes(exerciseTypes); } else if (headRecord[0] == "ExerciseAbility") { } else if (headRecord[0] == "ExercisePlanTemplate") { final List exercisePlanTemplates = json.map((exercisePlanTemplate) => ExercisePlanTemplate.fromJson(exercisePlanTemplate)).toList(); Cache().setExercisePlanTemplates(exercisePlanTemplates); } else if (headRecord[0] == "ExerciseTreeParents") { exerciseTreeParents = json.map((exerciseTreeParent) => ExerciseTreeParents.fromJson(exerciseTreeParent)).toList(); } else if (headRecord[0] == "Evaluation") { final List evaluations = json.map((evaluation) => Evaluation.fromJson(evaluation)).toList(); Cache().evaluations = evaluations; } else if (headRecord[0] == "Sport") { final List sports = json.map((sport) => Sport.fromJson(sport)).toList(); Cache().setSports(sports); } else if (headRecord[0] == "Tutorial") { final Iterable json = jsonDecode(headRecord[1]); final List tutorials = json.map((tutorial) => Tutorial.fromJson(tutorial)).toList(); Cache().setTutorials(tutorials); } else if (headRecord[0] == "Description") { final Iterable json = jsonDecode(headRecord[1]); final List descriptions = json.map((description) => Description.fromJson(description)).toList(); //print("Description: $descriptions"); Cache().setDescriptions(descriptions); } else if (headRecord[0] == "Faq") { final Iterable json = jsonDecode(headRecord[1]); final List faqs = json.map((faq) => Faq.fromJson(faq)).toList(); //print("Faq: $faqs"); Cache().setFaqs(faqs); } else if (headRecord[0] == "TrainingPlan") { final Iterable json = jsonDecode(headRecord[1]); final List plans = json.map((plan) => TrainingPlan.fromJson(plan)).toList(); List activePlans = []; for (var element in plans) { if (element.active) { activePlans.add(element); } } Cache().setTrainingPlans(activePlans); } else if (headRecord[0] == "SplitTests") { final Iterable json = jsonDecode(headRecord[1]); final List tests = json.map((test) => SplitTest.fromJson(test)).toList(); //print("A/B tests: $tests"); Cache().setSplitTests(tests); } else if (headRecord[0] == "TrainingPlanDay") { final Iterable json = jsonDecode(headRecord[1]); final List days = json.map((day) => TrainingPlanDay.fromJson(day)).toList(); Cache().setTrainingPlanDays(days); } }); exerciseTree = getExerciseTreeParents(exerciseTree, exerciseTreeParents); await Future.forEach(exerciseTree, (element) async { ExerciseTree tree = element; tree.imageUrl = await ExerciseTreeApi().buildImage(tree.imageUrl, tree.treeId); }); Cache().setExerciseTree(exerciseTree); TrainingPlanDayRepository trainingPlanDayRepository = const TrainingPlanDayRepository(); trainingPlanDayRepository.assignTrainingPlanDays(); return; } List getExerciseTreeParents(final List exerciseTree, final List exerciseTreeParents) { List copyList = ExerciseTreeApi().copyList(exerciseTree); int treeIndex = 0; copyList.forEach((element) async { int index = 0; for (var parent in exerciseTreeParents) { 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 getCustomerPackage(int customerId) async { try { final body = await _client.get("app_customer_package/$customerId", ""); final List models = body.split("|||"); await Future.forEach(models, (elem) async { final String element = elem; 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] == "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] == "CustomerPropertyAll") { final Iterable json = jsonDecode(headRecord[1]); final List allCustomerProperties = json.map((property) => CustomerProperty.fromJson(property)).toList(); print(" All Properties ---- $allCustomerProperties"); Cache().setCustomerPropertyAll(allCustomerProperties); } 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 */ } else if (headRecord[0] == "CustomerActivity") { final Iterable json = jsonDecode(headRecord[1]); final List customerActivities = json.map((activity) => CustomerActivity.fromJson(activity)).toList(); Cache().setCustomerActivities(customerActivities); } }); } on NotFoundException catch (e) { throw Exception("Please log in $e"); } } }