im just start using paging library and code just work fine but i got no idea how to handle error like progress bar and Toast a error message, so there my code this is repo
public class SearchRepository extends PageKeyedDataSource<Integer, Doc> {
public static final int PAGE_SIZE = 5;
private static final int FIRST_PAGE = 1;
private static final String token = "1234";
private static final String TAG = "www";
public SearchRepository() {
}
@Override
public void loadInitial(@NonNull final LoadInitialParams<Integer> params, @NonNull final LoadInitialCallback<Integer, Doc> callback) {
Log.e(TAG, "loadInitial: ");
APIService apiService = RetroClass2.getAPIService();
apiService.SeachPro(token, "name", "ascending", PAGE_SIZE, FIRST_PAGE, "")
.enqueue(new Callback<Products>() {
@Override
public void onResponse(Call<Products> call, Response<Products> response) {
if (response.body() != null) {
callback.onResult(response.body().getResult().getDocs(), null, FIRST_PAGE + 1);
}
}
@Override
public void onFailure(Call<Products> call, Throwable t) {
}
});
}
@Override
public void loadBefore(@NonNull final LoadParams<Integer> params, @NonNull final LoadCallback<Integer, Doc> callback) {
Log.e(TAG, "loadBefore: ");
APIService apiService = RetroClass2.getAPIService();
apiService.SeachPro(token, "name", "ascending", PAGE_SIZE, params.key, "")
.enqueue(new Callback<Products>() {
@Override
public void onResponse(Call<Products> call, Response<Products> response) {
if (response.body() != null) {
Integer key = (params.key > 1) ? params.key - 1 : null;
callback.onResult(response.body().getResult().getDocs(), key);
}
}
@Override
public void onFailure(Call<Products> call, Throwable t) {
}
});
}
@Override
public void loadAfter(@NonNull final LoadParams<Integer> params, @NonNull final LoadCallback<Integer, Doc> callback) {
Log.e(TAG, "loadAfter: ");
APIService apiService = RetroClass2.getAPIService();
apiService.SeachPro(token, "name", "ascending", PAGE_SIZE, params.key, "")
.enqueue(new Callback<Products>() {
@Override
public void onResponse(Call<Products> call, Response<Products> response) {
if (response.body() != null) {
Integer key = response.body().getResult().getDocs().size() != 0 ? params.key + 1 : null;
callback.onResult(response.body().getResult().getDocs(), key);
}
}
@Override
public void onFailure(Call<Products> call, Throwable t) {
}
});
}
and this my factory keep data
public class SearchRepoFactory extends DataSource.Factory {
private MutableLiveData<PageKeyedDataSource<Integer, Doc>> listMutableLiveData = new MutableLiveData<>();
@Override
public DataSource create() {
SearchRepository searchRepository = new SearchRepository();
listMutableLiveData.postValue(searchRepository);
return searchRepository;
}
public MutableLiveData<PageKeyedDataSource<Integer, Doc>> getListMutableLiveData() {
return listMutableLiveData;
}
}
this is my viewmodel
public class SearchViewModel extends ViewModel {
public LiveData<PagedList<Doc>> listLiveData;
private LiveData<PageKeyedDataSource<Integer, Doc>> liveData;
private static final String TAG = "yyy";
public SearchViewModel() {
SearchRepoFactory factory = new SearchRepoFactory();
liveData = factory.getListMutableLiveData();
PagedList.Config config = (new PagedList.Config.Builder()).setEnablePlaceholders(false).setPageSize(SearchRepository.PAGE_SIZE).build();
listLiveData = (new LivePagedListBuilder(factory, config)).build();
}
}
and my adapter work fine so im gonna skip adapter
and this activity
recyclerView = findViewById(R.id.rv1);
progressBar = findViewById(R.id.probar);
final SearchAdapter adapter = new SearchAdapter(this);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
SearchViewModel searchViewModel = new SearchViewModel(this);
searchViewModel.listLiveData.observe(this, new Observer<PagedList<Doc>>() {
@Override
public void onChanged(@Nullable PagedList<Doc> docs) {
adapter.submitList(docs);
}
});
recyclerView.setAdapter(adapter);
im gonna say again code works fine when there is not error but i want handling error like no connection Toast and hide progress bar , and i dont want user public static , i getting my server response in SearchRepository and long way to UI , and i know my class naming is not perfect :) , so any ideas ?