Search in sources :

Example 1 with NetworkBoundTask

use of in.bugzy.data.remote.NetworkBoundTask in project bugzy by cpunq.

the class CasesRepository method searchCases.

public LiveData<SearchResultsResource<ListCasesData>> searchCases(final String query) {
    this.recordResearch(query);
    NetworkBoundTask<Response<ListCasesData>> task = new NetworkBoundTask<Response<ListCasesData>>(mAppExecutors, mGson) {

        @Override
        public void saveCallResult(@NonNull Response<ListCasesData> result) {
        }

        @NonNull
        @Override
        protected Call<Response<ListCasesData>> createCall() {
            return mApiService.searchCasesCall(new SearchCasesRequest(mColsForCaseList, query));
        }
    };
    mAppExecutors.networkIO().execute(task);
    return Transformations.map(task.asLiveData(), v -> {
        Resource<ListCasesData> resource = new Resource(v.status, v.data == null ? null : v.data.getData(), v.message);
        return new SearchResultsResource<>(query, resource);
    });
// 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() {
// return mApiService.searchCases(new SearchCasesRequest(mColsForCaseList, query));
// }
// }.asLiveData(), v -> {
// return new SearchResultsResource<>(query, v);
// });
}
Also used : ApiResponse(in.bugzy.data.remote.ApiResponse) Response(in.bugzy.data.remote.model.Response) ListCasesData(in.bugzy.data.remote.model.ListCasesData) NonNull(android.support.annotation.NonNull) NetworkBoundResource(in.bugzy.data.remote.NetworkBoundResource) SearchResultsResource(in.bugzy.data.model.SearchResultsResource) Resource(in.bugzy.data.model.Resource) SearchCasesRequest(in.bugzy.data.remote.model.SearchCasesRequest) SearchResultsResource(in.bugzy.data.model.SearchResultsResource) NetworkBoundTask(in.bugzy.data.remote.NetworkBoundTask)

Example 2 with NetworkBoundTask

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

use of in.bugzy.data.remote.NetworkBoundTask 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)3 ApiResponse (in.bugzy.data.remote.ApiResponse)3 NetworkBoundTask (in.bugzy.data.remote.NetworkBoundTask)3 Response (in.bugzy.data.remote.model.Response)3 Attachment (in.bugzy.data.model.Attachment)1 Resource (in.bugzy.data.model.Resource)1 SearchResultsResource (in.bugzy.data.model.SearchResultsResource)1 NetworkBoundResource (in.bugzy.data.remote.NetworkBoundResource)1 EditCaseData (in.bugzy.data.remote.model.EditCaseData)1 ListCasesData (in.bugzy.data.remote.model.ListCasesData)1 LoginData (in.bugzy.data.remote.model.LoginData)1 LoginRequest (in.bugzy.data.remote.model.LoginRequest)1 SearchCasesRequest (in.bugzy.data.remote.model.SearchCasesRequest)1 ArrayList (java.util.ArrayList)1