import '../../../utils/ObjectConvertors.dart'; import 'GroupObjective.dart'; /// A goal the whole group shares, broken into objectives everyone logs /// against. Like a personal goal it carries no debt of its own. class GroupGoal { String id; String title; String description; DateTime? targetDate; String colourHex; List objectives; GroupGoal({ this.id = "", this.title = "", this.description = "", this.targetDate, this.colourHex = "", List? objectives, }) : objectives = objectives ?? []; /// The average of the objectives, so one runaway objective cannot carry /// the rest. double get progress { if (objectives.isEmpty) { return 0; } double total = 0; for (GroupObjective objective in objectives) { total = total + objective.progress; } return total / objectives.length; } int get completeObjectives => objectives.where((objective) => objective.complete).length; factory GroupGoal.fromJson(Map json) { return GroupGoal( id: json['id'] ?? "", title: json['title'] ?? "", description: json['description'] ?? "", targetDate: json['targetDate'] == null || json['targetDate'] == "" ? null : DateTime.tryParse(json['targetDate']), colourHex: json['colourHex'] ?? "", objectives: getGroupObjectiveList(json['objectives']), ); } Map toJson() { final Map data = {}; data['id'] = id; data['title'] = title; data['description'] = description; data['targetDate'] = targetDate?.toIso8601String(); data['colourHex'] = colourHex; data['objectives'] = objectives.map((item) => item.toJson()).toList(); return data; } }