package io.pivotal.arca.adapters;

import android.annotation.TargetApi;
import android.content.Context;
import android.database.Cursor;
import android.database.DataSetObserver;
import android.os.Build;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import java.util.Collection;

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class RecyclerViewCursorAdapter extends RecyclerView.Adapter<RecyclerViewCursorAdapter.ViewHolder> {

    private final ModernCursorAdapter mCursorAdapter;

    public RecyclerViewCursorAdapter(final Context context, final int layout, final Collection<Binding> bindings) {
        mCursorAdapter = new ModernCursorAdapter(context, layout, bindings);
        mCursorAdapter.registerDataSetObserver(new RecyclerViewDataSetObserver());
        setHasStableIds(true);
    }

    @Override
    public RecyclerViewCursorAdapter.ViewHolder onCreateViewHolder(final ViewGroup parent, final int viewType) {
        final View view = mCursorAdapter.newView(parent.getContext(), mCursorAdapter.getCursor(), parent);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final RecyclerViewCursorAdapter.ViewHolder holder, final int position) {
        final Cursor cursor = (Cursor) mCursorAdapter.getItem(position);
        mCursorAdapter.bindView(holder.getView(), holder.getView().getContext(), cursor);
    }

    @Override
    public int getItemCount() {
        return mCursorAdapter.getCount();
    }

    @Override
    public long getItemId(final int position) {
        return mCursorAdapter.getItemId(position);
    }

    public Object getItem(final int position) {
        return mCursorAdapter.getItem(position);
    }

    public void setViewBinder(final ViewBinder viewBinder) {
        mCursorAdapter.setViewBinder(viewBinder);
    }

    public Cursor swapCursor(final Cursor newCursor) {
        return mCursorAdapter.swapCursor(newCursor);
    }

    public Cursor getCursor() {
        return mCursorAdapter.getCursor();
    }

    public boolean hasResults() {
        final Cursor cursor = getCursor();
        return cursor != null && cursor.getCount() > 0;
    }

    private class RecyclerViewDataSetObserver extends DataSetObserver {
        @Override
        public void onChanged() {
            notifyDataSetChanged();
        }
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        private final View mView;

        public ViewHolder(final View itemView) {
            super(itemView);
            mView = itemView;
            mView.setClickable(true);
        }

        public View getView() {
            return mView;
        }
    }
}

@@ -583,157 +583,44 @@
 nal 
-Context context, final int layout, final Collection%3CBinding%3E bindings) %7B%0A        mCursorAdapter = new ModernCursorAdapter(context, layout, bindings);
+ModernCursorAdapter cursorAdapter) %7B
 %0A   
@@ -616,34 +616,33 @@
 pter) %7B%0A        
-mC
+c
 ursorAdapter.reg
@@ -710,28 +710,257 @@
     
-setHasStableIds(true
+mCursorAdapter = cursorAdapter;%0A        setHasStableIds(true);%0A    %7D%0A%0A    public RecyclerViewCursorAdapter(final Context context, final int layout, final Collection%3CBinding%3E bindings) %7B%0A        this(new ModernCursorAdapter(context, layout, bindings)
 );%0A 
@@ -1525,32 +1525,163 @@
 cursor);%0A    %7D%0A%0A
+    @Override%0A    public int getItemViewType(final int position) %7B%0A        return mCursorAdapter.getItemViewType(position);%0A    %7D%0A%0A
     @Override%0A  

Update RecyclerViewAdapter to use cursor view type