MessageFieldController constructor

MessageFieldController(
  1. ChatService? _chatService,
  2. UserService? _userService,
  3. AbstractSettingsRepository? _settingsRepository, {
  4. void onSubmit()?,
  5. void onChanged()?,
  6. void onCall(
    1. bool
    )?,
  7. String? text,
  8. List<ChatItemQuoteInput> quotes = const [],
  9. List<Attachment> attachments = const [],
  10. bool onKeyUp(
    1. LogicalKeyboardKey
    )?,
})

Implementation

MessageFieldController(
  this._chatService,
  this._userService,
  this._settingsRepository, {
  this.onSubmit,
  this.onChanged,
  this.onCall,
  String? text,
  List<ChatItemQuoteInput> quotes = const [],
  List<Attachment> attachments = const [],
  this.onKeyUp,
}) : quotes = RxList(quotes),
     attachments = RxList(
       attachments.map((e) => MapEntry(GlobalKey(), e)).toList(),
     ) {
  field = TextFieldState(
    text: text,
    onFocus: (s) {
      onChanged?.call();
    },
    submitted: false,
    onSubmitted: (s) {
      field.unsubmit();
      onSubmit?.call();
    },
    focus: FocusNode(onKeyEvent: (_, KeyEvent e) => handleNewLines(e, field)),
  );

  field.focus.addListener(_focusListener);

  _repliesWorker ??= ever(replied, (_) => onChanged?.call());
  _attachmentsWorker ??= ever(this.attachments, (_) => onChanged?.call());
  _editedWorker ??= ever(edited, (item) {
    if (item != null) {
      field.text = item.text?.val ?? '';
      this.attachments.value = item.attachments
          .map((e) => MapEntry(GlobalKey(), e))
          .toList();
      replied.value = item.repliesTo
          .map((e) => e.original)
          .nonNulls
          .map((e) => Rx(e))
          .toList();
    } else {
      field.text = '';
      this.attachments.clear();
      replied.clear();
    }

    onChanged?.call();
  });
}