Search in sources :

Example 1 with ListCasesRequest

use of com.bluestacks.bugzy.data.remote.model.ListCasesRequest 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());
}
Also used : Response(com.bluestacks.bugzy.data.remote.model.Response) ApiResponse(com.bluestacks.bugzy.data.remote.ApiResponse) ListCasesData(com.bluestacks.bugzy.data.remote.model.ListCasesData) ListCasesRequest(com.bluestacks.bugzy.data.remote.model.ListCasesRequest) NonNull(android.support.annotation.NonNull) NetworkBoundResource(com.bluestacks.bugzy.data.remote.NetworkBoundResource) FilterCasesResult(com.bluestacks.bugzy.data.model.FilterCasesResult) Nullable(android.support.annotation.Nullable) ApiResponse(com.bluestacks.bugzy.data.remote.ApiResponse)

Aggregations

NonNull (android.support.annotation.NonNull)1 Nullable (android.support.annotation.Nullable)1 FilterCasesResult (com.bluestacks.bugzy.data.model.FilterCasesResult)1 ApiResponse (com.bluestacks.bugzy.data.remote.ApiResponse)1 NetworkBoundResource (com.bluestacks.bugzy.data.remote.NetworkBoundResource)1 ListCasesData (com.bluestacks.bugzy.data.remote.model.ListCasesData)1 ListCasesRequest (com.bluestacks.bugzy.data.remote.model.ListCasesRequest)1 Response (com.bluestacks.bugzy.data.remote.model.Response)1