Search in sources :

Example 6 with NetworkBoundResource

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

the class CasesRepository method cases.

public LiveData<Resource<FilterCasesResult>> cases(final String filter, boolean sortChanged) {
    return sorted(new NetworkBoundResource<FilterCasesResult, Response<ListCasesData>>(mAppExecutors) {

        @Override
        protected void saveCallResult(@NonNull Response<ListCasesData> item) {
            db.beginTransaction();
            try {
                mCaseDao.upsertCases(item.getData().getCases());
                mCaseDao.upsertFilterCaseIds(new FilterCasesResult(filter, item.getData().getCaseIds(), null));
                db.setTransactionSuccessful();
            } finally {
                db.endTransaction();
            }
        }

        @Override
        protected boolean shouldFetch(@Nullable FilterCasesResult data) {
            if (sortChanged) {
                // network thing this time.
                return false;
            }
            return true;
        }

        @NonNull
        @Override
        protected LiveData<FilterCasesResult> loadFromDb() {
            return Transformations.switchMap(mCaseDao.loadCasesForFilter(filter), filterCasesData -> {
                if (filterCasesData == null) {
                    return AbsentLiveData.create();
                }
                return Transformations.map(mCaseDao.loadCasesById(filterCasesData.getCaseIds()), kases -> {
                    filterCasesData.setCases(kases);
                    return filterCasesData;
                });
            });
        }

        @NonNull
        @Override
        protected LiveData<ApiResponse<Response<ListCasesData>>> createCall() {
            String[] cols = new String[] { "sTitle", "ixPriority", "sStatus", "sProject", "sFixFor", "sPersonAssignedTo", "sPersonOpenedBy", "sArea", "sCategory" };
            ListCasesRequest request = new ListCasesRequest(cols, filter);
            return mApiService.listCases(request);
        }
    }.asLiveData());
}
Also used : Response(com.bluestacks.bugzy.data.remote.model.Response) ApiResponse(com.bluestacks.bugzy.data.remote.ApiResponse) ListCasesData(com.bluestacks.bugzy.data.remote.model.ListCasesData) ListCasesRequest(com.bluestacks.bugzy.data.remote.model.ListCasesRequest) NonNull(android.support.annotation.NonNull) NetworkBoundResource(com.bluestacks.bugzy.data.remote.NetworkBoundResource) FilterCasesResult(com.bluestacks.bugzy.data.model.FilterCasesResult) Nullable(android.support.annotation.Nullable) ApiResponse(com.bluestacks.bugzy.data.remote.ApiResponse)

Example 7 with NetworkBoundResource

use of com.bluestacks.bugzy.data.remote.NetworkBoundResource 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)

Example 8 with NetworkBoundResource

use of com.bluestacks.bugzy.data.remote.NetworkBoundResource 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)

Example 9 with NetworkBoundResource

use of com.bluestacks.bugzy.data.remote.NetworkBoundResource 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)9 Nullable (android.support.annotation.Nullable)9 ApiResponse (com.bluestacks.bugzy.data.remote.ApiResponse)9 NetworkBoundResource (com.bluestacks.bugzy.data.remote.NetworkBoundResource)9 Response (com.bluestacks.bugzy.data.remote.model.Response)9 ArrayList (java.util.ArrayList)8 List (java.util.List)8 ListPeopleRequest (com.bluestacks.bugzy.data.remote.model.ListPeopleRequest)7 FiltersRequest (com.bluestacks.bugzy.data.remote.model.FiltersRequest)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 ListCasesData (com.bluestacks.bugzy.data.remote.model.ListCasesData)2 LiveData (android.arch.lifecycle.LiveData)1 MutableLiveData (android.arch.lifecycle.MutableLiveData)1 Area (com.bluestacks.bugzy.data.model.Area)1 Case (com.bluestacks.bugzy.data.model.Case)1 CaseStatus (com.bluestacks.bugzy.data.model.CaseStatus)1 Category (com.bluestacks.bugzy.data.model.Category)1 FilterCasesResult (com.bluestacks.bugzy.data.model.FilterCasesResult)1