toggleSearch method

void toggleSearch([
  1. bool enable = true
])

Enables and initializes or disables and disposes the search.

Implementation

void toggleSearch([bool enable = true]) {
  search.value?.onClose();
  search.value?.search.focus.removeListener(_disableSearchFocusListener);
  _searchSubscription?.cancel();

  if (enable) {
    search.value = SearchController(
      _chatService,
      _userService,
      _contactService,
      _myUserService,
      categories: const [SearchCategory.contact, SearchCategory.user],
    )..onInit();

    _searchSubscription =
        StreamGroup.merge([
          search.value!.contacts.stream,
          search.value!.users.stream,
        ]).listen((_) {
          elements.clear();

          if (search.value?.contacts.isNotEmpty == true) {
            elements.add(const DividerElement(SearchCategory.contact));
            for (RxChatContact c in search.value!.contacts.values) {
              elements.add(ContactElement(c));
            }
          }

          if (search.value?.users.isNotEmpty == true) {
            elements.add(const DividerElement(SearchCategory.user));
            for (RxUser c in search.value!.users.values) {
              elements.add(UserElement(c));
            }
          }
        });

    search.value!.search.focus.addListener(_disableSearchFocusListener);
    search.value!.search.focus.requestFocus();
  } else {
    search.value = null;
    elements.clear();
  }
}