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