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() {
send = MessageFieldController(
_chatService,
_userService,
_settingsRepository,
text: text,
quotes: quotes,
attachments: attachments,
onSubmit: () async {
if (selected.value?.isEmpty != false) {
send.field.unsubmit();
return;
}
send.field.status.value = RxStatus.loading();
send.field.editable.value = false;
try {
final List<Future> uploads =
send.attachments
.map((e) => e.value)
.whereType<LocalAttachment>()
.map((e) => e.upload.value?.future)
.nonNulls
.toList();
if (uploads.isNotEmpty) {
await Future.wait(uploads);
}
if (send.attachments.whereType<LocalAttachment>().isNotEmpty) {
throw const ConnectionException(
ForwardChatItemsException(
ForwardChatItemsErrorCode.unknownAttachment,
),
);
}
final List<AttachmentId>? attachments =
send.attachments.isEmpty
? null
: send.attachments.map((a) => a.value.id).toList();
final ChatMessageText? text =
send.field.text.isEmpty ? null : ChatMessageText(send.field.text);
final List<ChatItemQuoteInput> quotes = send.quotes.reversed.toList();
// Displays a [MessagePopup.error] visually representing a blocked by
// the provided [user] error.
Future<void> showBlockedPopup(User? user) async {
if (user == null) {
await MessagePopup.error('err_blocked'.l10n);
} else {
await MessagePopup.error(
'err_blocked_by'.l10nfmt({'user': '${user.name ?? user.num}'}),
);
}
}
final List<Future<void>> futures = [
...selected.value!.chats.map((e) {
return _chatService
.forwardChatItems(
from,
e.chat.value.id,
quotes,
text: text,
attachments: attachments,
)
.onError<ForwardChatItemsException>((_, __) async {
await showBlockedPopup(
e.members.values
.firstWhereOrNull((u) => u.user.id != me)
?.user
.user
.value,
);
}, test: (e) => e.code == ForwardChatItemsErrorCode.blocked);
}),
...selected.value!.users.map((u) {
final User user = u.user.value;
final ChatId dialog = user.dialog;
return _chatService
.forwardChatItems(
from,
dialog,
quotes,
text: text,
attachments: attachments,
)
.onError<ForwardChatItemsException>(
(_, __) => showBlockedPopup(user),
test: (e) => e.code == ForwardChatItemsErrorCode.blocked,
);
}),
...selected.value!.contacts.map((c) {
final User user = c.user.value!.user.value;
final ChatId dialog = user.dialog;
return _chatService
.forwardChatItems(
from,
dialog,
quotes,
text: text,
attachments: attachments,
)
.onError<ForwardChatItemsException>(
(_, __) => showBlockedPopup(user),
test: (e) => e.code == ForwardChatItemsErrorCode.blocked,
);
}),
];
await Future.wait(futures);
pop?.call(true);
onSent?.call();
} on ForwardChatItemsException catch (e) {
MessagePopup.error(e);
} catch (e) {
MessagePopup.error(e);
rethrow;
} finally {
send.field.unsubmit();
}
},
);
super.onInit();
}