6
6
7
7
import com .google .android .gms .tasks .OnCompleteListener ;
8
8
import com .google .android .gms .tasks .OnFailureListener ;
9
+ import com .google .android .gms .tasks .OnSuccessListener ;
9
10
import com .google .android .gms .tasks .Task ;
10
11
import com .google .firebase .firestore .CollectionReference ;
11
12
import com .google .firebase .firestore .DocumentListenOptions ;
16
17
import com .google .firebase .firestore .FirebaseFirestore ;
17
18
import com .google .firebase .firestore .FirebaseFirestoreException ;
18
19
import com .google .firebase .firestore .ListenerRegistration ;
20
+ import com .google .firebase .firestore .Query ;
19
21
import com .google .firebase .firestore .QuerySnapshot ;
20
22
import com .google .firebase .firestore .SetOptions ;
21
23
import com .google .firebase .firestore .Transaction ;
@@ -283,17 +285,20 @@ public static Maybe<DocumentSnapshot> getDocument(@NonNull final DocumentReferen
283
285
return Maybe .create (new MaybeOnSubscribe <DocumentSnapshot >() {
284
286
@ Override
285
287
public void subscribe (final MaybeEmitter <DocumentSnapshot > emitter ) throws Exception {
286
- ref .get ().addOnCompleteListener (new OnCompleteListener <DocumentSnapshot >() {
288
+ ref .get ().addOnSuccessListener (new OnSuccessListener <DocumentSnapshot >() {
287
289
@ Override
288
- public void onComplete (@ NonNull Task <DocumentSnapshot > task ) {
289
- if (task .isSuccessful ()) {
290
- DocumentSnapshot document = task .getResult ();
291
- if (document != null ) {
292
- emitter .onSuccess (task .getResult ());
293
- }
290
+ public void onSuccess (DocumentSnapshot documentSnapshot ) {
291
+ if (documentSnapshot .exists ()) {
292
+ emitter .onSuccess (documentSnapshot );
293
+ } else {
294
294
emitter .onComplete ();
295
- } else if (!emitter .isDisposed ())
296
- emitter .onError (task .getException ());
295
+ }
296
+ }
297
+ }).addOnFailureListener (new OnFailureListener () {
298
+ @ Override
299
+ public void onFailure (@ NonNull Exception e ) {
300
+ if (!emitter .isDisposed ())
301
+ emitter .onError (e );
297
302
}
298
303
});
299
304
}
@@ -310,18 +315,51 @@ public static Maybe<QuerySnapshot> getCollection(@NonNull final CollectionRefere
310
315
return Maybe .create (new MaybeOnSubscribe <QuerySnapshot >() {
311
316
@ Override
312
317
public void subscribe (final MaybeEmitter <QuerySnapshot > emitter ) throws Exception {
313
- ref .get ().addOnCompleteListener (new OnCompleteListener <QuerySnapshot >() {
318
+ ref .get ().addOnSuccessListener (new OnSuccessListener <QuerySnapshot >() {
314
319
@ Override
315
- public void onComplete (@ NonNull Task <QuerySnapshot > task ) {
316
- if (task .isSuccessful ()) {
317
- QuerySnapshot query = task .getResult ();
318
- if (query != null ) {
319
- emitter .onSuccess (task .getResult ());
320
- }
320
+ public void onSuccess (QuerySnapshot documentSnapshots ) {
321
+ if (documentSnapshots .isEmpty ()) {
321
322
emitter .onComplete ();
322
- } else if (!emitter .isDisposed ())
323
- emitter .onError (task .getException ());
323
+ } else {
324
+ emitter .onSuccess (documentSnapshots );
325
+ }
326
+ }
327
+ }).addOnFailureListener (new OnFailureListener () {
328
+ @ Override
329
+ public void onFailure (@ NonNull Exception e ) {
330
+ if (!emitter .isDisposed ())
331
+ emitter .onError (e );
332
+ }
333
+ });
334
+ }
335
+ });
336
+ }
337
+
324
338
339
+ /**
340
+ * Reads the collection referenced by this DocumentReference
341
+ *
342
+ * @param query The given Collection query.
343
+ */
344
+ @ NonNull
345
+ public static Maybe <QuerySnapshot > getCollection (@ NonNull final Query query ) {
346
+ return Maybe .create (new MaybeOnSubscribe <QuerySnapshot >() {
347
+ @ Override
348
+ public void subscribe (final MaybeEmitter <QuerySnapshot > emitter ) throws Exception {
349
+ query .get ().addOnSuccessListener (new OnSuccessListener <QuerySnapshot >() {
350
+ @ Override
351
+ public void onSuccess (QuerySnapshot documentSnapshots ) {
352
+ if (documentSnapshots .isEmpty ()) {
353
+ emitter .onComplete ();
354
+ } else {
355
+ emitter .onSuccess (documentSnapshots );
356
+ }
357
+ }
358
+ }).addOnFailureListener (new OnFailureListener () {
359
+ @ Override
360
+ public void onFailure (@ NonNull Exception e ) {
361
+ if (!emitter .isDisposed ())
362
+ emitter .onError (e );
325
363
}
326
364
});
327
365
}
@@ -349,10 +387,7 @@ public void onEvent(DocumentSnapshot documentSnapshot, FirebaseFirestoreExceptio
349
387
emitter .onError (e );
350
388
return ;
351
389
}
352
-
353
- if (documentSnapshot != null && documentSnapshot .exists ()) {
354
- emitter .onNext (documentSnapshot );
355
- }
390
+ emitter .onNext (documentSnapshot );
356
391
}
357
392
});
358
393
emitter .setCancellable (new Cancellable () {
@@ -388,10 +423,7 @@ public void onEvent(DocumentSnapshot documentSnapshot, FirebaseFirestoreExceptio
388
423
emitter .onError (e );
389
424
return ;
390
425
}
391
-
392
- if (documentSnapshot != null && documentSnapshot .exists ()) {
393
- emitter .onNext (documentSnapshot );
394
- }
426
+ emitter .onNext (documentSnapshot );
395
427
}
396
428
});
397
429
emitter .setCancellable (new Cancellable () {
@@ -428,10 +460,7 @@ public void onEvent(DocumentSnapshot documentSnapshot, FirebaseFirestoreExceptio
428
460
emitter .onError (e );
429
461
return ;
430
462
}
431
-
432
- if (documentSnapshot != null && documentSnapshot .exists ()) {
433
- emitter .onNext (documentSnapshot );
434
- }
463
+ emitter .onNext (documentSnapshot );
435
464
}
436
465
});
437
466
emitter .setCancellable (new Cancellable () {
@@ -658,9 +687,9 @@ public static <T> Flowable<T> observeDocumentRef(@NonNull final DocumentReferenc
658
687
}
659
688
660
689
/**
661
- * Starts listening to the document referenced by this DocumentReference .
690
+ * Reads the collection referenced by this CollectionReference .
662
691
*
663
- * @param ref The given Document reference.
692
+ * @param ref The given Collection reference.
664
693
* @param clazz class type for the {@link DocumentSnapshot} items.
665
694
*/
666
695
@ NonNull
@@ -669,13 +698,48 @@ public static <T> Maybe<List<T>> getCollection(@NonNull final CollectionReferenc
669
698
return getCollection (ref , DocumentSnapshotMapper .listOf (clazz ));
670
699
}
671
700
701
+ /**
702
+ * SReads the collection referenced by this CollectionReference.
703
+ *
704
+ * @param ref The given Collection reference.
705
+ * @param mapper specific function to map the dispatched events.
706
+ */
707
+ @ NonNull
672
708
private static <T > Maybe <List <T >> getCollection (CollectionReference ref ,
673
- DocumentSnapshotMapper <QuerySnapshot , List <T >> mapper ) {
709
+ DocumentSnapshotMapper <QuerySnapshot ,
710
+ List <T >> mapper ) {
674
711
return getCollection (ref )
675
712
.filter (QUERY_EXISTENCE_PREDICATE )
676
713
.map (mapper );
677
714
}
678
715
716
+ /**
717
+ * Reads the collection referenced by this Query.
718
+ *
719
+ * @param query The given Collection query.
720
+ * @param clazz class type for the {@link DocumentSnapshot} items.
721
+ */
722
+ @ NonNull
723
+ public static <T > Maybe <List <T >> getCollection (@ NonNull final Query query ,
724
+ @ NonNull final Class <T > clazz ) {
725
+ return getCollection (query , DocumentSnapshotMapper .listOf (clazz ));
726
+ }
727
+
728
+ /**
729
+ * Reads the collection referenced by this Query.
730
+ *
731
+ * @param query The given Collection query.
732
+ * @param mapper specific function to map the dispatched events.
733
+ */
734
+ @ NonNull
735
+ private static <T > Maybe <List <T >> getCollection (@ NonNull Query query ,
736
+ @ NonNull DocumentSnapshotMapper <QuerySnapshot ,
737
+ List <T >> mapper ) {
738
+ return getCollection (query )
739
+ .filter (QUERY_EXISTENCE_PREDICATE )
740
+ .map (mapper );
741
+ }
742
+
679
743
/**
680
744
* Reads the document referenced by this DocumentReference.
681
745
*
0 commit comments