package org.lagonette.app.app.arch;


import android.arch.lifecycle.LifecycleOwner;
import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.Observer;
import android.support.v4.util.ArrayMap;

import java.util.concurrent.atomic.AtomicBoolean;

public class LiveEvent<T> extends LiveData<T> {

    private AtomicBoolean mPending = new AtomicBoolean(false);

    private ArrayMap<Observer<T>, Observer<T>> mObservers = new ArrayMap<>();

    @Override
    public void observe(LifecycleOwner owner, Observer<T> observer) {
        Observer<T> wrappedObserver = it -> {
            if (mPending.compareAndSet(true, false)) {
                observer.onChanged(it);
            }
        };
        mObservers.put(observer, wrappedObserver);
        super.observe(owner, wrappedObserver);
    }

    @Override
    public void removeObserver(Observer<T> observer) {
        super.removeObserver(mObservers.get(observer));
    }

    @Override
    protected void setValue(T value) {
        mPending.set(true);
        super.setValue(value);
    }

    //TODO see https://github.com/googlesamples/android-architecture-components/issues/63

}
@@ -31,16 +31,106 @@
 arch;%0A%0A%0A
+import android.arch.lifecycle.Lifecycle;%0Aimport android.arch.lifecycle.LifecycleObserver;%0A
 import a
@@ -879,32 +879,163 @@
 server);%0A    %7D%0A%0A
+    @Override%0A    public void observeForever(Observer%3CT%3E observer) %7B%0A        //TODO%0A        super.observeForever(observer);%0A    %7D%0A%0A
     @Override%0A  
@@ -1079,32 +1079,32 @@
 %3CT%3E observer) %7B%0A
-
         super.re
@@ -1143,16 +1143,53 @@
 rver));%0A
+        mObservers.remove(observer);%0A
     %7D%0A%0A 
@@ -1299,23 +1299,70 @@
 value);%0A
-
     %7D%0A%0A
+    //TODO Use one mPending by LifecycleOwner%0A%0A
     //TO

Update TODO