package com.zzzmode.appopsx.ui.main.backup;

import android.support.v4.util.SparseArrayCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import com.zzzmode.appopsx.R;
import com.zzzmode.appopsx.ui.main.AppItemViewHolder;
import com.zzzmode.appopsx.ui.model.AppInfo;

import java.util.List;

/**
 * 导出
 * Created by zl on 2017/5/7.
 */

class ExportAdapter extends RecyclerView.Adapter<ExportAdapter.ExportViewHolder> implements View.OnClickListener,CompoundButton.OnCheckedChangeListener{

    private List<AppInfo> appInfos;

    private SparseArrayCompat<AppInfo> mCheckedApps = new SparseArrayCompat<AppInfo>();

    ExportAdapter(List<AppInfo> appInfos) {
        this.appInfos = appInfos;
        if(appInfos != null){
            int i=0;
            for (AppInfo appInfo : appInfos) {
                mCheckedApps.append(i,appInfo);
                i++;
                if(i > 10){
                    break;
                }
            }
        }
    }

    SparseArrayCompat<AppInfo> getCheckedApps() {
        return mCheckedApps;
    }

    @Override
    public ExportAdapter.ExportViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ExportAdapter.ExportViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_export_app,parent,false));
    }

    @Override
    public void onBindViewHolder(ExportAdapter.ExportViewHolder holder, int position) {
        holder.bindData(appInfos.get(position));
        holder.itemView.setOnClickListener(this);
        holder.itemView.setTag(holder);

        holder.checkBox.setTag(holder);
        holder.checkBox.setOnCheckedChangeListener(null);
        holder.checkBox.setChecked(isChecked(position));
        holder.checkBox.setOnCheckedChangeListener(this);
    }

    @Override
    public int getItemCount() {
        return appInfos.size();
    }

    @Override
    public void onClick(View v) {
        Object tag = v.getTag();
        if(tag instanceof ExportViewHolder){
            ExportViewHolder holder = (ExportViewHolder) tag;
            handleCheck(holder,true);
        }
    }

    private void handleCheck(ExportViewHolder holder,boolean change){
        int pos=holder.getAdapterPosition();

        if(holder.checkBox.isChecked()){
            mCheckedApps.delete(pos);
        }else {
            mCheckedApps.put(pos,appInfos.get(pos));
        }
        if(change) {
            holder.checkBox.setOnCheckedChangeListener(null);
            holder.checkBox.setChecked(isChecked(pos));
            holder.checkBox.setOnCheckedChangeListener(this);
        }
    }

    private boolean isChecked(int pos){
        return !(mCheckedApps.indexOfKey(pos) < 0);
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        Object tag = buttonView.getTag();
        if(tag instanceof ExportViewHolder){
            ExportViewHolder holder = (ExportViewHolder) tag;
            handleCheck(holder,false);
        }
    }


    static class ExportViewHolder extends AppItemViewHolder{

        CheckBox checkBox;

        ExportViewHolder(View itemView) {
            super(itemView);
            checkBox= (CheckBox) itemView.findViewById(R.id.checkbox);
        }
    }
}

@@ -1027,81 +1027,8 @@
 ++;%0A
-                if(i %3E 10)%7B%0A                    break;%0A                %7D%0A
     

remove test code