Rabbit-R1/original r1/java/sources/androidx/lifecycle/MediatorLiveData.java
2024-05-21 17:08:36 -04:00

86 lines
2.7 KiB
Java

package androidx.lifecycle;
import androidx.arch.core.internal.SafeIterableMap;
import java.util.Iterator;
import java.util.Map;
/* loaded from: classes2.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<>();
}
public <S> void addSource(LiveData<S> liveData, 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();
}
}
public <S> void removeSource(LiveData<S> liveData) {
Source<?> remove = this.mSources.remove(liveData);
if (remove != null) {
remove.unplug();
}
}
/* JADX INFO: Access modifiers changed from: protected */
@Override // androidx.lifecycle.LiveData
public void onActive() {
Iterator<Map.Entry<LiveData<?>, Source<?>>> it = this.mSources.iterator();
while (it.hasNext()) {
it.next().getValue().plug();
}
}
/* JADX INFO: Access modifiers changed from: protected */
@Override // androidx.lifecycle.LiveData
public void onInactive() {
Iterator<Map.Entry<LiveData<?>, Source<?>>> it = this.mSources.iterator();
while (it.hasNext()) {
it.next().getValue().unplug();
}
}
/* loaded from: classes2.dex */
private static class Source<V> implements Observer<V> {
final LiveData<V> mLiveData;
final Observer<? super V> mObserver;
int mVersion = -1;
Source(LiveData<V> liveData, Observer<? super V> observer) {
this.mLiveData = liveData;
this.mObserver = observer;
}
void plug() {
this.mLiveData.observeForever(this);
}
void unplug() {
this.mLiveData.removeObserver(this);
}
@Override // androidx.lifecycle.Observer
public void onChanged(V v) {
if (this.mVersion != this.mLiveData.getVersion()) {
this.mVersion = this.mLiveData.getVersion();
this.mObserver.onChanged(v);
}
}
}
}