package com.nhpatt.asde.mvp.presenters;

import android.os.Bundle;
import android.support.annotation.CheckResult;
import android.support.annotation.NonNull;

import com.trello.rxlifecycle.ActivityEvent;
import com.trello.rxlifecycle.ActivityLifecycleProvider;
import com.trello.rxlifecycle.RxLifecycle;

import rx.Observable;
import rx.subjects.BehaviorSubject;

public class PresenterImpl implements Presenter, ActivityLifecycleProvider {

    private final BehaviorSubject<ActivityEvent> lifecycleSubject = BehaviorSubject.create();

    @Override
    @NonNull
    @CheckResult
    public final Observable<ActivityEvent> lifecycle() {
        return lifecycleSubject.asObservable();
    }

    @Override
    @NonNull
    @CheckResult
    public final <T> Observable.Transformer<T, T> bindUntilEvent(@NonNull ActivityEvent event) {
        return RxLifecycle.bindUntilEvent(lifecycleSubject, event);
    }

    @Override
    @NonNull
    @CheckResult
    public final <T> Observable.Transformer<T, T> bindToLifecycle() {
        return RxLifecycle.bindActivity(lifecycleSubject);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        lifecycleSubject.onNext(ActivityEvent.CREATE);
    }

    @Override
    public void onStart() {
        lifecycleSubject.onNext(ActivityEvent.START);
    }

    @Override
    public void onResume() {
        lifecycleSubject.onNext(ActivityEvent.RESUME);
    }

    @Override
    public void onPause() {
        lifecycleSubject.onNext(ActivityEvent.PAUSE);
    }

    @Override
    public void onStop() {
        lifecycleSubject.onNext(ActivityEvent.STOP);
    }

    @Override
    public void onDestroy() {
        lifecycleSubject.onNext(ActivityEvent.DESTROY);
    }

}

@@ -151,16 +151,71 @@
 nNull;%0A%0A
+import com.nhpatt.asde.mvp.activities.PersistedObject;%0A
 import c
@@ -363,21 +363,129 @@
 ort 
-rx.Observable
+java.util.HashMap;%0A%0Aimport rx.Observable;%0Aimport rx.android.schedulers.AndroidSchedulers;%0Aimport rx.schedulers.Schedulers
 ;%0Aim
@@ -690,16 +690,832 @@
 reate();
+%0A    private PersistedObject persistedObject = new PersistedObject();%0A%0A    protected Observable getCachedBackgroundObservable(String key, Observable observable) %7B%0A        if (getPersistedObject().getInteractors().containsKey(key)) %7B%0A            return getPersistedObject().getInteractors().get(key);%0A        %7D else %7B%0A            Observable obs = observable.compose(background());%0A            getPersistedObject().getInteractors().put(key, obs);%0A            return obs;%0A        %7D%0A    %7D%0A%0A    @NonNull%0A    @CheckResult%0A    protected %3CT%3E Observable.Transformer%3CT, T%3E background() %7B%0A        return obs -%3E obs%0A                .subscribeOn(Schedulers.io())%0A                .observeOn(AndroidSchedulers.mainThread())%0A                .compose(bindToLifecycle())%0A                .replay()%0A                .autoConnect();%0A    %7D
 %0A%0A    @O
@@ -2709,10 +2709,355 @@
 %0A    %7D%0A%0A
+    @Override%0A    public PersistedObject getPersistedObject() %7B%0A        if (persistedObject == null) %7B%0A            persistedObject = new PersistedObject();%0A        %7D%0A        return persistedObject;%0A    %7D%0A%0A    @Override%0A    public void setPersistedObject(PersistedObject persistedObject) %7B%0A        this.persistedObject = persistedObject;%0A    %7D%0A%0A%0A
 %7D%0A

implement storing of the interactors