cursorInMentionTracker method

void cursorInMentionTracker(
  1. int cursorPosition,
  2. TextEditingController textEditingController,
  3. String previousText
)

Implementation

void cursorInMentionTracker(int cursorPosition,
    TextEditingController textEditingController, String previousText) {
  //first i am making a copy of the mentionedUsersMap
  var mentionedUsersMapCopy = mentionedUsersMap;

  //then i am iterating over the mentionedUsersMap

  int changes = 0;
  bool cursorAtEndOfMention = false;
  for (String mentionId in mentionedUsersMap.keys) {
    //i am finding the matches of the mentionId in the previous text
    var matches = RegExp(mentionId).allMatches(previousText);

    // i am making a copy of the users list
    var usersCopy = mentionedUsersMap[mentionId] ?? [];
    //then i am iterating over the matches
    for (int i = 0; i < matches.length; i++) {
      //checking if the cursor position is between the start and end of the match

      if (previousText.length > textEditingController.text.length &&
          matches.elementAt(i).end == cursorPosition) {
        cursorAtEndOfMention = true;
        mentionTracker = textEditingController.text
            .substring(matches.elementAt(i).start, cursorPosition);
        searchOnActiveMention = true;
        break;
      } else if (previousText.length < textEditingController.text.length &&
          matches.elementAt(i).start == cursorPosition) {
        changes++;
        break;
      } else if (matches.elementAt(i).start <= cursorPosition &&
          matches.elementAt(i).end > cursorPosition &&
          i < usersCopy.length) {
        if (searchOnActiveMention) {
          searchOnActiveMention = false;
        }
        //if yes then i am removing the user from the list
        String uidToRemove = usersCopy[i]?.uid ?? "";
        // mentionedUsersMapCopy[mentionId] = usersCopy..removeAt(i);
        usersCopy.removeAt(i);

        //decrementing the mention count
        changes++;
        //if the list is empty then i am removing the mentionId from the map
        if (usersCopy.isEmpty) {
          mentionedUsersMapCopy.remove(mentionId);
          if (uidToRemove.isNotEmpty) {
            mentionCount.remove(uidToRemove);
          }
        } else {
          // usersCopy.insert(i,null);
          mentionedUsersMapCopy[mentionId] = usersCopy;
          if (!(textEditingController.text.length > previousText.length &&
              textEditingController.text[cursorPosition - 1] ==
                  trackingCharacter)) {
            interceptedMention = mentionId;
          }
        }

        //i am setting the mentionTracker to the text between the start of the match and the cursor position
        if (!(textEditingController.text.length > previousText.length &&
            textEditingController.text[cursorPosition - 1] ==
                trackingCharacter)) {
          mentionTracker = textEditingController.text
              .substring(matches.elementAt(i).start, cursorPosition);
          mentionStartIndex = matches.elementAt(i).start;
          mentionEndIndex = cursorPosition - 1;
        }

        break;
      }
    }
    if (cursorAtEndOfMention || changes > 0) {
      break;
    }
  }
  if (cursorAtEndOfMention) {
    return;
  }

  //finally i am replacing the original the mentionedUsersMap to the copy
  if (changes > 0) {
    mentionedUsersMap = mentionedUsersMapCopy;
  } else {
    int lastIndexOfTrackingChar = textEditingController.text
        .substring(0, cursorPosition)
        .lastIndexOf(trackingCharacter!);
    if (lastIndexOfTrackingChar != -1) {
      String tempTracker = textEditingController.text
          .substring(lastIndexOfTrackingChar, cursorPosition);
      if ((!tempTracker.contains("\n") || !tempTracker.contains("    "))) {
        mentionTracker = tempTracker;
        mentionStartIndex = lastIndexOfTrackingChar;
        mentionEndIndex = cursorPosition - 1;
      }
    }
  }
}