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