update pubspec
This commit is contained in:
@@ -16,22 +16,22 @@ class PaginatorListener<T> {
|
||||
|
||||
List<String> ids = [];
|
||||
List<T> data = [];
|
||||
DocumentSnapshot prev;
|
||||
DocumentSnapshot? prev;
|
||||
int rowPerLoad = 10;
|
||||
bool ended = false;
|
||||
bool isLoading = false;
|
||||
bool insertNewByListener = false;
|
||||
ToObj toObj;
|
||||
CallBack onChange;
|
||||
CallBack? onChange;
|
||||
|
||||
StreamSubscription<QuerySnapshot> listener;
|
||||
Query listeningQuery;
|
||||
Query pageQuery;
|
||||
StreamSubscription<QuerySnapshot>? listener;
|
||||
Query? listeningQuery;
|
||||
Query? pageQuery;
|
||||
|
||||
PaginatorListener(this.toObj,
|
||||
{this.onChange, this.rowPerLoad = 10, this.insertNewByListener = false});
|
||||
|
||||
Future<bool> refresh({Query listeningQuery, Query pageQuery}) {
|
||||
Future<bool> refresh({Query? listeningQuery, Query? pageQuery}) {
|
||||
this.listeningQuery = listeningQuery ?? this.listeningQuery;
|
||||
this.pageQuery = pageQuery ?? this.pageQuery;
|
||||
_clearState();
|
||||
@@ -45,7 +45,7 @@ class PaginatorListener<T> {
|
||||
data = [];
|
||||
ended = false;
|
||||
isLoading = false;
|
||||
if (listener != null) listener.cancel();
|
||||
if (listener != null) listener!.cancel();
|
||||
listener = null;
|
||||
}
|
||||
|
||||
@@ -58,35 +58,39 @@ class PaginatorListener<T> {
|
||||
final String isDeletedField = 'is_deleted';
|
||||
void _initListener() {
|
||||
Query _query =
|
||||
listeningQuery.orderBy(updateTimeField, descending: true).limit(1);
|
||||
_query.getDocuments(source: Source.server).then((QuerySnapshot snapshot) {
|
||||
int count = snapshot.documents.length;
|
||||
listeningQuery!.orderBy(updateTimeField, descending: true).limit(1);
|
||||
_query
|
||||
.get(GetOptions(source: Source.server))
|
||||
.then((QuerySnapshot snapshot) {
|
||||
int count = snapshot.docs.length;
|
||||
int updateTime = 0;
|
||||
if (count == 1) {
|
||||
updateTime = snapshot.documents[0].data[updateTimeField];
|
||||
Map<String, dynamic> data =
|
||||
snapshot.docs[0].data()! as Map<String, dynamic>;
|
||||
updateTime = data[updateTimeField];
|
||||
}
|
||||
|
||||
Query _queryListener = listeningQuery
|
||||
Query _queryListener = listeningQuery!
|
||||
.where(updateTimeField, isGreaterThan: updateTime)
|
||||
.orderBy(updateTimeField, descending: true);
|
||||
|
||||
if (listener != null) listener.cancel();
|
||||
if (listener != null) listener!.cancel();
|
||||
listener =
|
||||
_queryListener.snapshots(includeMetadataChanges: true).listen((qs) {
|
||||
qs.documentChanges.forEach((c) {
|
||||
qs.docChanges.forEach((c) {
|
||||
switch (c.type) {
|
||||
case DocumentChangeType.added:
|
||||
_update(c.document.documentID, c.document.data);
|
||||
_update(c.doc.id, c.doc.data() as Map<String, dynamic>);
|
||||
break;
|
||||
case DocumentChangeType.modified:
|
||||
_update(c.document.documentID, c.document.data);
|
||||
_update(c.doc.id, c.doc.data() as Map<String, dynamic>);
|
||||
break;
|
||||
case DocumentChangeType.removed:
|
||||
_remove(c.document.documentID, c.document.data);
|
||||
_remove(c.doc.id, c.doc.data() as Map<String, dynamic>);
|
||||
break;
|
||||
default:
|
||||
}
|
||||
if (onChange != null) onChange();
|
||||
if (onChange != null) onChange!();
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -129,26 +133,26 @@ class PaginatorListener<T> {
|
||||
return this._load();
|
||||
}
|
||||
|
||||
Future<bool> _load({CallBack onStarted, CallBack onFinished}) async {
|
||||
Future<bool> _load({CallBack? onStarted, CallBack? onFinished}) async {
|
||||
if (ended) return ended;
|
||||
Query _query =
|
||||
prev != null ? pageQuery.startAfterDocument(prev) : pageQuery;
|
||||
prev != null ? pageQuery!.startAfterDocument(prev!) : pageQuery!;
|
||||
try {
|
||||
isLoading = true;
|
||||
if (onStarted != null) {
|
||||
onStarted();
|
||||
}
|
||||
if (onChange != null) onChange();
|
||||
if (onChange != null) onChange!();
|
||||
await _query
|
||||
.where(isDeletedField, isEqualTo: false)
|
||||
.limit(rowPerLoad)
|
||||
.getDocuments(source: Source.server)
|
||||
.get(GetOptions(source: Source.server))
|
||||
.then((QuerySnapshot snapshot) {
|
||||
int count = snapshot.documents.length;
|
||||
int count = snapshot.docs.length;
|
||||
ended = count < rowPerLoad;
|
||||
prev = count > 0 ? snapshot.documents[count - 1] : prev;
|
||||
snapshot.documents.forEach((e) {
|
||||
_add(e.documentID, e.data);
|
||||
prev = count > 0 ? snapshot.docs[count - 1] : prev;
|
||||
snapshot.docs.forEach((e) {
|
||||
_add(e.id, e.data() as Map<String, dynamic>);
|
||||
});
|
||||
});
|
||||
} catch (e) {
|
||||
@@ -156,7 +160,7 @@ class PaginatorListener<T> {
|
||||
} finally {
|
||||
isLoading = false;
|
||||
if (onFinished != null) onFinished();
|
||||
if (onChange != null) onChange();
|
||||
if (onChange != null) onChange!();
|
||||
}
|
||||
return ended;
|
||||
}
|
||||
|
||||
@@ -10,19 +10,17 @@ typedef OnScroll = void Function(bool down);
|
||||
class PaginatorListView<T> extends StatelessWidget {
|
||||
final PaginatorListener<T> paginatorListener;
|
||||
final RowBuilder rowBuilder;
|
||||
final OnScroll onScroll;
|
||||
final OnScroll? onScroll;
|
||||
final ScrollController _scrollController;
|
||||
final Color color;
|
||||
|
||||
PaginatorListView(
|
||||
{Key key,
|
||||
this.paginatorListener,
|
||||
this.rowBuilder,
|
||||
{Key? key,
|
||||
required this.paginatorListener,
|
||||
required this.rowBuilder,
|
||||
this.onScroll,
|
||||
this.color = Colors.blueAccent})
|
||||
: _scrollController = ScrollController(),
|
||||
assert(paginatorListener != null),
|
||||
assert(rowBuilder != null),
|
||||
super(key: key) {
|
||||
_scrollController.addListener(() async {
|
||||
if (_scrollController.position.pixels ==
|
||||
@@ -32,7 +30,7 @@ class PaginatorListView<T> extends StatelessWidget {
|
||||
if (onScroll != null) {
|
||||
var down = _scrollController.position.userScrollDirection ==
|
||||
ScrollDirection.forward;
|
||||
onScroll(down);
|
||||
onScroll!(down);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user