addAtSign method

Future<bool> addAtSign({
  1. String? atSign,
  2. String? nickName,
})

Function to validate, fetch details and save to current atsign's contact list

Implementation

Future<bool> addAtSign({
  String? atSign,
  String? nickName,
}) async {
  if (atSign == null || atSign == '') {
    getAtSignError = TextStrings().emptyAtsign;

    return false;
  } else if (atSign[0] != '@') {
    atSign = '@$atSign';
  }
  atSign = atSign.toLowerCase().trim();

  if (atSign == atClientManager.atClient.getCurrentAtSign()) {
    getAtSignError = TextStrings().addingLoggedInUser;

    return false;
  }
  try {
    isContactPresent = false;

    getAtSignError = '';
    var contact = AtContact();

    checkAtSign = await checkAtsign(atSign);

    if (!checkAtSign!) {
      getAtSignError = TextStrings().unknownAtsign(atSign);
    } else {
      for (var element in contactList) {
        if (element.atSign == atSign) {
          getAtSignError = TextStrings().atsignExists(atSign);
          isContactPresent = true;
          break;
        }
      }
    }
    if (!isContactPresent && checkAtSign!) {
      var details = await getContactDetails(atSign, nickName);
      contact = AtContact(
        atSign: atSign,
        tags: details,
      );
      print('details==>${contact.atSign}');
      var result = await atContactImpl.add(contact).catchError((e) {
        print('error to add contact => $e');
        return false;
      });

      if (result) {
        if (!cachedContactList.contains(contact)) {
          cachedContactList.add(contact);
        }
      }

      print(result);
      allContactsList.add(contact.atSign!);
      contactList.add(contact);

      compareContactListForUpdatedStateForOneContact(contact);
      contactSink.add(baseContactList);
      return true;
    } else {
      return false;
    }
  } catch (e) {
    print(' error in adding atsign: $e');
    return false;
  }
}