Is it possible to use paging library with MVP architecture in android?

1.4k views Asked by At

I want to use paging library in my application, I have seen multiple sample they all are using view modal component with MVVM architecture, But most of the part of my application is in MVP architecture and I don't want to use MVVM, So is there any way to implement pagination with paging library with MVP architecture?

1

There are 1 answers

0
Annie Aye Myat On

Yes, you can. You can initiate Datasource Factory and implement methods in Presenter like

private void initData(String id) {
    executor = Executors.newFixedThreadPool(5);

    factory = new EvaluationDataSourceFactory(compositeDisposable, api, id);

    networkState = Transformations.switchMap(factory.getDataSource(), source -> {
                Timber.d("network status get");
                return source.getNetworkState();
            }
    );

    PagedList.Config pagedListConfig =
            (new PagedList.Config.Builder())
                    .setEnablePlaceholders(false)
                    .setInitialLoadSizeHint(5)
                    .setPrefetchDistance(5)
                    .setPageSize(5).build();

    error = Transformations.switchMap(factory.getDataSource(), source -> {
        return source.getError();
    });

    assessments = (new LivePagedListBuilder(factory, pagedListConfig)).setFetchExecutor(executor).build();
}

public void getAssessments(String id) {
    initData(id);
}

Then, you can call presenter method from your activity or fragment like

presenter.getAssessments(id);

It worked for me.