Skip to content

Commit

Permalink
feat: added method to get trusted senders in group service
Browse files Browse the repository at this point in the history
  • Loading branch information
Sardar1208 committed Feb 21, 2024
1 parent 182a017 commit 1bcfe19
Showing 1 changed file with 80 additions and 23 deletions.
103 changes: 80 additions & 23 deletions packages/at_contacts_group_flutter/lib/services/group_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,27 +72,36 @@ class GroupService {
StreamSink<AtGroup> get groupViewSink => _groupViewStreamController.sink;

/// Controller for all contacts stream
final StreamController<List<GroupContactsModel?>> _allContactsStreamController =
final StreamController<List<GroupContactsModel?>>
_allContactsStreamController =
StreamController<List<GroupContactsModel?>>.broadcast();

/// all contacts stream
Stream<List<GroupContactsModel?>> get allContactsStream => _allContactsStreamController.stream;
Stream<List<GroupContactsModel?>> get allContactsStream =>
_allContactsStreamController.stream;

/// Sink for all contacts stream
StreamSink<List<GroupContactsModel?>> get allContactsSink => _allContactsStreamController.sink;
StreamSink<List<GroupContactsModel?>> get allContactsSink =>
_allContactsStreamController.sink;

/// Controller for selected group contact stream
final _selectedContactsStreamController = StreamController<List<GroupContactsModel?>>.broadcast();
final _selectedContactsStreamController =
StreamController<List<GroupContactsModel?>>.broadcast();

/// Selected group contact stream
Stream<List<GroupContactsModel?>> get selectedContactsStream => _selectedContactsStreamController.stream;
Stream<List<GroupContactsModel?>> get selectedContactsStream =>
_selectedContactsStreamController.stream;

/// Sink for selected group contact stream
StreamSink<List<GroupContactsModel?>> get selectedContactsSink => _selectedContactsStreamController.sink;
StreamSink<List<GroupContactsModel?>> get selectedContactsSink =>
_selectedContactsStreamController.sink;

/// get list contact
List<AtContact> listContact = [];

/// get list trusted contact
List<AtContact> trustedContacts = [];

// show loader stream
final _showLoaderStreamController = StreamController<bool>.broadcast();
Stream<bool> get showLoaderStream => _showLoaderStreamController.stream;
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -160,6 +171,42 @@ class GroupService {
}
}

/// Clear the selectedGroupContacts
void emptySelectedGroupContact() {
selectedGroupContacts = [];
selectedContactsSink.add(selectedGroupContacts);
}

/// Function to get trusted contact
Future<void> getTrustedContacts() async {
List<AtContact> fetchedTrustedContact = [];

if (trustedContacts.isEmpty) {
final List<AtKey> 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,
Expand All @@ -176,19 +223,21 @@ 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));
}
}

if (expandIndex != null) {
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;
}
Expand Down Expand Up @@ -224,7 +273,8 @@ class GroupService {
}

/// Function to delete members of a group
Future<dynamic> deletGroupMembers(List<AtContact> contacts, AtGroup group) async {
Future<dynamic> deletGroupMembers(
List<AtContact> contacts, AtGroup group) async {
try {
var result = await atContactImpl.deleteMembers(Set.from(contacts), group);
await updateGroupStreams(group, expandGroup: true);
Expand All @@ -236,7 +286,8 @@ class GroupService {
}

/// Function to add members to a group
Future<dynamic> addGroupMembers(List<AtContact?> contacts, AtGroup group) async {
Future<dynamic> addGroupMembers(
List<AtContact?> 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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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;
}
}
Expand Down

0 comments on commit 1bcfe19

Please sign in to comment.