Search in sources :

Example 1 with Request

use of in.bugzy.data.remote.model.Request 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 : ApiResponse(in.bugzy.data.remote.ApiResponse) Response(in.bugzy.data.remote.model.Response) Project(in.bugzy.data.model.Project) 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) ListProjectsData(in.bugzy.data.remote.model.ListProjectsData) Nullable(android.support.annotation.Nullable) ApiResponse(in.bugzy.data.remote.ApiResponse)

Example 2 with Request

use of in.bugzy.data.remote.model.Request 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 : ApiResponse(in.bugzy.data.remote.ApiResponse) Response(in.bugzy.data.remote.model.Response) Priority(in.bugzy.data.model.Priority) 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) ListPrioritiesData(in.bugzy.data.remote.model.ListPrioritiesData) Nullable(android.support.annotation.Nullable) ApiResponse(in.bugzy.data.remote.ApiResponse)

Example 3 with Request

use of in.bugzy.data.remote.model.Request 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 : ApiResponse(in.bugzy.data.remote.ApiResponse) Response(in.bugzy.data.remote.model.Response) Area(in.bugzy.data.model.Area) 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) ListAreasData(in.bugzy.data.remote.model.ListAreasData) Nullable(android.support.annotation.Nullable) ApiResponse(in.bugzy.data.remote.ApiResponse)

Example 4 with Request

use of in.bugzy.data.remote.model.Request 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(in.bugzy.data.remote.model.ListCategoriesData) ApiResponse(in.bugzy.data.remote.ApiResponse) Response(in.bugzy.data.remote.model.Response) Category(in.bugzy.data.model.Category) 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 5 with Request

use of in.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 : ApiResponse(in.bugzy.data.remote.ApiResponse) Response(in.bugzy.data.remote.model.Response) CaseStatus(in.bugzy.data.model.CaseStatus) 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) ListStatusesData(in.bugzy.data.remote.model.ListStatusesData) 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)

Aggregations

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