use of com.bluestacks.bugzy.data.remote.NetworkBoundResource 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 -> {
filterCasesData.setCases(kases);
return filterCasesData;
});
});
}
@NonNull
@Override
protected LiveData<ApiResponse<Response<ListCasesData>>> createCall() {
String[] cols = new String[] { "sTitle", "ixPriority", "sStatus", "sProject", "sFixFor", "sPersonAssignedTo", "sPersonOpenedBy", "sArea", "sCategory" };
ListCasesRequest request = new ListCasesRequest(cols, filter);
return mApiService.listCases(request);
}
}.asLiveData());
}
use of com.bluestacks.bugzy.data.remote.NetworkBoundResource 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;
}
use of com.bluestacks.bugzy.data.remote.NetworkBoundResource in project bugzy by cpunq.
the class Repository method getPriorities.
public LiveData<Resource<List<Priority>>> getPriorities(boolean mustFetch) {
if (mFetchPrioritiesLiveData != null) {
if (mFetchPrioritiesLiveData.getValue().status == Status.LOADING && !mustFetch) {
// If the content is in loading state and the request doesn't require us to fetch again
return mPrioritiesPublicLiveData;
}
mPrioritiesPublicLiveData.removeSource(mFetchPrioritiesLiveData);
mFetchPrioritiesLiveData = null;
}
mFetchPrioritiesLiveData = new NetworkBoundResource<List<Priority>, Response<ListPrioritiesData>>(mAppExecutors) {
@Override
protected void saveCallResult(@NonNull Response<ListPrioritiesData> item) {
db.beginTransaction();
try {
mMiscDao.insertPriorities(item.getData().getPriorities());
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
}
@Override
protected boolean shouldFetch(@Nullable List<Priority> data) {
if (data == null || data.size() == 0) {
return true;
}
if (mustFetch) {
return true;
}
return false;
}
@NonNull
@Override
protected LiveData<List<Priority>> loadFromDb() {
return mMiscDao.loadPriorities();
}
@NonNull
@Override
protected LiveData<ApiResponse<Response<ListPrioritiesData>>> createCall() {
return mApiService.getPriorities(new Request("listPriorities"));
}
}.asLiveData();
mPrioritiesPublicLiveData.addSource(mFetchPrioritiesLiveData, value -> {
mPrioritiesPublicLiveData.setValue(value);
});
return mPrioritiesPublicLiveData;
}
use of com.bluestacks.bugzy.data.remote.NetworkBoundResource 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