handlePreMessageSend method

  1. @override
TextMessage handlePreMessageSend(
  1. BuildContext context,
  2. BaseMessage message
)
override

handlePreMessageSend is a method which is used to handle the pre message send context is a BuildContext which is used to build the widget message is a BaseMessage which is used to send the message

Implementation

@override
TextMessage handlePreMessageSend(BuildContext context, BaseMessage message) {
  String messagesText = (message as TextMessage).text;

  mentionedUsersMap.forEach((mention, mentionedUsers) {
    Iterable<RegExpMatch> matches = RegExp(mention).allMatches(messagesText);

    String newMessageText = "";
    int start = 0;
    int end = messagesText.length;

    for (int i = 0; i < matches.length; i++) {
      if (mentionedUsers[i] != null) {
        newMessageText +=
            messagesText.substring(start, matches.elementAt(i).start);
        newMessageText += "<@uid:${mentionedUsers[i]!.uid}>";
      } else {
        newMessageText +=
            messagesText.substring(start, matches.elementAt(i).end);
      }

      start = matches.elementAt(i).end;
    }
    newMessageText += messagesText.substring(start, end);
    messagesText = newMessageText;
  });

  message.text = messagesText;
  message.mentionedUsers = getMentionedUsers(messagesText);
  mentionedUsersMap.clear();
  mentionCount.clear();
  if (mentionTracker.isNotEmpty) {
    resetMentionsTracker();
  }
  return message;
}