Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated versions of gradle & libraries. Added check for an operation on the main thread #91

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ This repository started as a personal usage of [Nick Moskalenko](https://github.

```groovy
dependencies {
compile 'com.github.FrangSierra:RxFirebase:1.5.6'
implementation "com.github.FrangSierra:RxFirebase:1.5.6"
}
```
```
Expand Down Expand Up @@ -192,6 +192,15 @@ public static Completable updateEmail(@NonNull final FirebaseUser firebaseUser,
`RxCompletableHandler` manages the CompletableEmitters in the same way that `RxHandler` manages the `Subscriber`.
You can check all the differences between RxJava and RxJava 2.0 in the next [Link](https://github.com/ReactiveX/RxJava/wiki/What's-different-in-2.0)

### Plugins

```java
public static boolean allowMainThreadQueries = false;
```
The flag, that controls can be extensions be called on the main thread or not.
By default it doesn't permit the main thread queries


## License

MIT License
Expand Down
44 changes: 19 additions & 25 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,50 +1,44 @@
buildscript {
ext {
rx_version = "2.1.10"
rx_version = "2.2.20"
rx_android_version = "2.1.1"
firebase_auth_version = '19.2.0'
firebase_database_version = '19.2.1'
firebase_storage_version = '19.1.1'
firebase_firestore_version = '21.4.1'
firebase_functions_version = '19.0.2'
firebase_remote_version = '19.1.2'
firebase_auth_version = "21.1.0"
firebase_database_version = "20.1.0"
firebase_storage_version = "20.1.0"
firebase_firestore_version = "24.4.1"
firebase_functions_version = "20.2.1"
firebase_remote_version = "21.2.0"
support_version = "28.0.3"
}
}

apply plugin: 'com.android.library'

android {

packagingOptions {
exclude 'META-INF/LICENSE'
exclude 'META-INF/LICENSE-FIREBASE.txt'
exclude 'META-INF/NOTICE'
}

compileSdkVersion 28
buildToolsVersion "28.0.3"
compileSdkVersion 31

defaultConfig {
minSdkVersion 14
targetSdkVersion 28
targetSdkVersion 31
versionCode 6
versionName "1.5.5"
consumerProguardFiles 'consumer-proguard-rules.pro'
}
}

repositories{
repositories {
google()
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
compileOnly "com.google.firebase:firebase-auth:$firebase_auth_version"
compileOnly "com.google.firebase:firebase-database:$firebase_database_version"
compileOnly "com.google.firebase:firebase-storage:$firebase_storage_version"
compileOnly "com.google.firebase:firebase-firestore:$firebase_firestore_version"
compileOnly "com.google.firebase:firebase-functions:$firebase_functions_version"
compileOnly "com.google.firebase:firebase-config:$firebase_remote_version"
implementation "com.google.firebase:firebase-auth:$firebase_auth_version"
implementation "com.google.firebase:firebase-database:$firebase_database_version"
implementation "com.google.firebase:firebase-storage:$firebase_storage_version"
implementation "com.google.firebase:firebase-firestore:$firebase_firestore_version"
implementation "com.google.firebase:firebase-functions:$firebase_functions_version"
implementation "com.google.firebase:firebase-config:$firebase_remote_version"

implementation "io.reactivex.rxjava2:rxjava:$rx_version"
implementation "io.reactivex.rxjava2:rxandroid:$rx_android_version"
Expand All @@ -54,6 +48,6 @@ dependencies {
testImplementation "com.google.firebase:firebase-storage:$firebase_storage_version"
testImplementation "com.google.firebase:firebase-firestore:$firebase_firestore_version"
testImplementation "com.google.firebase:firebase-config:$firebase_remote_version"
testImplementation 'junit:junit:4.13'
testImplementation "org.mockito:mockito-core:3.3.1"
testImplementation 'junit:junit:4.13.2'
testImplementation "org.mockito:mockito-core:4.0.0"
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import java.util.LinkedHashMap;
import java.util.List;

import androidx.annotation.NonNull;
import durdinapps.rxfirebase2.exceptions.RxFirebaseDataCastException;
import io.reactivex.exceptions.Exceptions;
import io.reactivex.functions.Function;
Expand Down Expand Up @@ -158,10 +157,5 @@ public RxFirebaseChildEvent<U> apply(final RxFirebaseChildEvent<DataSnapshot> rx
}
}

static final Predicate<DataSnapshot> DATA_SNAPSHOT_EXISTENCE_PREDICATE = new Predicate<DataSnapshot>() {
@Override
public boolean test(@NonNull DataSnapshot dataSnapshot) throws Exception {
return dataSnapshot.exists();
}
};
static final Predicate<DataSnapshot> DATA_SNAPSHOT_EXISTENCE_PREDICATE = DataSnapshot::exists;
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package durdinapps.rxfirebase2;


import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.QuerySnapshot;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import androidx.annotation.NonNull;
import io.reactivex.functions.Function;
import io.reactivex.functions.Predicate;

Expand Down Expand Up @@ -96,17 +94,7 @@ public LinkedHashMap<String, U> apply(final QuerySnapshot querySnapshot) {
}
}

static final Predicate<QuerySnapshot> QUERY_EXISTENCE_PREDICATE = new Predicate<QuerySnapshot>() {
@Override
public boolean test(@NonNull QuerySnapshot querySnapshot) throws Exception {
return !querySnapshot.isEmpty();
}
};
static final Predicate<QuerySnapshot> QUERY_EXISTENCE_PREDICATE = querySnapshot -> !querySnapshot.isEmpty();

static final Predicate<DocumentSnapshot> DOCUMENT_EXISTENCE_PREDICATE = new Predicate<DocumentSnapshot>() {
@Override
public boolean test(@NonNull DocumentSnapshot documentSnapshot) throws Exception {
return documentSnapshot.exists();
}
};
static final Predicate<DocumentSnapshot> DOCUMENT_EXISTENCE_PREDICATE = DocumentSnapshot::exists;
}
25 changes: 25 additions & 0 deletions app/src/main/java/durdinapps/rxfirebase2/Plugins.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package durdinapps.rxfirebase2;

import android.os.Looper;

/**
* Utility class to make behavior of extensions more flexible and testable
*/
public class Plugins {

/**
* The flag, that controls can be extensions be called on the main thread or not
* @apiNote some extensions not supported this feature, so see documentation
*/
public static boolean allowMainThreadQueries = false;

/**
* Throws an error if you make network call to Firebase on the main thread
* @throws IllegalStateException in case of calling on the main thread
*/
public static void throwExceptionIfMainThread() {
if (!allowMainThreadQueries && Looper.myLooper() == Looper.getMainLooper()) {
throw new IllegalStateException("Network on main thread is not permitted");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@

import io.reactivex.CompletableEmitter;


public class RxCompletableHandler implements OnFailureListener, OnSuccessListener, OnCompleteListener {
public class RxCompletableHandler<T> implements OnFailureListener, OnSuccessListener<T>, OnCompleteListener<T> {

private final CompletableEmitter completableEmitter;

Expand All @@ -19,7 +18,7 @@ private RxCompletableHandler(CompletableEmitter completableEmitter) {
}

public static <T> void assignOnTask(CompletableEmitter completableEmitter, Task<T> task) {
RxCompletableHandler handler = new RxCompletableHandler(completableEmitter);
RxCompletableHandler<T> handler = new RxCompletableHandler<>(completableEmitter);
task.addOnFailureListener(handler);
task.addOnSuccessListener(handler);
try {
Expand All @@ -29,7 +28,6 @@ public static <T> void assignOnTask(CompletableEmitter completableEmitter, Task<
}
}


@Override
public void onFailure(@NonNull Exception e) {
if (!completableEmitter.isDisposed())
Expand Down
31 changes: 11 additions & 20 deletions app/src/main/java/durdinapps/rxfirebase2/RxFirebaseAuth.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package durdinapps.rxfirebase2;

import static durdinapps.rxfirebase2.Plugins.throwExceptionIfMainThread;

import androidx.annotation.NonNull;

import com.google.firebase.auth.ActionCodeResult;
Expand All @@ -16,9 +18,6 @@
import io.reactivex.MaybeEmitter;
import io.reactivex.MaybeOnSubscribe;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.functions.Cancellable;

public class RxFirebaseAuth {

Expand Down Expand Up @@ -205,27 +204,19 @@ public void subscribe(CompletableEmitter emitter) throws Exception {
*
* @param firebaseAuth firebaseAuth instance.
* @return an {@link Observable} which emits every time that the {@link FirebaseAuth} state change.
* @throws IllegalStateException if operation is happening on the main thread
* @see Plugins
*/
@NonNull
public static Observable<FirebaseAuth> observeAuthState(@NonNull final FirebaseAuth firebaseAuth) {
return Observable.create(emitter -> {
throwExceptionIfMainThread();

return Observable.create(new ObservableOnSubscribe<FirebaseAuth>() {
@Override
public void subscribe(final ObservableEmitter<FirebaseAuth> emitter) throws Exception {
final FirebaseAuth.AuthStateListener authStateListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
emitter.onNext(firebaseAuth);
}
};
firebaseAuth.addAuthStateListener(authStateListener);
emitter.setCancellable(new Cancellable() {
@Override
public void cancel() throws Exception {
firebaseAuth.removeAuthStateListener(authStateListener);
}
});
}
final FirebaseAuth.AuthStateListener authStateListener = emitter::onNext;

firebaseAuth.addAuthStateListener(authStateListener);

emitter.setCancellable(() -> firebaseAuth.removeAuthStateListener(authStateListener));
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package durdinapps.rxfirebase2;


import androidx.annotation.NonNull;

public class RxFirebaseChildEvent<T> {

private EventType eventType;
private String key;
private T value;
private final EventType eventType;
private final String key;
private final T value;
private String previousChildName;

public RxFirebaseChildEvent(@NonNull String key,
Expand Down
Loading