/*
 * Copyright (c) 2016.  Joe
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.lovejjfg.swiperefreshrecycleview;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.util.Log;

import com.lovejjfg.powerrecycle.DefaultAnimator;
import com.lovejjfg.powerrecycle.LoadMoreScrollListener;
import com.lovejjfg.powerrecycle.TouchHelperCallback;
import com.lovejjfg.swiperefreshrecycleview.model.TestBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;


public class NormalActivity extends AppCompatActivity {
    private static final String TAG = NormalActivity.class.getSimpleName();
    @Bind(R.id.recyclerview)
    RecyclerView mRecycleView;
    @Bind(R.id.toolbar)
    Toolbar mToolBar;
    NormalAdapter adapter;

    private List<TestBean> list;
    private Runnable refreshAction;
    private Runnable loadMoreAction;
    private boolean isRun;
    private boolean enable = true;
    private static final int DEFAULT_TIME = 1000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        setSupportActionBar(mToolBar);
        adapter = new NormalAdapter();
        //初始化一个TouchHelperCallback
        TouchHelperCallback callback = new TouchHelperCallback();
        //添加一个回调
        callback.setItemDragSwipeCallBack(adapter);
        //初始化一个ItemTouchHelper
        ItemTouchHelper itemTouchHelper = new ItemTouchHelper(callback);
        //关联相关的RecycleView
        itemTouchHelper.attachToRecyclerView(mRecycleView);

        LinearLayoutManager manager = new LinearLayoutManager(this);
        //1.setLayoutManager
        mRecycleView.setLayoutManager(manager);
        //2.setAdapter
        mRecycleView.setAdapter(adapter);
//        mRecycleView.setItemAnimator(new DefaultAnimator());
        mRecycleView.addOnScrollListener(new LoadMoreScrollListener(mRecycleView));
        adapter.setOnItemClickListener((v, p) -> Log.e(TAG, "onItemClick: " + p));
//        adapter.setTotalCount(100);
        adapter.setLoadMoreListener(() -> {
            if (isRun) {
                Log.e("TAG", "onLoadMore:正在执行，直接返回。。。 ");
                return;
            }
            Log.e("TAG", "onLoadMore: ");
            isRun = true;
            mRecycleView.postDelayed(loadMoreAction, DEFAULT_TIME);
        });
//        mRecycleView.setOnRefreshListener(this);
        this.list = new ArrayList<>();
        for (int i = 0; i < 30; i++) {
            this.list.add(new TestBean("这是" + i));
        }
        //3.initData
        adapter.setList(this.list);
        loadMoreAction = () -> {
            int i = ((int) (Math.random() * 10)) % 3;
            if (i == 0 || i == 1) {
                ArrayList<TestBean> arrayList = new ArrayList<>();
                arrayList.add(new TestBean("ccc1"));
                arrayList.add(new TestBean("ccc2"));
                arrayList.add(new TestBean("ccc3"));
                arrayList.add(new TestBean("ccc4"));
                arrayList.add(new TestBean("ccc5"));
                adapter.appendList(arrayList);
                Log.e("TAG", "run: 正常加载更多！！");
            } else {
                adapter.loadMoreError();
                Log.e("TAG", "run: 加载失败！！！");
            }
            isRun = false;
        };
//        refreshAction = () -> {
//            this.list = new ArrayList<>();
//            for (int i = 0; i < 40; i++) {
//                this.list.add(new TestBean("这是" + i));
//            }
//            adapter.setList(this.list);
//            mRecycleView.setRefresh(false);
//        };

    }


//    @Override
//    public void onRefresh() {
//        mRecycleView.postDelayed(refreshAction, DEFAULT_TIME);
//    }

//    @Override
//    public void onLoadMore() {
//        if (isRun) {
//            return;
//        }
//        Log.e("TAG", "onLoadMore: ");
//        isRun = true;
//        mRecycleView.postDelayed(loadMoreAction, DEFAULT_TIME);
//    }


//    @Override
//    public void onBackPressed() {
//        super.onBackPressed();
//    }
}

@@ -870,125 +870,18 @@
 oid.
-support.v7.widget.helper.ItemTouchHelper;%0Aimport android.util.Log;%0A%0Aimport com.lovejjfg.powerrecycle.DefaultAnimator;
+util.Log;%0A
 %0Aimp
@@ -938,62 +938,8 @@
 er;%0A
-import com.lovejjfg.powerrecycle.TouchHelperCallback;%0A
 impo
@@ -1869,370 +1869,8 @@
 ();%0A
-        //%E5%88%9D%E5%A7%8B%E5%8C%96%E4%B8%80%E4%B8%AATouchHelperCallback%0A        TouchHelperCallback callback = new TouchHelperCallback();%0A        //%E6%B7%BB%E5%8A%A0%E4%B8%80%E4%B8%AA%E5%9B%9E%E8%B0%83%0A        callback.setItemDragSwipeCallBack(adapter);%0A        //%E5%88%9D%E5%A7%8B%E5%8C%96%E4%B8%80%E4%B8%AAItemTouchHelper%0A        ItemTouchHelper itemTouchHelper = new ItemTouchHelper(callback);%0A        //%E5%85%B3%E8%81%94%E7%9B%B8%E5%85%B3%E7%9A%84RecycleView%0A        itemTouchHelper.attachToRecyclerView(mRecycleView);%0A%0A
     
@@ -3617,774 +3617,15 @@
  %7D;%0A
-//        refreshAction = () -%3E %7B%0A//            this.list = new ArrayList%3C%3E();%0A//            for (int i = 0; i %3C 40; i++) %7B%0A//                this.list.add(new TestBean(%22%E8%BF%99%E6%98%AF%22 + i));%0A//            %7D%0A//            adapter.setList(this.list);%0A//            mRecycleView.setRefresh(false);%0A//        %7D;%0A%0A    %7D%0A%0A%0A//    @Override%0A//    public void onRefresh() %7B%0A//        mRecycleView.postDelayed(refreshAction, DEFAULT_TIME);%0A//    %7D%0A%0A//    @Override%0A//    public void onLoadMore() %7B%0A//        if (isRun) %7B%0A//            return;%0A//        %7D%0A//        Log.e(%22TAG%22, %22onLoadMore: %22);%0A//        isRun = true;%0A//        mRecycleView.postDelayed(loadMoreAction, DEFAULT_TIME);%0A//    %7D%0A%0A%0A//    @Override%0A//    public void onBackPressed() %7B%0A//        super.onBackPressed();%0A//    %7D
+%0A    %7D%0A%0A
 %0A%7D%0A

remove some unused code