Files
rr3-apk/decompiled/sources/androidx/lifecycle/MediatorLiveData.java
Daniel Elliott f9d20bb3fc Add decompiled APK source code (JADX)
- 28,932 files
- Full Java source code
- Smali files
- Resources

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-02-18 14:52:23 -08:00

92 lines
2.8 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;
/* loaded from: classes.dex */
public class MediatorLiveData<T> extends MutableLiveData<T> {
private SafeIterableMap<LiveData<?>, Source<?>> mSources;
public MediatorLiveData() {
this.mSources = new SafeIterableMap<>();
}
public MediatorLiveData(T t) {
super(t);
this.mSources = new SafeIterableMap<>();
}
@MainThread
public <S> void addSource(@NonNull LiveData<S> liveData, @NonNull Observer<? super S> observer) {
if (liveData == null) {
throw new NullPointerException("source cannot be null");
}
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");
}
if (putIfAbsent == null && hasActiveObservers()) {
source.plug();
}
}
@MainThread
public <S> void removeSource(@NonNull LiveData<S> liveData) {
Source<?> remove = this.mSources.remove(liveData);
if (remove != null) {
remove.unplug();
}
}
@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();
}
}
public static class Source<V> implements Observer<V> {
final LiveData<V> mLiveData;
final Observer<? super V> mObserver;
int mVersion = -1;
public Source(LiveData<V> liveData, Observer<? super V> observer) {
this.mLiveData = liveData;
this.mObserver = observer;
}
public void plug() {
this.mLiveData.observeForever(this);
}
public void unplug() {
this.mLiveData.removeObserver(this);
}
@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);
}
}
}
}