From 2ccbaf23bf034739963bbdc737c49e7607693d20 Mon Sep 17 00:00:00 2001 From: Sahil Kumar Date: Fri, 29 Nov 2024 22:40:58 +0100 Subject: [PATCH 1/3] fix: fix generating load trigger notification when itemCount is less than prefetchIndex Signed-off-by: Sahil Kumar --- .../bidirectional_paging_list_view.dart | 25 ++++++++++++------- lib/src/widget/paging_sliver_list.dart | 23 +++++++++++------ 2 files changed, 32 insertions(+), 16 deletions(-) diff --git a/lib/src/widget/bidirectional_paging_list_view.dart b/lib/src/widget/bidirectional_paging_list_view.dart index ac5d645..459f58a 100644 --- a/lib/src/widget/bidirectional_paging_list_view.dart +++ b/lib/src/widget/bidirectional_paging_list_view.dart @@ -471,16 +471,23 @@ class _BidirectionalPagingListViewState // 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; - - // Generate prepend notification. + // Generate notifications at the beginning and end of the list if the + // [itemCount] is less than [prefetchIndex]. + if (itemCount < fetchIndex) { + 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 - fetchIndex, index == fetchIndex), + false => (index == fetchIndex, index == itemCount - fetchIndex), + }; + + // Generate notifications. if (nearTop) onBuildingPrependLoadTriggerItem?.call(); - // Generate append notification. if (nearBottom) onBuildingAppendLoadTriggerItem?.call(); } diff --git a/lib/src/widget/paging_sliver_list.dart b/lib/src/widget/paging_sliver_list.dart index 95830a2..80955db 100644 --- a/lib/src/widget/paging_sliver_list.dart +++ b/lib/src/widget/paging_sliver_list.dart @@ -265,20 +265,29 @@ class _PagingSliverListState 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 (itemCount < prefetchIndex) { + 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; From 3bc3cb9f9d95516f22294204bc8040322458daec Mon Sep 17 00:00:00 2001 From: Sahil Kumar Date: Fri, 29 Nov 2024 22:40:58 +0100 Subject: [PATCH 2/3] chore: switch operands, rename to prefetchIndex Signed-off-by: Sahil Kumar --- lib/src/widget/bidirectional_paging_list_view.dart | 10 +++++----- lib/src/widget/paging_sliver_list.dart | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/src/widget/bidirectional_paging_list_view.dart b/lib/src/widget/bidirectional_paging_list_view.dart index 459f58a..5e30e6d 100644 --- a/lib/src/widget/bidirectional_paging_list_view.dart +++ b/lib/src/widget/bidirectional_paging_list_view.dart @@ -463,17 +463,17 @@ class _BidirectionalPagingListViewState }) { 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; + if (prefetchIndex == null) return; // Generate notifications at the beginning and end of the list if the // [itemCount] is less than [prefetchIndex]. - if (itemCount < fetchIndex) { + if (prefetchIndex > itemCount) { if (index == 0) onBuildingPrependLoadTriggerItem?.call(); if (index == itemCount - 1) onBuildingAppendLoadTriggerItem?.call(); return; @@ -482,8 +482,8 @@ class _BidirectionalPagingListViewState // 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 - fetchIndex, index == fetchIndex), - false => (index == fetchIndex, index == itemCount - fetchIndex), + true => (index == itemCount - prefetchIndex, index == prefetchIndex), + false => (index == prefetchIndex, index == itemCount - prefetchIndex), }; // Generate notifications. diff --git a/lib/src/widget/paging_sliver_list.dart b/lib/src/widget/paging_sliver_list.dart index 80955db..4ce01ae 100644 --- a/lib/src/widget/paging_sliver_list.dart +++ b/lib/src/widget/paging_sliver_list.dart @@ -273,7 +273,7 @@ class _PagingSliverListState // Generate notifications at the beginning and end of the list if the // [itemCount] is less than [prefetchIndex]. - if (itemCount < prefetchIndex) { + if (prefetchIndex > itemCount) { if (index == 0) onBuildingPrependLoadTriggerItem?.call(); if (index == itemCount - 1) onBuildingAppendLoadTriggerItem?.call(); return; From 732373febfb89c526cb617c70ff43d11677508c3 Mon Sep 17 00:00:00 2001 From: Sahil Kumar Date: Fri, 29 Nov 2024 23:52:17 +0100 Subject: [PATCH 3/3] chore: update CHANGELOG.md Signed-off-by: Sahil Kumar --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2b8a381..2668e57 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## Upcoming + +- [[#7](https://github.com/xsahil03x/super_paging/issues/7)] Fix generating load trigger notification when `itemCount` is less than `prefetchIndex`. + ## 0.1.0 - Initial Release. \ No newline at end of file