package com.laxture.skeleton.adapter;

import android.os.Build;
import android.widget.ArrayAdapter;

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

import java.util.List;

public abstract class SimpleApiAdapter<T, ApiResult> extends ArrayAdapter<T> {

    private AbstractApiTask<ApiResult> mApiTask;

    public SimpleApiAdapter(AbstractApiTask<ApiResult> apiTask) {
        super(RuntimeContext.getApplication(), 0);
        mApiTask = apiTask;

        mApiTask.addFinishedListener(new TaskListener.TaskFinishedListener<ApiResult>() {
            @Override
            public void onTaskFinished(ApiResult result) {
                List<T> resultList = convertApiResultToData(result);

                clear();
                if (Build.VERSION.SDK_INT > 10) {
                    addAll(resultList);
                } else {
                    for (T item : resultList) {
                        add(item);
                    }
                }
            }
        });
    }

    //*************************************************************************
    // Task Delegate Method
    //*************************************************************************

    public void addArgument(String key, Object value) {
        mApiTask.addArgument(key, value.toString());
    }

    public void addArgument(String key, int value) {
        mApiTask.addArgument(key, value);
    }

    public void addArgument(String key, boolean value) {
        mApiTask.addArgument(key, value);
    }

    public void addArgument(String key, long value) {
        mApiTask.addArgument(key, value);
    }

    public void addArgument(String key, float value) {
        mApiTask.addArgument(key, value);
    }

    public void addArgument(String key, double value) {
        mApiTask.addArgument(key, value);
    }

    public void addArgument(String key, long[] value) {
        mApiTask.addArgument(key, value);
    }

    public void addArgument(String key, String[] value) {
        mApiTask.addArgument(key, value);
    }

    public void addStartListener(TaskListener.TaskStartListener callback) {
        mApiTask.addStartListener(callback);
    }

    public void addProgressUpdatedListener(TaskListener.TaskProgressUpdatedListener callback) {
        mApiTask.addProgressUpdatedListener(callback);
    }

    public void addFinishedListener(TaskListener.TaskFinishedListener<ApiResult> callback) {
        mApiTask.addFinishedListener(callback);
    }

    public void addCancelledListener(TaskListener.TaskCancelledListener<ApiResult> callback) {
        mApiTask.addCancelledListener(callback);
    }

    public void addFailedListener(TaskListener.TaskFailedListener callback) {
        mApiTask.addFailedListener(callback);
    }

    public void addDataChangedListener(TaskListener.TaskDataChangedListener callback) {
        mApiTask.addDataChangedListener(callback);
    }

    public boolean isCancelled() {
        return mApiTask.isCancelled();
    }

    public AbstractTask.State getState() {
        return mApiTask.getState();
    }

    //*************************************************************************
    // Public Method
    //*************************************************************************

    public void loadData() {
        TaskManager.runImmediately(mApiTask);
    }

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

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

}

@@ -898,68 +898,20 @@
     
-if (Build.VERSION.SDK_INT %3E 10) %7B%0A                    addAll
+resetAllData
 (res
@@ -920,20 +920,16 @@
 tList);%0A
-    
         
@@ -937,15 +937,8 @@
    %7D
- else %7B
 %0A   
@@ -946,80 +946,146 @@
     
+%7D);%0A
     
+%7D%0A%0A
     
-    for (T item : resultList) %7B%0A                        add(item
+public void resetAllData(List%3CT%3E data) %7B%0A        clear();%0A        if (Build.VERSION.SDK_INT %3E 10) %7B%0A            addAll(data
 );%0A 
@@ -1083,33 +1083,41 @@
 (data);%0A        
- 
+%7D else %7B%0A
            %7D%0A   
@@ -1103,33 +1103,54 @@
 se %7B%0A           
-%7D
+ for (T item : data) %7B
 %0A               
@@ -1142,33 +1142,42 @@
                 
-%7D
+add(item);
 %0A            %7D%0A 
@@ -1167,32 +1167,32 @@
 ;%0A            %7D%0A
+
         %7D);%0A    
@@ -1176,34 +1176,32 @@
      %7D%0A        %7D
-);
 %0A    %7D%0A%0A    //**

Add reset data to SimpleApiAdapter