Search in sources :

Example 6 with Case

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

Aggregations

Case (in.bugzy.data.model.Case)6 Bundle (android.os.Bundle)2 Transaction (android.arch.persistence.room.Transaction)1 Intent (android.content.Intent)1 NonNull (android.support.annotation.NonNull)1 Nullable (android.support.annotation.Nullable)1 UiThread (android.support.annotation.UiThread)1 FragmentTransaction (android.support.v4.app.FragmentTransaction)1 CaseEvent (in.bugzy.data.model.CaseEvent)1 FilterCasesResult (in.bugzy.data.model.FilterCasesResult)1 ApiResponse (in.bugzy.data.remote.ApiResponse)1 NetworkBoundResource (in.bugzy.data.remote.NetworkBoundResource)1 ListCasesData (in.bugzy.data.remote.model.ListCasesData)1 ListCasesRequest (in.bugzy.data.remote.model.ListCasesRequest)1 Response (in.bugzy.data.remote.model.Response)1