├── .gitattributes ├── .gitignore ├── LICENSE ├── README.md ├── build.gradle ├── proguard-rules.pro ├── rxfbase.iml └── src ├── androidTest └── java │ └── isfaaghyth │ └── app │ └── rxfbase │ └── ExampleInstrumentedTest.java ├── main ├── AndroidManifest.xml ├── java │ └── isfaaghyth │ │ └── app │ │ └── rxfbase │ │ └── RxFirebase.java └── res │ └── values │ └── strings.xml └── test └── java └── isfaaghyth └── app └── rxfbase └── ExampleUnitTest.java /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /app 4 | /local.properties 5 | /.idea/workspace.xml 6 | /.idea/libraries 7 | .DS_Store 8 | /build 9 | /captures 10 | .externalNativeBuild 11 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Muh Isfhani Ghiath 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Android Arsenal](https://img.shields.io/badge/Android%20Arsenal-RxFBase-brightgreen.svg?style=flat)](https://android-arsenal.com/details/1/6345) 2 | 3 | # RxFBase 4 | wrapping firebase with rx 5 | 6 | In the past, I have a problem when doing real-time marker mapping (maps) using firebase. So, I tried to create this library for my case. enjoy! 7 | 8 | TODO: 9 | - [x] firebase database 10 | - [ ] firebase auth 11 | - [ ] firebase storage 12 | 13 | ## Installation 14 | Add it in your root build.gradle at the end of repositories: 15 | 16 | ``` 17 | allprojects { 18 | repositories { 19 | ... 20 | maven { url 'https://jitpack.io' } 21 | } 22 | } 23 | ``` 24 | 25 | Add the dependency 26 | 27 | ``` 28 | implementation 'com.github.isfaaghyth:RxFBase:1.0.0' 29 | ``` 30 | 31 | ## Usage 32 | set value: 33 | ```java 34 | HashMap data = new HashMap<>(); 35 | data.put("title", "this is title"); 36 | data.put("message", "awesome message!"); 37 | RxFirebase.setValue(dbRef, data) 38 | .subscribe(isSuccess -> { 39 | //isSuccess true/false 40 | }); 41 | ``` 42 | 43 | for singleValue: 44 | ```java 45 | RxFirebase.singleValue(dbRef) 46 | .subscribe(dataSnapshot -> { 47 | //do something! 48 | }, throwable -> { 49 | //ops! 50 | }); 51 | ``` 52 | for childValue: 53 | ```java 54 | RxFirebase.childValue(dbRef) 55 | .subscribe(dataSnapshot -> { 56 | //do something! 57 | }, throwable -> { 58 | //ops! 59 | }); 60 | ``` 61 | 62 | enjoy! 63 | 64 | 65 | MIT 66 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | 3 | android { 4 | compileSdkVersion 25 5 | buildToolsVersion "25.0.3" 6 | 7 | defaultConfig { 8 | minSdkVersion 18 9 | targetSdkVersion 25 10 | versionCode 1 11 | versionName "1.0" 12 | 13 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 14 | 15 | } 16 | buildTypes { 17 | release { 18 | minifyEnabled false 19 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 20 | } 21 | } 22 | } 23 | 24 | dependencies { 25 | compile fileTree(dir: 'libs', include: ['*.jar']) 26 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 27 | exclude group: 'com.android.support', module: 'support-annotations' 28 | }) 29 | provided 'com.google.firebase:firebase-auth:10.2.6' 30 | provided 'com.google.firebase:firebase-database:10.2.6' 31 | provided 'com.google.firebase:firebase-storage:10.2.6' 32 | provided 'com.google.firebase:firebase-config:10.2.6' 33 | 34 | compile 'io.reactivex:rxjava:1.1.6' 35 | compile 'io.reactivex:rxandroid:1.2.1' 36 | compile 'com.android.support:appcompat-v7:25.3.1' 37 | testCompile 'junit:junit:4.12' 38 | } 39 | -------------------------------------------------------------------------------- /proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /Users/isfaaghyth/Library/Android/sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | 19 | # Uncomment this to preserve the line number information for 20 | # debugging stack traces. 21 | #-keepattributes SourceFile,LineNumberTable 22 | 23 | # If you keep the line number information, uncomment this to 24 | # hide the original source file name. 25 | #-renamesourcefileattribute SourceFile 26 | -------------------------------------------------------------------------------- /rxfbase.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 8 | 9 | 10 | 11 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | -------------------------------------------------------------------------------- /src/androidTest/java/isfaaghyth/app/rxfbase/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package isfaaghyth.app.rxfbase; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumentation test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("isfaaghyth.app.rxfbase.test", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 5 | 6 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /src/main/java/isfaaghyth/app/rxfbase/RxFirebase.java: -------------------------------------------------------------------------------- 1 | package isfaaghyth.app.rxfbase; 2 | 3 | import android.support.annotation.NonNull; 4 | 5 | import com.google.firebase.database.ChildEventListener; 6 | import com.google.firebase.database.DataSnapshot; 7 | import com.google.firebase.database.DatabaseError; 8 | import com.google.firebase.database.DatabaseReference; 9 | import com.google.firebase.database.Query; 10 | import com.google.firebase.database.ValueEventListener; 11 | 12 | import rx.Observable; 13 | import rx.Subscriber; 14 | import rx.functions.Action0; 15 | import rx.subscriptions.Subscriptions; 16 | 17 | /** 18 | * Created by isfaaghyth on 8/14/17. 19 | * github: @isfaaghyth 20 | */ 21 | 22 | public class RxFirebase { 23 | 24 | public static Observable setValue(final DatabaseReference ref, final Object obj) { 25 | return Observable.create(new Observable.OnSubscribe() { 26 | @Override public void call(Subscriber subscriber) { 27 | if (!subscriber.isUnsubscribed()) { 28 | try { 29 | ref.setValue(obj); 30 | subscriber.onNext(true); 31 | subscriber.onCompleted(); 32 | } catch (Exception e) { 33 | subscriber.onNext(false); 34 | } 35 | } 36 | } 37 | }); 38 | } 39 | 40 | @NonNull public static Observable singleValue(final Query query) { 41 | return Observable.create(new Observable.OnSubscribe() { 42 | @Override public void call(final Subscriber subscriber) { 43 | final ValueEventListener valueEvent = query.addValueEventListener(new ValueEventListener() { 44 | @Override public void onDataChange(DataSnapshot dataSnapshot) { 45 | if (!subscriber.isUnsubscribed()) { 46 | subscriber.onNext(dataSnapshot); 47 | } 48 | } 49 | @Override public void onCancelled(DatabaseError databaseError) { 50 | subscriber.onError(databaseError.toException()); 51 | } 52 | }); 53 | subscriber.add(Subscriptions.create(new Action0() { 54 | @Override public void call() { 55 | query.removeEventListener(valueEvent); 56 | } 57 | })); 58 | } 59 | }); 60 | } 61 | 62 | @NonNull public Observable childValue(final Query query) { 63 | return Observable.create(new Observable.OnSubscribe() { 64 | @Override public void call(final Subscriber subscriber) { 65 | final ChildEventListener childEvent = query.addChildEventListener(new ChildEventListener() { 66 | @Override public void onChildAdded(DataSnapshot dataSnapshot, String s) { 67 | if (!subscriber.isUnsubscribed()) { 68 | subscriber.onNext(dataSnapshot); 69 | } 70 | } 71 | @Override public void onChildChanged(DataSnapshot dataSnapshot, String s) { 72 | if (!subscriber.isUnsubscribed()) { 73 | subscriber.onNext(dataSnapshot); 74 | } 75 | } 76 | @Override public void onChildRemoved(DataSnapshot dataSnapshot) { 77 | if (!subscriber.isUnsubscribed()) { 78 | subscriber.onNext(dataSnapshot); 79 | } 80 | } 81 | @Override public void onChildMoved(DataSnapshot dataSnapshot, String s) { 82 | if (!subscriber.isUnsubscribed()) { 83 | subscriber.onNext(dataSnapshot); 84 | } 85 | } 86 | @Override public void onCancelled(DatabaseError databaseError) { 87 | subscriber.onError(databaseError.toException()); 88 | } 89 | }); 90 | subscriber.add(Subscriptions.create(new Action0() { 91 | @Override public void call() { 92 | query.removeEventListener(childEvent); 93 | } 94 | })); 95 | } 96 | }); 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | rxfbase 3 | 4 | -------------------------------------------------------------------------------- /src/test/java/isfaaghyth/app/rxfbase/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package isfaaghyth.app.rxfbase; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * @see Testing documentation 11 | */ 12 | public class ExampleUnitTest { 13 | @Test 14 | public void addition_isCorrect() throws Exception { 15 | assertEquals(4, 2 + 2); 16 | } 17 | } --------------------------------------------------------------------------------