use of in.bugzy.data.model.Category in project bugzy by cpunq.
the class Repository method getCategories.
public LiveData<Resource<List<Category>>> getCategories(boolean mustFetch) {
if (mFetchCategoriesLiveData != null) {
if (mFetchCategoriesLiveData.getValue().status == Status.LOADING && !mustFetch) {
// If the content is in loading state and the request doesn't require us to fetch again
return mCategoriesPublicLiveData;
}
mCategoriesPublicLiveData.removeSource(mFetchCategoriesLiveData);
mFetchCategoriesLiveData = null;
}
mFetchCategoriesLiveData = new NetworkBoundResource<List<Category>, Response<ListCategoriesData>>(mAppExecutors) {
@Override
protected void saveCallResult(@NonNull Response<ListCategoriesData> item) {
db.beginTransaction();
try {
mMiscDao.insertCategories(item.getData().getCategories());
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
}
@Override
protected boolean shouldFetch(@Nullable List<Category> data) {
if (data == null || data.size() == 0) {
return true;
}
if (mustFetch) {
return true;
}
return false;
}
@NonNull
@Override
protected LiveData<List<Category>> loadFromDb() {
return mMiscDao.loadCategories();
}
@NonNull
@Override
protected LiveData<ApiResponse<Response<ListCategoriesData>>> createCall() {
return mApiService.getCategories(new Request("listCategories"));
}
}.asLiveData();
mCategoriesPublicLiveData.addSource(mFetchCategoriesLiveData, value -> {
mCategoriesPublicLiveData.setValue(value);
});
return mCategoriesPublicLiveData;
}
Aggregations