From 229182f1b3dc865037c49a4cf589b6ce6d649b7d Mon Sep 17 00:00:00 2001 From: Brandon Trautmann Date: Mon, 28 May 2018 12:54:37 -0400 Subject: [PATCH] Allow for merging a document on set, update version. --- publishing-config.gradle | 4 ++-- .../oakwoodsc/rxfirestore/RxFirestoreDb.java | 20 ++++++++++++++++++- .../oakwoodsc/rxfirestore/SetOnSubscribe.java | 13 ++++++++++-- .../oakwoodsc/rxfirestorekt/RxFirestoreDb.kt | 3 +++ 4 files changed, 35 insertions(+), 5 deletions(-) diff --git a/publishing-config.gradle b/publishing-config.gradle index 37bcce6..cba835f 100644 --- a/publishing-config.gradle +++ b/publishing-config.gradle @@ -1,7 +1,7 @@ ext { // Publishing - libraryVersion = '1.0.4' - libraryVersionName = '1.0.4' + libraryVersion = '1.1.0' + libraryVersionName = '1.1.0' bintrayRepo = 'maven' publishedGroupId = 'com.oakwoodsc.rxfirestore' libraryDescription = 'An RxJava2 wrapper for Cloud Firestore' diff --git a/rxfirestore/src/main/java/com/oakwoodsc/rxfirestore/RxFirestoreDb.java b/rxfirestore/src/main/java/com/oakwoodsc/rxfirestore/RxFirestoreDb.java index e72f23b..3caac80 100644 --- a/rxfirestore/src/main/java/com/oakwoodsc/rxfirestore/RxFirestoreDb.java +++ b/rxfirestore/src/main/java/com/oakwoodsc/rxfirestore/RxFirestoreDb.java @@ -120,7 +120,25 @@ public static Observable> queryObject(@NonNull Docume @NonNull @CheckResult public static Completable set(@NonNull DocumentReference reference, @NonNull T value) { - return Completable.create(new SetOnSubscribe<>(reference, value)); + return Completable.create(new SetOnSubscribe<>(reference, value, false)); + } + + /** + * Creates or merges a document at the given {@link DocumentReference} + *

+ * 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 DocumentReference} to create or set the document at + * @param value the object to place at the {@link DocumentReference} + * @param the type of the model + * @return {@link Completable} + */ + @NonNull + @CheckResult + public static Completable setAndMerge(@NonNull DocumentReference reference, @NonNull T value) { + return Completable.create(new SetOnSubscribe<>(reference, value, true)); } /** diff --git a/rxfirestore/src/main/java/com/oakwoodsc/rxfirestore/SetOnSubscribe.java b/rxfirestore/src/main/java/com/oakwoodsc/rxfirestore/SetOnSubscribe.java index f6a1a81..4e8d7d2 100644 --- a/rxfirestore/src/main/java/com/oakwoodsc/rxfirestore/SetOnSubscribe.java +++ b/rxfirestore/src/main/java/com/oakwoodsc/rxfirestore/SetOnSubscribe.java @@ -5,6 +5,7 @@ import com.google.android.gms.tasks.OnCompleteListener; import com.google.android.gms.tasks.Task; import com.google.firebase.firestore.DocumentReference; +import com.google.firebase.firestore.SetOptions; import io.reactivex.CompletableEmitter; import io.reactivex.CompletableOnSubscribe; @@ -19,9 +20,12 @@ public class SetOnSubscribe implements CompletableOnSubscribe { private final T value; - public SetOnSubscribe(DocumentReference reference, T value) { + private final boolean merge; + + public SetOnSubscribe(DocumentReference reference, T value, boolean merge) { this.reference = reference; this.value = value; + this.merge = merge; } @Override @@ -41,7 +45,12 @@ public void onComplete(@NonNull Task task) { } }; - reference.set(value).addOnCompleteListener(listener); + if (merge) { + reference.set(value, SetOptions.merge()).addOnCompleteListener(listener); + } else { + reference.set(value).addOnCompleteListener(listener); + } + } } diff --git a/rxfirestorekt/src/main/java/com/oakwoodsc/rxfirestorekt/RxFirestoreDb.kt b/rxfirestorekt/src/main/java/com/oakwoodsc/rxfirestorekt/RxFirestoreDb.kt index 601f831..2c70fbf 100644 --- a/rxfirestorekt/src/main/java/com/oakwoodsc/rxfirestorekt/RxFirestoreDb.kt +++ b/rxfirestorekt/src/main/java/com/oakwoodsc/rxfirestorekt/RxFirestoreDb.kt @@ -27,6 +27,9 @@ fun DocumentReference.queryObject(objectClass: Class): Observable DocumentReference.setDoc(value: T): Completable = RxFirestoreDb.set(this, value) +fun DocumentReference.setAndMergeDoc(value: T): Completable = + RxFirestoreDb.setAndMerge(this, value) + fun DocumentReference.updateDoc(updates: Map): Completable = RxFirestoreDb.update(this, updates)