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