package com.mikepenz.fastadapter_extensions.scroll;

import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.view.View;

public abstract class EndlessRecyclerOnScrollListener extends RecyclerView.OnScrollListener {

    private int mPreviousTotal = 0;
    private boolean mLoading = true;
    private int mVisibleThreshold = -1;
    private int mFirstVisibleItem, mVisibleItemCount, mTotalItemCount;

    private int mCurrentPage = 1;

    private RecyclerView.LayoutManager mLayoutManager;

    public EndlessRecyclerOnScrollListener(RecyclerView.LayoutManager layoutManager) {
        this.mLayoutManager = layoutManager;
    }

    public EndlessRecyclerOnScrollListener(RecyclerView.LayoutManager layoutManager, int visibleThreshold) {
        this.mLayoutManager = layoutManager;
        this.mVisibleThreshold = visibleThreshold;
    }

    private int findFirstVisibleItemPosition(RecyclerView recyclerView) {
        final View child = findOneVisibleChild(0, mLayoutManager.getChildCount(), false, true);
        return child == null ? RecyclerView.NO_POSITION : recyclerView.getChildAdapterPosition(child);
    }

    private int findLastVisibleItemPosition(RecyclerView recyclerView) {
        final View child = findOneVisibleChild(recyclerView.getChildCount() - 1, -1, false, true);
        return child == null ? RecyclerView.NO_POSITION : recyclerView.getChildAdapterPosition(child);
    }

    private View findOneVisibleChild(int fromIndex, int toIndex, boolean completelyVisible,
                                     boolean acceptPartiallyVisible) {
        OrientationHelper helper;
        if (mLayoutManager.canScrollVertically()) {
            helper = OrientationHelper.createVerticalHelper(mLayoutManager);
        } else {
            helper = OrientationHelper.createHorizontalHelper(mLayoutManager);
        }

        final int start = helper.getStartAfterPadding();
        final int end = helper.getEndAfterPadding();
        final int next = toIndex > fromIndex ? 1 : -1;
        View partiallyVisible = null;
        for (int i = fromIndex; i != toIndex; i += next) {
            final View child = mLayoutManager.getChildAt(i);
            if (child != null) {
                final int childStart = helper.getDecoratedStart(child);
                final int childEnd = helper.getDecoratedEnd(child);
                if (childStart < end && childEnd > start) {
                    if (completelyVisible) {
                        if (childStart >= start && childEnd <= end) {
                            return child;
                        } else if (acceptPartiallyVisible && partiallyVisible == null) {
                            partiallyVisible = child;
                        }
                    } else {
                        return child;
                    }
                }
            }
        }
        return partiallyVisible;
    }

    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);
        if (mVisibleThreshold == -1)
            mVisibleThreshold = findLastVisibleItemPosition(recyclerView) - findFirstVisibleItemPosition(recyclerView);

        mVisibleItemCount = recyclerView.getChildCount();
        mTotalItemCount = mLayoutManager.getItemCount();
        mFirstVisibleItem = findFirstVisibleItemPosition(recyclerView);

        if (mLoading) {
            if (mTotalItemCount > mPreviousTotal) {
                mLoading = false;
                mPreviousTotal = mTotalItemCount;
            }
        }
        if (!mLoading && (mTotalItemCount - mVisibleItemCount)
                <= (mFirstVisibleItem + mVisibleThreshold)) {

            mCurrentPage++;

            onLoadMore(mCurrentPage);

            mLoading = true;
        }
    }

    public int getTotalItemCount() {
        return mTotalItemCount;
    }

    public int getFirstVisibleItem() {
        return mFirstVisibleItem;
    }

    public int getVisibleItemCount() {
        return mVisibleItemCount;
    }

    public int getCurrentPage() {
        return mCurrentPage;
    }

    public abstract void onLoadMore(int currentPage);
}
@@ -170,16 +170,52 @@
 ew.View;
+%0Aimport android.widget.LinearLayout;
 %0A%0Apublic
@@ -488,16 +488,114 @@
 Count;%0A%0A
+    private int mOrientationHelperOrientation;%0A    private OrientationHelper mOrientationHelper;%0A%0A
     priv
@@ -1757,42 +1757,8 @@
 ) %7B%0A
-        OrientationHelper helper;%0A
     
@@ -1809,33 +1809,226 @@
 ) %7B%0A            
-h
+if (mOrientationHelper == null %7C%7C mOrientationHelperOrientation == LinearLayout.HORIZONTAL) %7B%0A                mOrientationHelperOrientation = LinearLayout.VERTICAL;%0A                mOrientationH
 elper = Orientat
@@ -2067,32 +2067,46 @@
 LayoutManager);%0A
+            %7D%0A
         %7D else %7B
@@ -2118,17 +2118,210 @@
         
-h
+if (mOrientationHelper == null %7C%7C mOrientationHelperOrientation == LinearLayout.VERTICAL) %7B%0A                mOrientationHelperOrientation = LinearLayout.HORIZONTAL;%0A                mOrientationH
 elper = 
@@ -2370,32 +2370,46 @@
 LayoutManager);%0A
+            %7D%0A
         %7D%0A%0A     
@@ -2425,25 +2425,37 @@
 int start = 
-h
+mOrientationH
 elper.getSta
@@ -2492,25 +2492,37 @@
 l int end = 
-h
+mOrientationH
 elper.getEnd
@@ -2822,17 +2822,29 @@
 Start = 
-h
+mOrientationH
 elper.ge
@@ -2904,17 +2904,29 @@
 ldEnd = 
-h
+mOrientationH
 elper.ge

update EndlessRecyclerOnScrollListener