Browse Source

Added auto fetch more posts in feed at init. But disabled due to some weird list merging.

renovate/org.robolectric-robolectric-4.x
Ammar Githam 4 years ago
parent
commit
c5d55f3661
  1. 22
      app/src/main/java/awais/instagrabber/customviews/PostsRecyclerView.java

22
app/src/main/java/awais/instagrabber/customviews/PostsRecyclerView.java

@ -22,6 +22,7 @@ import androidx.work.WorkManager;
import com.google.common.collect.ImmutableList;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
@ -77,8 +78,7 @@ public class PostsRecyclerView extends RecyclerView {
if (settingsHelper.getBoolean(Constants.TOGGLE_KEYWORD_FILTER)) {
final ArrayList<String> items = new ArrayList<>(settingsHelper.getStringSet(Constants.KEYWORD_FILTERS));
modelsCopy.addAll(new KeywordsFilterUtils(items).filter(result));
}
else {
} else {
modelsCopy.addAll(result);
}
mediaViewModel.getList().postValue(modelsCopy);
@ -194,7 +194,9 @@ public class PostsRecyclerView extends RecyclerView {
private void initSelf() {
mediaViewModel = new ViewModelProvider(viewModelStoreOwner).get(MediaViewModel.class);
mediaViewModel.getList().observe(lifeCycleOwner, list -> {
if (list.size() > 0) feedAdapter.submitList(list, () -> {
if (list.size() <= 0) return;
feedAdapter.submitList(list, () -> {
// postDelayed(this::fetchMoreIfPossible, 1000);
if (!shouldScrollToTop) return;
smoothScrollToPosition(0);
shouldScrollToTop = false;
@ -217,6 +219,20 @@ public class PostsRecyclerView extends RecyclerView {
dispatchFetchStatus();
}
private void fetchMoreIfPossible() {
if (!postFetcher.hasMore()) return;
if (feedAdapter.getItemCount() == 0) return;
final LayoutManager layoutManager = getLayoutManager();
if (!(layoutManager instanceof StaggeredGridLayoutManager)) return;
final int[] itemPositions = ((StaggeredGridLayoutManager) layoutManager).findLastCompletelyVisibleItemPositions(null);
final boolean allNoPosition = Arrays.stream(itemPositions).allMatch(position -> position == RecyclerView.NO_POSITION);
if (allNoPosition) return;
final boolean match = Arrays.stream(itemPositions).anyMatch(position -> position == feedAdapter.getItemCount() - 1);
if (!match) return;
postFetcher.fetch();
dispatchFetchStatus();
}
private void initDownloadWorkerListener() {
WorkManager.getInstance(getContext())
.getWorkInfosByTagLiveData("download")

Loading…
Cancel
Save