package uk.co.ribot.androidboilerplate.util;

import javax.inject.Inject;
import javax.inject.Singleton;

import rx.Observable;
import rx.functions.Func1;
import rx.subjects.PublishSubject;

/**
 * A simple event bus built with RxJava
 */
@Singleton
public class RxEventBus {

    private final PublishSubject<Object> mBusSubject;

    @Inject
    public RxEventBus() {
        mBusSubject = PublishSubject.create();
    }

    /**
     * Posts an object (usually an Event) to the bus
     */
    public void post(Object event) {
        mBusSubject.onNext(event);
    }

    /**
     * Observable that will emmit everything posted to the event bus.
     */
    public Observable<Object> observable() {
        return mBusSubject;
    }

    /**
     * Observable that only emits events of a specific class.
     * Use this if you only want to subscribe to one type of events.
     */
    public <T> Observable<T> filteredObservable(final Class<T> eventClass) {
        return mBusSubject.filter(new Func1<Object, Boolean>() {
            @Override
            public Boolean call(Object event) {
                return eventClass.isInstance(event);
            }
        }).map(new Func1<Object, T>() {
            //Safe to cast because of the previous filter
            @SuppressWarnings("unchecked")
            @Override
            public T call(Object event) {
                return (T) event;
            }
        });
    }
}
@@ -125,35 +125,8 @@
 le;%0A
-import rx.functions.Func1;%0A
 impo
@@ -959,445 +959,25 @@
 ect.
-filter(new Func1%3CObject, Boolean%3E() %7B%0A            @Override%0A            public Boolean call(Object event) %7B%0A                return eventClass.isInstance(event);%0A            %7D%0A        %7D).map(new Func1%3CObject, T%3E() %7B%0A            //Safe to cast because of the previous filter%0A            @SuppressWarnings(%22unchecked%22)%0A            @Override%0A            public T call(Object event) %7B%0A                return (T) event;%0A            %7D%0A        %7D
+ofType(eventClass
 );%0A 

use rxjava inner ofType (#56)