package com.neoranga55.androidconfchangeloaders.presenters;

import android.os.AsyncTask;

/**
 * Created by neoranga on 28/03/2016.
 */
public class DemoPresenter implements DemoContract.UserActions<DemoContract.ViewActions> {

    private DemoContract.ViewActions mViewActions;
    private Thread mSlowTask;

    @Override
    public void onViewAttached(DemoContract.ViewActions viewActions) {
        mViewActions = viewActions;
    }

    @Override
    public void onViewDetached() {
        mViewActions = null;
    }

    @Override
    public void onDestroyed() {
        // Nothing to clean up
    }

    @Override
    public void loadButtonPressed() {
        mViewActions.showLoading();
        mSlowTask = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(30000);
                    mViewActions.showContentLoaded();
                } catch (InterruptedException ignored) {
                }
            }
        });
        mSlowTask.start();
    }

    @Override
    public void cancelButtonPressed() {
        if (mSlowTask != null) {
            mSlowTask.interrupt();
        }
        mViewActions.showCancelledRequest();
    }
}

@@ -58,38 +58,8 @@
 s;%0A%0A
-import android.os.AsyncTask;%0A%0A
 /**%0A
@@ -272,16 +272,121 @@
 lowTask;
+%0A    private boolean isLoading;%0A    private boolean isContentLoaded;%0A    private boolean isLoadCancelled;
 %0A%0A    @O
@@ -496,24 +496,276 @@
 iewActions;%0A
+        if (isLoading) %7B%0A            mViewActions.showLoading();%0A        %7D%0A        if (isContentLoaded) %7B%0A            mViewActions.showContentLoaded();%0A        %7D%0A        if (isLoadCancelled) %7B%0A            mViewActions.showCancelledRequest();%0A        %7D%0A
     %7D%0A%0A    @
@@ -972,32 +972,124 @@
 ttonPressed() %7B%0A
+        isLoading = true;%0A        isContentLoaded = false;%0A        isLoadCancelled = false;%0A
         mViewAct
@@ -1273,16 +1273,99 @@
 30000);%0A
+                    isLoading = false;%0A                    isContentLoaded = true;%0A
         
@@ -1402,32 +1402,70 @@
 ontentLoaded();%0A
+                    mSlowTask = null;%0A
                 
@@ -1629,32 +1629,59 @@
 ttonPressed() %7B%0A
+        isLoading = false;%0A
         if (mSlo
@@ -1693,24 +1693,97 @@
  != null) %7B%0A
+            isLoadCancelled = true;%0A            isContentLoaded = false;%0A
             
@@ -1805,34 +1805,58 @@
 rupt();%0A        
-%7D%0A
+    mSlowTask = null;%0A    
         mViewAct
@@ -1880,20 +1880,30 @@
 dRequest();%0A
+        %7D%0A
     %7D%0A%7D%0A

Save and restore state of view on attach to presenter