import '../../../utils/ObjectConvertors.dart'; import 'GroupGoal.dart'; import 'GroupMember.dart'; /// People keeping each other honest: members, the goals they share, and the /// chat they talk in. class Group { String id; String name; String description; String colourHex; /// Six characters a friend types to join. String inviteCode; List members; List goals; /// Messages the signed-in user has not seen yet. int unread; String lastMessage; String lastAuthor; DateTime? lastMessageAt; Group({ this.id = "", this.name = "", this.description = "", this.colourHex = "", this.inviteCode = "", List? members, List? goals, this.unread = 0, this.lastMessage = "", this.lastAuthor = "", this.lastMessageAt, }) : members = members ?? [], goals = goals ?? []; /// Average progress across the shared goals. double get progress { if (goals.isEmpty) { return 0; } double total = 0; for (GroupGoal goal in goals) { total = total + goal.progress; } return total / goals.length; } bool get iOwn => members.any((member) => member.me && member.owner); factory Group.fromJson(Map json) { return Group( id: json['id'] ?? "", name: json['name'] ?? "", description: json['description'] ?? "", colourHex: json['colourHex'] ?? "", inviteCode: json['inviteCode'] ?? "", members: getGroupMemberList(json['members']), goals: getGroupGoalList(json['goals']), unread: json['unread'] ?? 0, lastMessage: json['lastMessage'] ?? "", lastAuthor: json['lastAuthor'] ?? "", lastMessageAt: json['lastMessageAt'] == null ? null : DateTime.tryParse(json['lastMessageAt']), ); } Map toJson() { final Map data = {}; data['id'] = id; data['name'] = name; data['description'] = description; data['colourHex'] = colourHex; data['inviteCode'] = inviteCode; data['members'] = members.map((item) => item.toJson()).toList(); data['goals'] = goals.map((item) => item.toJson()).toList(); data['unread'] = unread; data['lastMessage'] = lastMessage; data['lastAuthor'] = lastAuthor; data['lastMessageAt'] = lastMessageAt?.toIso8601String(); return data; } }