Skip to content

Commit

Permalink
refactor: move paging status to extension
Browse files Browse the repository at this point in the history
  • Loading branch information
clragon committed Oct 13, 2024
1 parent 51367db commit 96850e9
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 61 deletions.
70 changes: 9 additions & 61 deletions lib/src/model/paging_state.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import 'package:flutter/foundation.dart';
import 'package:infinite_scroll_pagination/src/model/paging_status.dart';

/// The current item's list, error, and next page key state for a paginated
/// widget.
/// The current item's list, error, and next page key
/// for a paginated widget.
@immutable
class PagingState<PageKeyType, ItemType> {
const PagingState({
Expand All @@ -20,45 +19,17 @@ class PagingState<PageKeyType, ItemType> {
/// The key for the next page to be fetched.
final PageKeyType? nextPageKey;

/// The current pagination status.
PagingStatus get status {
if (_isOngoing) {
return PagingStatus.ongoing;
}

if (_isCompleted) {
return PagingStatus.completed;
}

if (_isLoadingFirstPage) {
return PagingStatus.loadingFirstPage;
}

if (_hasSubsequentPageError) {
return PagingStatus.subsequentPageError;
}

if (_isEmpty) {
return PagingStatus.noItemsFound;
} else {
return PagingStatus.firstPageError;
}
}

@override
String toString() =>
'${objectRuntimeType(this, 'PagingState')}(itemList: \u2524'
'$itemList\u251C, error: $error, nextPageKey: $nextPageKey)';
String toString() => '${objectRuntimeType(this, 'PagingState')}'
'(itemList: \u2524$itemList\u251C, error: $error, nextPageKey: $nextPageKey)';

@override
bool operator ==(Object other) {
if (identical(this, other)) {
return true;
}
return other is PagingState &&
other.itemList == itemList &&
other.error == error &&
other.nextPageKey == nextPageKey;
return identical(this, other) ||
(other is PagingState &&
other.itemList == itemList &&
other.error == error &&
other.nextPageKey == nextPageKey);
}

@override
Expand All @@ -67,27 +38,4 @@ class PagingState<PageKeyType, ItemType> {
error.hashCode,
nextPageKey.hashCode,
);

int? get _itemCount => itemList?.length;

bool get _hasNextPage => nextPageKey != null;

bool get _hasItems {
final itemCount = _itemCount;
return itemCount != null && itemCount > 0;
}

bool get _hasError => error != null;

bool get _isListingUnfinished => _hasItems && _hasNextPage;

bool get _isOngoing => _isListingUnfinished && !_hasError;

bool get _isCompleted => _hasItems && !_hasNextPage;

bool get _isLoadingFirstPage => _itemCount == null && !_hasError;

bool get _hasSubsequentPageError => _isListingUnfinished && _hasError;

bool get _isEmpty => _itemCount != null && _itemCount == 0;
}
53 changes: 53 additions & 0 deletions lib/src/model/paging_status.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'package:infinite_scroll_pagination/infinite_scroll_pagination.dart';

/// All possible status for a pagination.
enum PagingStatus {
completed,
Expand All @@ -7,3 +9,54 @@ enum PagingStatus {
firstPageError,
subsequentPageError,
}

/// Extension methods for [PagingState] to determine the current status.
extension PagingStatusExtension on PagingState {
int? get _itemCount => itemList?.length;

bool get _hasNextPage => nextPageKey != null;

bool get _hasItems {
final itemCount = _itemCount;
return itemCount != null && itemCount > 0;
}

bool get _hasError => error != null;

bool get _isListingUnfinished => _hasItems && _hasNextPage;

bool get _isOngoing => _isListingUnfinished && !_hasError;

bool get _isCompleted => _hasItems && !_hasNextPage;

bool get _isLoadingFirstPage => _itemCount == null && !_hasError;

bool get _hasSubsequentPageError => _isListingUnfinished && _hasError;

bool get _isEmpty => _itemCount != null && _itemCount == 0;

/// The current pagination status.
PagingStatus get status {
if (_isOngoing) {
return PagingStatus.ongoing;
}

if (_isCompleted) {
return PagingStatus.completed;
}

if (_isLoadingFirstPage) {
return PagingStatus.loadingFirstPage;
}

if (_hasSubsequentPageError) {
return PagingStatus.subsequentPageError;
}

if (_isEmpty) {
return PagingStatus.noItemsFound;
} else {
return PagingStatus.firstPageError;
}
}
}

0 comments on commit 96850e9

Please sign in to comment.