Search in sources :

Example 1 with NetworkBoundResource

use of com.bluestacks.bugzy.data.remote.NetworkBoundResource 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 : Response(com.bluestacks.bugzy.data.remote.model.Response) ApiResponse(com.bluestacks.bugzy.data.remote.ApiResponse) Milestone(com.bluestacks.bugzy.data.model.Milestone) ListMilestonesData(com.bluestacks.bugzy.data.remote.model.ListMilestonesData) 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) Nullable(android.support.annotation.Nullable) ApiResponse(com.bluestacks.bugzy.data.remote.ApiResponse)

Example 2 with NetworkBoundResource

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

the class Repository method getAreas.

public MediatorLiveData<Resource<List<Area>>> getAreas(boolean mustFetch) {
    if (mFetchAreasLiveData != null) {
        if (mFetchAreasLiveData.getValue().status == Status.LOADING && !mustFetch) {
            // If the content is in loading state and the request doesn't require us to fetch again
            return mAreasPublicLiveData;
        }
        mAreasPublicLiveData.removeSource(mFetchAreasLiveData);
        mFetchAreasLiveData = null;
    }
    mFetchAreasLiveData = new NetworkBoundResource<List<Area>, Response<ListAreasData>>(mAppExecutors) {

        @Override
        protected void saveCallResult(@NonNull Response<ListAreasData> item) {
            mSsRespository.updateAreaSearchSuggestion(item.getData().getAreas());
            db.beginTransaction();
            try {
                mMiscDao.insert(item.getData().getAreas());
                db.setTransactionSuccessful();
            } finally {
                db.endTransaction();
            }
        }

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

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

        @NonNull
        @Override
        protected LiveData<ApiResponse<Response<ListAreasData>>> createCall() {
            return mApiService.getAreas(new Request("listAreas"));
        }
    }.asLiveData();
    mAreasPublicLiveData.addSource(mFetchAreasLiveData, value -> {
        mAreasPublicLiveData.setValue(value);
    });
    return mAreasPublicLiveData;
}
Also used : Response(com.bluestacks.bugzy.data.remote.model.Response) ApiResponse(com.bluestacks.bugzy.data.remote.ApiResponse) Area(com.bluestacks.bugzy.data.model.Area) 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) ListAreasData(com.bluestacks.bugzy.data.remote.model.ListAreasData) Nullable(android.support.annotation.Nullable) ApiResponse(com.bluestacks.bugzy.data.remote.ApiResponse)

Example 3 with NetworkBoundResource

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

the class Repository method getCategories.

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

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

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

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

        @NonNull
        @Override
        protected LiveData<ApiResponse<Response<ListCategoriesData>>> createCall() {
            return mApiService.getCategories(new Request("listCategories"));
        }
    }.asLiveData();
    mCategoriesPublicLiveData.addSource(mFetchCategoriesLiveData, value -> {
        mCategoriesPublicLiveData.setValue(value);
    });
    return mCategoriesPublicLiveData;
}
Also used : ListCategoriesData(com.bluestacks.bugzy.data.remote.model.ListCategoriesData) Response(com.bluestacks.bugzy.data.remote.model.Response) ApiResponse(com.bluestacks.bugzy.data.remote.ApiResponse) Category(com.bluestacks.bugzy.data.model.Category) 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) Nullable(android.support.annotation.Nullable) ApiResponse(com.bluestacks.bugzy.data.remote.ApiResponse)

Example 4 with NetworkBoundResource

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

the class Repository method getProjects.

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

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

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

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

        @NonNull
        @Override
        protected LiveData<ApiResponse<Response<ListProjectsData>>> createCall() {
            return mApiService.getProjects(new Request("listProjects"));
        }
    }.asLiveData();
    mProjectsPublicLiveData.addSource(mFetchProjectsLiveData, value -> {
        mProjectsPublicLiveData.setValue(value);
    });
    return mProjectsPublicLiveData;
}
Also used : Response(com.bluestacks.bugzy.data.remote.model.Response) ApiResponse(com.bluestacks.bugzy.data.remote.ApiResponse) Project(com.bluestacks.bugzy.data.model.Project) 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) ListProjectsData(com.bluestacks.bugzy.data.remote.model.ListProjectsData) Nullable(android.support.annotation.Nullable) ApiResponse(com.bluestacks.bugzy.data.remote.ApiResponse)

Example 5 with NetworkBoundResource

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

the class CasesRepository method searchCases.

public LiveData<SearchResultsResource<List<Case>>> searchCases(final String query) {
    this.recordResearch(query);
    return Transformations.map(new NetworkBoundResource<List<Case>, Response<ListCasesData>>(mAppExecutors) {

        List<Case> mCases = null;

        @Override
        protected void saveCallResult(@NonNull Response<ListCasesData> item) {
            mCases = item.getData().getCases();
        }

        @Override
        protected boolean shouldFetch(@Nullable List<Case> data) {
            // Fetch always
            return true;
        }

        @NonNull
        @Override
        protected LiveData<List<Case>> loadFromDb() {
            return new LiveData<List<Case>>() {

                @Override
                protected void onActive() {
                    super.onActive();
                    setValue(mCases);
                }
            };
        }

        @NonNull
        @Override
        protected LiveData<ApiResponse<Response<ListCasesData>>> createCall() {
            String[] cols = new String[] { "sTitle", "ixPriority", "sStatus", "sProject", "sFixFor", "sArea", "sPersonAssignedTo", "sPersonOpenedBy" };
            return mApiService.searchCases(new SearchCasesRequest(cols, query));
        }
    }.asLiveData(), v -> {
        return new SearchResultsResource<>(query, v);
    });
}
Also used : AbsentLiveData(com.bluestacks.bugzy.ui.search.AbsentLiveData) LiveData(android.arch.lifecycle.LiveData) MutableLiveData(android.arch.lifecycle.MutableLiveData) SearchCasesRequest(com.bluestacks.bugzy.data.remote.model.SearchCasesRequest) ApiResponse(com.bluestacks.bugzy.data.remote.ApiResponse) Case(com.bluestacks.bugzy.data.model.Case) Response(com.bluestacks.bugzy.data.remote.model.Response) ApiResponse(com.bluestacks.bugzy.data.remote.ApiResponse) ListCasesData(com.bluestacks.bugzy.data.remote.model.ListCasesData) NonNull(android.support.annotation.NonNull) SearchResultsResource(com.bluestacks.bugzy.data.model.SearchResultsResource) NetworkBoundResource(com.bluestacks.bugzy.data.remote.NetworkBoundResource) ArrayList(java.util.ArrayList) List(java.util.List) Nullable(android.support.annotation.Nullable)

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