Search in sources :

Example 1 with AsyncEventListener

use of com.google.firebase.firestore.core.AsyncEventListener 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));
}
Also used : ViewSnapshot(com.google.firebase.firestore.core.ViewSnapshot) AsyncEventListener(com.google.firebase.firestore.core.AsyncEventListener) NonNull(androidx.annotation.NonNull) Task(com.google.android.gms.tasks.Task) Collections.singletonList(java.util.Collections.singletonList) Code(com.google.firebase.firestore.FirebaseFirestoreException.Code) Map(java.util.Map) ParsedSetData(com.google.firebase.firestore.core.UserData.ParsedSetData) Util.voidErrorTransformer(com.google.firebase.firestore.util.Util.voidErrorTransformer) Assert(com.google.firebase.firestore.util.Assert) ListenerRegistrationImpl(com.google.firebase.firestore.core.ListenerRegistrationImpl) ParsedUpdateData(com.google.firebase.firestore.core.UserData.ParsedUpdateData) Precondition(com.google.firebase.firestore.model.mutation.Precondition) ActivityScope(com.google.firebase.firestore.core.ActivityScope) ListenOptions(com.google.firebase.firestore.core.EventManager.ListenOptions) Util(com.google.firebase.firestore.util.Util) Preconditions.checkNotNull(com.google.firebase.firestore.util.Preconditions.checkNotNull) Document(com.google.firebase.firestore.model.Document) Executor(java.util.concurrent.Executor) Executors(com.google.firebase.firestore.util.Executors) ResourcePath(com.google.firebase.firestore.model.ResourcePath) QueryListener(com.google.firebase.firestore.core.QueryListener) DeleteMutation(com.google.firebase.firestore.model.mutation.DeleteMutation) ExecutionException(java.util.concurrent.ExecutionException) Nullable(androidx.annotation.Nullable) Tasks(com.google.android.gms.tasks.Tasks) TaskCompletionSource(com.google.android.gms.tasks.TaskCompletionSource) Assert.fail(com.google.firebase.firestore.util.Assert.fail) Activity(android.app.Activity) Collections(java.util.Collections) DocumentKey(com.google.firebase.firestore.model.DocumentKey) Document(com.google.firebase.firestore.model.Document) ViewSnapshot(com.google.firebase.firestore.core.ViewSnapshot) ListenerRegistrationImpl(com.google.firebase.firestore.core.ListenerRegistrationImpl) QueryListener(com.google.firebase.firestore.core.QueryListener) AsyncEventListener(com.google.firebase.firestore.core.AsyncEventListener)

Example 2 with AsyncEventListener

use of com.google.firebase.firestore.core.AsyncEventListener 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));
}
Also used : ViewSnapshot(com.google.firebase.firestore.core.ViewSnapshot) ListenerRegistrationImpl(com.google.firebase.firestore.core.ListenerRegistrationImpl) QueryListener(com.google.firebase.firestore.core.QueryListener) AsyncEventListener(com.google.firebase.firestore.core.AsyncEventListener)

Aggregations

AsyncEventListener (com.google.firebase.firestore.core.AsyncEventListener)2 ListenerRegistrationImpl (com.google.firebase.firestore.core.ListenerRegistrationImpl)2 QueryListener (com.google.firebase.firestore.core.QueryListener)2 ViewSnapshot (com.google.firebase.firestore.core.ViewSnapshot)2 Activity (android.app.Activity)1 NonNull (androidx.annotation.NonNull)1 Nullable (androidx.annotation.Nullable)1 Task (com.google.android.gms.tasks.Task)1 TaskCompletionSource (com.google.android.gms.tasks.TaskCompletionSource)1 Tasks (com.google.android.gms.tasks.Tasks)1 Code (com.google.firebase.firestore.FirebaseFirestoreException.Code)1 ActivityScope (com.google.firebase.firestore.core.ActivityScope)1 ListenOptions (com.google.firebase.firestore.core.EventManager.ListenOptions)1 ParsedSetData (com.google.firebase.firestore.core.UserData.ParsedSetData)1 ParsedUpdateData (com.google.firebase.firestore.core.UserData.ParsedUpdateData)1 Document (com.google.firebase.firestore.model.Document)1 DocumentKey (com.google.firebase.firestore.model.DocumentKey)1 ResourcePath (com.google.firebase.firestore.model.ResourcePath)1 DeleteMutation (com.google.firebase.firestore.model.mutation.DeleteMutation)1 Precondition (com.google.firebase.firestore.model.mutation.Precondition)1