Я получил действие, которое показывает список элементов пользователю и использует библиотеку подкачки. Моя проблема в том, что я не могу перезагрузить список и размер списка в первый раз и не могу снова получить данные с сервера.
Вот моя фабрика источников данных:
public class GetProductsListRepositoryFactory extends DataSource.Factory {
MutableLiveData<GetProductsListRepository> mutableLiveData;
private String IdClassification;
private String FilterText;
private int sortby;
private int type;
public GetProductsListRepositoryFactory( String IdClassification,String FilterText,int sortby,int type) {
mutableLiveData = new MutableLiveData<>();
this.IdClassification = IdClassification;
this.FilterText = FilterText;
this.sortby = sortby;
this.type = type;
}
@Override
public DataSource create() {
try{
GetProductsListRepository getProductsListRepository = new GetProductsListRepository(IdClassification, FilterText, sortby, type);
mutableLiveData.postValue(getProductsListRepository);
return getProductsListRepository;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public MutableLiveData<GetProductsListRepository> getMutableLiveData() {
return mutableLiveData;
}
}
это мой репозиторий:
public class GetProductsListRepository extends PageKeyedDataSource<Integer, ProductListModel>{
//the size of a page that we want
public static final int PAGE_SIZE = 20;
private LozanJsonPlaceHolderApi ecJsonPlaceHolderApi1;
//we will start from the first page which is 1
private static final int FIRST_PAGE = 0;
String IdClassification;
String FilterText;
int sortby;
int type;
public GetProductsListRepository( String IdClassification,String FilterText,int sortby,int type) {
this.IdClassification =IdClassification;
this.FilterText =FilterText;
this.sortby =sortby;
this.type =type;
}
@Override
public void loadInitial(@NonNull PageKeyedDataSource.LoadInitialParams<Integer> params, @NonNull final PageKeyedDataSource.LoadInitialCallback<Integer, ProductListModel> callback) {
// Log.e("DataSourceLoadInitial","INSIDE");
try{
ecJsonPlaceHolderApi1 = RetrofitClient.cteateService(LozanJsonPlaceHolderApi.class);
Map<String, String> parameters = new HashMap<>();
parameters.put("ClassificationID",IdClassification);
if(Locale.getDefault().getLanguage().equals("en"))
parameters.put("Culture", "EN");
else parameters.put("Culture", "AR");
parameters.put("PageIndex", String.valueOf(FIRST_PAGE));
parameters.put("PageSize", String.valueOf(PAGE_SIZE));
parameters.put("_type", String.valueOf(type));
parameters.put("sortby", String.valueOf(sortby));
parameters.put("FilterText", FilterText);
parameters.put("filter", "0");
Log.e("parameters",parameters.toString());
Call<List<ProductListModel>> data = ecJsonPlaceHolderApi1.getProductsList(UserDataManager.getTokenAppPref(),parameters);
data.enqueue(new Callback<List<ProductListModel>>() {
@Override
public void onResponse(Call<List<ProductListModel>> call, Response<List<ProductListModel>> response) {
try{
Log.e("ProductsListRepository", String.valueOf(response.body().size()));
List<ProductListModel> productListModel = response.body();
callback.onResult(productListModel,null, (int) 1);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onFailure(Call<List<ProductListModel>> call, Throwable t) {
Log.e("ProductsListRepository44", String.valueOf(t.getMessage()));
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void loadBefore(@NonNull PageKeyedDataSource.LoadParams<Integer> params, @NonNull PageKeyedDataSource.LoadCallback<Integer, ProductListModel> callback) {
}
@Override
public void loadAfter(@NonNull final PageKeyedDataSource.LoadParams<Integer> params, @NonNull final PageKeyedDataSource.LoadCallback<Integer, ProductListModel> callback) {
try{
ecJsonPlaceHolderApi1 = RetrofitClient.cteateService(LozanJsonPlaceHolderApi.class);
Map<String, String> parameters = new HashMap<>();
parameters.put("ClassificationID",IdClassification);
if(Locale.getDefault().getLanguage().equals("en"))
parameters.put("Culture", "EN");
else parameters.put("Culture", "AR");
parameters.put("PageIndex", String.valueOf(params.key));
parameters.put("PageSize", String.valueOf(PAGE_SIZE));
parameters.put("_type", String.valueOf(type));
parameters.put("sortby", String.valueOf(sortby));
parameters.put("FilterText", FilterText);
parameters.put("filter", "0");
// Log.e("parameters",parameters.toString());
Call<List<ProductListModel>> data = ecJsonPlaceHolderApi1.getProductsList(UserDataManager.getTokenAppPref(),parameters);
data.enqueue(new Callback<List<ProductListModel>>() {
@Override
public void onResponse(Call<List<ProductListModel>> call, Response<List<ProductListModel>> response) {
try{
Log.e("ProductsListRepository44", String.valueOf(response.body().size()));
List<ProductListModel> productListModel = response.body();
callback.onResult(productListModel, params.key + 1);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onFailure(Call<List<ProductListModel>> call, Throwable t) {
Log.e("ProductsListRepository44", String.valueOf(t.getMessage()));
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}
это мой ViewModel:
public class GetProductsListViewModel extends ViewModel {
GetProductsListRepositoryFactory dataSourceFactory;
MutableLiveData<GetProductsListRepository> getProductsListRepositoryMutableLiveData;
Executor executor;
LiveData<PagedList<ProductListModel>> pagedListLiveData;
public void init(String IdClassification,String FilterText,int sortby,int type){
dataSourceFactory = new GetProductsListRepositoryFactory(IdClassification,FilterText, sortby, type);
getProductsListRepositoryMutableLiveData = dataSourceFactory.getMutableLiveData();
PagedList.Config config = (new PagedList.Config.Builder())
.setEnablePlaceholders(true)
.setInitialLoadSizeHint(10)
.setPageSize(20)
.setPrefetchDistance(4)
.build();
executor = Executors.newFixedThreadPool(5);
pagedListLiveData = (new LivePagedListBuilder<Long, ProductListModel>(dataSourceFactory,config))
.setFetchExecutor(executor)
.build();
}
public LiveData<PagedList<ProductListModel>> getPagedListLiveData() {
return pagedListLiveData;
}
}
мои наблюдения во фрагменте
public void getProductList(int IdClassification,int sortby,int type) {
classID=IdClassification;
// progressBar.setVisibility(View.VISIBLE);
getProductsListViewModel = ViewModelProviders.of(this).get(GetProductsListViewModel.class);
getProductsListViewModel.init(String.valueOf(IdClassification),"", sortby, type);
getProductsListViewModel.getPagedListLiveData().observe(this, new Observer<PagedList<ProductListModel>>() {
@Override
public void onChanged(PagedList<ProductListModel> productListModels) {
try{
shimmerRecycler.showShimmerAdapter();
ProductListAdapter itemRecyclerViewAdapter = new ProductListAdapter(getContext(),productListModels);
itemRecyclerViewAdapter.submitList(productListModels);
recyclerView.setAdapter( itemRecyclerViewAdapter);
recyclerView.setLayoutManager(new GridLayoutManager( getContext(), 2));
shimmerRecycler.setVisibility(View.GONE);
}catch (Exception ex) {
// BugManager.AddBug(getClass().getName(), ex.getMessage(), MagicWordApps.Shopping);
}
}
});
}