use of com.google.firebase.firestore.core.QueryListener in project firebase-android-sdk by firebase.
the class SpecTestCase method doListen.
//
// Methods for doing the steps of the spec test.
//
private void doListen(JSONObject listenSpec) throws Exception {
int expectedId = listenSpec.getInt("targetId");
Query query = parseQuery(listenSpec.getJSONObject("query"));
// TODO: Allow customizing listen options in spec tests
ListenOptions options = new ListenOptions();
options.includeDocumentMetadataChanges = true;
options.includeQueryMetadataChanges = true;
QueryListener listener = new QueryListener(query, options, (value, error) -> {
QueryEvent event = new QueryEvent();
event.query = query;
if (value != null) {
event.view = value;
} else {
event.error = error;
}
events.add(event);
});
queryListeners.put(query, listener);
queue.runSync(() -> {
int actualId = eventManager.addQueryListener(listener);
assertEquals(expectedId, actualId);
});
}
use of com.google.firebase.firestore.core.QueryListener in project firebase-android-sdk by firebase.
the class DocumentReference method addSnapshotListenerInternal.
/**
* Internal helper method to create add a snapshot listener.
*
* <p>Will be Activity scoped if the activity parameter is non-{@code null}.
*
* @param userExecutor The executor to use to call the listener.
* @param options The options to use for this listen.
* @param activity Optional activity this listener is scoped to.
* @param userListener The user-supplied event listener that will be called with document
* snapshots.
* @return A registration object that can be used to remove the listener.
*/
private ListenerRegistration addSnapshotListenerInternal(Executor userExecutor, ListenOptions options, @Nullable Activity activity, EventListener<DocumentSnapshot> userListener) {
// Convert from ViewSnapshots to DocumentSnapshots.
EventListener<ViewSnapshot> viewListener = (snapshot, error) -> {
if (error != null) {
userListener.onEvent(null, error);
return;
}
Assert.hardAssert(snapshot != null, "Got event without value or error set");
Assert.hardAssert(snapshot.getDocuments().size() <= 1, "Too many documents returned on a document query");
Document document = snapshot.getDocuments().getDocument(key);
DocumentSnapshot documentSnapshot;
if (document != null) {
boolean hasPendingWrites = snapshot.getMutatedKeys().contains(document.getKey());
documentSnapshot = DocumentSnapshot.fromDocument(firestore, document, snapshot.isFromCache(), hasPendingWrites);
} else {
// We don't raise `hasPendingWrites` for deleted documents.
documentSnapshot = DocumentSnapshot.fromNoDocument(firestore, key, snapshot.isFromCache());
}
userListener.onEvent(documentSnapshot, null);
};
// Call the viewListener on the userExecutor.
AsyncEventListener<ViewSnapshot> asyncListener = new AsyncEventListener<>(userExecutor, viewListener);
com.google.firebase.firestore.core.Query query = asQuery();
QueryListener queryListener = firestore.getClient().listen(query, options, asyncListener);
return ActivityScope.bind(activity, new ListenerRegistrationImpl(firestore.getClient(), queryListener, asyncListener));
}
use of com.google.firebase.firestore.core.QueryListener in project firebase-android-sdk by firebase.
the class Query method addSnapshotListenerInternal.
/**
* Internal helper method to create add a snapshot listener.
*
* <p>Will be Activity scoped if the activity parameter is non-{@code null}.
*
* @param executor The executor to use to call the listener.
* @param options The options to use for this listen.
* @param activity Optional activity this listener is scoped to.
* @param userListener The user-supplied event listener that will be called with the snapshots.
* @return A registration object that can be used to remove the listener.
*/
private ListenerRegistration addSnapshotListenerInternal(Executor executor, ListenOptions options, @Nullable Activity activity, EventListener<QuerySnapshot> userListener) {
validateHasExplicitOrderByForLimitToLast();
// Convert from ViewSnapshots to QuerySnapshots.
EventListener<ViewSnapshot> viewListener = (@Nullable ViewSnapshot snapshot, @Nullable FirebaseFirestoreException error) -> {
if (error != null) {
userListener.onEvent(null, error);
return;
}
hardAssert(snapshot != null, "Got event without value or error set");
QuerySnapshot querySnapshot = new QuerySnapshot(this, snapshot, firestore);
userListener.onEvent(querySnapshot, null);
};
// Call the viewListener on the userExecutor.
AsyncEventListener<ViewSnapshot> asyncListener = new AsyncEventListener<>(executor, viewListener);
QueryListener queryListener = firestore.getClient().listen(query, options, asyncListener);
return ActivityScope.bind(activity, new ListenerRegistrationImpl(firestore.getClient(), queryListener, asyncListener));
}
use of com.google.firebase.firestore.core.QueryListener in project firebase-android-sdk by firebase.
the class SpecTestCase method doUnlisten.
private void doUnlisten(JSONArray unlistenSpec) throws Exception {
Query query = parseQuery(unlistenSpec.get(1));
QueryListener listener = queryListeners.remove(query);
queue.runSync(() -> eventManager.removeQueryListener(listener));
}
Aggregations