fetchItems method
void
fetchItems({
- bool firstTimeFetch = false,
- required TextEditingController textEditingController,
- String? searchKeyword,
fetchItems is a method which is used to fetch the items
Implementation
void fetchItems(
{bool firstTimeFetch = false,
required TextEditingController textEditingController,
String? searchKeyword}) async {
await _request.fetchNext(onSuccess: (users) {
if (users.isEmpty) {
if (suggestionListEventSink != null) {
suggestionListEventSink?.add([]);
}
if (firstTimeFetch) {
mentionTracker = "";
mentionStartIndex = 0;
mentionEndIndex = 0;
if (onSearch != null) {
onSearch!(null);
}
CometChatUIEvents.hidePanel(
composerId, CustomUIPosition.composerPreview);
}
} else {
if (listItems.isEmpty) {
CometChatUIEvents.hidePanel(
composerId, CustomUIPosition.composerPreview);
listItems = users;
} else {
listItems.addAll(users);
}
if (suggestionListEventSink != null && users.isNotEmpty) {
suggestionListEventSink?.add((users as List<User>)
.map((user) => SuggestionListItem(
id: user.uid,
title: user.name,
avatarStyle: const AvatarStyle(
width: 30,
height: 30,
),
avatarUrl: user.avatar,
avatarName: user.name,
onTap: () {
String mention = "@${user.name}";
int cursorPos = textEditingController.selection.base.offset;
int leftMatchesFound = RegExp(mention)
.allMatches(textEditingController.text
.substring(0, cursorPos - mentionTracker.length))
.length;
int rightMatchesFound = RegExp(mention)
.allMatches(
textEditingController.text.substring(cursorPos))
.length;
if (mentionedUsersMap.containsKey(mention)) {
while (mentionedUsersMap[mention]!.length <
leftMatchesFound + rightMatchesFound) {
mentionedUsersMap[mention]!
.insert(leftMatchesFound, null);
}
if (mentionTracker.trim() == mention) {
mentionedUsersMap[mention]![leftMatchesFound] = user;
} else if (conflictingIndex != null &&
conflictingIndex! <
mentionedUsersMap[mention]!.length) {
mentionedUsersMap[mention]?[conflictingIndex!] = user;
conflictingIndex = null;
} else {
mentionedUsersMap[mention]!
.insert(leftMatchesFound, user);
}
if (!mentionCount.contains(user.uid)) {
mentionCount.add(user.uid);
}
} else if (!mentionedUsersMap.containsKey(mention) &&
leftMatchesFound + rightMatchesFound > 0) {
mentionedUsersMap[mention] = [user];
if (!mentionCount.contains(user.uid)) {
mentionCount.add(user.uid);
}
while (mentionedUsersMap[mention]!.length <
leftMatchesFound + rightMatchesFound + 1) {
if (leftMatchesFound > rightMatchesFound) {
mentionedUsersMap[mention]!
.insert(leftMatchesFound - 1, null);
} else if (rightMatchesFound > leftMatchesFound) {
mentionedUsersMap[mention]!
.insert(rightMatchesFound - 1, null);
}
}
} else {
mentionedUsersMap[mention] = [user];
if (!mentionCount.contains(user.uid)) {
mentionCount.add(user.uid);
}
}
String textOnLeftOfMention = textEditingController.text
.substring(0, cursorPos - mentionTracker.length);
String textOnRightOfMention =
textEditingController.text.substring(cursorPos);
textEditingController.text =
"$textOnLeftOfMention$mention $textOnRightOfMention";
updatePreviousText(textEditingController.text);
textEditingController.selection = TextSelection(
baseOffset: cursorPos -
mentionTracker.length +
mention.length +
1,
extentOffset: cursorPos -
mentionTracker.length +
mention.length +
1,
);
lastCursorPos =
cursorPos - mentionTracker.length + mention.length + 1;
resetMentionsTracker();
CometChatUIEvents.hidePanel(
composerId, CustomUIPosition.composerPreview);
}))
.toList());
}
}
hasMore = !users.isEmpty;
}, onError: (exception) {
if (onError != null) {
onError!(exception);
}
});
}