package com.chscodecamp.android.simpletodo;

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 android.widget.TextView;

import java.util.List;

class TaskRecyclerAdapter extends RecyclerView.Adapter<TaskRecyclerAdapter.TaskViewHolder> {

    private List<Task> tasks;
    private Callback callback;

    TaskRecyclerAdapter(List<Task> tasks, Callback callback) {
        this.tasks = tasks;
        this.callback = callback;
    }

    @Override
    public TaskViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        return new TaskViewHolder(LayoutInflater.from(viewGroup.getContext())
                .inflate(R.layout.row_todo_item, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(TaskViewHolder holder, int position) {

        final Task task = tasks.get(position);

        holder.itemName.setText(task.getTitle());
        holder.checkBox.setChecked(task.isCompleted());

        holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                task.setCompleted(isChecked);
                if (callback != null) {
                    callback.onTaskUpdated();
                }
            }
        });
    }

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

    class TaskViewHolder extends RecyclerView.ViewHolder {
        private TextView itemName;
        private CheckBox checkBox;

        TaskViewHolder(View v) {
            super(v);
            itemName = (TextView) v.findViewById(R.id.item_name);
            checkBox = (CheckBox) v.findViewById(R.id.item_checkbox);
        }
    }

    interface Callback {
        void onTaskUpdated();
    }
}
@@ -890,16 +890,22 @@
 wHolder(
+final 
 TaskView
@@ -935,16 +935,16 @@
 tion) %7B%0A
-
 %0A       
@@ -1121,28 +1121,20 @@
 x.setOnC
-heckedChange
+lick
 Listener
@@ -1142,38 +1142,20 @@
 new 
-CompoundButton.OnCheckedChange
+View.OnClick
 List
@@ -1184,28 +1184,16 @@
 Override
-%0A           
  public 
@@ -1204,113 +1204,151 @@
  onC
-heckedChanged(CompoundButton buttonView, boolean isChecked) %7B%0A                task.setCompleted(isChecked
+lick(View view) %7B%0A                task.setCompleted(holder.checkBox.isChecked());%0A                holder.checkBox.setChecked(task.isCompleted()
 );%0A 

fix view recycling