package com.bytedance.ies.sdk.widgets;

import X.0CH;
import X.0CM;
import X.1Bn;
import com.bytedance.covode.number.Covode;
import java.util.HashMap;
import java.util.Map;

public class NextLiveData<T> extends 1Bn<T> {
    public int mLatestVersion = -1;
    public Map<0CM, NextObserver> nextObserverMap = new HashMap();

    static {
        Covode.recordClassIndex(30535);
    }

    public void observeForever(0CM<T> r2) {
        observeForever(r2, false);
    }

    public void setValue(T t) {
        this.mLatestVersion++;
        NextLiveData.super.setValue(t);
    }

    public class NextObserver<T> implements 0CM<T> {
        public int initVersion;
        public boolean notifyWhenObserve;
        public 0CM<T> observer;

        static {
            Covode.recordClassIndex(30536);
        }

        public void onChanged(T t) {
            if (this.notifyWhenObserve || this.initVersion < NextLiveData.this.mLatestVersion) {
                this.observer.onChanged(t);
            }
        }

        public NextObserver(int i, 0CM<T> r3, boolean z) {
            this.initVersion = i;
            this.observer = r3;
            this.notifyWhenObserve = z;
        }
    }

    public void removeObserver(0CM<T> r4) {
        NextObserver remove = this.nextObserverMap.remove(r4);
        if (remove != null) {
            NextLiveData.super.removeObserver(remove);
            return;
        }
        NextLiveData.super.removeObserver(r4);
        if (r4 instanceof NextObserver) {
            for (Map.Entry next : this.nextObserverMap.entrySet()) {
                if (r4.equals(next.getValue())) {
                    Object key = next.getKey();
                    if (key != null) {
                        this.nextObserverMap.remove(key);
                        return;
                    }
                    return;
                }
            }
        }
    }

    public void observe(0CH r2, 0CM<T> r3) {
        observe(r2, r3, false);
    }

    public void observeForever(0CM<T> r3, boolean z) {
        if (!this.nextObserverMap.containsKey(r3)) {
            NextObserver nextObserver = new NextObserver(this.mLatestVersion, r3, z);
            this.nextObserverMap.put(r3, nextObserver);
            NextLiveData.super.observeForever(nextObserver);
        }
    }

    public void observe(0CH r3, 0CM<T> r4, boolean z) {
        if (!this.nextObserverMap.containsKey(r4)) {
            NextObserver nextObserver = new NextObserver(this.mLatestVersion, r4, z);
            this.nextObserverMap.put(r4, nextObserver);
            NextLiveData.super.observe(r3, nextObserver);
        }
    }
}
