onInit method
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,
onSubmit: () async {
_stopTyping();
if (chat == null) {
return;
}
if (send.forwarding.value) {
if (send.replied.isNotEmpty) {
if (send.replied.any((e) => e is ChatCall)) {
MessagePopup.error('err_cant_forward_calls'.l10n);
return;
}
await ChatForwardView.show(
onContext?.call() ?? router.context!,
id,
send.replied
.map((e) => ChatItemQuoteInput(item: e.value))
.toList(),
text: send.field.text,
attachments: send.attachments.map((e) => e.value).toList(),
onSent: send.clear,
);
}
} else {
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);
status.value = RxStatus.success();
}
});
super.onInit();
}