Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix generating load trigger notification when itemCount is less than prefetchIndex. #9

Merged
merged 4 commits into from
Dec 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## Upcoming

- [[#7](https://github.com/xsahil03x/super_paging/issues/7)] Fix generating load trigger notification when `itemCount` is less than `prefetchIndex`.
- Added support for the `Pager.refresh` method to accept an optional `refreshKey` parameter.

## 0.1.0
Expand Down
29 changes: 18 additions & 11 deletions lib/src/widget/bidirectional_paging_list_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -463,24 +463,31 @@ class _BidirectionalPagingListViewState<Key, Value>
}) {
final items = pages.items;
final itemCount = items.length;
final fetchIndex = pager.config.prefetchIndex;
final prefetchIndex = pager.config.prefetchIndex;

// Helper function to generate prepend and append load trigger notifications
void generatePrependAppendLoadTriggerNotification(int index) {
// If there is no prefetch index, we don't need to generate any
// notifications.
if (fetchIndex == null) return;

// Check if the index corresponds to near the top or bottom based on the
// 'reverse' flag.
final nearTop =
reverse ? index == itemCount - fetchIndex : index == fetchIndex;
final nearBottom =
reverse ? index == fetchIndex : index == itemCount - fetchIndex;
if (prefetchIndex == null) return;

// Generate notifications at the beginning and end of the list if the
// [itemCount] is less than [prefetchIndex].
if (prefetchIndex > itemCount) {
if (index == 0) onBuildingPrependLoadTriggerItem?.call();
if (index == itemCount - 1) onBuildingAppendLoadTriggerItem?.call();
return;
}

// Check if the index corresponds to near the top or bottom of the list
// based on the [reverse] flag.
final (nearTop, nearBottom) = switch (reverse) {
true => (index == itemCount - prefetchIndex, index == prefetchIndex),
false => (index == prefetchIndex, index == itemCount - prefetchIndex),
};

// Generate prepend notification.
// Generate notifications.
if (nearTop) onBuildingPrependLoadTriggerItem?.call();
// Generate append notification.
if (nearBottom) onBuildingAppendLoadTriggerItem?.call();
}

Expand Down
23 changes: 16 additions & 7 deletions lib/src/widget/paging_sliver_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -265,20 +265,29 @@ class _PagingSliverListState<Key, Value>
final itemCount = items.length;
final prefetchIndex = pager.config.prefetchIndex;

// Helper function to generate prepend and append load trigger notifications
void generatePrependAppendLoadTriggerNotification(int index) {
// If there is no prefetch index, we don't have to generate any
// notification.
if (prefetchIndex == null) return;

// Generate prepend notification.
if (index == prefetchIndex) {
onBuildingPrependLoadTriggerItem?.call();
// Generate notifications at the beginning and end of the list if the
// [itemCount] is less than [prefetchIndex].
if (prefetchIndex > itemCount) {
if (index == 0) onBuildingPrependLoadTriggerItem?.call();
if (index == itemCount - 1) onBuildingAppendLoadTriggerItem?.call();
return;
}

// Generate append notification.
if (index == itemCount - prefetchIndex) {
onBuildingAppendLoadTriggerItem?.call();
}
// Check if the index corresponds to near the top or bottom of the list.
final (nearTop, nearBottom) = (
index == prefetchIndex,
index == itemCount - prefetchIndex,
);

// Generate notifications.
if (nearTop) onBuildingPrependLoadTriggerItem?.call();
if (nearBottom) onBuildingAppendLoadTriggerItem?.call();
}

final itemBuilder = widget.itemBuilder;
Expand Down