gildaswise / firestore_ui

A port of firebase_auth's FirebaseAnimatedList to Firestore, called FirestoreAnimatedList
BSD 3-Clause "New" or "Revised" License
35 stars 28 forks source link

Combine multiple Stream<QuerySnapshot> in list #10

Closed opringle closed 4 years ago

opringle commented 4 years ago

I have two separate firestore queries I want to combine and use in a single FirestoreAnimatedList. I am combining my streams using a streamgroup.

When a user's status changes from "active" to "inactive" I want the FirestoreAnimatedList to trigger onDocumentChanged. However, instead the querysnapshot shows that a document was removed from activeUsers and a document was added to inactiveUsers, resulting in the list calling onDocumentAdded & onDocumentRemoved in rapid succession.

Here is a simple example:

Stream<QuerySnapshot> activeUsers = firestore.collection("users").where("status", isEqualTo: "active").snapshots()

Stream<QuerySnapshot> inactiveUsers = firestore.collection("users").where("status", isEqualTo: "inactive").snapshots()

Stream<QuerySnapshot> query = StreamGroup.merge([activeUsers, inactiveUsers]);

FirestoreAnimatedList(
    query: query,
    itemBuilder: (
        BuildContext context,
        DocumentSnapshot snapshot,
        Animation<double> animation,
        int index,
    ) => FadeTransition(
            opacity: animation,
            child: MessageListTile(
            index: index,
            document: snapshot,
            onTap: _removeMessage,
        ),
    ),
);

Is there a way I can produce a combined Stream<QuerySnapshot> that returns DocumentChangeType.modified instead? What is the simplest way to achieve my desired functionality?

gildaswise commented 4 years ago

The simplest way would be changing to Observable.combineLatest2 and inside the combiner function you would have to build your own QuerySnapshot that handles changing to DocumentChangeType.modified, but that's actually hard to catch since it'll be two DocumentChange and I don't know how often they'll come together, or if they'll be separate QuerySnapshot instances; my suggestion here would be changing your UI to having a TabBar where the user can select either Active or Inactive and each of those would have their own FirestoreAnimatedList without having to worry on merging.

gildaswise commented 4 years ago

Actually I'm closing this since #16 made it so you can't use Stream<QuerySnapshot> directly anymore, so this isn't doable.