package com.genius.groupie.example;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

public abstract class InfiniteScrollListener extends RecyclerView.OnScrollListener {

    private int previousTotal = 0; // The total number of items in the dataset after the last load
    private boolean loading = true; // True if we are still waiting for the last set of data to load.
    private final int visibleThreshold = 5; // The minimum amount of items to have below your current scroll position before loading more.
    private int firstVisibleItem, visibleItemCount, totalItemCount;
    private int current_page = 0;
    private LinearLayoutManager linearLayoutManager;

    public InfiniteScrollListener(LinearLayoutManager linearLayoutManager) {
        this.linearLayoutManager = linearLayoutManager;
    }

    public void setLinearLayoutManager(LinearLayoutManager linearLayoutManager) {
        this.linearLayoutManager = linearLayoutManager;
    }

    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);

        visibleItemCount = recyclerView.getChildCount();
        totalItemCount = linearLayoutManager.getItemCount();
        firstVisibleItem = linearLayoutManager.findFirstVisibleItemPosition();

        if (loading) {
            if (totalItemCount > previousTotal || totalItemCount == 0) {
                loading = false;
                previousTotal = totalItemCount;
            }
        }

        // End has been reached
        if (!loading && (totalItemCount - visibleItemCount) <= (firstVisibleItem + visibleThreshold)) {
            current_page++;
            onLoadMore(current_page);
            loading = true;
        }
    }

    public abstract void onLoadMore(int current_page);
}

@@ -30,16 +30,43 @@
 ample;%0A%0A
+import android.os.Handler;%0A
 import a
@@ -739,16 +739,45 @@
 Manager;
+%0A    private Handler handler;
 %0A%0A    pu
@@ -895,32 +895,70 @@
 rLayoutManager;%0A
+        this.handler = new Handler();%0A
     %7D%0A%0A    publi
@@ -1134,16 +1134,22 @@
 crolled(
+final 
 Recycler
@@ -1230,24 +1230,137 @@
 , dx, dy);%0A%0A
+        final Runnable runnable = new Runnable() %7B%0A            @Override%0A            public void run() %7B%0A        
         visi
@@ -1396,32 +1396,40 @@
 etChildCount();%0A
+        
         totalIte
@@ -1465,32 +1465,40 @@
 getItemCount();%0A
+        
         firstVis
@@ -1557,24 +1557,32 @@
 osition();%0A%0A
+        
         if (
@@ -1588,24 +1588,32 @@
 (loading) %7B%0A
+        
             
@@ -1681,32 +1681,40 @@
                 
+ 
+       
 loading = false;
@@ -1714,16 +1714,24 @@
  false;%0A
+        
         
@@ -1786,18 +1786,28 @@
     
+  
-%7D%0A
+      %7D%0A  
         
 %7D%0A%0A 
@@ -1798,24 +1798,30 @@
 %7D%0A          
+      
 %7D%0A%0A        /
@@ -1819,16 +1819,24 @@
         
+ 
+       
 // End h
@@ -1851,16 +1851,24 @@
 reached%0A
+        
         
@@ -1975,16 +1975,24 @@
         
+   
+     
 current_
@@ -1999,16 +1999,24 @@
 page++;%0A
+        
         
@@ -2049,32 +2049,40 @@
 e);%0A            
+    
+    
 loading = true;%0A
@@ -2085,25 +2085,339 @@
 ue;%0A        
-%7D
+        %7D%0A            %7D%0A        %7D;%0A%0A        // We shouldn't directly mutate the RecyclerView data in onScrolled%0A        // This is because it may be called during the measure & layout pass where you cannot change the data%0A        // Post-pone any adapter changes until the next frame%0A        handler.post(runnable);
 %0A    %7D%0A%0A    

Fix RecyclerView IllegalStateException: Closes #21