package com.laxture.skeleton.adapter;

import android.annotation.SuppressLint;

import com.laxture.lib.task.TaskException;
import com.laxture.lib.task.TaskListener;
import com.laxture.lib.task.TaskManager;
import com.laxture.skeleton.request.AbstractApiTask;

import java.util.List;

@SuppressLint("ViewConstructor")
public abstract class ApiPaginalAdapter<T, ApiResult> extends PaginalAdapter<T>
        implements TaskListener.TaskFinishedListener<ApiResult>, TaskListener.TaskFailedListener<ApiResult> {

    @Override
    public void onTaskFinished(ApiResult result) {
        List<T> resultList = convertApiResultToData(result);

        switch (loadAction) {
            case Refresh:
                reloadData(true);
                onRefreshFromServerFinished(result);
                break;

            case FetchMore:
                appendData(resultList);
                checkHasMoreAfterFetchServerData(resultList);
                break;

            default: // do nothing
        }
        stopLoadingView(false);
        mLoading = false;
    }

    @Override
    public void onTaskFailed(ApiResult result, TaskException ex) {
        stopLoadingView(false);
        onLoadFailed(ex.getMessage());
        mLoading = false;
    }

    //*************************************************************************
    // Abstract Method
    //*************************************************************************

    protected abstract AbstractApiTask<ApiResult> createRefreshApiTask();

    protected abstract AbstractApiTask<ApiResult> createFetchMoreApiTask();

    protected abstract List<T> convertApiResultToData(ApiResult result);

    protected abstract void onLoadFailed(String errorMessage);

    protected void onRefreshFromServerStart() {}

    protected void onRefreshFromServerFinished(ApiResult result) {}

    //*************************************************************************
    // Internal Stuff
    //*************************************************************************

    @Override
    protected void refresh() {
        AbstractApiTask<ApiResult> apiTask = createRefreshApiTask();
        apiTask.addFinishedListener(this);
        apiTask.addFailedListener(this);
        onRefreshFromServerStart();
        TaskManager.runImmediately(apiTask);
    }

    @Override
    protected void fetchMoreFromServer() {
        AbstractApiTask<ApiResult> apiTask = createFetchMoreApiTask();
        apiTask.addFinishedListener(this);
        apiTask.addFailedListener(this);
        TaskManager.runImmediately(apiTask);
    }

}

@@ -389,24 +389,16 @@
 apter%3CT%3E
-%0A       
  impleme
@@ -400,16 +400,64 @@
 plements
+%0A        TaskListener.TaskStartListener,%0A       
  TaskLis
@@ -494,16 +494,24 @@
 Result%3E,
+%0A       
  TaskLis
@@ -549,16 +549,113 @@
 ult%3E %7B%0A%0A
+    @Override%0A    public void onTaskStart() %7B%0A        setLoadingView(LoadAction.Refresh);%0A    %7D%0A%0A
     @Ove
@@ -2271,32 +2271,72 @@
 freshApiTask();%0A
+        apiTask.addStartListener(this);%0A
         apiTask.
@@ -2611,32 +2611,72 @@
 hMoreApiTask();%0A
+        apiTask.addStartListener(this);%0A
         apiTask.

Update setRefreshing