fetchNext method

Future<List<User>> fetchNext({
  1. required dynamic onSuccess(
    1. List<User> userList
    )?,
  2. required dynamic onError(
    1. CometChatException excep
    )?,
  3. BlockedUsersFetchListener? listener,
})

Fetch the list of Users that logged-in user have blocked.

Android Reference: BlockedUsersRequest.fetchNext()

Implementation

Future<List<User>> fetchNext({
  required Function(List<User> userList)? onSuccess,
  required Function(CometChatException excep)? onError,
  BlockedUsersFetchListener? listener,
}) async {
  try {
    if (limit > _maxLimit) {
      final error = CometChatException(
        ErrorCode.errorLimitExceeded,
        'Limit cannot exceed $_maxLimit',
        'Limit cannot exceed $_maxLimit',
      );
      if (onError != null) onError(error);
      listener?.onError(error);
      return [];
    }

    if (_nextPage > _totalPages && _totalPages != -1) {
      if (onSuccess != null) onSuccess([]);
      return [];
    }

    if (_inProgress) {
      if (onSuccess != null) onSuccess([]);
      return [];
    }

    // Phase 1 — cache (listener-only, first page only). The cached blocked
    // set is COMPLETE, so unlike a conversation page this is not a truncated
    // view; it can still be stale, which the server phase corrects.
    // First page only. `-1` is the never-fetched sentinel and `1` is an
    // explicitly-requested first page (`setPage(0)` stores page + 1); after a
    // fetch `_nextPage` is at least 2, so later pages skip the cache phase
    // rather than re-delivering the same head of the list.
    if (listener != null && _nextPage <= 1) {
      try {
        final cached = await OfflineReadCache.cachedBlockedUsers(
          direction: direction,
          searchKeyword: searchKeyword,
          limit: limit,
        );
        if (cached != null) listener.onCachedBlockedUsers(cached);
      } catch (_) {
        // Non-fatal; the server phase still runs.
      }
    }

    _inProgress = true;

    final sdk = SdkRegistry.getInstance();

    // Captured as the request goes out: a page answered after this
    // session ended is never written (BEH-SYNC-06).
    final issuedIn = OfflineRuntime.instance.readSession;
    final result = await sdk.users.getBlockedUsers(
      limit: limit,
      token: _token != 0 ? _token : null,
      page: _nextPage > 0 ? _nextPage : null,
      searchKeyword: searchKeyword,
      direction: direction,
    );

    if (result.pagination != null) {
      if (result.pagination!.totalPages != null) {
        _totalPages = result.pagination!.totalPages!;
      }
      if (result.pagination!.currentPage != null) {
        _currentPage = result.pagination!.currentPage!;
        _nextPage = _currentPage + _pageOffset;
      }
    }

    if (result.nextToken != null) {
      _token = result.nextToken!;
      key = _token.toString();
    }

    _inProgress = false;

    final users = result.users;

    // Write-through before delivering, so a reader that acts on the callback
    // and immediately re-reads the cache sees the same answer.
    // A cache-write failure never fails the read (BEH-READ-08).
    try {
      await OfflineReadCache.persistBlockedUsers(
        users,
        fetchedTs: DateTime.now().millisecondsSinceEpoch,
        issuedIn: issuedIn,
      );
    } catch (e) {
      // Best-effort: the read still settles with its server page.
      Logger.debug(
        'BlockedUsersRequest',
        'Blocked-users cache write skipped: $e',
      );
    }

    if (onSuccess != null) onSuccess(users);
    listener?.onServerBlockedUsers(users);
    return users;
  } on SdkException catch (sdkEx) {
    _inProgress = false;
    final cometChatEx = CometChatException(
      sdkEx.code,
      sdkEx.details ?? sdkEx.message,
      sdkEx.message,
    );
    if (onError != null) onError(cometChatEx);
    listener?.onError(cometChatEx);
  } catch (e) {
    _inProgress = false;
    final cometChatEx = CometChatException(
      ErrorCode.errorUnhandledException,
      e.toString(),
      e.toString(),
    );
    if (onError != null) onError(cometChatEx);
    listener?.onError(cometChatEx);
  }
  return [];
}