import 'dart:collection'; import 'package:aitrainer_app/model/cache.dart'; import 'package:aitrainer_app/model/exercise_plan.dart'; import 'package:aitrainer_app/model/exercise_plan_detail.dart'; import 'package:aitrainer_app/model/model_change.dart'; import 'package:aitrainer_app/repository/exercise_plan_repository.dart'; import 'package:aitrainer_app/service/exercise_plan_service.dart'; import 'package:mockito/mockito.dart'; class MockExercisePlanApi extends Mock implements ExercisePlanApi { static final MockExercisePlanApi _singleton = MockExercisePlanApi._internal(); factory MockExercisePlanApi() { return _singleton; } MockExercisePlanApi._internal(); final List memoryExercisePlan = []; final List memoryExercisePlanDetail = []; Future saveExercisePlan(ExercisePlan plan) async { memoryExercisePlan.add(plan); plan.exercisePlanId = memoryExercisePlan.length; return plan; } Future saveExercisePlanDetail(ExercisePlanDetail detail) async { memoryExercisePlanDetail.add(detail); detail.exercisePlanDetailId = memoryExercisePlanDetail.length; return detail; } Future updateExercisePlanDetail(ExercisePlanDetail detail, int planDetailId) async { ExercisePlanDetail? updated; memoryExercisePlanDetail.forEach((element) { if (element.exercisePlanDetailId == planDetailId) { element = detail; } }); return updated!; } Future deleteExercisePlanDetail(int exercisePlanId) async { int index = -1; int countIndex = 0; memoryExercisePlanDetail.forEach((element) { if (element.exercisePlanId == exercisePlanId) { index = countIndex; } countIndex++; }); if (index > -1) { memoryExercisePlanDetail.removeAt(index); } } Future> getExercisePlanDetail(int exercisePlanId) async { List foundList = []; memoryExercisePlanDetail.forEach((element) { if (element.exercisePlanId == exercisePlanId) { foundList.add(element); } }); return foundList; } Future getLastExercisePlan(int customerId) async { ExercisePlan? found; memoryExercisePlan.forEach((element) { if (element.customerId == customerId) { found = element; } }); return found; } } class SimExercisePlanRepository with ExercisePlanRepository { Future getExercisePlanDetails() async { if (exercisePlan == null) { ExercisePlan? exercisePlan = await this.getLastExercisePlan(); if (exercisePlan == null) { exercisePlanDetails = LinkedHashMap(); return; } } List list = []; LinkedHashMap listCache = Cache().getMyExercisePlanDetails(); if (listCache.length > 0) { exercisePlanDetails = listCache; return; } else { list = await MockExercisePlanApi().getExercisePlanDetail(exercisePlan!.exercisePlanId!); } exercisePlanDetails = LinkedHashMap(); list.forEach((element) { newPlan = false; ExercisePlanDetail detail = element; exercisePlanDetails[detail.exerciseTypeId] = detail; }); Cache().setMyExercisePlanDetails(exercisePlanDetails); return; } Future getLastExercisePlan() async { if (customerId == 0) { return null; } ExercisePlan? myExercisePlan = Cache().getMyExercisePlan(); if (myExercisePlan != null) { newPlan = false; return myExercisePlan; } exercisePlan = await MockExercisePlanApi().getLastExercisePlan(customerId); newPlan = (exercisePlan == null); Cache().setMyExercisePlan(exercisePlan!); return exercisePlan; } Future saveExercisePlan() async { if (exercisePlan == null) { if (Cache().userLoggedIn == null) { throw Exception("please log in"); } String exercisePlanName; if (this.customerId == Cache().userLoggedIn!.customerId) { exercisePlanName = Cache().userLoggedIn!.name! + " private"; } else { exercisePlanName = Cache().getTrainee()!.name! + " " + Cache().getTrainee()!.firstname! + " private"; } exercisePlan = ExercisePlan(exercisePlanName, this.customerId); } if (newPlan) { exercisePlan!.dateAdd = DateTime.now(); exercisePlan!.private = true; ExercisePlan savedExercisePlan = await MockExercisePlanApi().saveExercisePlan(exercisePlan!); LinkedHashMap savedExercisePlanDetails = LinkedHashMap(); exercisePlanDetails.forEach((exerciseTypeId, exercisePlanDetail) async { exercisePlanDetail.exercisePlanId = savedExercisePlan.exercisePlanId!; ExercisePlanDetail savedDetail = await MockExercisePlanApi().saveExercisePlanDetail(exercisePlanDetail); savedExercisePlanDetails[savedDetail.exerciseTypeId] = savedDetail; }); exercisePlan = savedExercisePlan; } else { await MockExercisePlanApi().updateExercisePlan(exercisePlan!, exercisePlan!.exercisePlanId!); exercisePlanDetails.forEach((exerciseTypeId, exercisePlanDetail) async { if (exercisePlanDetail.change == ModelChange.delete) { await MockExercisePlanApi().deleteExercisePlanDetail(exercisePlanDetail.exercisePlanDetailId!); Cache().deleteMyExercisePlanDetail(exercisePlanDetail); } else if (exercisePlanDetail.change == ModelChange.update) { await MockExercisePlanApi().updateExercisePlanDetail(exercisePlanDetail, exercisePlanDetail.exercisePlanDetailId!); Cache().updateMyExercisePlanDetail(exercisePlanDetail); } else if (exercisePlanDetail.change == ModelChange.add) { await MockExercisePlanApi().saveExercisePlanDetail(exercisePlanDetail); Cache().addToMyExercisePlanDetails(exercisePlanDetail); } }); } } }