2020-05-05 16:49:50 +00:00
|
|
|
import 'package:comunic/lists/abstract_list.dart';
|
|
|
|
import 'package:comunic/models/membership.dart';
|
|
|
|
|
|
|
|
/// Memberships list
|
|
|
|
///
|
|
|
|
/// @author Pierre Hubert
|
|
|
|
|
2022-03-10 18:39:57 +00:00
|
|
|
class MembershipList extends AbstractList<Membership?> {
|
2020-05-05 17:33:04 +00:00
|
|
|
/// Get the IDs of all the users included in some way in this list
|
2022-03-10 18:39:57 +00:00
|
|
|
Set<int?> get usersId {
|
|
|
|
final s = Set<int?>();
|
2020-05-05 17:33:04 +00:00
|
|
|
|
|
|
|
forEach((m) {
|
2022-03-10 18:39:57 +00:00
|
|
|
switch (m!.type) {
|
2020-05-05 17:33:04 +00:00
|
|
|
case MembershipType.FRIEND:
|
2022-03-10 18:39:57 +00:00
|
|
|
s.add(m.friend!.id);
|
2020-05-05 17:33:04 +00:00
|
|
|
break;
|
|
|
|
case MembershipType.GROUP:
|
|
|
|
break;
|
|
|
|
case MembershipType.CONVERSATION:
|
2022-03-10 18:39:57 +00:00
|
|
|
s.addAll(m.conversation!.membersID);
|
2020-05-05 17:33:04 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the ID of the groups included in this list
|
2022-03-10 18:39:57 +00:00
|
|
|
Set<int?> get groupsId => where((f) => f!.type == MembershipType.GROUP)
|
|
|
|
.map((f) => f!.groupID)
|
2020-05-05 17:33:04 +00:00
|
|
|
.toSet();
|
2020-05-06 11:38:11 +00:00
|
|
|
|
|
|
|
/// Remove a friend membership from the list
|
|
|
|
void removeFriend(int friendID) => remove(firstWhere(
|
2022-03-10 18:39:57 +00:00
|
|
|
(f) => f!.type == MembershipType.FRIEND && f.friend!.id == friendID));
|
2021-04-07 14:28:59 +00:00
|
|
|
|
|
|
|
/// Get the list of conversations of a group
|
2022-03-10 18:39:57 +00:00
|
|
|
Set<Membership?> getGroupConversations(int groupID) => where((element) =>
|
|
|
|
element!.type == MembershipType.CONVERSATION &&
|
|
|
|
element.conversation!.groupID == groupID).toSet();
|
2020-05-05 17:33:04 +00:00
|
|
|
}
|