Skip to content

Commit

Permalink
Merge pull request #15 from ofalvai/observable-add
Browse files Browse the repository at this point in the history
Adding documents with Single<DocumentReference> besides Completable
  • Loading branch information
Brandon Trautmann authored Nov 26, 2018
2 parents 45378e5 + d93e239 commit 0c47082
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 10 deletions.
4 changes: 2 additions & 2 deletions publishing-config.gradle
Original file line number Diff line number Diff line change
@@ -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',
Expand Down
Original file line number Diff line number Diff line change
@@ -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<T> implements CompletableOnSubscribe {
public class AddOnSubscribe<T> implements CompletableOnSubscribe, SingleOnSubscribe<DocumentReference> {

private final CollectionReference reference;
private final T value;
Expand Down Expand Up @@ -44,4 +44,23 @@ public void onComplete(@NonNull Task<DocumentReference> task) {

reference.add(value).addOnCompleteListener(listener);
}

@Override
public void subscribe(final SingleEmitter<DocumentReference> emitter) throws Exception {
final OnCompleteListener<DocumentReference> listener =
new OnCompleteListener<DocumentReference>() {
@Override
public void onComplete(@NonNull Task<DocumentReference> task) {
if (!emitter.isDisposed()) {
if (!task.isSuccessful()) {
emitter.onError(task.getException());
} else {
emitter.onSuccess(task.getResult());
}
}
}
};

reference.add(value).addOnCompleteListener(listener);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

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;
Expand All @@ -12,14 +11,13 @@
import com.google.firebase.firestore.QuerySnapshot;
import com.google.firebase.firestore.Transaction;
import com.google.firebase.firestore.WriteBatch;

import java.util.Map;
import java.util.concurrent.Executor;

import io.reactivex.Completable;
import io.reactivex.Observable;
import io.reactivex.Single;

import java.util.Map;
import java.util.concurrent.Executor;

/**
* The main class used to interact with Cloud Firestore
*/
Expand Down Expand Up @@ -211,6 +209,23 @@ public static <T> 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.
* <p>
* Note, this is a <b>blocking</b> 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 <T> Single<DocumentReference> addAndProvideDocId(@NonNull CollectionReference reference, T value) {
return Single.create(new AddOnSubscribe<>(reference, value));
}

/**
* Runs a {@link Transaction} (series of reads and writes) for the given {@link FirebaseFirestore}
* instance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ fun DocumentReference.deleteDoc(): Completable =
fun <T> CollectionReference.addDoc(value: T): Completable =
RxFirestoreDb.add(this, value)

fun <T> CollectionReference.addDocAndProvideId(value: T): Single<DocumentReference> =
RxFirestoreDb.addAndProvideDocId(this, value)

fun <T> FirebaseFirestore.run(transaction: Transaction.Function<T>)
: Completable = RxFirestoreDb.runTransaction(this, transaction)

Expand Down

0 comments on commit 0c47082

Please sign in to comment.