use of in.bugzy.data.remote.ApiResponse in project bugzy by cpunq.
the class Repository method getCategories.
public LiveData<Resource<List<Category>>> getCategories(boolean mustFetch) {
if (mFetchCategoriesLiveData != null) {
if (mFetchCategoriesLiveData.getValue().status == Status.LOADING && !mustFetch) {
// If the content is in loading state and the request doesn't require us to fetch again
return mCategoriesPublicLiveData;
}
mCategoriesPublicLiveData.removeSource(mFetchCategoriesLiveData);
mFetchCategoriesLiveData = null;
}
mFetchCategoriesLiveData = new NetworkBoundResource<List<Category>, Response<ListCategoriesData>>(mAppExecutors) {
@Override
protected void saveCallResult(@NonNull Response<ListCategoriesData> item) {
db.beginTransaction();
try {
mMiscDao.insertCategories(item.getData().getCategories());
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
}
@Override
protected boolean shouldFetch(@Nullable List<Category> data) {
if (data == null || data.size() == 0) {
return true;
}
if (mustFetch) {
return true;
}
return false;
}
@NonNull
@Override
protected LiveData<List<Category>> loadFromDb() {
return mMiscDao.loadCategories();
}
@NonNull
@Override
protected LiveData<ApiResponse<Response<ListCategoriesData>>> createCall() {
return mApiService.getCategories(new Request("listCategories"));
}
}.asLiveData();
mCategoriesPublicLiveData.addSource(mFetchCategoriesLiveData, value -> {
mCategoriesPublicLiveData.setValue(value);
});
return mCategoriesPublicLiveData;
}
use of in.bugzy.data.remote.ApiResponse 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;
}
use of in.bugzy.data.remote.ApiResponse in project bugzy by cpunq.
the class CasesRepository method cases.
public LiveData<Resource<FilterCasesResult>> cases(final String filter, boolean sortChanged) {
return sorted(new NetworkBoundResource<FilterCasesResult, Response<ListCasesData>>(mAppExecutors) {
@Override
protected void saveCallResult(@NonNull Response<ListCasesData> item) {
db.beginTransaction();
try {
mCaseDao.upsertCases(item.getData().getCases());
mCaseDao.upsertFilterCaseIds(new FilterCasesResult(filter, item.getData().getCaseIds(), null));
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
}
@Override
protected boolean shouldFetch(@Nullable FilterCasesResult data) {
if (sortChanged) {
// network thing this time.
return false;
}
return true;
}
@NonNull
@Override
protected LiveData<FilterCasesResult> loadFromDb() {
return Transformations.switchMap(mCaseDao.loadCasesForFilter(filter), filterCasesData -> {
if (filterCasesData == null) {
return AbsentLiveData.create();
}
return Transformations.map(mCaseDao.loadCasesById(filterCasesData.getCaseIds()), kases -> {
Collections.sort(kases, new Comparator<Case>() {
@Override
public int compare(Case aCase, Case t1) {
return Long.compare(t1.getLastUpdatedAt().getTime(), aCase.getLastUpdatedAt().getTime());
}
});
filterCasesData.setCases(kases);
return filterCasesData;
});
});
}
@NonNull
@Override
protected LiveData<ApiResponse<Response<ListCasesData>>> createCall() {
ListCasesRequest request = new ListCasesRequest(mColsForCaseList, filter);
return mApiService.listCases(request);
}
}.asLiveData());
}
use of in.bugzy.data.remote.ApiResponse in project bugzy by cpunq.
the class Repository method getMilestones.
public LiveData<Resource<List<Milestone>>> getMilestones(boolean mustFetch) {
if (mFetchMilestonesLiveData != null) {
if (mFetchMilestonesLiveData.getValue().status == Status.LOADING && !mustFetch) {
// If the content is in loading state and the request doesn't require us to fetch again
return mMilestonesPublicLiveData;
}
mMilestonesPublicLiveData.removeSource(mFetchMilestonesLiveData);
mFetchMilestonesLiveData = null;
}
mFetchMilestonesLiveData = new NetworkBoundResource<List<Milestone>, Response<ListMilestonesData>>(mAppExecutors) {
@Override
protected void saveCallResult(@NonNull Response<ListMilestonesData> item) {
mSsRespository.updateMilestoneSearchSuggestion(item.getData().getMilestones());
db.beginTransaction();
try {
mMiscDao.insertMilestones(item.getData().getMilestones());
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
}
@Override
protected boolean shouldFetch(@Nullable List<Milestone> data) {
if (data == null || data.size() == 0) {
return true;
}
if (mustFetch) {
return true;
}
return false;
}
@NonNull
@Override
protected LiveData<List<Milestone>> loadFromDb() {
return mMiscDao.loadMilestones();
}
@NonNull
@Override
protected LiveData<ApiResponse<Response<ListMilestonesData>>> createCall() {
return mApiService.getMilestones(new Request("listFixFors"));
}
}.asLiveData();
mMilestonesPublicLiveData.addSource(mFetchMilestonesLiveData, value -> {
mMilestonesPublicLiveData.setValue(value);
});
return mMilestonesPublicLiveData;
}
use of in.bugzy.data.remote.ApiResponse 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