package com.eowise.recyclerview.stickyheaders;

import android.support.v4.view.ViewCompat;
import android.support.v7.widget.RecyclerView;

/**
 * Created by aurel on 16/10/14.
 */
public class StickyHeaderBuilder {

    private Boolean isAnimationEnabled;
    private RecyclerView recyclerView;
    private RecyclerView.Adapter adapter;
    private StickyHeadersAdapter headersAdapter;
    private HeaderPosition headerPosition;

    public StickyHeaderBuilder() {
        this.isAnimationEnabled = false;
    }

    public StickyHeaderBuilder setRecyclerView(RecyclerView recyclerView) {
        this.recyclerView = recyclerView;
        return this;
    }

    public StickyHeaderBuilder setStickyHeadersAdapter(StickyHeadersAdapter adapter) {
        return setStickyHeadersAdapter(adapter, HeaderPosition.TOP);
    }

    public StickyHeaderBuilder setStickyHeadersAdapter(StickyHeadersAdapter adapter, HeaderPosition headerPosition) {
        this.headersAdapter = adapter;
        this.headerPosition = headerPosition;
        return this;
    }

    public StickyHeaderBuilder setAdapter(RecyclerView.Adapter adapter) {
        this.adapter = adapter;
        return this;
    }

    public StickyHeaderBuilder enableAnimations() {
        this.isAnimationEnabled = true;
        return this;
    }

    public StickyHeadersItemDecoration build() {

        // TODO: Throw if adapter doesn't have stables ids

        StickyHeadersItemDecoration decoration =  new StickyHeadersItemDecoration(headersAdapter, recyclerView, headerPosition);


        if (isAnimationEnabled) {
            adapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {

                @Override
                public void onItemRangeInserted(int positionStart, int itemCount) {
                    ViewCompat.postOnAnimationDelayed(recyclerView, new InvalidateAnimationRunnable(recyclerView), 50);
                }

                @Override
                public void onItemRangeRemoved(int positionStart, int itemCount) {
                    ViewCompat.postOnAnimationDelayed(recyclerView, new InvalidateAnimationRunnable(recyclerView), 50);
                }
            });
        }


        decoration.registerAdapterDataObserver(adapter);

        return decoration;
    }
}

@@ -213,48 +213,8 @@
  %7B%0A%0A
-    private Boolean isAnimationEnabled;%0A
     
@@ -422,49 +422,8 @@
 ) %7B%0A
-        this.isAnimationEnabled = false;%0A
     
@@ -1103,128 +1103,8 @@
  %7D%0A%0A
-    public StickyHeaderBuilder enableAnimations() %7B%0A        this.isAnimationEnabled = true;%0A        return this;%0A    %7D%0A%0A
     
@@ -1343,657 +1343,8 @@
 );%0A%0A
-%0A        if (isAnimationEnabled) %7B%0A            adapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() %7B%0A%0A                @Override%0A                public void onItemRangeInserted(int positionStart, int itemCount) %7B%0A                    ViewCompat.postOnAnimationDelayed(recyclerView, new InvalidateAnimationRunnable(recyclerView), 50);%0A                %7D%0A%0A                @Override%0A                public void onItemRangeRemoved(int positionStart, int itemCount) %7B%0A                    ViewCompat.postOnAnimationDelayed(recyclerView, new InvalidateAnimationRunnable(recyclerView), 50);%0A                %7D%0A            %7D);%0A        %7D%0A%0A%0A
     

Remove animations in builder