package com.android.app_base.utils.rxbus;
|
|
import androidx.lifecycle.LifecycleOwner;
|
import com.uber.autodispose.AutoDispose;
|
import com.uber.autodispose.ObservableSubscribeProxy;
|
import com.uber.autodispose.android.lifecycle.AndroidLifecycleScopeProvider;
|
|
import java.util.Map;
|
import java.util.concurrent.ConcurrentHashMap;
|
|
import io.reactivex.Observable;
|
import io.reactivex.subjects.PublishSubject;
|
import io.reactivex.subjects.Subject;
|
|
/**
|
* @author Ljj
|
* @date 2023.05.18. 21:54
|
* @desc RxBus
|
*/
|
public class RxBus {
|
private volatile static RxBus mDefaultInstance;
|
//事件总线
|
private final Subject<Object> mBus;
|
//粘性事件存储
|
private final Map<Class<?>, Object> mStickyEventMap;
|
|
private RxBus() {
|
mBus = PublishSubject.create().toSerialized();
|
mStickyEventMap = new ConcurrentHashMap<>();
|
}
|
|
public static RxBus getInstance() {
|
if (mDefaultInstance == null) {
|
synchronized (RxBus.class) {
|
if (mDefaultInstance == null) {
|
mDefaultInstance = new RxBus();
|
}
|
}
|
}
|
return mDefaultInstance;
|
}
|
|
/**
|
* 发送事件
|
*/
|
public void post(Object event) {
|
mBus.onNext(event);
|
}
|
|
/**
|
* 使用 AutoDispose解决RxJava引起的内存泄漏
|
* 返回 ObservableSubscribeProxy,实际用处和Observable一样
|
*
|
*/
|
public <T> ObservableSubscribeProxy<T> toObservable(LifecycleOwner owner, final Class<T> eventType) {
|
AndroidLifecycleScopeProvider provider = AndroidLifecycleScopeProvider.from(owner);
|
return mBus.ofType(eventType).as(AutoDispose.autoDisposable(provider));
|
}
|
|
|
/**
|
* 判断是否有订阅者
|
*/
|
public boolean hasObservers() {
|
return mBus.hasObservers();
|
}
|
|
public void reset() {
|
mDefaultInstance = null;
|
}
|
|
/**
|
* 发送一个新Sticky事件
|
*/
|
public void postSticky(Object event) {
|
synchronized (mStickyEventMap) {
|
mStickyEventMap.put(event.getClass(), event);
|
}
|
post(event);
|
}
|
|
/**
|
* 根据eventType获取Sticky事件,如果没有则返回null
|
*/
|
public <T> ObservableSubscribeProxy<T> toObservableSticky(LifecycleOwner owner, final Class<T> eventType) {
|
synchronized (mStickyEventMap) {
|
AndroidLifecycleScopeProvider provider = AndroidLifecycleScopeProvider.from(owner);
|
Observable<T> observable = mBus.ofType(eventType);
|
final Object event = mStickyEventMap.get(eventType);
|
if (event != null) {
|
return observable.mergeWith(Observable.create(subscriber -> subscriber.onNext(eventType.cast(event)))).as(AutoDispose.autoDisposable(provider));
|
} else {
|
return observable.as(AutoDispose.autoDisposable(provider));
|
}
|
}
|
}
|
|
/**
|
* 根据eventType获取Sticky事件
|
*/
|
public <T> T getStickyEvent(Class<T> eventType) {
|
synchronized (mStickyEventMap) {
|
return eventType.cast(mStickyEventMap.get(eventType));
|
}
|
}
|
|
/**
|
* 移除指定eventType的Sticky事件
|
*/
|
public <T> T removeStickyEvent(Class<T> eventType) {
|
synchronized (mStickyEventMap) {
|
return eventType.cast(mStickyEventMap.remove(eventType));
|
}
|
}
|
|
/**
|
* 移除所有的Sticky事件
|
*/
|
public void removeAllStickyEvents() {
|
synchronized (mStickyEventMap) {
|
mStickyEventMap.clear();
|
}
|
}
|
|
}
|