fetchGroupsAndContacts method

dynamic fetchGroupsAndContacts({
  1. bool isDesktop = false,
})

Implementation

fetchGroupsAndContacts({bool isDesktop = false}) async {
  try {
    /// contacts list is already present, we do not fetch it again.
    if (allContacts.isNotEmpty) {
      listContact.sort((a, b) {
        int? index = a.atSign
            .toString()
            .substring(1)
            .compareTo((b.atSign).toString().substring(1));
        return index;
      });

      /// If both contacts list are same, no chanbges have been made by the user.
      if (listContact == ContactService().contactList) {
        WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
          _allContactsStreamController.add(allContacts);
        });
        return;
      }
    }
    allContacts = [];
    listContact = [];
    var contactList = await fetchContacts();
    if (contactList != null) {
      for (AtContact? contact in contactList) {
        var index = -1;
        if (contact != null) {
          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));
        }
      }
      await getAllGroupsDetails(addToGroupSink: !isDesktop);
      _allContactsStreamController.add(allContacts);
    }
  } catch (e) {
    _allContactsStreamController.add([]);
  }
}