package de.michiruf.android.viewholder;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

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

/**
 * @author Michael Ruf
 * @since 2017-03-05
 */
public abstract class RecyclerHolderArrayAdapter<ItemType> extends RecyclerView.Adapter {

    private final int itemLayoutRes;
    private final List<ItemType> items;

    public RecyclerHolderArrayAdapter(int itemLayoutRes) {
        this.itemLayoutRes = itemLayoutRes;
        items = new ArrayList<>();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(itemLayoutRes, parent, false);
        return constructHolder(view);
    }

    @SuppressWarnings("unchecked")
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (!(holder instanceof ViewHolder)) {
            throw new RuntimeException("Recycler view's view-holder must be instance of " +
                    "RecyclerHolderAdapter.ViewHolder!");
        }

        ViewHolder<ItemType> myHolder = (ViewHolder<ItemType>) holder;
        myHolder.apply(items.get(position), position);
    }


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

    public void add(ItemType entry) {
        items.add(entry);
    }

    public void add(int position, ItemType itemType) {
        items.add(position, itemType);
    }

    public ItemType remove(int position) {
        return items.remove(position);
    }

    public void clear() {
        items.clear();
    }

    protected abstract ViewHolder<ItemType> constructHolder(View view);

    public static abstract class ViewHolder<ItemType> extends RecyclerView.ViewHolder {

        public ViewHolder(View view) {
            super(view);
        }

        public abstract void apply(ItemType item, int position);
    }
}

@@ -34,16 +34,103 @@
 older;%0A%0A
+import android.support.annotation.NonNull;%0Aimport android.support.annotation.Nullable;%0A
 import a
@@ -535,16 +535,70 @@
 items;%0A%0A
+    private OnItemClickListener onItemClickListener;%0A%0A
     publ
@@ -1390,16 +1390,229 @@
 holder;%0A
+%0A        int adapterPosition = holder.getAdapterPosition();%0A        // Cancel binding and stuff if the element was removed%0A        if (adapterPosition == RecyclerView.NO_POSITION) %7B%0A            return;%0A        %7D%0A%0A
         
@@ -1636,17 +1636,24 @@
 ems.get(
-p
+adapterP
 osition)
@@ -1654,17 +1654,24 @@
 ition), 
-p
+adapterP
 osition)
@@ -1676,118 +1676,579 @@
 n);%0A
-    %7D%0A%0A%0A    @Override%0A    public int getItemCount() %7B%0A        return items.size();%0A    %7D%0A%0A    public void add(
+%0A        if (onItemClickListener != null) %7B%0A            if (myHolder.internalListener == null) %7B%0A                myHolder.internalListener = new InternalOnItemClickListener(onItemClickListener);%0A                myHolder.itemView.setOnClickListener(myHolder.internalListener);%0A            %7D%0A            myHolder.internalListener.position = adapterPosition;%0A        %7D%0A    %7D%0A%0A    @Override%0A    public int getItemCount() %7B%0A        return items.size();%0A    %7D%0A%0A    public ItemType getItem(int position) %7B%0A        return items.get(position);%0A    %7D%0A%0A    public void add(@NonNull 
 Item
@@ -2327,16 +2327,25 @@
 osition,
+ @NonNull
  ItemTyp
@@ -2545,24 +2545,157 @@
 r();%0A    %7D%0A%0A
+    public void setOnItemClickListener(@Nullable OnItemClickListener listener) %7B%0A        this.onItemClickListener = listener;%0A    %7D%0A%0A
     protecte
@@ -2840,24 +2840,87 @@
 ewHolder %7B%0A%0A
+        private InternalOnItemClickListener internalListener;%0A%0A
         publ
@@ -3052,11 +3052,590 @@
 );%0A    %7D
+%0A%0A    public interface OnItemClickListener %7B%0A%0A        void onItemClick(View view, int position);%0A    %7D%0A%0A    private static class InternalOnItemClickListener implements View.OnClickListener %7B%0A%0A        private final OnItemClickListener onItemClickListener;%0A        private int position;%0A%0A        private InternalOnItemClickListener(OnItemClickListener onItemClickListener) %7B%0A            this.onItemClickListener = onItemClickListener;%0A        %7D%0A%0A        @Override%0A        public void onClick(View view) %7B%0A            onItemClickListener.onItemClick(view, position);%0A        %7D%0A    %7D
 %0A%7D%0A

Add OnItemClick functionality to recycler adapter (under testing for now)