package io.doist.recyclerviewext.sticky_headers;

import android.graphics.Canvas;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

/**
 * Adds sticky headers capabilities to your {@link RecyclerView.Adapter} based on {@link Canvas} drawing.
 *
 * Fast, but limited. Since it's manually drawn, the sticky header has no support for animations and click listeners.
 * For a slightly slower but more powerful approach, see {@link StickyHeaderViewItemDecoration}.
 */
public class StickyHeaderCanvasItemDecoration<T extends RecyclerView.Adapter & StickyHeaders>
        extends StickyHeaderItemDecoration<T> {
    public StickyHeaderCanvasItemDecoration(T adapter) {
        super(adapter);
    }

    public StickyHeaderCanvasItemDecoration(T adapter, boolean vertical) {
        super(adapter, vertical);
    }

    public StickyHeaderCanvasItemDecoration(T adapter, boolean vertical, boolean reverse) {
        super(adapter, vertical, reverse);
    }

    @Override
    protected void onDisplayStickyHeader(RecyclerView.ViewHolder stickyHeader, RecyclerView parent, Canvas canvas,
                                         float x, float y) {
        View view = stickyHeader.itemView;
        int count = canvas.save();
        canvas.translate(view.getLeft() + x, view.getTop() + y);
        view.draw(canvas);
        canvas.restoreToCount(count);
    }

    @Override
    protected void onCreateStickyHeader(RecyclerView.ViewHolder stickyHeader, RecyclerView parent, boolean vertical,
                                        int position) {
        // Nothing to do. There's no container to add the view to since it's draw manually.
    }

    @Override
    protected void onBindStickyHeader(RecyclerView.ViewHolder stickyHeader, RecyclerView parent, boolean vertical,
                                      int position) {
        RecyclerView.LayoutManager manager = parent.getLayoutManager();
        if (manager != null) {
            View view = stickyHeader.itemView;

            // Set the correct layout params.
            ViewGroup.LayoutParams lp = view.getLayoutParams();
            if (lp == null) {
                view.setLayoutParams(manager.generateDefaultLayoutParams());
            } else if (!(lp instanceof RecyclerView.LayoutParams)) {
                view.setLayoutParams(manager.generateLayoutParams(lp));
            }

            // Measure and layout child.
            manager.measureChildWithMargins(view, 0, 0);
            if (vertical) {
                manager.layoutDecorated(view, parent.getPaddingLeft(), 0,
                                        parent.getPaddingLeft() + view.getMeasuredWidth(), view.getMeasuredHeight());
            } else {
                manager.layoutDecorated(view, 0, parent.getPaddingTop(), view.getMeasuredWidth(),
                                        parent.getPaddingTop() + view.getMeasuredHeight());
            }
        }
    }

    @Override
    protected void onScrapStickyHeader(RecyclerView.ViewHolder stickyHeader, RecyclerView parent) {
        // Nothing to do. There's no container to remove the view from since it's draw manually.
    }
}

@@ -654,16 +654,49 @@
 on%3CT%3E %7B%0A
+%0A    private boolean mDecorate;%0A%0A
     publ
@@ -752,29 +752,28 @@
         
-super
+this
 (adapter
 );%0A    %7D
@@ -764,16 +764,22 @@
 (adapter
+, true
 );%0A    %7D
@@ -855,37 +855,36 @@
 ical) %7B%0A        
-super
+this
 (adapter, vertic
@@ -885,16 +885,23 @@
 vertical
+, false
 );%0A    %7D
@@ -990,24 +990,298 @@
  reverse) %7B%0A
+        this(adapter, vertical, reverse, true);%0A    %7D%0A%0A    /**%0A     * @param decorate true to layout header views with decoration insets; false, otherwise.%0A     */%0A    public StickyHeaderCanvasItemDecoration(T adapter, boolean vertical, boolean reverse, boolean decorate) %7B%0A
         supe
@@ -1307,24 +1307,54 @@
 , reverse);%0A
+        mDecorate = decorate;%0A
     %7D%0A%0A    @
@@ -2892,32 +2892,24 @@
     
-manager.layoutDecorated(
+layout(manager, 
 view
@@ -2941,48 +2941,8 @@
 , 0,
-%0A                                       
  par
@@ -2988,16 +2988,39 @@
 Width(),
+%0A                      
  view.ge
@@ -3080,32 +3080,24 @@
     
-manager.layoutDecorated(
+layout(manager, 
 view
@@ -3150,33 +3150,16 @@
 idth(),%0A
-                 
         
@@ -3248,32 +3248,326 @@
        %7D%0A    %7D%0A%0A
+    private void layout(RecyclerView.LayoutManager manager, View view, int left, int top, int right, int bottom) %7B%0A        if (mDecorate) %7B%0A            manager.layoutDecorated(view, left, top, right, bottom);%0A        %7D else %7B%0A            view.layout(left, top, right, bottom);%0A        %7D%0A    %7D%0A%0A
     @Override%0A  

Support for sticky headers without decoration insets