1

I'm new to Android Paging (Don't shoot me), and I'm trying to implement Paging with Database and Network like android sample.

I am observing getPagedList() of my ViewModel in the fragment, and there I submit the list to my adapter, like this:

public class MyFragment extends Fragment{
...

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        refreshLayout.setOnRefreshListener(this::requestRefresh);

        myViewModel.getPagedList().observe(this, myList -> {
            myAdapter.submitList(myList);
        });

    }

    private void requestRefresh(){
        if (myAdapter != null && myAdapter.getCurrentList() != null) {
            myAdapter.getList().getDataSource().invalidate();
        }

        myViewModel.requestRefresh(this);
    }

}

Here's my ViewModel and how I create my PagedList:

public class MyViewModel extends ViewModel {
...

    @Inject
    public MyViewModel(MyRepository repository) {
        this.repository = repository;
        initPagedList();

    }

    private void initPagedList() {
        DataSource.Factory<Integer, MyModel> factory = repository.getCurrentList();
        PagingBoundaryCallback boundaryCallback = new PagingBoundaryCallback();

        PagedList.Config config = (new PagedList.Config.Builder())
                .setPageSize(10)
                .setInitialLoadSizeHint(20)
                .build();

        mPagedList = new LivePagedListBuilder<>(factory, config)
                .setBoundaryCallback(boundaryCallback)
                .build();
    }



    public LiveData<PagedList<MyModel>> getPagedList() {
        return mPagedList;
    }

    public void requestRefresh() {
        initPagedList();
    }

}


And Here's my BoundaryCallback:

public class PagingBoundaryCallback extends PagedList.BoundaryCallback<MyModel> {
    private MyRepository repository;
    private boolean isLoading = false;
    private boolean allItemsAreLoaded = false;
    private int currentPage = 1;

    public PagingBoundaryCallback() {
        repository = MyApplication.getInstance().getAppComponent().MyRepository();

    }

    @Override
    public void onZeroItemsLoaded() {
        allItemsAreLoaded = false;
        currentPage = 1;
        requestFetch(1);

    }

    @Override
    public void onItemAtEndLoaded(@NonNull MyModel itemAtEnd) {
        if (!isLoading && !allItemsAreLoaded){
            requestFetch(currentPage);
        }

    }

    private void requestFetch(int page) {
        isLoading = true;
        repository.requestFetch(page, new FinishCallback() {
            @Override
            public void onFinish(List<MyModel> myList) {
                currentPage++;
                isLoading = false;

                if (myList.size() == 0)
                    allItemsAreLoaded = true;
            }

            @Override
            public void onError(String message) {
                isLoading = false;
            }
        });
    }
}

Additional Info:

  1. repository.requestFetch() requests a certain page from web service, and there the response would be inserted in database (Room). If the response is for the first page then all the old data would be deleted before inserting the new data.

  2. repository.getCurrentList() returns DataSource.Factory created by database dao

The issue is that when I refresh the list, the variables in BoundaryCallback are not renewed, meaning "currentPage" continues to increment, and it's not reset to 1 (When clearly a new BoundaryCallback gets created with each call to initPagedList). Anyway I don't know what I'm missing here, any help would be appreciated.

e.y66
  • 99
  • 10

0 Answers0