package com.example.android.recyclerplayground;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.view.View;

/**
 * ItemDecoration implementation that applies and inset margin
 * around each child of the RecyclerView. It also draws item dividers
 * that are expected from a vertical list implementation, such as
 * ListView.
 */
public class GridDividerDecoration extends RecyclerView.ItemDecoration {

    private static final int[] ATTRS = { android.R.attr.listDivider };

    private Drawable mDivider;
    private int mInsets;

    public GridDividerDecoration(Context context) {
        TypedArray a = context.obtainStyledAttributes(ATTRS);
        mDivider = a.getDrawable(0);
        a.recycle();

        mInsets = context.getResources().getDimensionPixelSize(R.dimen.card_insets);
    }

    @Override
    public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
        drawVertical(c, parent);
        drawHorizontal(c, parent);
    }

    /** Draw dividers at each expected grid interval */
    public void drawVertical(Canvas c, RecyclerView parent) {
        if (parent.getChildCount() == 0) return;

        final int left = parent.getPaddingLeft();
        final int right = parent.getWidth() - parent.getPaddingRight();

        final View child = parent.getChildAt(0);
        if (child.getHeight() == 0) return;

        final RecyclerView.LayoutParams params =
                (RecyclerView.LayoutParams) child.getLayoutParams();
        int top = child.getBottom() + params.bottomMargin + mInsets;
        int bottom = top + mDivider.getIntrinsicHeight();

        final int parentBottom = parent.getHeight() - parent.getPaddingBottom();
        while (bottom < parentBottom) {
            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(c);

            top += mInsets + params.topMargin + child.getHeight() + params.bottomMargin + mInsets;
            bottom = top + mDivider.getIntrinsicHeight();
        }
    }

    /** Draw dividers to the right of each child view */
    public void drawHorizontal(Canvas c, RecyclerView parent) {
        final int top = parent.getPaddingTop();
        final int bottom = parent.getHeight() - parent.getPaddingBottom();

        final int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View child = parent.getChildAt(i);
            final RecyclerView.LayoutParams params =
                    (RecyclerView.LayoutParams) child.getLayoutParams();
            final int left = child.getRight() + params.rightMargin + mInsets;
            final int right = left + mDivider.getIntrinsicWidth();
            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(c);
        }
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        //We can supply forced insets for each item view here in the Rect
        outRect.set(mInsets, mInsets, mInsets, mInsets);
    }
}

@@ -1337,27 +1337,33 @@
   final int 
-lef
+childCoun
 t = parent.g
@@ -1368,23 +1368,23 @@
 .get
-PaddingLef
+ChildCoun
 t();%0A
+%0A
     
@@ -1392,72 +1392,50 @@
    f
-inal int right = parent.getWidth() - parent.getPaddingRight();%0A%0A
+or (int i = 0; i %3C childCount; i++) %7B%0A    
     
@@ -1479,57 +1479,16 @@
 dAt(
-0
+i
 );%0A    
-    if (child.getHeight() == 0) return;%0A%0A
     
@@ -1524,32 +1524,36 @@
 Params params =%0A
+    
                 
@@ -1605,24 +1605,26 @@
 rams();%0A
+%0A 
         
 int top 
@@ -1615,23 +1615,33 @@
         
-int top
+   final int left
  = child
@@ -1648,26 +1648,24 @@
 .get
-Bottom() +
+Left() -
  params.
 bott
@@ -1656,38 +1656,36 @@
 () - params.
-bottom
+left
 Margin 
-+
+-
  mInsets;%0A  
@@ -1694,179 +1694,225 @@
     
-int bottom = top + mDivider.getIntrinsicHeight();%0A%0A        final int parentBottom = parent.getHeight() - parent.getPaddingBottom();%0A        while (bottom %3C parentBottom) %7B
+    final int right = child.getRight() + params.rightMargin + mInsets;%0A            final int top = child.getBottom() + params.bottomMargin + mInsets;%0A            final int bottom = top + mDivider.getIntrinsicHeight();
 %0A   
@@ -1997,166 +1997,8 @@
 aw(c
-);%0A%0A            top += mInsets + params.topMargin + child.getHeight() + params.bottomMargin + mInsets;%0A            bottom = top + mDivider.getIntrinsicHeight(
 );%0A 
@@ -2138,132 +2138,8 @@
 ) %7B%0A
-        final int top = parent.getPaddingTop();%0A        final int bottom = parent.getHeight() - parent.getPaddingBottom();%0A%0A
     
@@ -2185,24 +2185,25 @@
 ildCount();%0A
+%0A
         for 
@@ -2412,24 +2412,25 @@
 utParams();%0A
+%0A
             
@@ -2558,24 +2558,179 @@
 sicWidth();%0A
+            final int top = child.getTop() - params.topMargin - mInsets;%0A            final int bottom = child.getBottom() + params.bottomMargin + mInsets;%0A
             

Update grid divider to use broken segments.