Search in sources :

Example 1 with ListCasesData

use of in.bugzy.data.remote.model.ListCasesData in project bugzy by cpunq.

the class CasesRepository method searchCases.

public LiveData<SearchResultsResource<ListCasesData>> searchCases(final String query) {
    this.recordResearch(query);
    NetworkBoundTask<Response<ListCasesData>> task = new NetworkBoundTask<Response<ListCasesData>>(mAppExecutors, mGson) {

        @Override
        public void saveCallResult(@NonNull Response<ListCasesData> result) {
        }

        @NonNull
        @Override
        protected Call<Response<ListCasesData>> createCall() {
            return mApiService.searchCasesCall(new SearchCasesRequest(mColsForCaseList, query));
        }
    };
    mAppExecutors.networkIO().execute(task);
    return Transformations.map(task.asLiveData(), v -> {
        Resource<ListCasesData> resource = new Resource(v.status, v.data == null ? null : v.data.getData(), v.message);
        return new SearchResultsResource<>(query, resource);
    });
// return Transformations.map(new NetworkBoundResource<List<Case>, Response<ListCasesData>>(mAppExecutors) {
// List<Case> mCases = null;
// 
// @Override
// protected void saveCallResult(@NonNull Response<ListCasesData> item) {
// mCases = item.getData().getCases();
// }
// 
// @Override
// protected boolean shouldFetch(@Nullable List<Case> data) {
// // Fetch always
// return true;
// }
// 
// @NonNull
// @Override
// protected LiveData<List<Case>> loadFromDb() {
// return new LiveData<List<Case>>() {
// @Override
// protected void onActive() {
// super.onActive();
// setValue(mCases);
// }
// };
// }
// 
// @NonNull
// @Override
// protected LiveData<ApiResponse<Response<ListCasesData>>> createCall() {
// return mApiService.searchCases(new SearchCasesRequest(mColsForCaseList, query));
// }
// }.asLiveData(), v -> {
// return new SearchResultsResource<>(query, v);
// });
}
Also used : ApiResponse(in.bugzy.data.remote.ApiResponse) Response(in.bugzy.data.remote.model.Response) ListCasesData(in.bugzy.data.remote.model.ListCasesData) NonNull(android.support.annotation.NonNull) NetworkBoundResource(in.bugzy.data.remote.NetworkBoundResource) SearchResultsResource(in.bugzy.data.model.SearchResultsResource) Resource(in.bugzy.data.model.Resource) SearchCasesRequest(in.bugzy.data.remote.model.SearchCasesRequest) SearchResultsResource(in.bugzy.data.model.SearchResultsResource) NetworkBoundTask(in.bugzy.data.remote.NetworkBoundTask)

Example 2 with ListCasesData

use of in.bugzy.data.remote.model.ListCasesData 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

NonNull (android.support.annotation.NonNull)2 ApiResponse (in.bugzy.data.remote.ApiResponse)2 NetworkBoundResource (in.bugzy.data.remote.NetworkBoundResource)2 ListCasesData (in.bugzy.data.remote.model.ListCasesData)2 Response (in.bugzy.data.remote.model.Response)2 Nullable (android.support.annotation.Nullable)1 Case (in.bugzy.data.model.Case)1 FilterCasesResult (in.bugzy.data.model.FilterCasesResult)1 Resource (in.bugzy.data.model.Resource)1 SearchResultsResource (in.bugzy.data.model.SearchResultsResource)1 NetworkBoundTask (in.bugzy.data.remote.NetworkBoundTask)1 ListCasesRequest (in.bugzy.data.remote.model.ListCasesRequest)1 SearchCasesRequest (in.bugzy.data.remote.model.SearchCasesRequest)1