mirror of
https://github.com/VibedByKaKi/alexandroid.git
synced 2026-11-14 01:47:17 +01:00
92 lines
3 KiB
Java
92 lines
3 KiB
Java
package androidx.lifecycle;
|
|
|
|
import androidx.annotation.CallSuper;
|
|
import androidx.annotation.MainThread;
|
|
import androidx.arch.core.internal.SafeIterableMap;
|
|
import java.util.Iterator;
|
|
import java.util.Map;
|
|
import rx.Single$$ExternalSyntheticBUOutline0;
|
|
|
|
/* compiled from: r8-map-id-b9432e0f623b4a4bff4ba50a8a24c4aafad2c1e11092747b9fb5e6a22af5a940 */
|
|
/* loaded from: classes14.dex */
|
|
public class MediatorLiveData<T> extends MutableLiveData<T> {
|
|
private SafeIterableMap<LiveData<?>, Source<?>> mSources;
|
|
|
|
/* compiled from: r8-map-id-b9432e0f623b4a4bff4ba50a8a24c4aafad2c1e11092747b9fb5e6a22af5a940 */
|
|
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;
|
|
}
|
|
|
|
@Override // androidx.lifecycle.Observer
|
|
public void onChanged(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);
|
|
}
|
|
}
|
|
|
|
public MediatorLiveData() {
|
|
this.mSources = new SafeIterableMap<>();
|
|
}
|
|
|
|
@MainThread
|
|
public <S> void addSource(LiveData<S> liveData, Observer<? super S> observer) {
|
|
if (liveData == null) {
|
|
Single$$ExternalSyntheticBUOutline0.m$1("source cannot be null");
|
|
return;
|
|
}
|
|
Source<?> source = new Source<>(liveData, observer);
|
|
Source<?> putIfAbsent = this.mSources.putIfAbsent(liveData, source);
|
|
if (putIfAbsent != null && putIfAbsent.mObserver != observer) {
|
|
Single$$ExternalSyntheticBUOutline0.m$3("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<?>>> it2 = this.mSources.iterator();
|
|
while (it2.hasNext()) {
|
|
it2.next().getValue().plug();
|
|
}
|
|
}
|
|
|
|
@Override // androidx.lifecycle.LiveData
|
|
@CallSuper
|
|
public void onInactive() {
|
|
Iterator<Map.Entry<LiveData<?>, Source<?>>> it2 = this.mSources.iterator();
|
|
while (it2.hasNext()) {
|
|
it2.next().getValue().unplug();
|
|
}
|
|
}
|
|
|
|
@MainThread
|
|
public <S> void removeSource(LiveData<S> liveData) {
|
|
Source<?> remove = this.mSources.remove(liveData);
|
|
if (remove != null) {
|
|
remove.unplug();
|
|
}
|
|
}
|
|
|
|
public MediatorLiveData(T t) {
|
|
super(t);
|
|
this.mSources = new SafeIterableMap<>();
|
|
}
|
|
}
|