Search in sources :

Example 1 with ApiResponse

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

use of in.bugzy.data.remote.ApiResponse 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;
}
Also used : ApiResponse(in.bugzy.data.remote.ApiResponse) Response(in.bugzy.data.remote.model.Response) ListPeopleRequest(in.bugzy.data.remote.model.ListPeopleRequest) NonNull(android.support.annotation.NonNull) ListPeopleData(in.bugzy.data.remote.model.ListPeopleData) NetworkBoundResource(in.bugzy.data.remote.NetworkBoundResource) List(java.util.List) ArrayList(java.util.ArrayList) Person(in.bugzy.data.model.Person) Nullable(android.support.annotation.Nullable) ApiResponse(in.bugzy.data.remote.ApiResponse)

Example 3 with ApiResponse

use of in.bugzy.data.remote.ApiResponse in project bugzy by cpunq.

the class Repository method getProjects.

public LiveData<Resource<List<Project>>> getProjects(boolean mustFetch) {
    if (mFetchProjectsLiveData != null) {
        if (mFetchProjectsLiveData.getValue().status == Status.LOADING && !mustFetch) {
            // If the content is in loading state and the request doesn't require us to fetch again
            return mProjectsPublicLiveData;
        }
        mProjectsPublicLiveData.removeSource(mFetchProjectsLiveData);
        mFetchProjectsLiveData = null;
    }
    mFetchProjectsLiveData = new NetworkBoundResource<List<Project>, Response<ListProjectsData>>(mAppExecutors) {

        @Override
        protected void saveCallResult(@NonNull Response<ListProjectsData> item) {
            db.beginTransaction();
            try {
                mMiscDao.insertProjects(item.getData().getProjects());
                db.setTransactionSuccessful();
            } finally {
                db.endTransaction();
            }
        }

        @Override
        protected boolean shouldFetch(@Nullable List<Project> data) {
            if (data == null || data.size() == 0) {
                return true;
            }
            if (mustFetch) {
                return true;
            }
            return false;
        }

        @NonNull
        @Override
        protected LiveData<List<Project>> loadFromDb() {
            return mMiscDao.loadProjects();
        }

        @NonNull
        @Override
        protected LiveData<ApiResponse<Response<ListProjectsData>>> createCall() {
            return mApiService.getProjects(new Request("listProjects"));
        }
    }.asLiveData();
    mProjectsPublicLiveData.addSource(mFetchProjectsLiveData, value -> {
        mProjectsPublicLiveData.setValue(value);
    });
    return mProjectsPublicLiveData;
}
Also used : ApiResponse(in.bugzy.data.remote.ApiResponse) Response(in.bugzy.data.remote.model.Response) Project(in.bugzy.data.model.Project) NonNull(android.support.annotation.NonNull) FiltersRequest(in.bugzy.data.remote.model.FiltersRequest) LoginRequest(in.bugzy.data.remote.model.LoginRequest) Request(in.bugzy.data.remote.model.Request) ListPeopleRequest(in.bugzy.data.remote.model.ListPeopleRequest) MyDetailsRequest(in.bugzy.data.remote.model.MyDetailsRequest) NetworkBoundResource(in.bugzy.data.remote.NetworkBoundResource) List(java.util.List) ArrayList(java.util.ArrayList) ListProjectsData(in.bugzy.data.remote.model.ListProjectsData) Nullable(android.support.annotation.Nullable) ApiResponse(in.bugzy.data.remote.ApiResponse)

Example 4 with ApiResponse

use of in.bugzy.data.remote.ApiResponse 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;
}
Also used : ApiResponse(in.bugzy.data.remote.ApiResponse) Response(in.bugzy.data.remote.model.Response) Priority(in.bugzy.data.model.Priority) NonNull(android.support.annotation.NonNull) FiltersRequest(in.bugzy.data.remote.model.FiltersRequest) LoginRequest(in.bugzy.data.remote.model.LoginRequest) Request(in.bugzy.data.remote.model.Request) ListPeopleRequest(in.bugzy.data.remote.model.ListPeopleRequest) MyDetailsRequest(in.bugzy.data.remote.model.MyDetailsRequest) NetworkBoundResource(in.bugzy.data.remote.NetworkBoundResource) List(java.util.List) ArrayList(java.util.ArrayList) ListPrioritiesData(in.bugzy.data.remote.model.ListPrioritiesData) Nullable(android.support.annotation.Nullable) ApiResponse(in.bugzy.data.remote.ApiResponse)

Example 5 with ApiResponse

use of in.bugzy.data.remote.ApiResponse in project bugzy by cpunq.

the class Repository method getAreas.

public MediatorLiveData<Resource<List<Area>>> getAreas(boolean mustFetch) {
    if (mFetchAreasLiveData != null) {
        if (mFetchAreasLiveData.getValue().status == Status.LOADING && !mustFetch) {
            // If the content is in loading state and the request doesn't require us to fetch again
            return mAreasPublicLiveData;
        }
        mAreasPublicLiveData.removeSource(mFetchAreasLiveData);
        mFetchAreasLiveData = null;
    }
    mFetchAreasLiveData = new NetworkBoundResource<List<Area>, Response<ListAreasData>>(mAppExecutors) {

        @Override
        protected void saveCallResult(@NonNull Response<ListAreasData> item) {
            mSsRespository.updateAreaSearchSuggestion(item.getData().getAreas());
            db.beginTransaction();
            try {
                mMiscDao.insert(item.getData().getAreas());
                db.setTransactionSuccessful();
            } finally {
                db.endTransaction();
            }
        }

        @Override
        protected boolean shouldFetch(@Nullable List<Area> data) {
            if (data == null || data.size() == 0) {
                return true;
            }
            if (mustFetch) {
                return true;
            }
            return false;
        }

        @NonNull
        @Override
        protected LiveData<List<Area>> loadFromDb() {
            return mMiscDao.loadAreas();
        }

        @NonNull
        @Override
        protected LiveData<ApiResponse<Response<ListAreasData>>> createCall() {
            return mApiService.getAreas(new Request("listAreas"));
        }
    }.asLiveData();
    mAreasPublicLiveData.addSource(mFetchAreasLiveData, value -> {
        mAreasPublicLiveData.setValue(value);
    });
    return mAreasPublicLiveData;
}
Also used : ApiResponse(in.bugzy.data.remote.ApiResponse) Response(in.bugzy.data.remote.model.Response) Area(in.bugzy.data.model.Area) NonNull(android.support.annotation.NonNull) FiltersRequest(in.bugzy.data.remote.model.FiltersRequest) LoginRequest(in.bugzy.data.remote.model.LoginRequest) Request(in.bugzy.data.remote.model.Request) ListPeopleRequest(in.bugzy.data.remote.model.ListPeopleRequest) MyDetailsRequest(in.bugzy.data.remote.model.MyDetailsRequest) NetworkBoundResource(in.bugzy.data.remote.NetworkBoundResource) List(java.util.List) ArrayList(java.util.ArrayList) ListAreasData(in.bugzy.data.remote.model.ListAreasData) Nullable(android.support.annotation.Nullable) ApiResponse(in.bugzy.data.remote.ApiResponse)

Aggregations

NonNull (android.support.annotation.NonNull)10 ApiResponse (in.bugzy.data.remote.ApiResponse)10 NetworkBoundResource (in.bugzy.data.remote.NetworkBoundResource)10 Response (in.bugzy.data.remote.model.Response)10 Nullable (android.support.annotation.Nullable)9 ListPeopleRequest (in.bugzy.data.remote.model.ListPeopleRequest)8 ArrayList (java.util.ArrayList)8 List (java.util.List)8 FiltersRequest (in.bugzy.data.remote.model.FiltersRequest)7 LoginRequest (in.bugzy.data.remote.model.LoginRequest)7 MyDetailsRequest (in.bugzy.data.remote.model.MyDetailsRequest)7 Request (in.bugzy.data.remote.model.Request)7 ListCasesData (in.bugzy.data.remote.model.ListCasesData)2 Area (in.bugzy.data.model.Area)1 Case (in.bugzy.data.model.Case)1 CaseStatus (in.bugzy.data.model.CaseStatus)1 Category (in.bugzy.data.model.Category)1 FilterCasesResult (in.bugzy.data.model.FilterCasesResult)1 Milestone (in.bugzy.data.model.Milestone)1 Person (in.bugzy.data.model.Person)1