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 #33 #34

Merged
merged 7 commits into from
Dec 27, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## 3.0.1

- fix: docs not loading after the first sroll
- fix: docs not loading after the first scroll

## 3.0.0

Expand Down
77 changes: 63 additions & 14 deletions lib/src/realtime_db_pagination.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class RealtimeDBPagination extends StatefulWidget {
required this.itemBuilder,
required this.orderBy,
super.key,
this.descending = false,
this.separatorBuilder,
this.limit = 10,
this.viewType = ViewType.list,
Expand All @@ -69,6 +70,8 @@ class RealtimeDBPagination extends StatefulWidget {
/// ### Note:
/// - The query must **NOT** contain a `limitToFirst` or `limitToLast` itself.
/// - The `limit` must be set using the [limit] property of this widget.
/// - The query must **NOT** contain `orderBy` itself. The field to order the
/// data by must be set using the [orderBy] property of this widget.
final Query query;

/// The builder to use to build the items in the list.
Expand All @@ -90,6 +93,11 @@ class RealtimeDBPagination extends StatefulWidget {
/// If null, the data will be sorted by the key.
final String? orderBy;

/// Fetches data is decending order for the given [orderBy] field.
///
/// Default value is `false`.
final bool descending;

/// The builder to use to render the separator.
///
/// Only used if [viewType] is [ViewType.list].
Expand Down Expand Up @@ -202,14 +210,37 @@ class _RealtimeDBPaginationState extends State<RealtimeDBPagination> {

if (getMore) setState(() => _isFetching = true);

// Sets limit of nodes to fetch.
// If currently 15 items are loaded, and limit is 5 then total 20 items
// will be fetched including the ones already present.
final docsLimit = _data.length + (getMore ? widget.limit : 0);
var docsQuery = widget.query.limitToFirst(docsLimit);
var docsQuery = widget.descending
? widget.query.limitToLast(docsLimit)
: widget.query.limitToFirst(docsLimit);

if (_data.isNotEmpty) {
docsQuery = docsQuery.startAt(
Map<String, dynamic>.from(
_data.first.value! as Map<Object?, Object?>,
)[widget.orderBy],
);
if (widget.descending) {
// Sets ending point from where before data should be fetched.
// If currently 15 items are loaded, and limit is 5 then total 20 items
// will be fetched where below mentioned value will be the largest and
// last in the fetched array (But first in callback array as using
// reversed in build method)
docsQuery = docsQuery.endAt(
Map<String, dynamic>.from(
_data.last.value! as Map<Object?, Object?>,
)[widget.orderBy],
);
} else {
// Sets starting point from where after data should be fetched.
// If currently 15 items are loaded, and limit is 5 then total 20 items
// will be fetched where below mentioned value will be the smallest and
// first in array
docsQuery = docsQuery.startAt(
Map<String, dynamic>.from(
_data.first.value! as Map<Object?, Object?>,
)[widget.orderBy],
);
}
}

_streamSub = docsQuery.onValue.listen((DatabaseEvent snapshot) async {
Expand Down Expand Up @@ -260,21 +291,39 @@ class _RealtimeDBPaginationState extends State<RealtimeDBPagination> {
// To cancel previous live listener when new one is set.
final tempSub = _liveStreamSub;

var latestDocQuery = widget.query.limitToFirst(1);
var latestDocQuery = widget.descending
? widget.query.limitToLast(1)
: widget.query.limitToFirst(1);

if (_data.isNotEmpty) {
latestDocQuery = latestDocQuery.endBefore(
Map<String, dynamic>.from(
_data.first.value! as Map<Object?, Object?>,
)[widget.orderBy],
);
if (widget.descending) {
// Sets query to fetch data after the last element in the array,
// which is the largest value.
latestDocQuery = latestDocQuery.startAfter(
Map<String, dynamic>.from(
_data.last.value! as Map<Object?, Object?>,
)[widget.orderBy],
);
} else {
// Sets query to fetch data before the first element in the array,
// whch is the smallest value
latestDocQuery = latestDocQuery.endBefore(
Map<String, dynamic>.from(
_data.first.value! as Map<Object?, Object?>,
)[widget.orderBy],
);
}
}

_liveStreamSub = latestDocQuery.onValue.listen(
(DatabaseEvent snapshot) async {
await tempSub?.cancel();
if (snapshot.snapshot.children.isEmpty) return;

_data.insert(0, snapshot.snapshot.children.first);
_data.insert(
widget.descending ? _data.length : 0,
snapshot.snapshot.children.first,
);

// To handle newly added data after this curently loaded data.
await _setLiveListener();
Expand Down Expand Up @@ -319,7 +368,7 @@ class _RealtimeDBPaginationState extends State<RealtimeDBPagination> {
: _data.isEmpty
? widget.onEmpty
: BuildPagination(
items: _data,
items: widget.descending ? _data.reversed.toList() : _data,
itemBuilder: widget.itemBuilder,
separatorBuilder: widget.separatorBuilder ?? separatorBuilder,
isLoading: _isFetching,
Expand Down