onInit method

  1. @override
void onInit()
override

Called immediately after the widget is allocated in memory. You might use this to initialize something for the controller.

Implementation

@override
void onInit() {
  if (PlatformUtils.isMobile && !PlatformUtils.isWeb) {
    BackButtonInterceptor.add(_onBack, ifNotYetIntercepted: true);
  }

  send = MessageFieldController(
    _chatService,
    _userService,
    _settingsRepository,
    onChanged: updateDraft,
    onCall: call,
    onKeyUp: (key) {
      if (send.field.controller.text.isNotEmpty) {
        return false;
      }

      if (key == LogicalKeyboardKey.arrowUp) {
        final previous = chat?.messages.lastWhereOrNull((e) {
          return e.value is ChatMessage && !e.value.id.isLocal;
        });

        if (previous != null) {
          if (previous.value.isEditable(chat!.chat.value, me!)) {
            editMessage(previous.value);
            return true;
          }
        }
      }

      return false;
    },
    onSubmit: () async {
      _stopTyping();

      if (chat == null) {
        return;
      }

      if (send.field.text.trim().isNotEmpty ||
          send.attachments.isNotEmpty ||
          send.replied.isNotEmpty) {
        _chatService
            .sendChatMessage(
              chat?.chat.value.id ?? id,
              text: send.field.text.trim().isEmpty
                  ? null
                  : ChatMessageText(send.field.text.trim()),
              repliesTo: send.replied.map((e) => e.value).toList(),
              attachments: send.attachments.map((e) => e.value).toList(),
            )
            .then(
              (_) => AudioUtils.once(
                AudioSource.asset('audio/message_sent.mp3'),
              ),
            )
            .onError<PostChatMessageException>(
              (_, __) => _showBlockedPopup(),
              test: (e) => e.code == PostChatMessageErrorCode.blocked,
            )
            .onError<UploadAttachmentException>(
              (e, _) => MessagePopup.error(e),
            )
            .onError<ConnectionException>((e, _) {});

        send.clear(unfocus: false);

        chat?.setDraft();
      }
    },
  );

  PlatformUtils.isActive.then((value) => active.value = value);
  _onActivityChanged = PlatformUtils.onActivityChanged.listen((v) {
    active.value = v;

    if (v) {
      readChat(_lastSeenItem.value);
    }
  });

  _selectingWorker = ever(selecting, (bool value) {
    if (!value) {
      selected.clear();
    }
  });

  _onFocusChanged = PlatformUtils.onFocusChanged.listen((value) {
    if (!value) {
      _stopTyping();
    }
  });

  // Stop the [_typingSubscription] when the send field loses its focus.
  send.field.focus.addListener(_stopTypingOnUnfocus);

  search.focus.addListener(_disableSearchFocusListener);

  _searchDebounce = debounce(query, (String? query) async {
    status.value = RxStatus.loadingMore();

    if (query == null || query.isEmpty) {
      if (searching.value) {
        switchToMessages();
        status.value = RxStatus.success();
      }
    } else {
      _fragment = null;
      elements.clear();

      final Paginated<ChatItemId, Rx<ChatItem>>? fragment = await chat!
          .around(withText: ChatMessageText(query));

      _searchSubscription?.cancel();
      _searchSubscription = fragment!.updates.listen(
        null,
        onDone: () {
          _fragments.remove(fragment);
          _fragmentSubscriptions.remove(_searchSubscription?..cancel());

          // If currently used fragment is the one disposed, then switch to
          // the [RxChat.messages] for the [elements].
          if (_fragment == fragment) {
            switchToMessages();
          }
        },
      );

      _fragment = fragment;

      await _fragment!.around();

      elements.clear();
      _fragment!.items.values.forEach(_add);
      _subscribeFor(fragment: _fragment);
      _updateFabStates();

      status.value = RxStatus.success();
    }
  });

  super.onInit();
}