Skip to content

Instantly share code, notes, and snippets.

@cesclong
Created May 7, 2023 15:39
Show Gist options
  • Save cesclong/b9d618332ed78211ed499d25c97b1702 to your computer and use it in GitHub Desktop.
Save cesclong/b9d618332ed78211ed499d25c97b1702 to your computer and use it in GitHub Desktop.
LiveEventBus.java
package com.pplive.archcomponent.jetpack.livedata;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.Observer;
import android.os.Handler;
import android.os.Looper;
import androidx.annotation.NonNull;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
/**
* Created by hailiangliao on 2018/7/4.
*/
public final class LiveEventBus {
private final Map<String, BusLiveEvent<Object>> bus;
private LiveEventBus() {
bus = new HashMap<>();
}
private static class SingletonHolder {
private static final LiveEventBus DEFAULT_BUS = new LiveEventBus();
}
public static LiveEventBus get() {
return SingletonHolder.DEFAULT_BUS;
}
private boolean lifecycleObserverAlwaysActive = true;
public synchronized <T> Observable<T> with(String key, Class<T> type) {
if (!bus.containsKey(key)) {
bus.put(key, new BusLiveEvent<>(key));
}
return (Observable<T>) bus.get(key);
}
public Observable<Object> with(String key) {
return with(key, Object.class);
}
public void lifecycleObserverAlwaysActive(boolean active) {
lifecycleObserverAlwaysActive = active;
}
public interface Observable<T> {
void setValue(T value);
void postValue(T value);
void postValueDelay(T value, long delay);
void postValueDelay(T value, long delay, TimeUnit unit);
void observe(@NonNull LifecycleOwner owner, @NonNull Observer<T> observer);
void observeSticky(@NonNull LifecycleOwner owner, @NonNull Observer<T> observer);
void observeForever(@NonNull Observer<T> observer);
void observeStickyForever(@NonNull Observer<T> observer);
void removeObserver(@NonNull Observer<T> observer);
}
private class BusLiveEvent<T> extends LiveEvent<T> implements Observable<T> {
private class PostValueTask implements Runnable {
private Object newValue;
public PostValueTask(@NonNull Object newValue) {
this.newValue = newValue;
}
@Override
public void run() {
setValue((T) newValue);
}
}
@NonNull
private final String key;
private Handler mainHandler = new Handler(Looper.getMainLooper());
private BusLiveEvent(String key) {
this.key = key;
}
@Override
protected Lifecycle.State observerActiveLevel() {
return lifecycleObserverAlwaysActive ? Lifecycle.State.CREATED : Lifecycle.State.STARTED;
}
@Override
public void postValueDelay(T value, long delay) {
mainHandler.postDelayed(new PostValueTask(value), delay);
}
@Override
public void postValueDelay(T value, long delay, TimeUnit unit) {
postValueDelay(value, TimeUnit.MILLISECONDS.convert(delay, unit));
}
@Override
public void removeObserver(@NonNull Observer<T> observer) {
super.removeObserver(observer);
if (!hasObservers()) {
LiveEventBus.get().bus.remove(key);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment