package cardinalsolutions.com.kittenexplodr;

import android.support.annotation.NonNull;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.squareup.picasso.Picasso;

class CatAdapter extends RecyclerView.Adapter<CatAdapter.CatViewHolder> {

    private final View.OnClickListener onClickListener;

    int[] images = {R.drawable.kitten1, R.drawable.kitten2, R.drawable.kitten3, R.drawable.kitten4};
    boolean[] exploded = new boolean[images.length]; //Inits to false by default

    CatAdapter(@NonNull View.OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    @Override
    public CatViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_view_cell_cat, parent, false);
        return new CatViewHolder(view);
    }

    @Override
    public void onBindViewHolder(CatViewHolder holder, int position) {
        Picasso.with(holder.itemView.getContext())
                .load(images[position])
                .fit()
                .centerCrop()
                .into(holder.imageView);
        holder.imageView.setOnClickListener(this.onClickListener);

        if (!exploded[position]) {
            this.reset(holder.itemView);
        }
    }

    private void reset(View root) {
        if (root instanceof ViewGroup) {
            ViewGroup parent = (ViewGroup) root;
            for (int i = 0; i < parent.getChildCount(); i++) {
                reset(parent.getChildAt(i));
            }
        } else {
            root.setScaleX(1);
            root.setScaleY(1);
            root.setAlpha(1);
        }
    }

    @Override
    public int getItemCount() {
        return images.length;
    }

    void reset() {
        this.exploded = new boolean[images.length];
        this.notifyDataSetChanged();
    }


    static class CatViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        CardView cardView;

        CatViewHolder(View itemView) {
            super(itemView);
            this.imageView = (ImageView) itemView.findViewById(R.id.catImage);
            this.cardView = (CardView) itemView.findViewById(R.id.card_view);
        }
    }
}

@@ -58,34 +58,153 @@
 oid.
-support.annotation.NonNull
+graphics.Bitmap;%0Aimport android.graphics.drawable.Drawable;%0Aimport android.support.annotation.NonNull;%0Aimport android.support.v7.graphics.Palette
 ;%0Aim
@@ -454,16 +454,52 @@
 Picasso;
+%0Aimport com.squareup.picasso.Target;
 %0A%0Aclass 
@@ -1264,24 +1264,25 @@
 position) %7B%0A
+%0A
         Pica
@@ -1364,61 +1364,8 @@
 n%5D)%0A
-                .fit()%0A                .centerCrop()%0A
     
@@ -1389,25 +1389,22 @@
 (holder.
-imageView
+target
 );%0A     
@@ -2254,16 +2254,40 @@
 dView;%0A%0A
+        Target target;%0A%0A
         
@@ -2296,24 +2296,30 @@
 tViewHolder(
+final 
 View itemVie
@@ -2427,24 +2427,24 @@
 .catImage);%0A
-
             
@@ -2504,24 +2504,1006 @@
 .card_view);
+%0A%0A            target = new Target() %7B%0A                @Override%0A                public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) %7B%0A                    imageView.setImageBitmap(bitmap);%0A%0A                    int defaultBackground = itemView.getContext().getResources().getColor(android.R.color.white);%0A                    // TODO: it's recommended to generate Palettes on a background thread%0A                    int background = new Palette.Builder(bitmap).generate().getLightMutedColor(defaultBackground);%0A%0A                    cardView.setBackgroundColor(background);%0A                %7D%0A%0A                @Override%0A                public void onBitmapFailed(Drawable errorDrawable) %7B%0A                    imageView.setImageDrawable(errorDrawable);%0A                %7D%0A%0A                @Override%0A                public void onPrepareLoad(Drawable placeHolderDrawable) %7B%0A                    imageView.setImageDrawable(placeHolderDrawable);%0A                %7D%0A            %7D;
 %0A        %7D%0A 

Use the Palette library to generate card background colors