Search in sources :

Example 6 with Request

use of com.bluestacks.bugzy.data.remote.model.Request in project bugzy by cpunq.

the class Repository method getStatuses.

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

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

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

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

        @NonNull
        @Override
        protected LiveData<ApiResponse<Response<ListStatusesData>>> createCall() {
            return mApiService.getStatuses(new Request("listStatuses"));
        }
    }.asLiveData();
    mStatusesPublicLiveData.addSource(mFetchStatusesLiveData, value -> {
        mStatusesPublicLiveData.setValue(value);
    });
    return mStatusesPublicLiveData;
}
Also used : Response(com.bluestacks.bugzy.data.remote.model.Response) ApiResponse(com.bluestacks.bugzy.data.remote.ApiResponse) CaseStatus(com.bluestacks.bugzy.data.model.CaseStatus) 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) ListStatusesData(com.bluestacks.bugzy.data.remote.model.ListStatusesData) NetworkBoundResource(com.bluestacks.bugzy.data.remote.NetworkBoundResource) List(java.util.List) ArrayList(java.util.ArrayList) Nullable(android.support.annotation.Nullable) ApiResponse(com.bluestacks.bugzy.data.remote.ApiResponse)

Aggregations

NonNull (android.support.annotation.NonNull)6 Nullable (android.support.annotation.Nullable)6 ApiResponse (com.bluestacks.bugzy.data.remote.ApiResponse)6 NetworkBoundResource (com.bluestacks.bugzy.data.remote.NetworkBoundResource)6 FiltersRequest (com.bluestacks.bugzy.data.remote.model.FiltersRequest)6 ListPeopleRequest (com.bluestacks.bugzy.data.remote.model.ListPeopleRequest)6 LoginRequest (com.bluestacks.bugzy.data.remote.model.LoginRequest)6 MyDetailsRequest (com.bluestacks.bugzy.data.remote.model.MyDetailsRequest)6 Request (com.bluestacks.bugzy.data.remote.model.Request)6 Response (com.bluestacks.bugzy.data.remote.model.Response)6 ArrayList (java.util.ArrayList)6 List (java.util.List)6 Area (com.bluestacks.bugzy.data.model.Area)1 CaseStatus (com.bluestacks.bugzy.data.model.CaseStatus)1 Category (com.bluestacks.bugzy.data.model.Category)1 Milestone (com.bluestacks.bugzy.data.model.Milestone)1 Priority (com.bluestacks.bugzy.data.model.Priority)1 Project (com.bluestacks.bugzy.data.model.Project)1 ListAreasData (com.bluestacks.bugzy.data.remote.model.ListAreasData)1 ListCategoriesData (com.bluestacks.bugzy.data.remote.model.ListCategoriesData)1