import 'dart:collection'; import 'dart:convert'; import 'package:aitrainer_app/util/common.dart'; import 'package:intl/intl.dart'; import 'package:aitrainer_app/model/customer_training_plan_details.dart'; enum CustomerTrainingPlanType { custom, template, none } extension CustomerTrainingPlanTypeExt on CustomerTrainingPlanType { String toStr() => this.toString().split(".").last; bool equalsTo(CustomerTrainingPlanType type) => this.toString() == type.toString(); bool equalsStringTo(String type) => this.toStr() == type; } class CustomerTrainingPlan { int? customerTrainingPlanId; int? customerId; int? trainingPlanId; DateTime? dateAdd; bool? active; String? status; String? name; CustomerTrainingPlanType type = CustomerTrainingPlanType.none; CustomerTrainingPlan(); List details = []; HashMap> days = HashMap(); CustomerTrainingPlan.fromJson(Map json) { this.customerTrainingPlanId = json['customerTrainingPlanId']; this.customerId = json['customerId']; this.trainingPlanId = json['trainingPlanId']; this.dateAdd = DateTime.parse(json['dateAdd']); this.active = json['active']; this.status = json['status']; this.name = json['name']; } CustomerTrainingPlan.fromJsonWithDetails(Map json) { this.customerTrainingPlanId = json['customerTrainingPlanId']; this.customerId = json['customerId']; this.trainingPlanId = json['trainingPlanId']; this.dateAdd = json['dateAdd'] != null ? DateTime.parse(json['dateAdd']) : DateTime.now(); this.active = json['active']; this.status = json['status']; this.name = json['name']; try { final String details = json['details']; String jsonDetails = details.replaceAllMapped(RegExp(r'([a-zA-Z]+)\:'), (Match m) => "\"${m[1]}\":"); jsonDetails = jsonDetails.replaceAllMapped(RegExp(r'\: ([a-zA-Z /]+)'), (Match m) => ":\"${m[1]}\""); jsonDetails = jsonDetails.replaceAllMapped(RegExp(r'([0-9]{4}\-[0-9]{2}\-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2})'), (Match m) => "\"${m[0]}\""); print("detail: $jsonDetails"); Iterable iterable = jsonDecode(jsonDetails); this.details = iterable.map((detail) => CustomerTrainingPlanDetails.fromJsonWithExerciseList(detail)).toList(); this.details.forEach((detail) { detail.alternatives = Common.getExerciseTypeAlternatives(detail.exerciseTypeId); }); } on Exception catch (e) { print("JsonDecode error " + e.toString()); } } Map toJson() => { "customerTrainingPlanId": this.customerTrainingPlanId, "customerId": this.customerId, "trainingPlanId": this.trainingPlanId, "dateAdd": DateFormat('yyyy-MM-dd HH:mm:ss').format(this.dateAdd!), "name": this.name, "active": this.active, "status": this.status }; Map toJsonWithDetails() => { "customerTrainingPlanId": this.customerTrainingPlanId, "customerId": this.customerId, "trainingPlanId": this.trainingPlanId, "dateAdd": DateFormat('yyyy-MM-dd HH:mm:ss').format(this.dateAdd!).toString(), "name": this.name, "active": this.active, "status": this.status, 'details': details.isEmpty ? [].toString() : details.map((detail) => detail.toJsonWithExercises()).toList().toString(), }; @override String toString() => this.toJson().toString(); }