discord-jadx/app/src/main/java/androidx/lifecycle/MediatorLiveData.java

77 lines
2.5 KiB
Java

package androidx.lifecycle;
import androidx.annotation.CallSuper;
import androidx.annotation.MainThread;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.arch.core.internal.SafeIterableMap;
import java.util.Iterator;
import java.util.Map;
public class MediatorLiveData<T> extends MutableLiveData<T> {
private SafeIterableMap<LiveData<?>, Source<?>> mSources = new SafeIterableMap<>();
public static class Source<V> implements Observer<V> {
public final LiveData<V> mLiveData;
public final Observer<? super V> mObserver;
public int mVersion = -1;
public Source(LiveData<V> liveData, Observer<? super V> observer) {
this.mLiveData = liveData;
this.mObserver = observer;
}
@Override // androidx.lifecycle.Observer
public void onChanged(@Nullable V v) {
if (this.mVersion != this.mLiveData.getVersion()) {
this.mVersion = this.mLiveData.getVersion();
this.mObserver.onChanged(v);
}
}
public void plug() {
this.mLiveData.observeForever(this);
}
public void unplug() {
this.mLiveData.removeObserver(this);
}
}
@MainThread
public <S> void addSource(@NonNull LiveData<S> liveData, @NonNull Observer<? super S> observer) {
Source<?> source = new Source<>(liveData, observer);
Source<?> putIfAbsent = this.mSources.putIfAbsent(liveData, source);
if (putIfAbsent != null && putIfAbsent.mObserver != observer) {
throw new IllegalArgumentException("This source was already added with the different observer");
} else if (putIfAbsent == null && hasActiveObservers()) {
source.plug();
}
}
@Override // androidx.lifecycle.LiveData
@CallSuper
public void onActive() {
Iterator<Map.Entry<LiveData<?>, Source<?>>> it = this.mSources.iterator();
while (it.hasNext()) {
it.next().getValue().plug();
}
}
@Override // androidx.lifecycle.LiveData
@CallSuper
public void onInactive() {
Iterator<Map.Entry<LiveData<?>, Source<?>>> it = this.mSources.iterator();
while (it.hasNext()) {
it.next().getValue().unplug();
}
}
@MainThread
public <S> void removeSource(@NonNull LiveData<S> liveData) {
Source<?> remove = this.mSources.remove(liveData);
if (remove != null) {
remove.unplug();
}
}
}