Search in sources :

Example 1 with ListPeopleData

use of com.bluestacks.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 : Response(com.bluestacks.bugzy.data.remote.model.Response) ApiResponse(com.bluestacks.bugzy.data.remote.ApiResponse) ListPeopleRequest(com.bluestacks.bugzy.data.remote.model.ListPeopleRequest) NonNull(android.support.annotation.NonNull) ListPeopleData(com.bluestacks.bugzy.data.remote.model.ListPeopleData) NetworkBoundResource(com.bluestacks.bugzy.data.remote.NetworkBoundResource) List(java.util.List) ArrayList(java.util.ArrayList) Person(com.bluestacks.bugzy.data.model.Person) 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 Person (com.bluestacks.bugzy.data.model.Person)1 ApiResponse (com.bluestacks.bugzy.data.remote.ApiResponse)1 NetworkBoundResource (com.bluestacks.bugzy.data.remote.NetworkBoundResource)1 ListPeopleData (com.bluestacks.bugzy.data.remote.model.ListPeopleData)1 ListPeopleRequest (com.bluestacks.bugzy.data.remote.model.ListPeopleRequest)1 Response (com.bluestacks.bugzy.data.remote.model.Response)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1