LiveDataBus使用方式

调用方式

订阅消息

  • observe 生命周期感知,不需要手动取消订阅
LiveDataBus.get()
    .with("key_name", String.class)
    .observe(this, new Observer() {
        @Override
        public void onChanged(@Nullable String s) {

        }
    });
  • observeForever 需要手动取消订阅
LiveDataBus.get()
    .with("key_name", String.class)
    .observeForever(observer);
LiveDataBus.get()
    .with("key_name", String.class)
    .removeObserver(observer);

发送消息

  • setValue 在主线程发送消息
LiveDataBus.get().with("key_name").setValue(value);
  • postValue 在后台线程发送消息,订阅者会在主线程收到消息
LiveDataBus.get().with("key_name").postValue(value);

Sticky模式

支持在注册订阅者的时候设置Sticky模式,这样订阅者可以接收到订阅之前发送的消息

  • observeSticky 生命周期感知,不需要手动取消订阅,Sticky模式
LiveDataBus.get()
        .with("sticky_key", String.class)
        .observeSticky(this, new Observer() {
            @Override
            public void onChanged(@Nullable String s) {

            }
        });
  • observeStickyForever 需要手动取消订阅,Sticky模式
LiveDataBus.get()
        .with("sticky_key", String.class)
        .observeStickyForever(observer);
LiveDataBus.get()
        .with("sticky_key", String.class)
        .removeObserver(observer);
public final class LiveDataBus {

    private final Map> bus;

    private LiveDataBus() {
        bus = new HashMap<>();
    }

    private static class SingletonHolder {
        private static final LiveDataBus DEFAULT_BUS = new LiveDataBus();
    }

    public static LiveDataBus get() {
        return SingletonHolder.DEFAULT_BUS;
    }

    public synchronized  BusMutableLiveData with(String key, Class type) {
        if (!bus.containsKey(key)) {
            bus.put(key, new BusMutableLiveData<>());
        }
        return (BusMutableLiveData) bus.get(key);
    }

    public BusMutableLiveData with(String key) {
        return with(key, Object.class);
    }

    private static class ObserverWrapper implements Observer {

        private Observer observer;

        public ObserverWrapper(Observer observer) {
            this.observer = observer;
        }

        @Override
        public void onChanged(@Nullable T t) {
            if (observer != null) {
                if (isCallOnObserve()) {
                    return;
                }
                observer.onChanged(t);
            }
        }

        private boolean isCallOnObserve() {
            StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
            if (stackTrace != null && stackTrace.length > 0) {
                for (StackTraceElement element : stackTrace) {
                    if ("android.arch.lifecycle.LiveData".equals(element.getClassName()) &&
                            "observeForever".equals(element.getMethodName())) {
                        return true;
                    }
                }
            }
            return false;
        }
    }

    public static class BusMutableLiveData extends MutableLiveData {



        private class PostValueTask implements Runnable {
            private Object newValue;

            public PostValueTask(@NonNull Object newValue) {
                this.newValue = newValue;
            }

            @Override
            public void run() {
                setValue((T) newValue);
            }
        }

        private Map observerMap = new HashMap<>();
        private Handler mainHandler = new Handler(Looper.getMainLooper());

        @Override
        public void postValue(T value) {
            mainHandler.post(new PostValueTask(value));
        }

        @Override
        public void observe(@NonNull LifecycleOwner owner, @NonNull Observer observer) {
            super.observe(owner, observer);
            try {
                hook(observer);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        public void observeSticky(@NonNull LifecycleOwner owner, @NonNull Observer observer) {
            super.observe(owner, observer);
        }

        @Override
        public void observeForever(@NonNull Observer observer) {
            if (!observerMap.containsKey(observer)) {
                observerMap.put(observer, new ObserverWrapper(observer));
            }
            super.observeForever(observerMap.get(observer));
        }

        public void observeStickyForever(@NonNull Observer observer) {
            super.observeForever(observer);
        }

        @Override
        public void removeObserver(@NonNull Observer observer) {
            Observer realObserver = null;
            if (observerMap.containsKey(observer)) {
                realObserver = observerMap.remove(observer);
            } else {
                realObserver = observer;
            }
            super.removeObserver(realObserver);
        }

        private void hook(@NonNull Observer observer) throws Exception {
            //get wrapper's version
            Class classLiveData = LiveData.class;
            Field fieldObservers = classLiveData.getDeclaredField("mObservers");
            fieldObservers.setAccessible(true);
            Object objectObservers = fieldObservers.get(this);
            Class classObservers = objectObservers.getClass();
            Method methodGet = classObservers.getDeclaredMethod("get", Object.class);
            methodGet.setAccessible(true);
            Object objectWrapperEntry = methodGet.invoke(objectObservers, observer);
            Object objectWrapper = null;
            if (objectWrapperEntry instanceof Map.Entry) {
                objectWrapper = ((Map.Entry) objectWrapperEntry).getValue();
            }
            if (objectWrapper == null) {
                throw new NullPointerException("Wrapper can not be bull!");
            }
            Class classObserverWrapper = objectWrapper.getClass().getSuperclass();
            Field fieldLastVersion = classObserverWrapper.getDeclaredField("mLastVersion");
            fieldLastVersion.setAccessible(true);
            //get livedata's version
            Field fieldVersion = classLiveData.getDeclaredField("mVersion");
            fieldVersion.setAccessible(true);
            Object objectVersion = fieldVersion.get(this);
            //set wrapper's version
            fieldLastVersion.set(objectWrapper, objectVersion);
        }
    }
}
 
  

                            
                        
                    
                    
                    

你可能感兴趣的:(android)