RXBus


Source link: https://github.com/MFlisar/RXBus

###RXBus

#####RxJava V2: If you are looking for a version for RxJava V2, check out my RxBus2

What does it do?

  • it allows you to post events to a bus
  • it allows you to subscribe to special events whereever you want
  • it allows you to queue events until an activity is resumed (to make sure views are accessable for example)
  • it allows you to queue events as soon as activity is paused and emit events as soon soon as it is resumed
  • it's very lightweight

Gradle (via JitPack.io)

  1. add jitpack to your project's build.gradle:
repositories {

  maven {
 url "https://jitpack.io" 
}
 
}
  1. add the compile statement to your module's build.gradle:
dependencies {

  compile 'com.github.MFlisar:RXBus:1.0' 
}

Migration

If you update from version <0.5 or version <0.9, follow this short migration guide: MIGRATION GUIDE

Usage

Content

#####Demo

Just check out the DemoActivity, it will show the base usage and the difference between the default and the queued RXBus

#####Simple usage

Use the RXBusBuilder to create subscriptions or simple observables. Just like following:

// Variant 1 - create a simple observable : Observable<TestEvent> simpleObservable = RXBusBuilder.create(TestEvent.class).build();
  // Variant 2 - subscribe with the BUILDER: Subscription simpleSubscription = RXBusBuilder.create(TestEvent.class)
  .subscribe(new Action1<TestEvent>() {

@Override

public void call(TestEvent event) {

 // handle event...

  // event MUST have been send with either of following:

 // RXBus.get().sendEvent(new TestEvent());
 => class bound bus usage

 // RXBus.get().sendEvent(new TestEvent(), R.id.observer_key_1, true);
 => key bound bus usage, with sendToDefaultBusAsWell = true, which will result in that all class bound observers (like this one) retrieve this event as well

}

  
}
);

#####Sending an event

// Send an event to the bus - all observers that observe this class WITHOUT a key will receive this event RXBus.get().sendEvent(new TestEvent());
 // Send an event to the bus - only observers that observe the class AND key will receive this event RXBus.get().sendEvent(new TestEvent(), R.id.observer_key_1);
 // Send an event to the bus - all observers that either observe the class or the class AND key will receive this event RXBus.get().sendEvent(new TestEvent(), R.id.observer_key_1, true);

#####Advanced usage - QUEUING AND BINDING

You can use this library to subscribe to events and only get them when your activity is resumed, so that you can be sure views are available, for example. Just like following:

RXBusBuilder.create(TestEvent.class)
  // this enables the queuing mode! Passed object must implement IRXBusQueue interface, see the demo app for an example
  .withQueuing(rxBusQueue)
  .withOnNext(new Action1<TestEvent>() {

@Override

public void call(TestEvent s) {

 // activity IS resumed, you can safely update your UI for example

  // event MUST have been send with either of following:

 // RXBus.get().sendEvent(new TestEvent());
 => class bound bus usage

 // RXBus.get().sendEvent(new TestEvent(), R.id.observer_key_1, true);
 => key bound bus usage, with sendToDefaultBusAsWell = true, which will result in that all class bound observers (like this one) retrieve this event as well

}

  
}
)
  .buildSubscription();

Additionally, you can bind the subscription to an object and afterwards call RXSubscriptionManager.unsubscribe(boundObject); to unsubcribe ANY subscription that was bound to boundObject just like following:

// boundObject... can be for example your activity RXBusBuilder.create(TestEvent.class)
  // this enables the queuing mode! Passed object must implement IRXBusQueue interface, see the demo app for an example
  .withQueuing(rxBusQueue)
  .withBound(boundObject)
  .subscribe(new Action1<Object>() {

@Override

public void call(TestEvent s) {

 // activity IS resumed, you can safely update your UI for example

  // event MUST have been send with either of following:

 // RXBus.get().sendEvent(new TestEvent());
 => class bound bus usage

 // RXBus.get().sendEvent(new TestEvent(), R.id.observer_key_1, true);
 => key bound bus usage, with sendToDefaultBusAsWell = true, which will result in that all class bound observers (like this one) retrieve this event as well

}

  
}
);
 // call this for example in your activities onDestroy or whereever appropriate to unsubscribe ALL subscriptions at once that are bound to the boundOBject RXSubscriptionManager.unsubscribe(boundObject);

#####Advanced usage - KEYS

You can use this library to subscribe to events of a typ and ONLY get them when it was send to the bus with a special key (and only when your activity is resumed, as this example shows via .withQueuing()), so that you can distinct event subscriptions of the same class based on a key (the key can be an Integer or a String). Just like following:

RXBusBuilder.create(TestEvent.class)
  // this enables the binding to the key
  .withKey(R.id.observer_key_1) // you can provide multiple keys as well
  .withQueuing(rxBusQueue)
  .withBound(boundObject)
  .subscribe(new Action1<String>() {

@Override

public void call(TestEvent event) {

 // activity IS resumed, you can safely update your UI for example

  // event MUST have been with either of those:

 // RXBus.get().sendEvent(new TestEvent(), R.id.observer_key_1);
 => key bound bus usage, class bound observers WON't retrieve this event as well!

 // RXBus.get().sendEvent(new TestEvent(), R.id.observer_key_1, true);
 => key bound bus usage, with sendToDefaultBusAsWell = true, resulting in class bound observers WILL retrieve this event as well!

}

  
}
);

#####Advanced usage

You can pass in a Observable.Transformer to transform the observed event to whatever you want!

Observable.Transformer<TestEvent, TestEventTransformed> transformer = new Observable.Transformer<TestEvent, TestEventTransformed>() {

  @Override
  public Observable<TestEventTransformed> call(Observable<TestEvent> observable) {

return observable

  .map(new Func1<TestEvent, TestEventTransformed>() {

@Override

public TestEventTransformed call(TestEvent event) {

 return event.transform();

}

  
}
);

  
}
 
}
; RXBusBuilder.create(TestEvent.class)
  .withQueuing(rxBusQueue)
  .withBound(boundObject)
  .subscribe(new Action1<TestEventTransformed>() {

@Override

public void call(TestEventTransformed transformedEvent) {

}

  
}
, transformer);

#####Helper class - RXSubscriptionManager

This class helps to bind subscriptions to objects and offers an easy way to unsubscribe all subscriptions that are bound to an object at once. You can simply use is directly with the RXBusBuilder via the RXBusBuilder.withBound(boundObject). This will automatically add the subscription to the RXSubscriptionManager when you call `RXBusBuilder.subscribe(...).

Directly

Subscription subscription = RXBusBuilder.create(...).build();
 RXSubscriptionManager.addSubscription(activity, subscription);

RXBusBuilder

RXBusBuilder.create(...).withBound(activity);

Now you only have to make sure to unsubscribe again like following:

RXSubscriptionManager.unsubscribe(activity);

This will remove ANY subscription that is bound to activity and therefore this can be used in your activity's onDestroy method to make sure ALL subscriptions are unsubscribed at once and that you don't leak the activity.

Credits

The RxValve class is from this gist: https://gist.github.com/akarnokd/1c54e5a4f64f9b1e46bdcf62b4222f08

Resources

This library allows you to create a shadow effect for your layout based on your child.

Simple chart for Android.

Animations for support.v4.Fragment transitions.

A 3D Touch API for Android.

This library creates a notification with prompt. When user click, it opens a transparent Activity with request to permission.

Library to easily add swipe-to-open functionality to any RecyclerView.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes