use of in.bugzy.data.remote.model.ListPeopleData in project bugzy by cpunq.
the class Repository method getPeople.
public LiveData<Resource<List<Person>>> getPeople(boolean mustFetch) {
if (mFetchPeopleLiveData != null) {
if (mFetchPeopleLiveData.getValue().status == Status.LOADING && !mustFetch) {
// If the content is in loading state and the request doesn't require us to fetch again
return mPeoplePublicLiveData;
}
mPeoplePublicLiveData.removeSource(mFetchPeopleLiveData);
mFetchPeopleLiveData = null;
}
mFetchPeopleLiveData = new NetworkBoundResource<List<Person>, Response<ListPeopleData>>(mAppExecutors) {
@Override
protected void saveCallResult(@NonNull Response<ListPeopleData> item) {
mSsRespository.updatePeopleSearchSuggestion(item.getData().getPersons());
try {
db.beginTransaction();
db.miscDao().insertPersons(item.getData().getPersons());
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
}
@Override
protected boolean shouldFetch(@Nullable List<Person> data) {
if (data == null || data.size() == 0) {
return true;
}
if (mustFetch) {
return true;
}
return false;
}
@NonNull
@Override
protected LiveData<List<Person>> loadFromDb() {
return mMiscDao.loadPersons();
}
@NonNull
@Override
protected LiveData<ApiResponse<Response<ListPeopleData>>> createCall() {
return mApiService.listPeople(new ListPeopleRequest());
}
}.asLiveData();
mPeoplePublicLiveData.addSource(mFetchPeopleLiveData, value -> {
mPeoplePublicLiveData.setValue(value);
});
return mPeoplePublicLiveData;
}
Aggregations