package pl.srw.template.presenter;

import java.util.Collection;

import javax.inject.Inject;

import pl.srw.mfvp.di.scope.RetainActivityScope;
import pl.srw.mfvp.presenter.MvpPresenter;
import pl.srw.template.model.Todo;
import pl.srw.template.presenter.task.GetTask;
import pl.srw.template.presenter.task.PushTask;
import timber.log.Timber;

/**
 * List view presenter. Lives as long as activity.
 */
@RetainActivityScope
public class ListViewPresenter extends MvpPresenter<ListViewPresenter.ListView>
        implements GetTask.Caller {

    private Collection<Todo> entries;
    private GetTask getTask;
    private PushTask pushTask;

    @Inject
    public ListViewPresenter(GetTask getTask, PushTask pushTask) {
        this.getTask = getTask;
        this.pushTask = pushTask;
    }

    @Override
    protected void onFirstBind() {
        getTask.execute(this);
    }

    @Override
    protected void onNewViewRestoreState() {
        displayEntries();
    }

    public void checkboxClickedFor(Todo todo) {
        pushTask.execute(new Todo(!todo.isDone(), todo.getText()));
    }

    @Override
    public void onDataRetrieved(Collection<Todo> todos) {
        entries = todos;
        displayEntries();
    }

    private void displayEntries() {
        Timber.d("Displaying cached data");
        present(new UIChange<ListView>() {
            @Override
            public void change(ListView view) {
                view.showEntries(entries);
            }
        });
    }

    /**
     * List view actions
     */
    public interface ListView {

        void showEntries(Collection<Todo> entries);
    }
}

@@ -86,16 +86,46 @@
 .Inject;
+%0Aimport javax.inject.Provider;
 %0A%0Aimport
@@ -643,24 +643,33 @@
 private 
+Provider%3C
 PushTask
  pushTas
@@ -652,32 +652,33 @@
 rovider%3CPushTask
+%3E
  pushTask;%0A%0A    
@@ -670,16 +670,17 @@
 pushTask
+s
 ;%0A%0A    @
@@ -732,24 +732,33 @@
 etTask, 
+Provider%3C
 PushTask
  pushTas
@@ -749,16 +749,17 @@
 PushTask
+%3E
  pushTas
@@ -759,16 +759,17 @@
 pushTask
+s
 ) %7B%0A    
@@ -817,16 +817,17 @@
 pushTask
+s
  = pushT
@@ -825,24 +825,25 @@
 s = pushTask
+s
 ;%0A    %7D%0A%0A   
@@ -981,24 +981,68 @@
 reState() %7B%0A
+        Timber.d(%22Displaying cached data%22);%0A
         disp
@@ -1122,24 +1122,31 @@
     pushTask
+s.get()
 .execute(new
@@ -1362,52 +1362,8 @@
 ) %7B%0A
-        Timber.d(%22Displaying cached data%22);%0A
     

fix crash on calling same task twice