I am actually consuming Triposo's API to show the list of countries. https://www.triposo.com/api/
However, in responses, there is no numeric Id and I am struggling while incrementing page size. What should I perform on such responses to overcome? For instance, Github API is fully suitable for Paging Library since it returns itemId as numeric.
Example Response
{ "results": [ { "name": "Benin", "country_id": "Benin", "snippet": "A safe and relatively easy country for travellers to visit; birthplace of the Voodoo religion and former home of the Kingdom of Dahomey.", "parent_id": null, "score": 3.98127216481287, "id": "Benin" } ], "estimated_total": 30845, "more": true } ItemKeyedDataSource.java
public class ItemKeyedCountryDataSource extends ItemKeyedDataSource<Integer, CountryResult> { private EndpointHelper endpointHelper; ItemKeyedCountryDataSource() { endpointHelper = EndpointHelper.getInstance(); } @Override public void loadInitial(@NonNull LoadInitialParams<Integer> params, @NonNull final LoadInitialCallback<CountryResult> callback) { final List<CountryResult> countryResultList = new ArrayList<>(); endpointHelper.getCountryList(0) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .retry(1) .onErrorResumeNext(new Func1<Throwable, Observable<? extends CountryWrapper>>() { @Override public Observable<? extends CountryWrapper> call(Throwable throwable) { return Observable.error(throwable); } }) .subscribe(new Subscriber<CountryWrapper>() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { } @Override public void onNext(CountryWrapper countryWrapper) { if (countryWrapper.getResults().size() > 0) { countryResultList.addAll(countryWrapper.getResults()); callback.onResult(countryResultList); } } }); } @Override public void loadAfter(@NonNull LoadParams<Integer> params, @NonNull final LoadCallback<CountryResult> callback) { final List<CountryResult> countryResultList = new ArrayList<>(); endpointHelper.getCountryList(params.requestedLoadSize) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .retry(1) .onErrorResumeNext(new Func1<Throwable, Observable<? extends CountryWrapper>>() { @Override public Observable<? extends CountryWrapper> call(Throwable throwable) { return Observable.error(throwable); } }) .subscribe(new Subscriber<CountryWrapper>() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { } @Override public void onNext(CountryWrapper countryWrapper) { if (countryWrapper.getResults().size() > 0) { countryResultList.addAll(countryWrapper.getResults()); callback.onResult(countryResultList); } } }); } @Override public void loadBefore(@NonNull LoadParams<Integer> params, @NonNull LoadCallback<CountryResult> callback) { //Do nothing } @NonNull @Override public Integer getKey(@NonNull CountryResult item) { return ?; // what do I return here? } } ViewModel.java
public class SearchableActivityViewModel extends ViewModel { private LiveData<PagedList<CountryResult>> countryResult; SearchableActivityViewModel() { Executor executor = Executors.newFixedThreadPool(5); CountryResultDataSourceFactory countryResultDataSourceFactory = new CountryResultDataSourceFactory(); PagedList.Config config = new PagedList.Config.Builder() .setEnablePlaceholders(false) .setInitialLoadSizeHint(20) //first load .setPageSize(21) .build(); //noinspection unchecked countryResult = new LivePagedListBuilder<String, CountryResult>(countryResultDataSourceFactory, config) .setFetchExecutor(executor) .build(); } public LiveData<PagedList<CountryResult>> getCountryResult(){ return countryResult; } @Override protected void onCleared() { super.onCleared(); } }
offsetspecifies a position, then this feels like aPositionalDataSource.