package com.example.profbola.bakingtime.utils;

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

import com.example.profbola.bakingtime.R;
import com.example.profbola.bakingtime.models.Ingredient;

/**
 * Created by prof.BOLA on 7/2/2017.
 */

public class IngredientAdapter extends RecyclerView.Adapter<IngredientAdapter.IngredientViewHolder> {

    private Context mContext;
    private Ingredient[] mIngredients;

    public IngredientAdapter(Context context, Ingredient[] ingredients) {
        mContext = context;
        mIngredients = ingredients;
    }

    @Override
    public IngredientViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext)
                .inflate(R.layout.ingredient, parent, false);

        return new IngredientViewHolder(view);
    }

    @Override
    public void onBindViewHolder(IngredientViewHolder holder, int position) {
        Ingredient ingredient = mIngredients[position];
        holder.bind(ingredient);
    }

    @Override
    public int getItemCount() {
        if (null == mIngredients) return 0;
        return mIngredients.length;
    }

    public class IngredientViewHolder extends RecyclerView.ViewHolder {

        public final TextView name;
        public final TextView quantity;
        public final TextView measure;

        public IngredientViewHolder(View itemView) {
            super(itemView);

            name = (TextView) itemView.findViewById(R.id.ingredient_name);
            quantity = (TextView) itemView.findViewById(R.id.ingredient_quantity);
            measure = (TextView) itemView.findViewById(R.id.ingredient_measure);
        }

        void bind(Ingredient ingredient) {
            name.setText(ingredient.ingredient);
            quantity.setText(String.valueOf(ingredient.quantity));
            measure.setText(ingredient.measure);
        }
    }

    public void swapIngredients(Ingredient[] ingredients) {
        mIngredients = ingredients;
        notifyDataSetChanged();
    }
}
@@ -345,16 +345,79 @@
 redient;
+%0Aimport com.example.profbola.bakingtime.ui.IngredientsFragment;
 %0A%0A/**%0A *
@@ -627,16 +627,80 @@
 edients;
+%0A    private IngredientsFragment.OnIngredientSelected mCallback;
 %0A%0A    pu
@@ -745,38 +745,63 @@
 , Ingredient
-%5B%5D ingredients
+sFragment.OnIngredientSelected callback
 ) %7B%0A        
@@ -833,33 +833,27 @@
    m
-Ingredients = ingredients
+Callback = callback
 ;%0A  
@@ -1219,64 +1219,8 @@
 ) %7B%0A
-        Ingredient ingredient = mIngredients%5Bposition%5D;%0A
     
@@ -1235,26 +1235,24 @@
 er.bind(
-ingredient
+position
 );%0A    %7D
@@ -1455,16 +1455,60 @@
 ewHolder
+%0A            implements View.OnClickListener
  %7B%0A%0A    
@@ -1504,37 +1504,38 @@
 ner %7B%0A%0A        p
-ublic
+rivate
  final TextView 
@@ -1541,37 +1541,38 @@
  name;%0A        p
-ublic
+rivate
  final TextView 
@@ -1590,21 +1590,22 @@
        p
-ublic
+rivate
  final T
@@ -1622,16 +1622,57 @@
 asure;%0A%0A
+        private Ingredient mIngredient;%0A%0A
         
@@ -1989,37 +1989,114 @@
 e);%0A
-        %7D%0A%0A        void bind(
+%0A            itemView.setOnClickListener(this);%0A        %7D%0A%0A        void bind(int position) %7B%0A            m
 Ingr
@@ -2102,17 +2102,20 @@
 redient 
-i
+= mI
 ngredien
@@ -2115,19 +2115,28 @@
 gredient
-) %7B
+s%5Bposition%5D;
 %0A       
@@ -2145,33 +2145,34 @@
    name.setText(
-i
+mI
 ngredient.ingred
@@ -2222,17 +2222,18 @@
 valueOf(
-i
+mI
 ngredien
@@ -2274,17 +2274,18 @@
 setText(
-i
+mI
 ngredien
@@ -2305,16 +2305,137 @@
        %7D
+%0A%0A        @Override%0A        public void onClick(View v) %7B%0A            mCallback.ingredientClicked(mIngredient);%0A        %7D
 %0A    %7D%0A%0A

Implement OnClick, Add Callback and Change fields