loadPrevious method

Future<void> loadPrevious()

Loads previous (oldest direction) message lists.

Implementation

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

  if (_isLoading) throw QueryInProgressException();
  if (!_hasPrevious) return;
  if (_oldestMessage == null) return;

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

  _loadPreviousParams
    ..nextResultSize = 0
    ..inclusive = true;

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

  if (_isDisposed) return;

  if (messages.isNotEmpty) {
    final oldestMessage =
        _loadPreviousParams.reverse ? messages.last : messages.first;
    _oldestMessage = oldestMessage;

    _hasPrevious =
        messages.length - _getExistedMessageCountInMessageList(messages) ==
            _loadPreviousParams.previousResultSize;
  } else {
    _hasPrevious = false;
  }

  _isLoading = false;

  if (messages.isNotEmpty) {
    _chat.collectionManager.sendEventsToMessageCollection(
      messageCollection: this,
      groupChannel: channel,
      eventSource: CollectionEventSource.messageLoadPrevious,
      sendingStatus: SendingStatus.succeeded,
      addedMessages: messages,
      isReversedAddedMessages:
          !_loadPreviousParams.reverse, // Added ! because of loadPrevious()
    );
  }
}