From 4040ce868686970ae7a6aea96277a7dd0f0587e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliv=C3=A9r=20Falvai?= Date: Mon, 19 Nov 2018 15:58:39 +0100 Subject: [PATCH 1/4] Created addAsSingle() method which returns Single instead of Completable --- .../rxfirestore/AddOnSubscribeSingle.java | 39 +++++++++++++++++++ .../oakwoodsc/rxfirestore/RxFirestoreDb.java | 35 ++++++++++------- .../oakwoodsc/rxfirestorekt/RxFirestoreDb.kt | 3 ++ 3 files changed, 63 insertions(+), 14 deletions(-) create mode 100644 rxfirestore/src/main/java/com/oakwoodsc/rxfirestore/AddOnSubscribeSingle.java diff --git a/rxfirestore/src/main/java/com/oakwoodsc/rxfirestore/AddOnSubscribeSingle.java b/rxfirestore/src/main/java/com/oakwoodsc/rxfirestore/AddOnSubscribeSingle.java new file mode 100644 index 0000000..8ae630d --- /dev/null +++ b/rxfirestore/src/main/java/com/oakwoodsc/rxfirestore/AddOnSubscribeSingle.java @@ -0,0 +1,39 @@ +package com.oakwoodsc.rxfirestore; + +import android.support.annotation.NonNull; +import com.google.android.gms.tasks.OnCompleteListener; +import com.google.android.gms.tasks.Task; +import com.google.firebase.firestore.CollectionReference; +import com.google.firebase.firestore.DocumentReference; +import io.reactivex.SingleEmitter; +import io.reactivex.SingleOnSubscribe; + +public class AddOnSubscribeSingle implements SingleOnSubscribe { + + private final CollectionReference reference; + private final T value; + + public AddOnSubscribeSingle(CollectionReference reference, T value) { + this.reference = reference; + this.value = value; + } + + @Override + public void subscribe(final SingleEmitter emitter) throws Exception { + final OnCompleteListener listener = + new OnCompleteListener() { + @Override + public void onComplete(@NonNull Task task) { + if (!emitter.isDisposed()) { + if (!task.isSuccessful()) { + emitter.onError(task.getException()); + } else { + emitter.onSuccess(task.getResult()); + } + } + } + }; + + reference.add(value).addOnCompleteListener(listener); + } +} diff --git a/rxfirestore/src/main/java/com/oakwoodsc/rxfirestore/RxFirestoreDb.java b/rxfirestore/src/main/java/com/oakwoodsc/rxfirestore/RxFirestoreDb.java index 3caac80..18ca97e 100644 --- a/rxfirestore/src/main/java/com/oakwoodsc/rxfirestore/RxFirestoreDb.java +++ b/rxfirestore/src/main/java/com/oakwoodsc/rxfirestore/RxFirestoreDb.java @@ -2,25 +2,15 @@ import android.support.annotation.CheckResult; import android.support.annotation.NonNull; - -import com.google.firebase.firestore.CollectionReference; -import com.google.firebase.firestore.DocumentChange; -import com.google.firebase.firestore.DocumentReference; -import com.google.firebase.firestore.DocumentSnapshot; -import com.google.firebase.firestore.FirebaseFirestore; -import com.google.firebase.firestore.Query; -import com.google.firebase.firestore.QuerySnapshot; -import com.google.firebase.firestore.Transaction; -import com.google.firebase.firestore.WriteBatch; +import com.google.firebase.firestore.*; +import io.reactivex.Completable; +import io.reactivex.Observable; +import io.reactivex.Single; import java.util.HashMap; import java.util.Map; import java.util.concurrent.Executor; -import io.reactivex.Completable; -import io.reactivex.Observable; -import io.reactivex.Single; - /** * The main class used to interact with Cloud Firestore */ @@ -190,6 +180,23 @@ public static Completable add(@NonNull CollectionReference reference, T valu return Completable.create(new AddOnSubscribe<>(reference, value)); } + /** + * Adds a document at the given {@link CollectionReference}, then returns its {@link DocumentReference}. This is + * useful when the auto-generated ID of the new document is needed. + *

+ * Note, this is a blocking call because of how Firestore handles offline persistence. + * That means the onComplete() callback will not be called if the user is offline, so it is recommended + * not to block the UI until this completes + * + * @param reference the {@link CollectionReference} at which to add the document + * @return {@link Single} + */ + @NonNull + @CheckResult + public static Single addAsSingle(@NonNull CollectionReference reference, T value) { + return Single.create(new AddOnSubscribeSingle<>(reference, value)); + } + /** * Runs a {@link Transaction} (series of reads and writes) for the given {@link FirebaseFirestore} * instance diff --git a/rxfirestorekt/src/main/java/com/oakwoodsc/rxfirestorekt/RxFirestoreDb.kt b/rxfirestorekt/src/main/java/com/oakwoodsc/rxfirestorekt/RxFirestoreDb.kt index 2c70fbf..d2f958d 100644 --- a/rxfirestorekt/src/main/java/com/oakwoodsc/rxfirestorekt/RxFirestoreDb.kt +++ b/rxfirestorekt/src/main/java/com/oakwoodsc/rxfirestorekt/RxFirestoreDb.kt @@ -39,6 +39,9 @@ fun DocumentReference.deleteDoc(): Completable = fun CollectionReference.addDoc(value: T): Completable = RxFirestoreDb.add(this, value) +fun CollectionReference.addDocAsSingle(value: T): Single = + RxFirestoreDb.addAsSingle(this, value) + fun FirebaseFirestore.run(transaction: Transaction.Function) : Completable = RxFirestoreDb.runTransaction(this, transaction) From 06f3fe5484eec6dfe4d8dbb8eaa96b6e058d5341 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliv=C3=A9r=20Falvai?= Date: Sat, 24 Nov 2018 11:41:26 +0100 Subject: [PATCH 2/4] Removed AddOnSubscribeSingle, AddOnSubscribe now implements SingleOnSubscribe too --- .../oakwoodsc/rxfirestore/AddOnSubscribe.java | 25 ++++++++++-- .../rxfirestore/AddOnSubscribeSingle.java | 39 ------------------- .../oakwoodsc/rxfirestore/RxFirestoreDb.java | 12 +++++- 3 files changed, 32 insertions(+), 44 deletions(-) delete mode 100644 rxfirestore/src/main/java/com/oakwoodsc/rxfirestore/AddOnSubscribeSingle.java diff --git a/rxfirestore/src/main/java/com/oakwoodsc/rxfirestore/AddOnSubscribe.java b/rxfirestore/src/main/java/com/oakwoodsc/rxfirestore/AddOnSubscribe.java index 8d2e459..eb2cd89 100644 --- a/rxfirestore/src/main/java/com/oakwoodsc/rxfirestore/AddOnSubscribe.java +++ b/rxfirestore/src/main/java/com/oakwoodsc/rxfirestore/AddOnSubscribe.java @@ -1,20 +1,20 @@ package com.oakwoodsc.rxfirestore; import android.support.annotation.NonNull; - import com.google.android.gms.tasks.OnCompleteListener; import com.google.android.gms.tasks.Task; import com.google.firebase.firestore.CollectionReference; import com.google.firebase.firestore.DocumentReference; - import io.reactivex.CompletableEmitter; import io.reactivex.CompletableOnSubscribe; +import io.reactivex.SingleEmitter; +import io.reactivex.SingleOnSubscribe; /** * Created by Brandon Trautmann */ -public class AddOnSubscribe implements CompletableOnSubscribe { +public class AddOnSubscribe implements CompletableOnSubscribe, SingleOnSubscribe { private final CollectionReference reference; private final T value; @@ -44,4 +44,23 @@ public void onComplete(@NonNull Task task) { reference.add(value).addOnCompleteListener(listener); } + + @Override + public void subscribe(final SingleEmitter emitter) throws Exception { + final OnCompleteListener listener = + new OnCompleteListener() { + @Override + public void onComplete(@NonNull Task task) { + if (!emitter.isDisposed()) { + if (!task.isSuccessful()) { + emitter.onError(task.getException()); + } else { + emitter.onSuccess(task.getResult()); + } + } + } + }; + + reference.add(value).addOnCompleteListener(listener); + } } diff --git a/rxfirestore/src/main/java/com/oakwoodsc/rxfirestore/AddOnSubscribeSingle.java b/rxfirestore/src/main/java/com/oakwoodsc/rxfirestore/AddOnSubscribeSingle.java deleted file mode 100644 index 8ae630d..0000000 --- a/rxfirestore/src/main/java/com/oakwoodsc/rxfirestore/AddOnSubscribeSingle.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.oakwoodsc.rxfirestore; - -import android.support.annotation.NonNull; -import com.google.android.gms.tasks.OnCompleteListener; -import com.google.android.gms.tasks.Task; -import com.google.firebase.firestore.CollectionReference; -import com.google.firebase.firestore.DocumentReference; -import io.reactivex.SingleEmitter; -import io.reactivex.SingleOnSubscribe; - -public class AddOnSubscribeSingle implements SingleOnSubscribe { - - private final CollectionReference reference; - private final T value; - - public AddOnSubscribeSingle(CollectionReference reference, T value) { - this.reference = reference; - this.value = value; - } - - @Override - public void subscribe(final SingleEmitter emitter) throws Exception { - final OnCompleteListener listener = - new OnCompleteListener() { - @Override - public void onComplete(@NonNull Task task) { - if (!emitter.isDisposed()) { - if (!task.isSuccessful()) { - emitter.onError(task.getException()); - } else { - emitter.onSuccess(task.getResult()); - } - } - } - }; - - reference.add(value).addOnCompleteListener(listener); - } -} diff --git a/rxfirestore/src/main/java/com/oakwoodsc/rxfirestore/RxFirestoreDb.java b/rxfirestore/src/main/java/com/oakwoodsc/rxfirestore/RxFirestoreDb.java index 11bdc83..1f89777 100644 --- a/rxfirestore/src/main/java/com/oakwoodsc/rxfirestore/RxFirestoreDb.java +++ b/rxfirestore/src/main/java/com/oakwoodsc/rxfirestore/RxFirestoreDb.java @@ -2,7 +2,15 @@ import android.support.annotation.CheckResult; import android.support.annotation.NonNull; -import com.google.firebase.firestore.*; +import com.google.firebase.firestore.CollectionReference; +import com.google.firebase.firestore.DocumentChange; +import com.google.firebase.firestore.DocumentReference; +import com.google.firebase.firestore.DocumentSnapshot; +import com.google.firebase.firestore.FirebaseFirestore; +import com.google.firebase.firestore.Query; +import com.google.firebase.firestore.QuerySnapshot; +import com.google.firebase.firestore.Transaction; +import com.google.firebase.firestore.WriteBatch; import io.reactivex.Completable; import io.reactivex.Observable; import io.reactivex.Single; @@ -215,7 +223,7 @@ public static Completable add(@NonNull CollectionReference reference, T valu @NonNull @CheckResult public static Single addAsSingle(@NonNull CollectionReference reference, T value) { - return Single.create(new AddOnSubscribeSingle<>(reference, value)); + return Single.create(new AddOnSubscribe<>(reference, value)); } /** From 69efd5c462ed52ad6a6d363330e44c6a276fd5f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliv=C3=A9r=20Falvai?= Date: Sat, 24 Nov 2018 11:44:24 +0100 Subject: [PATCH 3/4] Renamed addAsSingle() to addAndProvideDocId() (Kotlin extensions too) --- .../main/java/com/oakwoodsc/rxfirestore/RxFirestoreDb.java | 2 +- .../main/java/com/oakwoodsc/rxfirestorekt/RxFirestoreDb.kt | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/rxfirestore/src/main/java/com/oakwoodsc/rxfirestore/RxFirestoreDb.java b/rxfirestore/src/main/java/com/oakwoodsc/rxfirestore/RxFirestoreDb.java index 1f89777..ab0ce1d 100644 --- a/rxfirestore/src/main/java/com/oakwoodsc/rxfirestore/RxFirestoreDb.java +++ b/rxfirestore/src/main/java/com/oakwoodsc/rxfirestore/RxFirestoreDb.java @@ -222,7 +222,7 @@ public static Completable add(@NonNull CollectionReference reference, T valu */ @NonNull @CheckResult - public static Single addAsSingle(@NonNull CollectionReference reference, T value) { + public static Single addAndProvideDocId(@NonNull CollectionReference reference, T value) { return Single.create(new AddOnSubscribe<>(reference, value)); } diff --git a/rxfirestorekt/src/main/java/com/oakwoodsc/rxfirestorekt/RxFirestoreDb.kt b/rxfirestorekt/src/main/java/com/oakwoodsc/rxfirestorekt/RxFirestoreDb.kt index d2f958d..29e0938 100644 --- a/rxfirestorekt/src/main/java/com/oakwoodsc/rxfirestorekt/RxFirestoreDb.kt +++ b/rxfirestorekt/src/main/java/com/oakwoodsc/rxfirestorekt/RxFirestoreDb.kt @@ -39,8 +39,8 @@ fun DocumentReference.deleteDoc(): Completable = fun CollectionReference.addDoc(value: T): Completable = RxFirestoreDb.add(this, value) -fun CollectionReference.addDocAsSingle(value: T): Single = - RxFirestoreDb.addAsSingle(this, value) +fun CollectionReference.addDocAndProvideId(value: T): Single = + RxFirestoreDb.addAndProvideDocId(this, value) fun FirebaseFirestore.run(transaction: Transaction.Function) : Completable = RxFirestoreDb.runTransaction(this, transaction) From d93e2390d1b3efbf86a3509b08a864fca0f44357 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliv=C3=A9r=20Falvai?= Date: Sat, 24 Nov 2018 11:49:23 +0100 Subject: [PATCH 4/4] Bumped version to 1.2.0 --- publishing-config.gradle | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/publishing-config.gradle b/publishing-config.gradle index dba0946..6f85079 100644 --- a/publishing-config.gradle +++ b/publishing-config.gradle @@ -1,7 +1,7 @@ ext.artifactInfo = [ // Publishing - libraryVersion : '1.1.1', - libraryVersionName : '1.1.1', + libraryVersion : '1.2.0', + libraryVersionName : '1.2.0', bintrayRepo : 'maven', publishedGroupId : 'com.oakwoodsc.rxfirestore', libraryDescription : 'An RxJava2 wrapper for Cloud Firestore',