Skip to content

Commit

Permalink
Changed docChanges data type in CollectionPagingListener.
Browse files Browse the repository at this point in the history
  • Loading branch information
hukusuke1007 committed Aug 24, 2020
1 parent 354cd7b commit 1dece88
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 82 deletions.
22 changes: 7 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,14 +258,12 @@ final collectionPaging = CollectionPaging<User>(
Can be used listener and paging features of documents by CollectionPagingListener.<br>

```dart
final ref = User().collectionRef;
final collectionPagingListener = CollectionPagingListener<User>(
query: ref.orderBy('updatedAt', descending: true),
collectionReference: ref,
query: User().collectionRef.orderBy('updatedAt', descending: true),
initialLimit: 20,
pagingLimit: 20,
decode: (snap, collectionRef) =>
User(snapshot: snap, collectionRef: collectionRef),
decode: (snap, _) =>
User(snapshot: snap, collectionRef: snap.reference.parent),
);
// Fetch to set listener.
Expand All @@ -282,10 +280,9 @@ collectionPagingListener.data.listen((event) {
// Get document changes status and cache status.
collectionPagingListener.docChanges.listen((event) {
print('docChanges ${event.length}');
for (var change in event) {
print(
'id: ${change.doc.id}, changeType: ${change.type}, oldIndex: ${change.oldIndex}, newIndex: ${change.newIndex} cache: ${change.doc.metadata.isFromCache}');
for (var item in event) {
final change = item.docChange;
print('id: ${item.doc.id}, changeType: ${change.type}, oldIndex: ${change.oldIndex}, newIndex: ${change.newIndex} cache: ${change.doc.metadata.isFromCache}');
}
});
Expand Down Expand Up @@ -345,13 +342,8 @@ await documentAccessor.delete(user);
await dispose.cancel();
```

Listen snapshot of collection documents.

Need to import cloud_firestore.
Listen snapshot of collection documents. And can be used also CollectionPagingListener.

```
import 'package:cloud_firestore/cloud_firestore.dart';
```

```dart
// Listen
Expand Down
23 changes: 7 additions & 16 deletions README_j.md
Original file line number Diff line number Diff line change
Expand Up @@ -275,14 +275,12 @@ CollectionPagingとSmartRefresherを使ったサンプルコードです。
CollectionPagingListenerを使うことで、ドキュメントのリアルタイム操作(作成、更新、削除)による取得とページングによる取得ができます。

```dart
final ref = User().collectionRef;
final collectionPagingListener = CollectionPagingListener<User>(
query: ref.orderBy('updatedAt', descending: true),
collectionReference: ref,
query: User().collectionRef.orderBy('updatedAt', descending: true),
initialLimit: 20,
pagingLimit: 20,
decode: (snap, collectionRef) =>
User(snapshot: snap, collectionRef: collectionRef),
decode: (snap, _) =>
User(snapshot: snap, collectionRef: snap.reference.parent),
);
// Fetch to set listener.
Expand All @@ -299,10 +297,9 @@ collectionPagingListener.data.listen((event) {
// Get document changes status and cache status.
collectionPagingListener.docChanges.listen((event) {
print('docChanges ${event.length}');
for (var change in event) {
print(
'id: ${change.doc.id}, changeType: ${change.type}, oldIndex: ${change.oldIndex}, newIndex: ${change.newIndex} cache: ${change.doc.metadata.isFromCache}');
for (var item in event) {
final change = item.docChange;
print('id: ${item.doc.id}, changeType: ${change.type}, oldIndex: ${change.oldIndex}, newIndex: ${change.newIndex} cache: ${change.doc.metadata.isFromCache}');
}
});
Expand Down Expand Up @@ -364,13 +361,7 @@ await documentAccessor.delete(user);
await disposer.cancel();
```

コレクションのスナップショットも監視することができます。

DocumentChangeType を利用する場合は cloud_firestore をインポートしてください。

```
import 'package:cloud_firestore/cloud_firestore.dart';
```
コレクションのスナップショットも監視することができます。CollectionPagingListenerでもスナップショットを監視することができます。

```dart
// Listen
Expand Down
6 changes: 5 additions & 1 deletion flamingo/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# 1.5.0
# 1.5.1
Changed docChanges data type in CollectionPagingListener.
Updated plugins.

## 1.5.0
Added CollectionPagingListener.

## 1.4.1+2
Expand Down
23 changes: 7 additions & 16 deletions flamingo/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,14 +258,12 @@ final collectionPaging = CollectionPaging<User>(
Can be used listener and paging features of documents by CollectionPagingListener.<br>

```dart
final ref = User().collectionRef;
final collectionPagingListener = CollectionPagingListener<User>(
query: ref.orderBy('updatedAt', descending: true),
collectionReference: ref,
query: User().collectionRef.orderBy('updatedAt', descending: true),
initialLimit: 20,
pagingLimit: 20,
decode: (snap, collectionRef) =>
User(snapshot: snap, collectionRef: collectionRef),
decode: (snap, _) =>
User(snapshot: snap, collectionRef: snap.reference.parent),
);
// Fetch to set listener.
Expand All @@ -282,10 +280,9 @@ collectionPagingListener.data.listen((event) {
// Get document changes status and cache status.
collectionPagingListener.docChanges.listen((event) {
print('docChanges ${event.length}');
for (var change in event) {
print(
'id: ${change.doc.id}, changeType: ${change.type}, oldIndex: ${change.oldIndex}, newIndex: ${change.newIndex} cache: ${change.doc.metadata.isFromCache}');
for (var item in event) {
final change = item.docChange;
print('id: ${item.doc.id}, changeType: ${change.type}, oldIndex: ${change.oldIndex}, newIndex: ${change.newIndex} cache: ${change.doc.metadata.isFromCache}');
}
});
Expand Down Expand Up @@ -345,13 +342,7 @@ await documentAccessor.delete(user);
await dispose.cancel();
```

Listen snapshot of collection documents.

Need to import cloud_firestore.

```
import 'package:cloud_firestore/cloud_firestore.dart';
```
Listen snapshot of collection documents. And can be used also CollectionPagingListener.

```dart
// Listen
Expand Down
4 changes: 2 additions & 2 deletions flamingo/example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ PODS:
- BoringSSL-GRPC/Implementation (0.0.7):
- BoringSSL-GRPC/Interface (= 0.0.7)
- BoringSSL-GRPC/Interface (0.0.7)
- cloud_firestore (0.14.0):
- cloud_firestore (0.14.0-2):
- Firebase/CoreOnly (~> 6.26.0)
- Firebase/Firestore (~> 6.26.0)
- firebase_core
Expand Down Expand Up @@ -352,7 +352,7 @@ EXTERNAL SOURCES:
SPEC CHECKSUMS:
abseil: 6c8eb7892aefa08d929b39f9bb108e5367e3228f
BoringSSL-GRPC: 8edf627ee524575e2f8d19d56f068b448eea3879
cloud_firestore: b6c95cc79e148eaddf455e8e07f32ef38a50271d
cloud_firestore: b054ccc068b5907645c32b669d38e82db2a11fc7
Firebase: 7cf5f9c67f03cb3b606d1d6535286e1080e57eb6
firebase_core: 3134fe79d257d430f163b558caf52a10a87efe8a
firebase_storage: 760ea52570d1896e3b08e2815f1123e61f25e685
Expand Down
14 changes: 6 additions & 8 deletions flamingo/example/lib/collection_paging_listener_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,12 @@ class _State extends State<CollectionPagingListenerPage> {
void initState() {
super.initState();

final ref = User().collectionRef;
collectionPagingListener = CollectionPagingListener<User>(
query: ref.orderBy('updatedAt', descending: true),
collectionReference: ref,
query: User().collectionRef.orderBy('updatedAt', descending: true),
initialLimit: 20,
pagingLimit: 20,
decode: (snap, collectionRef) =>
User(snapshot: snap, collectionRef: collectionRef),
decode: (snap, _) =>
User(snapshot: snap, collectionRef: snap.reference.parent),
)
..fetch()
..data.listen((event) {
Expand All @@ -45,10 +43,10 @@ class _State extends State<CollectionPagingListenerPage> {
});

collectionPagingListener.docChanges.listen((event) {
print('docChanges ${event.length}');
for (var change in event) {
for (var item in event) {
final change = item.docChange;
print(
'id: ${change.doc.id}, changeType: ${change.type}, oldIndex: ${change.oldIndex}, newIndex: ${change.newIndex} cache: ${change.doc.metadata.isFromCache}');
'id: ${item.doc.id}, changeType: ${change.type}, oldIndex: ${change.oldIndex}, newIndex: ${change.newIndex} cache: ${change.doc.metadata.isFromCache}');
}
});
}
Expand Down
12 changes: 1 addition & 11 deletions flamingo/lib/src/archive/collection_paging_listener_2.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,6 @@ import 'package:rxdart/rxdart.dart';
// TODO(shohei): リアルタイムアップデートのみのページング
/// やりたいことができなかった
class _PagingData<T extends Document<T>> {
_PagingData({
this.docs,
this.startAfterDocument,
});
final List<T> docs;
final DocumentSnapshot startAfterDocument;
}

/// CollectionPagingListener is SnapshotListener + Paging features.
class CollectionPagingListener<T extends Document<T>> {
CollectionPagingListener({
Expand Down Expand Up @@ -139,8 +130,7 @@ class _PagingListener<T extends Document<T>> {
_query = _query.startAfterDocument(startAfterDocument);
}
final qs = await _query.snapshots().first;
// return _query.endAtDocument(qs.docs.last).snapshots();
return _query.snapshots();
return _query.endAtDocument(qs.docs.last).snapshots();
}

Future<void> dispose() async {
Expand Down
33 changes: 22 additions & 11 deletions flamingo/lib/src/collection_paging_listener.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ import 'package:flamingo/flamingo.dart';
import 'package:flutter/foundation.dart';
import 'package:rxdart/rxdart.dart';

class DocumentChangeData<T extends Document<T>> {
DocumentChangeData({
@required this.doc,
@required this.docChange,
});
final T doc;
final DocumentChange docChange;
}

/// CollectionPagingListener is SnapshotListener + Paging features.
class CollectionPagingListener<T extends Document<T>> {
CollectionPagingListener({
Expand All @@ -25,15 +34,16 @@ class CollectionPagingListener<T extends Document<T>> {
final T Function(DocumentSnapshot, CollectionReference) decode;

ValueStream<List<T>> get data => _dataController.stream;
Stream<List<DocumentChange>> get docChanges => _docChangesController.stream;
Stream<List<DocumentChangeData<T>>> get docChanges =>
_docChangesController.stream;
int get count => _dataController.value.length;
bool get hasMore => _hasMore;

final _PagingListener<T> _pagingListenerController;
final BehaviorSubject<List<T>> _dataController =
BehaviorSubject<List<T>>.seeded([]);
final PublishSubject<List<DocumentChange>> _docChangesController =
PublishSubject<List<DocumentChange>>();
final PublishSubject<List<DocumentChangeData<T>>> _docChangesController =
PublishSubject<List<DocumentChangeData<T>>>();

bool _hasMore = true;
bool _initLoaded = false;
Expand Down Expand Up @@ -90,13 +100,14 @@ class _PagingListener<T extends Document<T>> {
final CollectionReference collectionReference;

ValueStream<List<T>> get data => _dataController.stream;
Stream<List<DocumentChange>> get docChanges => _docChangesController.stream;
Stream<List<DocumentChangeData<T>>> get docChanges =>
_docChangesController.stream;
Sink<int> get onLoad => _loadController.sink;

final BehaviorSubject<List<T>> _dataController =
BehaviorSubject<List<T>>.seeded([]);
final PublishSubject<List<DocumentChange>> _docChangesController =
PublishSubject<List<DocumentChange>>();
final PublishSubject<List<DocumentChangeData<T>>> _docChangesController =
PublishSubject<List<DocumentChangeData<T>>>();
final PublishSubject<int> _loadController = PublishSubject<int>();

StreamSubscription<QuerySnapshot> _disposer;
Expand All @@ -116,13 +127,12 @@ class _PagingListener<T extends Document<T>> {
}
_disposer = query.limit(limit).snapshots().listen((event) {
final docs = _dataController.value;
final changes = <DocumentChangeData<T>>[];
for (var change in event.docChanges) {
// print(
// 'id: ${change.doc.id}, changeType: ${change.type}, oldIndex: ${change.oldIndex}, newIndex: ${change.newIndex} cache: ${change.doc.metadata.isFromCache}');
final doc = decode(change.doc, collectionReference);
if (change.type == DocumentChangeType.added) {
docs.insert(change.newIndex, decode(change.doc, collectionReference));
docs.insert(change.newIndex, doc);
} else if (change.type == DocumentChangeType.modified) {
final doc = decode(change.doc, collectionReference);
if (change.oldIndex == change.newIndex) {
docs[change.newIndex] = doc;
} else {
Expand All @@ -133,9 +143,10 @@ class _PagingListener<T extends Document<T>> {
} else if (change.type == DocumentChangeType.removed) {
docs.removeAt(change.oldIndex);
}
changes.add(DocumentChangeData<T>(doc: doc, docChange: change));
}
_docChangesController.add(event.docChanges);
_dataController.add(docs);
_docChangesController.add(changes);
});
}
}
4 changes: 2 additions & 2 deletions flamingo/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: flamingo
description: This is Firebase Firestore Model Framework Library. Can be manage document reference and collection reference. And provide Firestore and Cloud Storage interface.
version: 1.5.0
version: 1.5.1
repository: https://github.com/hukusuke1007/flamingo
homepage: https://github.com/hukusuke1007/flamingo

Expand All @@ -11,7 +11,7 @@ dependencies:
flutter:
sdk: flutter
firebase_core: ^0.5.0
cloud_firestore: ^0.14.0
cloud_firestore: ^0.14.0+2
firebase_storage: ^4.0.0
rxdart: ^0.24.1

Expand Down

0 comments on commit 1dece88

Please sign in to comment.