Search in sources :

Example 1 with ListPrioritiesData

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