import 'dart:collection'; import 'package:workouttest_util/model/training_plan_detail.dart'; class TrainingPlan { late int trainingPlanId; String? type; late String name; String? internalName; String? description; late bool free; late bool active; int? treeId; HashMap nameTranslations = HashMap(); HashMap descriptionTranslations = HashMap(); List? details; TrainingPlan.fromJson(Map json) { trainingPlanId = json['trainingPlanId']; name = json['name']; type = json['type']; internalName = json['internalName']; description = json['description']; free = json['free']; active = json['active']; treeId = json['treeId']; nameTranslations['en'] = name; descriptionTranslations['en'] = description ?? ""; if (json['translations'] != null && json['translations'].length > 0) { json['translations'].forEach((translation) { nameTranslations[translation['languageCode']] = translation['nameTranslation']; descriptionTranslations[translation['languageCode']] = translation['descriptionTranslation']; }); } if (json['details'] != null && json['details'].length > 0) { details = json['details'].map((detail) => TrainingPlanDetail.fromJson(detail)).toList(); if (details != null && details!.isNotEmpty) { details!.sort((a, b) { if (a.sort == 0 || b.sort == 0) { if (a.trainingPlanDetailId <= b.trainingPlanDetailId) { return -1; } else { return 1; } } if (a.sort <= b.sort) { return -1; } else { return 1; } }); } } } Map toJson() => { "trainingPlanId": trainingPlanId, "treeId": treeId, "name": name, "type": type, "internalName": internalName, "free": free, "active": active, "description": description, "nameTranslation": nameTranslations.toString(), }; @override String toString() => toJson().toString(); }