Search in sources :

Example 11 with Response

use of in.bugzy.data.remote.model.Response in project bugzy by cpunq.

the class Repository method getMilestones.

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

        @Override
        protected void saveCallResult(@NonNull Response<ListMilestonesData> item) {
            mSsRespository.updateMilestoneSearchSuggestion(item.getData().getMilestones());
            db.beginTransaction();
            try {
                mMiscDao.insertMilestones(item.getData().getMilestones());
                db.setTransactionSuccessful();
            } finally {
                db.endTransaction();
            }
        }

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

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

        @NonNull
        @Override
        protected LiveData<ApiResponse<Response<ListMilestonesData>>> createCall() {
            return mApiService.getMilestones(new Request("listFixFors"));
        }
    }.asLiveData();
    mMilestonesPublicLiveData.addSource(mFetchMilestonesLiveData, value -> {
        mMilestonesPublicLiveData.setValue(value);
    });
    return mMilestonesPublicLiveData;
}
Also used : ApiResponse(in.bugzy.data.remote.ApiResponse) Response(in.bugzy.data.remote.model.Response) Milestone(in.bugzy.data.model.Milestone) ListMilestonesData(in.bugzy.data.remote.model.ListMilestonesData) 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) Nullable(android.support.annotation.Nullable) ApiResponse(in.bugzy.data.remote.ApiResponse)

Example 12 with Response

use of in.bugzy.data.remote.model.Response in project bugzy by cpunq.

the class Repository method getTags.

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

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

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

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

        @NonNull
        @Override
        protected LiveData<ApiResponse<Response<ListTagsData>>> createCall() {
            return mApiService.getTags(new Request(""));
        }
    }.asLiveData();
    mTagsPublicLiveData.addSource(mFetchTagsLiveData, value -> {
        mTagsPublicLiveData.setValue(value);
    });
    return mTagsPublicLiveData;
}
Also used : ListTagsData(in.bugzy.data.remote.model.ListTagsData) ApiResponse(in.bugzy.data.remote.ApiResponse) Response(in.bugzy.data.remote.model.Response) 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) Tag(in.bugzy.data.model.Tag) Nullable(android.support.annotation.Nullable) ApiResponse(in.bugzy.data.remote.ApiResponse)

Aggregations

NonNull (android.support.annotation.NonNull)12 ApiResponse (in.bugzy.data.remote.ApiResponse)12 Response (in.bugzy.data.remote.model.Response)12 NetworkBoundResource (in.bugzy.data.remote.NetworkBoundResource)10 Nullable (android.support.annotation.Nullable)9 ArrayList (java.util.ArrayList)9 ListPeopleRequest (in.bugzy.data.remote.model.ListPeopleRequest)8 LoginRequest (in.bugzy.data.remote.model.LoginRequest)8 List (java.util.List)8 FiltersRequest (in.bugzy.data.remote.model.FiltersRequest)7 MyDetailsRequest (in.bugzy.data.remote.model.MyDetailsRequest)7 Request (in.bugzy.data.remote.model.Request)7 NetworkBoundTask (in.bugzy.data.remote.NetworkBoundTask)3 ListCasesData (in.bugzy.data.remote.model.ListCasesData)2 Area (in.bugzy.data.model.Area)1 Attachment (in.bugzy.data.model.Attachment)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