loadMoreElements method

  1. @override
dynamic loadMoreElements({
  1. bool isIncluded(
    1. BaseMessage element
    )?,
})
override

Implementation

@override
loadMoreElements({bool Function(BaseMessage element)? isIncluded}) async {
  /// "Fetching again"
  isLoading = true;
  BaseMessage? lastMessage;
  loggedInUser ??= await CometChat.getLoggedInUser();
  await getUnreadCount();
  conversation ??= (await CometChat.getConversation(
      conversationWithId, conversationType, onSuccess: (conversation) {
    if (conversation.lastMessage != null) {
      /// "Marking as read"
      if (kDebugMode) {
        debugPrint("Marking as read from here");
      }
      markAsRead(conversation.lastMessage!);
    }
  }, onError: (_) {}));
  conversationId ??= conversation?.conversationId;

  try {
    await request.fetchPrevious(onSuccess: (List<BaseMessage> fetchedList) {
      if (fetchedList.isEmpty) {
        isLoading = false;
        hasMoreItems = false;
      } else {
        isLoading = false;
        hasMoreItems = true;
        for (var element in fetchedList.reversed) {
          if (element is InteractiveMessage) {
            element = InteractiveMessageUtils
                .getSpecificMessageFromInteractiveMessage(element);
          }
          if (isIncluded != null && isIncluded(element) == true) {
            list.add(element);
          } else {
            list.add(element);
          }
          if(lastParticipantMessage==null){
            if(element.sender?.uid!=loggedInUser?.uid){
              lastParticipantMessage=element;
              markAsRead(element);
            }
          }
        }
        if (inInitialized == false && list.isNotEmpty) {
          lastMessage = list[0];
        }
      }
      update();
    }, onError: (CometChatException e) {
      if (onError != null) {
        onError!(e);
      } else {
        error = e;
        hasError = true;
      }

      update();
    });
  } catch (e, s) {
    error = CometChatException("ERR", s.toString(), "Error");
    hasError = true;
    isLoading = false;
    hasMoreItems = false;
    update();
  }

  if (inInitialized == false) {
    inInitialized = true;
    CometChatUIEvents.ccActiveChatChanged(
        messageListId, lastMessage, user, group, initialUnreadCount ?? 0);
  }
}