package me.kaneki.sample.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import me.kaneki.sample.R;
import me.kaneki.sample.adapter.SampleAdapter;
import me.kaneki.sample.entity.Message;
import me.kaneki.sample.event.MessageEvent;
import me.kaneki.sample.utils.SharedPreferencesUtils;
import me.kaneki.xchatmessageview.XChatMessageView;
import me.kaneki.xchatmessageview.listener.OnLoadMoreListener;

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

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private static final int PAGE_COUNT = 10;

    private int currentPosition;
    private int lastPosition;

    private XChatMessageView<Message> xChatMessageView;
    private SampleAdapter sampleAdapter;
    private Gson gson;

    private Button buttonRecvImg;
    private Button buttonRecvText;
    private Button buttonSendImg;
    private Button buttonSendText;

    private ArrayList<Message> mDatas;
    private ArrayList<Message> localDatas;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        EventBus.getDefault().register(this);

        initLocalData();
        initView();
        initChatMessageView();
        addListeners();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected void onStop() {
        super.onStop();
        SharedPreferencesUtils.setParam(this, "data", gson.toJson(localDatas));
    }

    private void initLocalData() {
        gson = new Gson();
        mDatas = new ArrayList<>();

        String localStr = (String) SharedPreferencesUtils.getParam(this, "data", "");

        localDatas = gson.fromJson(localStr == null ? "" : localStr , new TypeToken<List<Message>>(){}.getType());

        if (localDatas == null)
            localDatas = new ArrayList<>();

        mDatas.addAll(getDataFromLocal());
    }

    private void initView() {
        xChatMessageView = (XChatMessageView) findViewById(R.id.xcmv_home);
        buttonRecvImg = (Button) findViewById(R.id.btn_recv_img);
        buttonSendImg = (Button) findViewById(R.id.btn_send_img);
        buttonRecvText = (Button) findViewById(R.id.btn_recv_text);
        buttonSendText = (Button) findViewById(R.id.btn_send_text);
    }

    private void initChatMessageView() {
        sampleAdapter = new SampleAdapter(this, mDatas);

        xChatMessageView.setMessageAdapter(sampleAdapter);

        if (mDatas.size() == PAGE_COUNT) {
            xChatMessageView.scrollToBottom();
            xChatMessageView.setIsNeedHeaderLoadMore(true);
            xChatMessageView.setOnLoadMoreListener(new OnLoadMoreListener() {
                @Override
                public void onHeaderLoadMore() {
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                //模拟加载
                                Thread.sleep(500);
                                final ArrayList<Message> messageArrayList = getDataFromLocal();
                                xChatMessageView.post(new Runnable() {
                                    @Override
                                    public void run() {
                                        if (messageArrayList.isEmpty())
                                            xChatMessageView.setIsNeedHeaderLoadMore(false);
                                        xChatMessageView.addMoreMessageAtFirst(messageArrayList);
                                    }
                                });
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }).start();
                }

                @Override
                public void onFooterLoadMore() {
                }
            });
        }
    }

    private void addListeners() {
        buttonRecvImg.setOnClickListener(this);
        buttonSendImg.setOnClickListener(this);
        buttonRecvText.setOnClickListener(this);
        buttonSendText.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_recv_img:
                Message message = new Message(true, "", Message.TYPE_IMG);
                localDatas.add(0, message);
                xChatMessageView.addMessageAtLast(message);
                break;
            case R.id.btn_send_img:
                message = new Message(false, "", Message.TYPE_IMG);
                localDatas.add(0, message);
                xChatMessageView.addMessageAtLast(message);
                break;
            case R.id.btn_send_text:
                message = new Message(false, "send text", Message.TYPE_TEXT);
                localDatas.add(0, message);
                xChatMessageView.addMessageAtLast(message);
                break;
            case R.id.btn_recv_text:
                message = new Message(true, "recv text", Message.TYPE_TEXT);
                localDatas.add(0, message);
                xChatMessageView.addMessageAtLast(message);
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MessageEvent event) {
        xChatMessageView.reomveMessage(event.getItemView());

        localDatas.remove(event.getMessage());
    }

    private ArrayList<Message> getDataFromLocal() {
        if (!localDatas.isEmpty()) {
            ArrayList<Message> messageArrayList = new ArrayList<>();
            for (currentPosition = lastPosition; currentPosition < localDatas.size() && currentPosition - lastPosition < PAGE_COUNT; currentPosition++) {
                messageArrayList.add(localDatas.get(currentPosition));
            }
            lastPosition = mDatas.size();
            return messageArrayList;
        } else {
            return new ArrayList<>();
        }
    }
}
@@ -878,41 +878,8 @@
 0;%0A%0A
-    private int currentPosition;%0A
     
@@ -2270,32 +2270,67 @@
 ocal());%0A    %7D%0A%0A
+    @SuppressWarnings(%22unchecked%22)%0A
     private void
@@ -4640,24 +4640,49 @@
 k(View v) %7B%0A
+        Message message;%0A
         swit
@@ -4746,32 +4746,24 @@
             
-Message 
 message = ne
@@ -4805,112 +4805,8 @@
 G);%0A
-                localDatas.add(0, message);%0A                xChatMessageView.addMessageAtLast(message);%0A
     
@@ -4932,112 +4932,8 @@
 G);%0A
-                localDatas.add(0, message);%0A                xChatMessageView.addMessageAtLast(message);%0A
     
@@ -5070,112 +5070,8 @@
 T);%0A
-                localDatas.add(0, message);%0A                xChatMessageView.addMessageAtLast(message);%0A
     
@@ -5203,32 +5203,152 @@
 _TEXT);%0A        
+        break;%0A            default:%0A                message = new Message(false, %22error%22, Message.TYPE_TEXT);%0A        %7D%0A
         localDat
@@ -5371,32 +5371,24 @@
 e);%0A        
- 
-       
 xChatMessage
@@ -5423,41 +5423,8 @@
 e);%0A
-                break;%0A        %7D%0A
     
@@ -5815,31 +5815,21 @@
    for (
-currentPosition
+int i
  = lastP
@@ -5837,31 +5837,17 @@
 sition; 
-currentPosition
+i
  %3C local
@@ -5862,31 +5862,17 @@
 ze() && 
-currentPosition
+i
  - lastP
@@ -5893,31 +5893,17 @@
 _COUNT; 
-currentPosition
+i
 ++) %7B%0A  
@@ -5956,23 +5956,9 @@
 get(
-currentPosition
+i
 ));%0A

fix demo activity detail