Skip to content

Commit

Permalink
Remove DataLoadingSubject from InfiniteScrolling class
Browse files Browse the repository at this point in the history
  • Loading branch information
florina-muntenescu authored and nickbutcher committed Mar 14, 2019
1 parent 77b05af commit 35010fb
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
12 changes: 11 additions & 1 deletion app/src/main/java/io/plaidapp/ui/HomeActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
import io.plaidapp.core.designernews.data.stories.model.Story;
import io.plaidapp.core.dribbble.data.api.model.Shot;
import io.plaidapp.core.feed.FeedAdapter;
import io.plaidapp.core.feed.FeedProgressUiModel;
import io.plaidapp.core.ui.ConnectivityChecker;
import io.plaidapp.core.ui.HomeGridItemAnimator;
import io.plaidapp.core.ui.PlaidItemsList;
Expand Down Expand Up @@ -233,11 +234,20 @@ public int getSpanSize(int position) {
grid.setLayoutManager(layoutManager);
grid.addOnScrollListener(toolbarElevation);
grid.addOnScrollListener(
new InfiniteScrollListener(layoutManager, viewModel.getDataManager()) {
new InfiniteScrollListener(layoutManager) {
@Override
public void onLoadMore() {
viewModel.loadData();
}

@Override
public boolean isDataLoading() {
FeedProgressUiModel uiModel = viewModel.getFeedProgress().getValue();
if(uiModel != null){
return uiModel.isLoading();
}
return false;
}
});
grid.setHasFixedSize(true);
grid.addItemDecoration(new GridItemDividerDecoration(this, R.dimen.divider_height,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,19 @@ package io.plaidapp.core.ui.recyclerview
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView

import io.plaidapp.core.data.DataLoadingSubject

/**
* A scroll listener for RecyclerView to load more items as you approach the end.
*
* Adapted from https://gist.github.com/ssinss/e06f12ef66c51252563e
*/
abstract class InfiniteScrollListener(
private val layoutManager: LinearLayoutManager,
private val dataLoading: DataLoadingSubject
private val layoutManager: LinearLayoutManager
) : RecyclerView.OnScrollListener() {
private val loadMoreRunnable = Runnable { onLoadMore() }

override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
// bail out if scrolling upward or already loading data
if (dy < 0 || dataLoading.isDataLoading) return
if (dy < 0 || isDataLoading()) return

val visibleItemCount = recyclerView.childCount
val totalItemCount = layoutManager.itemCount
Expand All @@ -47,6 +44,8 @@ abstract class InfiniteScrollListener(

abstract fun onLoadMore()

abstract fun isDataLoading(): Boolean

companion object {
// The minimum number of items remaining before we should loading more.
private const val VISIBLE_THRESHOLD = 5
Expand Down
5 changes: 4 additions & 1 deletion search/src/main/java/io/plaidapp/search/ui/SearchActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,13 @@ class SearchActivity : AppCompatActivity() {
itemAnimator = SlideInItemAnimator()
this.layoutManager = layoutManager
addOnScrollListener(object :
InfiniteScrollListener(layoutManager, viewModel.getDataLoadingSubject()) {
InfiniteScrollListener(layoutManager) {
override fun onLoadMore() {
viewModel.loadMore()
}
override fun isDataLoading(): Boolean {
return viewModel.searchProgress.value?.isLoading ?: false
}
})
setHasFixedSize(true)

Expand Down

0 comments on commit 35010fb

Please sign in to comment.