Skip to content

Commit 26a2cca

Browse files
fragarsiefragarsie
fragarsie
authored and
fragarsie
committed
Update methods and javadoc for Firestore methods
1 parent 3a40dfa commit 26a2cca

File tree

1 file changed

+97
-33
lines changed

1 file changed

+97
-33
lines changed

app/src/main/java/durdinapps/rxfirebase2/RxFirestore.java

Lines changed: 97 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import com.google.android.gms.tasks.OnCompleteListener;
88
import com.google.android.gms.tasks.OnFailureListener;
9+
import com.google.android.gms.tasks.OnSuccessListener;
910
import com.google.android.gms.tasks.Task;
1011
import com.google.firebase.firestore.CollectionReference;
1112
import com.google.firebase.firestore.DocumentListenOptions;
@@ -16,6 +17,7 @@
1617
import com.google.firebase.firestore.FirebaseFirestore;
1718
import com.google.firebase.firestore.FirebaseFirestoreException;
1819
import com.google.firebase.firestore.ListenerRegistration;
20+
import com.google.firebase.firestore.Query;
1921
import com.google.firebase.firestore.QuerySnapshot;
2022
import com.google.firebase.firestore.SetOptions;
2123
import com.google.firebase.firestore.Transaction;
@@ -283,17 +285,20 @@ public static Maybe<DocumentSnapshot> getDocument(@NonNull final DocumentReferen
283285
return Maybe.create(new MaybeOnSubscribe<DocumentSnapshot>() {
284286
@Override
285287
public void subscribe(final MaybeEmitter<DocumentSnapshot> emitter) throws Exception {
286-
ref.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
288+
ref.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
287289
@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 {
294294
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);
297302
}
298303
});
299304
}
@@ -310,18 +315,51 @@ public static Maybe<QuerySnapshot> getCollection(@NonNull final CollectionRefere
310315
return Maybe.create(new MaybeOnSubscribe<QuerySnapshot>() {
311316
@Override
312317
public void subscribe(final MaybeEmitter<QuerySnapshot> emitter) throws Exception {
313-
ref.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
318+
ref.get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
314319
@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()) {
321322
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+
324338

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);
325363
}
326364
});
327365
}
@@ -349,10 +387,7 @@ public void onEvent(DocumentSnapshot documentSnapshot, FirebaseFirestoreExceptio
349387
emitter.onError(e);
350388
return;
351389
}
352-
353-
if (documentSnapshot != null && documentSnapshot.exists()) {
354-
emitter.onNext(documentSnapshot);
355-
}
390+
emitter.onNext(documentSnapshot);
356391
}
357392
});
358393
emitter.setCancellable(new Cancellable() {
@@ -388,10 +423,7 @@ public void onEvent(DocumentSnapshot documentSnapshot, FirebaseFirestoreExceptio
388423
emitter.onError(e);
389424
return;
390425
}
391-
392-
if (documentSnapshot != null && documentSnapshot.exists()) {
393-
emitter.onNext(documentSnapshot);
394-
}
426+
emitter.onNext(documentSnapshot);
395427
}
396428
});
397429
emitter.setCancellable(new Cancellable() {
@@ -428,10 +460,7 @@ public void onEvent(DocumentSnapshot documentSnapshot, FirebaseFirestoreExceptio
428460
emitter.onError(e);
429461
return;
430462
}
431-
432-
if (documentSnapshot != null && documentSnapshot.exists()) {
433-
emitter.onNext(documentSnapshot);
434-
}
463+
emitter.onNext(documentSnapshot);
435464
}
436465
});
437466
emitter.setCancellable(new Cancellable() {
@@ -658,9 +687,9 @@ public static <T> Flowable<T> observeDocumentRef(@NonNull final DocumentReferenc
658687
}
659688

660689
/**
661-
* Starts listening to the document referenced by this DocumentReference.
690+
* Reads the collection referenced by this CollectionReference.
662691
*
663-
* @param ref The given Document reference.
692+
* @param ref The given Collection reference.
664693
* @param clazz class type for the {@link DocumentSnapshot} items.
665694
*/
666695
@NonNull
@@ -669,13 +698,48 @@ public static <T> Maybe<List<T>> getCollection(@NonNull final CollectionReferenc
669698
return getCollection(ref, DocumentSnapshotMapper.listOf(clazz));
670699
}
671700

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
672708
private static <T> Maybe<List<T>> getCollection(CollectionReference ref,
673-
DocumentSnapshotMapper<QuerySnapshot, List<T>> mapper) {
709+
DocumentSnapshotMapper<QuerySnapshot,
710+
List<T>> mapper) {
674711
return getCollection(ref)
675712
.filter(QUERY_EXISTENCE_PREDICATE)
676713
.map(mapper);
677714
}
678715

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+
679743
/**
680744
* Reads the document referenced by this DocumentReference.
681745
*

0 commit comments

Comments
 (0)