简要说明
关于RxJava:https://github.com/ReactiveX/RxJava
关于EventBus:https://github.com/greenrobot/EventBus
由于最近工程中依赖于RxAndroid与RxJava,因此以RxJava模式实现EventBus相同的API,移除重复功能的依赖,达到精简工程的目的。
第一步:构造单例
- 在EventBus中
EventBus.getDefault();
- 构造RxBus相同的API
public class RxBus {
private static volatile RxBus instance;
private RxBus() {}
public static RxBus getDefault() {
if (instance == null) {
synchronized (RxBus.class) {
if (instance == null) {
instance = new RxBus();
}
}
}
return instance;
}
- 在RxBus中
RxBus.getDefault();
第二步:事件注解
- 在EventBus中
@Subscribe
public void onMessageEvent(MessageEvent event) {/* Do something */};
- 构造RxBus相同的API
public class RxBus {
//省略第一步...
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Subscribe {}
}
- 在RxBus中
@RxBus.Subscribe
public void onMessageEvent(MessageEvent event) {/* Do something */};
第三步:订阅事件
- 在EventBus中
@Override
public void onStart() {
super.onStart();
EventBus.getDefault().register(this);
}
- 构造RxBus相同的API
public class RxBus {
//省略第一步...
private final Subject<Object, Object> bus = new SerializedSubject<>(PublishSubject.create());
private final ArrayMap<String, List<Subscription>> subscriptionArray = new ArrayMap<>();
public void register(final Object observable) {
boolean isRegisterSuccess = false;
final String subscriptionKey = observable.getClass().getName();
//反射获取方法列表
Method[] methods = observable.getClass().getMethods();
for (Method method : methods) {
//如果方法不存在Subscribe注解则不做处理
if (!method.isAnnotationPresent(Subscribe.class)) continue;
final Method subscriptionMethod = method;
//获取方法参数类型,即:MessageEvent.class
Class<?> key = method.getParameterTypes()[0];
//订阅MessageEvent.class类型实例,ofType作用已filter相近
Subscription subscription = bus.ofType(key).subscribe(new Action1<Object>() {
@Override
public void call(Object value) {
try {
//反射赋值,即传递事件值
subscriptionMethod.setAccessible(true);
subscriptionMethod.invoke(observable, value);
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(subscriptionKey + " isn't allowed to register!");
}
}
});
//存储订阅实例列表,用于后续解除注册
List<Subscription> subscriptions;
if (subscriptionArray.containsKey(subscriptionKey)) {
subscriptions = subscriptionArray.get(subscriptionKey);
} else {
subscriptions = new ArrayList<>();
}
subscriptions.add(subscription);
subscriptionArray.put(subscriptionKey, subscriptions);
isRegisterSuccess = true;
}
//如果注册的类中不存在Subscribe注解方法,则抛出异常提醒
if (!isRegisterSuccess)
throw new RuntimeException(subscriptionKey + " has no any RxBuxSubscribe Event!");
}
//省略第二步...
}
- 在RxBus中
@Override
public void onStart() {
super.onStart();
RxBus.getDefault().register(this);
}
取消订阅
- 在EventBus中
@Override
public void onStop() {
super.onStop();
EventBus.getDefault().unregister(this);
}
- 构造RxBus相同的API
public class RxBus {
//省略第一步...
//省略第三步...
public void unregister(Object observable) {
String subscriptionKey = observable.getClass().getName();
if (!subscriptionArray.containsKey(subscriptionKey)) return;
List<Subscription> subscriptions = subscriptionArray.get(subscriptionKey);
for (Subscription subscription : subscriptions) {
//如果已订阅,则取消订阅
if (!subscription.isUnsubscribed()) subscription.unsubscribe();
}
subscriptionArray.remove(subscriptionKey);
}
//省略第二步...
}
- 在RxBus中
@Override
public void onStop() {
super.onStop();
RxBus.getDefault().unregister(this);
}
发送事件
- 在EventBus中
EventBus.getDefault().post(new MessageEvent());
- 构造RxBus相同的API
public class RxBus {
//省略第一步...
//省略第三步...
//省略第四步...
public void post (Object event) {
bus.onNext(event);
}
//省略第二步...
}
- 在RxBus中
RxBus.getDefault().post(new MessageEvent());
关于ThreadMode
在订阅时根据需要使用RxJava的Schedulers即可。