loadNext method

Future<void> loadNext()

Loads next (latest direction) message lists.

Implementation

Future<void> loadNext() async {
  if (_isDisposed) {
    throw InvalidCollectionDisposedException();
  }
  if (!_isInitialized) return;

  if (_isLoading) throw QueryInProgressException();
  if (!_hasNext) return;
  if (_latestMessage == null) return;

  sbLog.i(StackTrace.current, 'loadNext()');
  _isLoading = true;

  _loadNextParams
    ..previousResultSize = 0
    ..inclusive = true;

  List<BaseMessage> messages =
      await _chat.apiClient.send<List<BaseMessage>>(ChannelMessagesGetRequest(
    _chat,
    channelType: ChannelType.group,
    channelUrl: _channel.channelUrl,
    params: _loadNextParams.toJson(),
    timestamp: _latestMessage!.createdAt,
  ));

  if (_isDisposed) return;

  if (messages.isNotEmpty) {
    final latestMessage =
        _loadNextParams.reverse ? messages.first : messages.last;
    _latestMessage = latestMessage;

    _hasNext =
        messages.length - _getExistedMessageCountInMessageList(messages) ==
            _loadNextParams.nextResultSize;
  } else {
    _hasNext = false;
  }

  _isLoading = false;

  if (messages.isNotEmpty) {
    _chat.collectionManager.sendEventsToMessageCollection(
      messageCollection: this,
      groupChannel: channel,
      eventSource: CollectionEventSource.messageLoadNext,
      sendingStatus: SendingStatus.succeeded,
      addedMessages: messages,
      isReversedAddedMessages: _loadNextParams.reverse,
    );
  }
}