search method

  1. @override
Paginated<ChatContactId, RxChatContact> search({
  1. UserName? name,
  2. UserEmail? email,
  3. UserPhone? phone,
})
override

Searches ChatContacts by the given criteria.

Implementation

@override
Paginated<ChatContactId, RxChatContact> search({
  UserName? name,
  UserEmail? email,
  UserPhone? phone,
}) {
  Log.debug('search($name, $email, $phone)', '$runtimeType');

  if (name == null && email == null && phone == null) {
    return PaginatedImpl();
  }

  Pagination<RxChatContact, ChatContactsCursor, ChatContactId>? pagination;
  if (name != null) {
    pagination = Pagination(
      perPage: 30,
      provider: GraphQlPageProvider(
        fetch: ({after, before, first, last}) {
          return searchByName(name, after: after, first: first);
        },
      ),
      onKey: (RxChatContact u) => u.id,
    );
  }

  final List<RxChatContact> contacts =
      this.contacts.values
          .where(
            (u) =>
                (phone != null && u.contact.value.phones.contains(phone)) ||
                (email != null && u.contact.value.emails.contains(email)) ||
                (name != null &&
                    u.contact.value.name.val.toLowerCase().contains(
                          name.val.toLowerCase(),
                        ) ==
                        true),
          )
          .toList();

  Map<ChatContactId, RxChatContact> toMap(RxChatContact? c) {
    if (c != null) {
      return {c.id: c};
    }

    return {};
  }

  return PaginatedImpl(
    pagination: pagination,
    initial: [
      {for (var u in contacts) u.id: u},
      if (email != null) searchByEmail(email).then(toMap),
      if (phone != null) searchByPhone(phone).then(toMap),
    ],
  );
}