diff --git a/packages/at_contacts_group_flutter/lib/services/group_service.dart b/packages/at_contacts_group_flutter/lib/services/group_service.dart index 5ca4024f..04962437 100644 --- a/packages/at_contacts_group_flutter/lib/services/group_service.dart +++ b/packages/at_contacts_group_flutter/lib/services/group_service.dart @@ -72,27 +72,36 @@ class GroupService { StreamSink get groupViewSink => _groupViewStreamController.sink; /// Controller for all contacts stream - final StreamController> _allContactsStreamController = + final StreamController> + _allContactsStreamController = StreamController>.broadcast(); /// all contacts stream - Stream> get allContactsStream => _allContactsStreamController.stream; + Stream> get allContactsStream => + _allContactsStreamController.stream; /// Sink for all contacts stream - StreamSink> get allContactsSink => _allContactsStreamController.sink; + StreamSink> get allContactsSink => + _allContactsStreamController.sink; /// Controller for selected group contact stream - final _selectedContactsStreamController = StreamController>.broadcast(); + final _selectedContactsStreamController = + StreamController>.broadcast(); /// Selected group contact stream - Stream> get selectedContactsStream => _selectedContactsStreamController.stream; + Stream> get selectedContactsStream => + _selectedContactsStreamController.stream; /// Sink for selected group contact stream - StreamSink> get selectedContactsSink => _selectedContactsStreamController.sink; + StreamSink> get selectedContactsSink => + _selectedContactsStreamController.sink; /// get list contact List listContact = []; + /// get list trusted contact + List trustedContacts = []; + // show loader stream final _showLoaderStreamController = StreamController.broadcast(); Stream get showLoaderStream => _showLoaderStreamController.stream; @@ -127,9 +136,11 @@ class GroupService { } /// Will show a dialog box, if yes is pressed, will clear the selectedGroupContacts - void clearSelectedGroupContacts({required BuildContext context, Function? onYesTap}) { + void clearSelectedGroupContacts( + {required BuildContext context, Function? onYesTap}) { if (selectedGroupContacts.isNotEmpty) { - shownConfirmationDialog(context, 'Selected contacts will not be added , confirm?', () { + shownConfirmationDialog( + context, 'Selected contacts will not be added , confirm?', () { selectedGroupContacts = []; selectedContactsSink.add(selectedGroupContacts); // to update the UI @@ -160,6 +171,42 @@ class GroupService { } } + /// Clear the selectedGroupContacts + void emptySelectedGroupContact() { + selectedGroupContacts = []; + selectedContactsSink.add(selectedGroupContacts); + } + + /// Function to get trusted contact + Future getTrustedContacts() async { + List fetchedTrustedContact = []; + + if (trustedContacts.isEmpty) { + final List trustedContactsKeys = + await AtClientManager.getInstance() + .atClient + .getAtKeys(regex: 'trusted_contact_'); + + try { + for (var key in trustedContactsKeys) { + AtValue keyValue = await AtClientManager.getInstance() + .atClient + .get(key) + .catchError((e) { + return AtValue(); + }); + fetchedTrustedContact.add(AtContact(atSign: keyValue.value)); + } + + for (var element in fetchedTrustedContact) { + trustedContacts.add(element); + } + } catch (e) { + print('ERROR=====>$e'); + } + } + } + // ignore: always_declare_return_types getAllGroupsDetails({ int? expandIndex, @@ -176,11 +223,12 @@ class GroupService { } for (AtGroup group in groupList) { - var index = - allContacts.indexWhere((element) => element!.group != null && element.group!.groupId == group.groupId); + var index = allContacts.indexWhere((element) => + element!.group != null && element.group!.groupId == group.groupId); if (index == -1) { - allContacts.add(GroupContactsModel(group: group, contactType: ContactsType.GROUP)); + allContacts.add(GroupContactsModel( + group: group, contactType: ContactsType.GROUP)); } } @@ -188,7 +236,8 @@ class GroupService { this.expandIndex = expandIndex; } else { if (expandGroup != null) { - this.expandIndex = groupList.indexWhere((element) => element.groupId == expandGroup.groupId); + this.expandIndex = groupList + .indexWhere((element) => element.groupId == expandGroup.groupId); } else { this.expandIndex = 0; } @@ -224,7 +273,8 @@ class GroupService { } /// Function to delete members of a group - Future deletGroupMembers(List contacts, AtGroup group) async { + Future deletGroupMembers( + List contacts, AtGroup group) async { try { var result = await atContactImpl.deleteMembers(Set.from(contacts), group); await updateGroupStreams(group, expandGroup: true); @@ -236,7 +286,8 @@ class GroupService { } /// Function to add members to a group - Future addGroupMembers(List contacts, AtGroup group) async { + Future addGroupMembers( + List contacts, AtGroup group) async { for (var i = 0; i < contacts.length; i++) { if (contacts[i]!.tags != null && contacts[i]!.tags!['image'] != null) { contacts[i]!.tags!['image'] = null; @@ -271,10 +322,12 @@ class GroupService { } // ignore: always_declare_return_types - updateGroupStreams(AtGroup group, {int? expandIndex, bool expandGroup = false}) async { + updateGroupStreams(AtGroup group, + {int? expandIndex, bool expandGroup = false}) async { var groupDetail = await (getGroupDetail(group.groupId!)); if (groupDetail is AtGroup) groupViewSink.add(groupDetail); - await getAllGroupsDetails(expandIndex: expandIndex, expandGroup: expandGroup ? group : null); + await getAllGroupsDetails( + expandIndex: expandIndex, expandGroup: expandGroup ? group : null); } /// Function to delete a group @@ -339,13 +392,15 @@ class GroupService { for (AtContact? contact in contactList) { var index = -1; if (contact != null) { - index = allContacts - .indexWhere((element) => element!.contact != null && element.contact!.atSign == contact.atSign); + index = allContacts.indexWhere((element) => + element!.contact != null && + element.contact!.atSign == contact.atSign); } if (index == -1) { listContact.add(contact!); - allContacts.add(GroupContactsModel(contact: contact, contactType: ContactsType.CONTACT)); + allContacts.add(GroupContactsModel( + contact: contact, contactType: ContactsType.CONTACT)); } } await getAllGroupsDetails(addToGroupSink: !isDesktop); @@ -364,12 +419,13 @@ class GroupService { }); if (_indexOfContact != -1) { - var _indexOfContactInGroup = - allContacts.indexWhere((element) => element!.contact != null && element.contact!.atSign == atSign); + var _indexOfContactInGroup = allContacts.indexWhere((element) => + element!.contact != null && element.contact!.atSign == atSign); if (_indexOfContactInGroup == -1) { allContacts.add(GroupContactsModel( - contact: ContactService().contactList[_indexOfContact], contactType: ContactsType.CONTACT)); + contact: ContactService().contactList[_indexOfContact], + contactType: ContactsType.CONTACT)); } } _allContactsStreamController.add(allContacts); @@ -480,7 +536,8 @@ class GroupService { AtGroup removeImageFromAtGroupMembers(AtGroup atGroup) { for (var i = 0; i < atGroup.members!.length; i++) { - if (atGroup.members!.elementAt(i).tags != null && atGroup.members!.elementAt(i).tags!['image'] != null) { + if (atGroup.members!.elementAt(i).tags != null && + atGroup.members!.elementAt(i).tags!['image'] != null) { atGroup.members!.elementAt(i).tags!['image'] = null; } }