NoNews / NoPaginate

Android pagination library (updated 01.05.2018)
Apache License 2.0
183 stars 31 forks source link

Support for SwipeRefreshLayout or reloading whole list #17

Open raghavsatyadev opened 5 years ago

raghavsatyadev commented 5 years ago

I tried to implement swiperefresh with your library via this code

if (!isRefresh) {
    showProgressBar();
} else if (noPaginate != null) {
    noPaginate.unbind();
}
noPaginate = NoPaginate.with(listNews)
    .setOnLoadMoreListener(new OnLoadMoreListener() {
        @Override
        public void onLoadMore() {
           //network code
        }
    })
    .setLoadingTriggerThreshold(5)
    .build();

but it is not working, paginate is not going into the load more method

NoNews commented 5 years ago

@raghavsatyadev Hi, thanks for this issue. I need full part of code of your activity/fragment

raghavsatyadev commented 5 years ago

I have solved that issue with a hack, currently using checkAdapterState() method for invalidating whole list and reloading it.

Here's the whole code :

public class NewsFragment extends CoreFragment<NewsFragment> implements SingleCallback {
    private RecyclerView listNews;
    private NewsListAdapter adapter;
    private SwipeRefreshLayout refreshLayout;
    private NoPaginate noPaginate;
    private boolean isRefresh;

    public static NewsFragment getInstance() {
        NewsFragment fragment = new NewsFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onFragmentRestart() {

    }

    @Override
    public void onDestroyView() {
        noPaginate.unbind();
        cancelCalls();
        super.onDestroyView();
    }

    @Override
    protected void createReference(View view, Bundle savedInstanceState) {
        listNews = view.findViewById(R.id.list_news);
        refreshLayout = view.findViewById(R.id.refresh_news);
        setupList();
        loadList(false);
    }

    private void loadList(boolean isRefresh) {
        this.isRefresh = isRefresh;
        if (!isRefresh) {
            showProgressBar();
        } else if (noPaginate != null) {
        // Currently using this method to hack the invalidation of NoPaginate Library
            noPaginate.setNoMoreItems(false);
            noPaginate.checkAdapterState();
        }
        if (noPaginate == null) {
            noPaginate = NoPaginate.with(listNews)
                    .setOnLoadMoreListener(() -> {
                        if (NetworkUtil.isConnectedToNetwork(listNews)) {
                            noPaginate.showLoading(true);
                            // some networking code
                        } else NewsFragment.this.hideProgressBar();
                    })
                    .setLoadingTriggerThreshold(5)
                    .build();
        }
    }

    @Override
    protected String getTitle() {
        return "title"
    }

    @Override
    protected boolean isBackEnabled() {
        return false;
    }

    @Override
    protected int getLayoutRes() {
        return R.layout.fragment_news;
    }

    @Override
    protected NewsFragment getCoreFragment() {
        return this;
    }

    @Override
    public void setListeners(boolean state) {
        if (state) {
            refreshLayout.setOnRefreshListener(() -> loadList(true));
            adapter.setOnItemClickListener((position, v) -> {
                if (v.getId() == R.id.btn_item_news_save) {
                    adapter.setSaved(position);
                }
            });
        } else {
            adapter.setOnItemClickListener(null);
            refreshLayout.setOnRefreshListener(null);
        }
    }

    private void setupList() {
        listNews.setLayoutManager(new LinearLayoutManager(getActivity()));
        listNews.setHasFixedSize(false);
        adapter = new NewsListAdapter();
        listNews.setAdapter(adapter);
    }

    @Override
    public void onSingleSuccess(Object object, WebserviceBuilder.ApiNames apiNames) {
        hideProgressBar();
        if (apiNames == WebserviceBuilder.ApiNames.news) {
            if (noPaginate != null) noPaginate.showLoading(false);
            ArrayList<NewsResponse> newsResponses = (ArrayList<NewsResponse>) object;
            if (newsResponses != null && !newsResponses.isEmpty()) {
                if (isRefresh) {
                    refreshLayout.setRefreshing(false);
                    adapter.replaceAll(newsResponses);
                    isRefresh = false;
                } else {
                    adapter.addAll(newsResponses);
                }
            } else {
                if (noPaginate != null) noPaginate.setNoMoreItems(true);
            }
        }
    }

    @Override
    public void onError(Throwable throwable, WebserviceBuilder.ApiNames apiNames) {
        hideProgressBar();
        if (noPaginate != null) noPaginate.showLoading(false);
        AppLog.log(false, "NewsFragment: " + "onError: ", throwable);
    }

    @Override
    public void onFailure(int code, String errorMessage, WebserviceBuilder.ApiNames apiNames) {
        hideProgressBar();
        if (noPaginate != null) noPaginate.showLoading(false);
        SnackBarUtil.showSnackBar(listNews, errorMessage);
    }
}
raghavsatyadev commented 5 years ago

@NoNews can you resolve this issue in next 2-3 days? My code is going to production and I think my solution is not working properly.