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