├── .gitignore ├── .idea ├── .name ├── compiler.xml ├── copyright │ └── profiles_settings.xml ├── encodings.xml ├── gradle.xml ├── misc.xml ├── modules.xml └── runConfigurations.xml ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── inthecheesefactory │ │ └── rxeventbus │ │ └── ApplicationTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── inthecheesefactory │ │ │ └── lab │ │ │ └── rxeventbus │ │ │ ├── MainApplication.java │ │ │ ├── activity │ │ │ └── MainActivity.java │ │ │ └── eventbus │ │ │ ├── MainBus.java │ │ │ └── event │ │ │ └── ClickEvent.java │ └── res │ │ ├── layout │ │ ├── activity_main.xml │ │ └── content_main.xml │ │ ├── menu │ │ └── menu_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 │ └── com │ └── inthecheesefactory │ └── rxeventbus │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── rxeventbus ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── inthecheesefactory │ │ └── rxeventbus │ │ └── ApplicationTest.java │ ├── main │ ├── AndroidManifest.xml │ └── java │ │ └── com │ │ └── inthecheesefactory │ │ └── rxeventbus │ │ └── RxEventBus.java │ └── test │ └── java │ └── com │ └── inthecheesefactory │ └── rxeventbus │ └── ExampleUnitTest.java └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | -------------------------------------------------------------------------------- /.idea/.name: -------------------------------------------------------------------------------- 1 | RxEventBus -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 24 | 25 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 19 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 46 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # RxEventBus 2 | A code example of the implementation of RxAndroid & RxJava as an EventBus on Android 3 | 4 | # Explaination 5 | 6 | This example depends on RxJava and RxAndroid dependencies. 7 | 8 | ```sh 9 | compile 'io.reactivex:rxandroid:1.1.0' 10 | compile 'io.reactivex:rxjava:1.1.0' 11 | ``` 12 | 13 | This is an implementation of `RxEventBus` class which apply RxJava and RxAndroid to be an EventBus with help from RxJava's `SerializedSubject` 14 | 15 | ```java 16 | public class RxEventBus { 17 | 18 | private final Subject bus = new SerializedSubject<>(PublishSubject.create()); 19 | 20 | public void post(Object o) { 21 | bus.onNext(o); 22 | } 23 | 24 | public Observable getBusObservable() { 25 | return bus; 26 | } 27 | 28 | } 29 | ``` 30 | 31 | Create a Main Bus as a Singleton. 32 | 33 | ```java 34 | public class MainBus extends RxEventBus { 35 | 36 | private static MainBus instance; 37 | 38 | public static MainBus getInstance() { 39 | if (instance == null) 40 | instance = new MainBus(); 41 | return instance; 42 | } 43 | 44 | private MainBus() { 45 | } 46 | 47 | } 48 | ``` 49 | 50 | Declare a Bus Event. 51 | 52 | ```java 53 | public class ClickEvent { 54 | 55 | private View view; 56 | 57 | public ClickEvent(View view) { 58 | this.view = view; 59 | } 60 | 61 | public View getView() { 62 | return view; 63 | } 64 | 65 | public void setView(View view) { 66 | this.view = view; 67 | } 68 | 69 | } 70 | ``` 71 | 72 | # Usage 73 | 74 | This part of code show you how to subscribe or unsubscribe to the EventBus. 75 | 76 | ```java 77 | 78 | public class MainActivity extends AppCompatActivity { 79 | 80 | ... 81 | 82 | /******************** 83 | * EventBus Example * 84 | *********************/ 85 | 86 | private Subscriber mainBusSubscriber = new Subscriber() { 87 | @Override 88 | public void onCompleted() { 89 | } 90 | 91 | @Override 92 | public void onError(Throwable e) { 93 | } 94 | 95 | @Override 96 | public void onNext(Object o) { 97 | if (o instanceof ClickEvent) { 98 | ClickEvent event = (ClickEvent) o; 99 | Snackbar.make(event.getView(), "Event Received", Snackbar.LENGTH_LONG) 100 | .setAction("OK", null) 101 | .show(); 102 | } 103 | } 104 | }; 105 | 106 | @Override 107 | protected void onResume() { 108 | super.onResume(); 109 | // Subscribe to EventBus 110 | MainBus.getInstance().getBusObservable() 111 | .subscribeOn(Schedulers.newThread()) 112 | .observeOn(AndroidSchedulers.mainThread()) 113 | .subscribe(mainBusSubscriber); 114 | } 115 | 116 | @Override 117 | protected void onPause() { 118 | super.onPause(); 119 | // Unsubscribe from EventBus 120 | mainBusSubscriber.unsubscribe(); 121 | } 122 | 123 | } 124 | ``` 125 | 126 | And this is how we post an Event to the bus. 127 | 128 | ```java 129 | FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 130 | fab.setOnClickListener(new View.OnClickListener() { 131 | @Override 132 | public void onClick(View view) { 133 | MainBus.getInstance().post(new ClickEvent(view)); 134 | } 135 | }); 136 | ``` 137 | 138 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 'Google Inc.:Google APIs:23' 5 | buildToolsVersion "23.0.2" 6 | 7 | defaultConfig { 8 | applicationId "com.inthecheesefactory.lab.rxeventbus" 9 | minSdkVersion 15 10 | targetSdkVersion 23 11 | versionCode 1 12 | versionName "1.0" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | compile fileTree(dir: 'libs', include: ['*.jar']) 24 | compile project(':rxeventbus') 25 | testCompile 'junit:junit:4.12' 26 | compile 'com.android.support:appcompat-v7:23.+' 27 | compile 'com.android.support:design:23.+' 28 | } 29 | -------------------------------------------------------------------------------- /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 D:\Neois\Installer\AndroidSDK\android-sdk-windows/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/androidTest/java/com/inthecheesefactory/rxeventbus/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package com.inthecheesefactory.rxeventbus; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Testing Fundamentals 8 | */ 9 | public class ApplicationTest extends ApplicationTestCase { 10 | public ApplicationTest() { 11 | super(Application.class); 12 | } 13 | } -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /app/src/main/java/com/inthecheesefactory/lab/rxeventbus/MainApplication.java: -------------------------------------------------------------------------------- 1 | package com.inthecheesefactory.lab.rxeventbus; 2 | 3 | import android.app.Application; 4 | 5 | /** 6 | * Created by nuuneoi on 1/19/2016. 7 | */ 8 | public class MainApplication extends Application { 9 | 10 | @Override 11 | public void onCreate() { 12 | super.onCreate(); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /app/src/main/java/com/inthecheesefactory/lab/rxeventbus/activity/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.inthecheesefactory.lab.rxeventbus.activity; 2 | 3 | import android.os.Bundle; 4 | import android.support.design.widget.FloatingActionButton; 5 | import android.support.design.widget.Snackbar; 6 | import android.support.v7.app.AppCompatActivity; 7 | import android.support.v7.widget.Toolbar; 8 | import android.view.View; 9 | import android.view.Menu; 10 | import android.view.MenuItem; 11 | 12 | import com.inthecheesefactory.lab.rxeventbus.R; 13 | import com.inthecheesefactory.lab.rxeventbus.eventbus.MainBus; 14 | import com.inthecheesefactory.lab.rxeventbus.eventbus.event.ClickEvent; 15 | 16 | import rx.Scheduler; 17 | import rx.Subscriber; 18 | import rx.android.schedulers.AndroidSchedulers; 19 | import rx.functions.Action1; 20 | import rx.schedulers.Schedulers; 21 | 22 | public class MainActivity extends AppCompatActivity { 23 | 24 | FloatingActionButton fab; 25 | 26 | @Override 27 | protected void onCreate(Bundle savedInstanceState) { 28 | super.onCreate(savedInstanceState); 29 | setContentView(R.layout.activity_main); 30 | Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 31 | setSupportActionBar(toolbar); 32 | 33 | fab = (FloatingActionButton) findViewById(R.id.fab); 34 | fab.setOnClickListener(new View.OnClickListener() { 35 | @Override 36 | public void onClick(View view) { 37 | MainBus.getInstance().post(new ClickEvent(view)); 38 | } 39 | }); 40 | } 41 | 42 | @Override 43 | public boolean onCreateOptionsMenu(Menu menu) { 44 | // Inflate the menu; this adds items to the action bar if it is present. 45 | getMenuInflater().inflate(R.menu.menu_main, menu); 46 | return true; 47 | } 48 | 49 | @Override 50 | public boolean onOptionsItemSelected(MenuItem item) { 51 | // Handle action bar item clicks here. The action bar will 52 | // automatically handle clicks on the Home/Up button, so long 53 | // as you specify a parent activity in AndroidManifest.xml. 54 | int id = item.getItemId(); 55 | 56 | //noinspection SimplifiableIfStatement 57 | if (id == R.id.action_settings) { 58 | return true; 59 | } 60 | 61 | return super.onOptionsItemSelected(item); 62 | } 63 | 64 | /******************** 65 | * EventBus Example * 66 | *********************/ 67 | 68 | private Subscriber mainBusSubscriber = new Subscriber() { 69 | @Override 70 | public void onCompleted() { 71 | } 72 | 73 | @Override 74 | public void onError(Throwable e) { 75 | } 76 | 77 | @Override 78 | public void onNext(Object o) { 79 | if (o instanceof ClickEvent) { 80 | ClickEvent event = (ClickEvent) o; 81 | Snackbar.make(event.getView(), "Event Received", Snackbar.LENGTH_LONG) 82 | .setAction("OK", null) 83 | .show(); 84 | } 85 | } 86 | }; 87 | 88 | @Override 89 | protected void onResume() { 90 | super.onResume(); 91 | // Subscribe to EventBus 92 | MainBus.getInstance().getBusObservable() 93 | .subscribeOn(Schedulers.newThread()) 94 | .observeOn(AndroidSchedulers.mainThread()) 95 | .subscribe(mainBusSubscriber); 96 | } 97 | 98 | @Override 99 | protected void onPause() { 100 | super.onPause(); 101 | // Unsubscribe from EventBus 102 | mainBusSubscriber.unsubscribe(); 103 | } 104 | 105 | 106 | } 107 | -------------------------------------------------------------------------------- /app/src/main/java/com/inthecheesefactory/lab/rxeventbus/eventbus/MainBus.java: -------------------------------------------------------------------------------- 1 | package com.inthecheesefactory.lab.rxeventbus.eventbus; 2 | 3 | import com.inthecheesefactory.rxeventbus.RxEventBus; 4 | 5 | /** 6 | * Created by nuuneoi on 1/19/2016. 7 | */ 8 | public class MainBus extends RxEventBus { 9 | 10 | private static MainBus instance; 11 | 12 | public static MainBus getInstance() { 13 | if (instance == null) 14 | instance = new MainBus(); 15 | return instance; 16 | } 17 | 18 | private MainBus() { 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /app/src/main/java/com/inthecheesefactory/lab/rxeventbus/eventbus/event/ClickEvent.java: -------------------------------------------------------------------------------- 1 | package com.inthecheesefactory.lab.rxeventbus.eventbus.event; 2 | 3 | import android.view.View; 4 | 5 | /** 6 | * Created by nuuneoi on 1/19/2016. 7 | */ 8 | public class ClickEvent { 9 | 10 | private View view; 11 | 12 | public ClickEvent(View view) { 13 | this.view = view; 14 | } 15 | 16 | public View getView() { 17 | return view; 18 | } 19 | 20 | public void setView(View view) { 21 | this.view = view; 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 14 | 15 | 21 | 22 | 23 | 24 | 25 | 26 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /app/src/main/res/layout/content_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 14 | 15 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /app/src/main/res/menu/menu_main.xml: -------------------------------------------------------------------------------- 1 | 5 | 10 | 11 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nuuneoi/RxEventBus/69f57d98fd9e2549965b28bc871148d7d7e6e0d5/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nuuneoi/RxEventBus/69f57d98fd9e2549965b28bc871148d7d7e6e0d5/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nuuneoi/RxEventBus/69f57d98fd9e2549965b28bc871148d7d7e6e0d5/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nuuneoi/RxEventBus/69f57d98fd9e2549965b28bc871148d7d7e6e0d5/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nuuneoi/RxEventBus/69f57d98fd9e2549965b28bc871148d7d7e6e0d5/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/values-v21/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 9 | 10 | -------------------------------------------------------------------------------- /app/src/main/res/values-w820dp/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 64dp 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16dp 4 | 16dp 5 | 16dp 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | RxEventBus 3 | Settings 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 15 | 16 |