【Potatotips #26】Replace EventBus with RxJava/RxAndroid

17
Replace EventBus with RxJava/RxAndroid Hiroyuki Kusu ( @hkusu_ ) 株株株株株株株 2016/02/17 potatotips #26

Transcript of 【Potatotips #26】Replace EventBus with RxJava/RxAndroid

Page 1: 【Potatotips #26】Replace EventBus with RxJava/RxAndroid

Replace EventBus with RxJava/RxAndroid

Hiroyuki Kusu ( @hkusu_ )株式会社ゆめみ

2016/02/17 potatotips #26

Page 2: 【Potatotips #26】Replace EventBus with RxJava/RxAndroid

Android 以外も色々やります。最近は主に JavaScript( Node.js + ES2015 ) を書いてます。

Page 3: 【Potatotips #26】Replace EventBus with RxJava/RxAndroid

greenrobot/EventBus

⇒ Replace with RxJava(RxAndroid)

Page 4: 【Potatotips #26】Replace EventBus with RxJava/RxAndroid

app/build.gradledependencies { // ... compile 'io.reactivex:rxjava:1.1.0' compile 'io.reactivex:rxandroid:1.1.0'}

Page 5: 【Potatotips #26】Replace EventBus with RxJava/RxAndroid

public class RxEventBus { private final Subject<Object, Object> subject = new SerializedSubject<>(PublishSubject.create());

public <T> Subscription onEvent(Class<T> clazz, Action1<T> handler) { return subject .ofType(clazz) .subscribe(handler); }

public void post(Object event) { subject.onNext(event); }}

RxEventBus.java

Page 6: 【Potatotips #26】Replace EventBus with RxJava/RxAndroid

public class RxEventBusProvider { private static final RxEventBus rxEventBus = new RxEventBus();

public static RxEventBus provide(){ return rxEventBus; }}

RxEventBusProvider.java

※ providing the singleton instance

Page 7: 【Potatotips #26】Replace EventBus with RxJava/RxAndroid

RxEventBus rxEventBus = RxEventBusProvider.provide();

rxEventBus.post(new ChangedEvent());

rxEventBus.onEvent(ChangedEvent.class, event -> { // something..});

※ applying Java8 & Retrolambda

Getting instance

Publish

Subscribe

Page 8: 【Potatotips #26】Replace EventBus with RxJava/RxAndroid

Subscription subscription;

// ...

subscription = rxEventBus.onEvent(ChangedEvent.class, event -> { // something..});

// ...

subscription.unsubscribe();

Unsubscribe ( order not to leak)

Page 9: 【Potatotips #26】Replace EventBus with RxJava/RxAndroid

Tips

Page 10: 【Potatotips #26】Replace EventBus with RxJava/RxAndroid

public class RxEventBus { private final Subject<Object, Object> subject = new SerializedSubject<>(PublishSubject.create());

public <T> Subscription onEvent(Class<T> clazz, Action1<T> handler) { return subject .ofType(clazz) .subscribe(handler); }

public <T> Subscription onEventMainThread(Class<T> clazz, Action1<T> handler) { return subject .ofType(clazz) .observeOn(AndroidSchedulers.mainThread()) .subscribe(handler); }

public void post(Object event) { subject.onNext(event); }}

Subscribing in the main thread

Page 11: 【Potatotips #26】Replace EventBus with RxJava/RxAndroid

public class ChangedEvent { private int id;

public ChangedEvent(int id) { this.id = id; }

public int getId() { return id; }}

rxEventBus.post(new ChangedEvent(99));

rxEventBus.onEvent(ChangedEvent.class, event -> { int id = event.getId(); // something..});

Getting a value from the event

Page 12: 【Potatotips #26】Replace EventBus with RxJava/RxAndroid

rxEventBus.onEvent(TodoRepository.ChangedEvent.class, event -> { // something.. });

Create event as an inner class of the class to publish (or subscribe) the event.

Because, it is unclear to post the event from where(or to where).

Page 13: 【Potatotips #26】Replace EventBus with RxJava/RxAndroid

Appendix

Providing a singleton instancein Dagger2

Page 14: 【Potatotips #26】Replace EventBus with RxJava/RxAndroid

@Modulepublic class AppModule { @Provides @Singleton public RxEventBus provideRxEventBus(){ return new RxEventBus(); }}

AppModule.java

@Component(modules = AppModule.class)public interface AppComponent { RxEventBus provideRxEventBus();}

AppComponent.java

Page 15: 【Potatotips #26】Replace EventBus with RxJava/RxAndroid

Getting instanceAppComponent appComponent = DaggerAppComponent.create();

// ...

RxEventBus rxEventBus = appComponent.provideRxEventBus();

In this example, Dagger2 looks redundant , but redundant description can be reduced by @inject annotation to the constructor.See https://github.com/hkusu/android-dagger-rxjava-sample .

Page 16: 【Potatotips #26】Replace EventBus with RxJava/RxAndroid

Sample code

hkusu/android-dagger-rxjava-sample

Page 17: 【Potatotips #26】Replace EventBus with RxJava/RxAndroid

Thanks!