beginLoadedHistoryReadPositionLocation method

NIMMessage? beginLoadedHistoryReadPositionLocation()

Starts a location using the current page when it covers the unread edge.

This path does not show loading because it performs no message query.

Implementation

NIMMessage? beginLoadedHistoryReadPositionLocation() {
  if (_isDisposed ||
      historyReadPositionState != HistoryReadPositionState.ready) {
    return null;
  }
  final readTime = historyReadPositionTracker.lastReadTime;
  if (readTime == null) return null;

  final candidates = _messageList
      .map((message) => message.nimMessage)
      .where(
        (message) => ChatMessageRepo.isValidHistoryReadPositionTarget(
          message,
          conversationId,
          readTime,
        ),
      )
      .toList();
  if (candidates.isEmpty) return null;

  final count = historyReadPositionCount;
  final coversUnreadByCount = !historyReadPositionCountTruncated &&
      count != null &&
      count > 0 &&
      candidates.length >= count;
  final coversReadBoundary = !hasMoreForwardMessages ||
      _messageList.any(
        (message) => (message.nimMessage.createTime ?? 0) <= readTime,
      );
  if (!coversUnreadByCount && !coversReadBoundary) return null;

  candidates.sort(
    (a, b) => (a.createTime ?? 0).compareTo(b.createTime ?? 0),
  );
  final target = candidates.first;
  if (!historyReadPositionTracker.beginLocation()) return null;
  _historyReadPositionTargetId = _historyReadPositionMessageId(target);
  _historyReadPositionLoading = false;
  notifyListeners();
  return target;
}