├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── agency │ │ │ └── tango │ │ │ └── databindingrxjava │ │ │ ├── MainActivity.java │ │ │ ├── RxUtils.java │ │ │ └── StringUtils.java │ └── res │ │ ├── layout │ │ └── activity_main.xml │ │ ├── mipmap-hdpi │ │ └── ic_launcher.png │ │ ├── mipmap-mdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxxhdpi │ │ └── ic_launcher.png │ │ ├── values-v21 │ │ └── styles.xml │ │ ├── values-w820dp │ │ └── dimens.xml │ │ └── values │ │ ├── colors.xml │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── agency │ └── tango │ └── databindingrxjava │ └── MainViewModelTests.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | # Built application files 2 | *.apk 3 | *.ap_ 4 | 5 | # Files for the ART/Dalvik VM 6 | *.dex 7 | 8 | # Java class files 9 | *.class 10 | 11 | # Generated files 12 | bin/ 13 | gen/ 14 | out/ 15 | 16 | # Gradle files 17 | .gradle/ 18 | build/ 19 | 20 | # Local configuration file (sdk path, etc) 21 | local.properties 22 | 23 | # Proguard folder generated by Eclipse 24 | proguard/ 25 | 26 | # Log Files 27 | *.log 28 | 29 | # Android Studio Navigation editor temp files 30 | .navigation/ 31 | 32 | # Android Studio captures folder 33 | captures/ 34 | 35 | # Intellij 36 | *.iml 37 | .idea/workspace.xml 38 | 39 | # Keystore files 40 | *.jks 41 | libs/ 42 | src/main/res/drawable/ 43 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: android 2 | jdk: 3 | - oraclejdk8 4 | android: 5 | components: 6 | - tools 7 | - tools 8 | - platform-tools 9 | - build-tools-24.0.3 10 | - android-24 11 | - extra-google-google_play_services 12 | - extra-android-m2repository 13 | - extra-google-m2repository 14 | licenses: 15 | - '.+' 16 | 17 | before_cache: 18 | - rm -f $HOME/.gradle/caches/modules-2/modules-2.lock 19 | - rm -fr $HOME/.gradle/caches/*/plugin-resolution/ 20 | 21 | cache: 22 | directories: 23 | - $HOME/.m2 24 | - $HOME/.gradle 25 | 26 | before_install: 27 | - mkdir "$ANDROID_HOME/licenses" || true 28 | - echo -e "\n8933bad161af4178b1185d1a37fbf41ea5269c55" > "$ANDROID_HOME/licenses/android-sdk-license" 29 | - echo -e "\n84831b9409646a918e30573bab4c9c91346d8abd" > "$ANDROID_HOME/licenses/android-sdk-preview-license" -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Tango Agency 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-data-binding-rxjava 2 | 3 | [![Android Arsenal android-data-binding-rxjava](https://img.shields.io/badge/Android%20Arsenal-android--data--binding--rxjava-green.svg?style=true)](https://android-arsenal.com/details/3/4369) 4 | [![Build Status](https://travis-ci.org/TangoAgency/android-data-binding-rxjava.svg?branch=master)](https://travis-ci.org/TangoAgency/android-data-binding-rxjava) 5 | 6 | Simple demo developed with love at [Tango](http://tango.agency) which demonstrates how to wrap Android DataBinding ObservableField into RxJava's Observable. 7 | 8 | With this solution it is possible to register for ObservableField's value changes and use it with RxJava operators. 9 | 10 | You can read Medium story which explains this concept - [RxJava meets Android Data Binding](https://medium.com/tangoagency/rxjava-meets-android-data-binding-4ca5e1144107#.wv63halu1). 11 | 12 | ### Example code 13 | 14 | ```java 15 | public static class MainViewModel { 16 | 17 | public ObservableField firstName = new ObservableField<>(); 18 | public ObservableField lastName = new ObservableField<>(); 19 | public ObservableField helloText = new ObservableField<>(); 20 | public ObservableBoolean helloButtonEnabled = new ObservableBoolean(false); 21 | 22 | public MainViewModel() { 23 | 24 | Observable.combineLatest(toObservable(firstName), toObservable(lastName), (firstName, lastName) -> StringUtils.isNotNullOrEmpty(firstName) && StringUtils.isNotNullOrEmpty(lastName)) 25 | .subscribe(result -> { 26 | helloButtonEnabled.set(result); 27 | if (!result) { 28 | helloText.set(StringUtils.EMPTY); 29 | } 30 | }, Throwable::printStackTrace); 31 | } 32 | 33 | public void buttonClicked() { 34 | helloText.set(String.format("Hello %s %s !", firstName.get(), lastName.get())); 35 | } 36 | } 37 | ``` 38 | 39 | ![rxjava-databinding](https://cloud.githubusercontent.com/assets/469111/18312397/db8996fc-7509-11e6-9bcd-0cee0bac0754.gif) 40 | 41 | ### How it works 42 | 43 | You can find ```toObservable``` method implementation in [RxUtils.java](https://github.com/TangoAgency/android-data-binding-rxjava/blob/master/app/src/main/java/agency/tango/databindingrxjava/RxUtils.java) class. 44 | 45 | It uses ObservableField's ```OnPropertyChangedCallback``` and expose property change events to the "RxWorld". 46 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | .gradle 2 | /local.properties 3 | /.idea/workspace.xml 4 | /.idea/libraries 5 | .DS_Store 6 | /build 7 | /captures 8 | 9 | 10 | # Built application files 11 | *.apk 12 | *.ap_ 13 | 14 | # Files for the Dalvik VM 15 | *.dex 16 | 17 | # Java class files 18 | *.class 19 | 20 | # Generated files 21 | bin/ 22 | gen/ 23 | 24 | # Gradle files 25 | .gradle/ 26 | build/ 27 | /*/build/ 28 | 29 | # Local configuration file (sdk path, etc) 30 | local.properties 31 | 32 | # Proguard folder generated by Eclipse 33 | proguard/ 34 | 35 | # Log Files 36 | *.log 37 | 38 | # Crashlytics configuations 39 | com_crashlytics_export_strings.xml 40 | *.iml 41 | .idea 42 | /app/*.iml 43 | /.idea 44 | /*.iml 45 | *.iml 46 | /presentation/*.iml 47 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | apply plugin: 'me.tatarka.retrolambda' 3 | 4 | android { 5 | compileSdkVersion 24 6 | buildToolsVersion "24.0.3" 7 | defaultConfig { 8 | applicationId "agency.tango.databindingrxjava" 9 | minSdkVersion 9 10 | targetSdkVersion 24 11 | versionCode 1 12 | versionName "1.0" 13 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 14 | } 15 | buildTypes { 16 | release { 17 | minifyEnabled false 18 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 19 | } 20 | } 21 | 22 | dataBinding { 23 | enabled = true 24 | } 25 | 26 | compileOptions { 27 | encoding "UTF-8" 28 | sourceCompatibility JavaVersion.VERSION_1_8 29 | targetCompatibility JavaVersion.VERSION_1_8 30 | } 31 | 32 | lintOptions { 33 | disable 'GoogleAppIndexingWarning','TypographyQuotes' 34 | textReport true 35 | textOutput 'stdout' 36 | } 37 | } 38 | 39 | dependencies { 40 | compile fileTree(dir: 'libs', include: ['*.jar']) 41 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 42 | exclude group: 'com.android.support', module: 'support-annotations' 43 | }) 44 | compile 'com.android.support:appcompat-v7:24.2.1' 45 | compile 'com.android.support:design:24.2.1' 46 | compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha9' 47 | compile 'com.android.support.constraint:constraint-layout-solver:1.0.0-alpha9' 48 | compile "io.reactivex:rxjava:1.1.10" 49 | 50 | testCompile 'junit:junit:4.12' 51 | } 52 | -------------------------------------------------------------------------------- /app/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 C:\Tools\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 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 11 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /app/src/main/java/agency/tango/databindingrxjava/MainActivity.java: -------------------------------------------------------------------------------- 1 | package agency.tango.databindingrxjava; 2 | 3 | import android.databinding.DataBindingUtil; 4 | import android.databinding.ObservableBoolean; 5 | import android.databinding.ObservableField; 6 | import android.os.Bundle; 7 | import android.support.v7.app.AppCompatActivity; 8 | import android.support.v7.widget.Toolbar; 9 | 10 | import agency.tango.databindingrxjava.databinding.ActivityMainBinding; 11 | import rx.Observable; 12 | 13 | import static agency.tango.databindingrxjava.RxUtils.toObservable; 14 | 15 | public class MainActivity extends AppCompatActivity { 16 | 17 | @Override 18 | protected void onCreate(Bundle savedInstanceState) { 19 | super.onCreate(savedInstanceState); 20 | ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main); 21 | binding.setViewModel(new MainViewModel()); 22 | Toolbar toolbar = binding.toolbar; 23 | setSupportActionBar(toolbar); 24 | } 25 | 26 | public static class MainViewModel { 27 | 28 | public ObservableField firstName = new ObservableField<>(); 29 | public ObservableField lastName = new ObservableField<>(); 30 | public ObservableField helloText = new ObservableField<>(); 31 | public ObservableBoolean helloButtonEnabled = new ObservableBoolean(false); 32 | 33 | public MainViewModel() { 34 | 35 | Observable.combineLatest(toObservable(firstName), toObservable(lastName), (firstName, lastName) -> StringUtils.isNotNullOrEmpty(firstName) && StringUtils.isNotNullOrEmpty(lastName)) 36 | .subscribe(result -> { 37 | helloButtonEnabled.set(result); 38 | if (!result) { 39 | helloText.set(StringUtils.EMPTY); 40 | } 41 | }, Throwable::printStackTrace); 42 | } 43 | 44 | public void buttonClicked() { 45 | helloText.set(String.format("Hello %s %s !", firstName.get(), lastName.get())); 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /app/src/main/java/agency/tango/databindingrxjava/RxUtils.java: -------------------------------------------------------------------------------- 1 | package agency.tango.databindingrxjava; 2 | 3 | 4 | import android.databinding.ObservableField; 5 | import android.support.annotation.NonNull; 6 | 7 | import rx.AsyncEmitter; 8 | import rx.Observable; 9 | 10 | import static android.databinding.Observable.OnPropertyChangedCallback; 11 | 12 | public class RxUtils { 13 | 14 | private RxUtils() { 15 | } 16 | 17 | public static Observable toObservable(@NonNull final ObservableField observableField) { 18 | return Observable.fromEmitter(asyncEmitter -> { 19 | 20 | final OnPropertyChangedCallback callback = new OnPropertyChangedCallback() { 21 | @Override 22 | public void onPropertyChanged(android.databinding.Observable dataBindingObservable, int propertyId) { 23 | if (dataBindingObservable == observableField) { 24 | asyncEmitter.onNext(observableField.get()); 25 | } 26 | } 27 | }; 28 | 29 | observableField.addOnPropertyChangedCallback(callback); 30 | 31 | asyncEmitter.setCancellation(() -> observableField.removeOnPropertyChangedCallback(callback)); 32 | 33 | }, AsyncEmitter.BackpressureMode.LATEST); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /app/src/main/java/agency/tango/databindingrxjava/StringUtils.java: -------------------------------------------------------------------------------- 1 | package agency.tango.databindingrxjava; 2 | 3 | 4 | public class StringUtils { 5 | public static String EMPTY = ""; 6 | 7 | private StringUtils() { 8 | } 9 | 10 | public static boolean isNullOrEmpty(String string) { 11 | return !isNotNullOrEmpty(string); 12 | } 13 | 14 | public static boolean isNotNullOrEmpty(String string) { 15 | return string != null && !string.equals(EMPTY); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 13 | 14 | 15 | 20 | 21 | 25 | 26 | 32 | 33 | 34 | 35 | 45 | 46 | 63 | 64 | 79 | 80 | 88 | 89 |