Search in sources :

Example 1 with ListPeopleData

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

Aggregations

NonNull (android.support.annotation.NonNull)1 Nullable (android.support.annotation.Nullable)1 Person (in.bugzy.data.model.Person)1 ApiResponse (in.bugzy.data.remote.ApiResponse)1 NetworkBoundResource (in.bugzy.data.remote.NetworkBoundResource)1 ListPeopleData (in.bugzy.data.remote.model.ListPeopleData)1 ListPeopleRequest (in.bugzy.data.remote.model.ListPeopleRequest)1 Response (in.bugzy.data.remote.model.Response)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1