Search in sources :

Example 6 with Response

use of in.bugzy.data.remote.model.Response 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 7 with Response

use of in.bugzy.data.remote.model.Response 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)

Example 8 with Response

use of in.bugzy.data.remote.model.Response 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 -> {
                    Collections.sort(kases, new Comparator<Case>() {

                        @Override
                        public int compare(Case aCase, Case t1) {
                            return Long.compare(t1.getLastUpdatedAt().getTime(), aCase.getLastUpdatedAt().getTime());
                        }
                    });
                    filterCasesData.setCases(kases);
                    return filterCasesData;
                });
            });
        }

        @NonNull
        @Override
        protected LiveData<ApiResponse<Response<ListCasesData>>> createCall() {
            ListCasesRequest request = new ListCasesRequest(mColsForCaseList, filter);
            return mApiService.listCases(request);
        }
    }.asLiveData());
}
Also used : ApiResponse(in.bugzy.data.remote.ApiResponse) Response(in.bugzy.data.remote.model.Response) ListCasesData(in.bugzy.data.remote.model.ListCasesData) ListCasesRequest(in.bugzy.data.remote.model.ListCasesRequest) NonNull(android.support.annotation.NonNull) NetworkBoundResource(in.bugzy.data.remote.NetworkBoundResource) FilterCasesResult(in.bugzy.data.model.FilterCasesResult) Nullable(android.support.annotation.Nullable) ApiResponse(in.bugzy.data.remote.ApiResponse) Case(in.bugzy.data.model.Case)

Example 9 with Response

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

the class CasesRepository method editCase.

public LiveData<Resource<Response<EditCaseData>>> editCase(final CaseEditRequest request, int mode, List<Attachment> attachments) {
    NetworkBoundTask<Response<EditCaseData>> task = new NetworkBoundTask<Response<EditCaseData>>(mAppExecutors, mGson) {

        @Override
        public void saveCallResult(@NonNull Response<EditCaseData> result) {
            // Save?? ;)
            if (result.getData() == null) {
                return;
            }
        }

        @NonNull
        @Override
        protected Call<Response<EditCaseData>> createCall() {
            List<MultipartBody.Part> fileParts = new ArrayList<>();
            int i = 0;
            for (Attachment attachment : attachments) {
                fileParts.add(prepareFilePart("File" + i, attachment.getUri()));
                i++;
            }
            request.setFileCount(fileParts.size());
            request.setToken(mPrefs.getString(PrefsHelper.Key.ACCESS_TOKEN));
            switch(mode) {
                case CaseEditActivity.MODE_EDIT:
                case CaseEditActivity.MODE_ASSIGN:
                    return mApiService.editCase(request, fileParts);
                case CaseEditActivity.MODE_NEW:
                    return mApiService.newCase(request, fileParts);
                case CaseEditActivity.MODE_CLOSE:
                    return mApiService.closeCase(request, fileParts);
                case CaseEditActivity.MODE_RESOLVE:
                    return mApiService.resolveCase(request, fileParts);
                case CaseEditActivity.MODE_REACTIVATE:
                    return mApiService.reactivateCase(request, fileParts);
                case CaseEditActivity.MODE_REOPEN:
                    return mApiService.reopenCase(request, fileParts);
                default:
                    return mApiService.editCase(request, fileParts);
            }
        }
    };
    mAppExecutors.networkIO().execute(task);
    return task.asLiveData();
}
Also used : ApiResponse(in.bugzy.data.remote.ApiResponse) Response(in.bugzy.data.remote.model.Response) EditCaseData(in.bugzy.data.remote.model.EditCaseData) NonNull(android.support.annotation.NonNull) ArrayList(java.util.ArrayList) Attachment(in.bugzy.data.model.Attachment) NetworkBoundTask(in.bugzy.data.remote.NetworkBoundTask)

Example 10 with Response

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

the class Repository method login.

public LiveData<Resource<Response<LoginData>>> login(String email, String password, String organisation) {
    mHostSelectionInterceptor.setHost(organisation.toLowerCase() + ".manuscript.com");
    NetworkBoundTask<Response<LoginData>> task = new NetworkBoundTask<Response<LoginData>>(mAppExecutors, mGson) {

        @Override
        public void saveCallResult(@NonNull Response<LoginData> result) {
            String token = result.getData().getToken();
            mPrefs.setString(PrefsHelper.Key.ORGANISATION, organisation.toLowerCase());
            mPrefs.setString(PrefsHelper.Key.ACCESS_TOKEN, token);
            mPrefs.setBoolean(PrefsHelper.Key.USER_LOGGED_IN, true);
            mSsRespository.insertDefaultSearchSuggestions();
            mBugzyUrlGenerator.setToken(token);
            mBugzyUrlGenerator.setOrganisationName(organisation);
            mAppExecutors.mainThread().execute(new Runnable() {

                @Override
                public void run() {
                    mToken.setValue(token);
                }
            });
        }

        @NonNull
        @Override
        protected Call<Response<LoginData>> createCall() {
            return mApiService.login(new LoginRequest(email, password));
        }
    };
    mAppExecutors.networkIO().execute(task);
    return task.asLiveData();
}
Also used : ApiResponse(in.bugzy.data.remote.ApiResponse) Response(in.bugzy.data.remote.model.Response) LoginData(in.bugzy.data.remote.model.LoginData) NonNull(android.support.annotation.NonNull) LoginRequest(in.bugzy.data.remote.model.LoginRequest) NetworkBoundTask(in.bugzy.data.remote.NetworkBoundTask)

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