fetchNextPageforCurrentSearchKeyword method

Future<PaginationResult<T>> fetchNextPageforCurrentSearchKeyword()

A method for fetching the next page of items in a search.

Implementation

Future<PaginationResult<T>> fetchNextPageforCurrentSearchKeyword() async {
  if (_currentPage <= 0 || limitPerPage <= 0) {
    throw Exception('Invalid page or limitPerPage.');
  }
  if (_isLoading || !_hasMore || _currentKeyword.isEmpty) {
    return PaginationResult.success(_items);
  }

  _isLoading = true;

  final result = await repository.fetchPaginatedSearchItems(
    keyword: _currentKeyword,
    page: _currentPage,
    limitPerPage: limitPerPage,
  );
  result.when(
    failure: (failure) => _isLoading = false,
    success: (newItems) {
      _items.addAll(newItems);
      _currentPage++;
      _isLoading = false;
      if (newItems.length < limitPerPage) {
        _hasMore = false;
      }
    },
  );

  return result;
}