import 'dart:convert'; import 'package:aitrainer_app/model/cache.dart'; import 'package:aitrainer_app/model/exercise.dart'; import 'package:aitrainer_app/model/exercise_plan_detail.dart'; import 'package:aitrainer_app/model/exercise_type.dart'; class CustomerTrainingPlanDetails { /// customerTrainingPlanDetails int? customerTrainingPlanDetailsId; /// exerciseTypeId int? exerciseTypeId; /// set int? set; /// repeats int? repeats; /// weight double? weight; int? restingTime; bool? parallel; String? day; /// exerciseType ExerciseType? exerciseType; ExercisePlanDetailState state = ExercisePlanDetailState.start; List exercises = []; CustomerTrainingPlanDetails(); CustomerTrainingPlanDetails.fromJson(Map json) { this.customerTrainingPlanDetailsId = json['customerTrainingPlanDetailsId']; this.exerciseTypeId = json['exerciseTypeId']; this.set = json['set']; this.repeats = json['repeats']; this.weight = json['weight']; this.restingTime = json['restingTime']; this.parallel = json['parallel']; this.day = json['day']; } CustomerTrainingPlanDetails.fromJsonWithExerciseList(Map json) { this.customerTrainingPlanDetailsId = json['customerTrainingPlanDetailsId'] == "null" || json['customerTrainingPlanDetailsId'] == null ? 0 : json['customerTrainingPlanDetailsId']; this.exerciseTypeId = json['exerciseTypeId']; this.set = json['set']; this.repeats = json['repeats']; this.weight = json['weight']; this.restingTime = json['restingTime']; this.parallel = json['parallel'] == "false" ? false : json['parallel'] == "true" ? true : null; this.day = json['day']; try { Iterable iterable = json['exercises']; this.exercises = iterable.map((exercise) => Exercise.fromJson(exercise)).toList(); } on Exception catch (e) { print("JsonDecode error " + e.toString()); } if (exercises.length >= this.set!) { this.state = ExercisePlanDetailState.finished; } else if (exercises.length > 0) { this.state = ExercisePlanDetailState.inProgress; } else { this.state = ExercisePlanDetailState.start; } this.exerciseType = Cache().getExerciseTypeById(exerciseTypeId!); } ExerciseType? getExerciseType() => exerciseType; Map toJson() => { "customerTrainingPlanDetailsId": this.customerTrainingPlanDetailsId, "exerciseTypeId": this.exerciseTypeId, "set": this.set, "repeats": this.repeats, "weight": this.weight, "restingTime": this.restingTime, "parallel": this.parallel, "day": this.day == null ? '1.' : this.day, }; Map toJsonWithExercises() { final Map jsonMap = { "customerTrainingPlanDetailsId": this.customerTrainingPlanDetailsId, "exerciseTypeId": this.exerciseTypeId, "set": this.set, "repeats": this.repeats, "weight": this.weight, "restingTime": this.restingTime, "parallel": this.parallel, 'exercises': exercises.isEmpty ? [].toString() : exercises.map((exercise) => exercise.toJson()).toList().toString(), }; if (this.day != null && this.day!.isNotEmpty) { jsonMap["day"] = this.day; } //print("Detail $jsonMap"); return jsonMap; } @override String toString() => this.toJson().toString(); }