Search in sources :

Example 1 with TaskCompletionSource

use of com.meniga.sdk.providers.tasks.TaskCompletionSource in project mobile-sdk-android by meniga.

the class MenigaTransactionOperationsMock method splitTransaction.

@Override
public Result<List<MenigaTransaction>> splitTransaction(long parentId, MenigaDecimal amount, String text, long categoryId, boolean isFlagged) {
    TaskCompletionSource<List<MenigaTransaction>> task = new TaskCompletionSource<>();
    task.setResult(new MenigaTransactionPage());
    return new MTask<>(task.getTask(), task);
}
Also used : TaskCompletionSource(com.meniga.sdk.providers.tasks.TaskCompletionSource) MTask(com.meniga.sdk.helpers.MTask) ArrayList(java.util.ArrayList) List(java.util.List) MenigaTransactionPage(com.meniga.sdk.models.transactions.MenigaTransactionPage)

Example 2 with TaskCompletionSource

use of com.meniga.sdk.providers.tasks.TaskCompletionSource in project mobile-sdk-android by meniga.

the class BasicTaskAdapter method adapt.

@Override
public <T> Result<T> adapt(Call<T> call, Callback<T> callBack) {
    if (callBack == null) {
        callBack = new Callback<T>() {

            @Override
            public void onResponse(Call<T> call, Response<T> response) {
            }

            @Override
            public void onFailure(Call<T> call, Throwable t) {
            }
        };
    }
    final Callback<T> finalCall = callBack;
    final TaskCompletionSource<T> tcs = new TaskCompletionSource<>(call);
    call.enqueue(new Callback<T>() {

        @Override
        public void onResponse(Call<T> call, Response<T> response) {
            if (call.isCanceled()) {
                tcs.setCancelled();
            } else if (response.isSuccessful()) {
                tcs.setResult(response.body());
                finalCall.onResponse(call, response);
            } else {
                ErrorHandler.reportAndHandle(new Exception(response.message()));
                String msg = response.message();
                try {
                    msg = response.errorBody().string();
                } catch (Exception ex) {
                // Do not crash
                }
                tcs.setError(new MenigaWebException(msg, response.code()));
            }
        }

        @Override
        public void onFailure(Call<T> call, Throwable error) {
            ErrorHandler.reportAndHandle(error);
            if (error == null && !tcs.getTask().isCancelled()) {
                tcs.setError(new Exception("[No error message provided]"));
            } else {
                if (call.isCanceled() && !tcs.getTask().isCompleted()) {
                    tcs.setCancelled();
                } else if (!tcs.getTask().isCompleted()) {
                    if (error != null) {
                        tcs.setError(new MenigaWebException(error.getMessage(), -1));
                    } else {
                        tcs.setError(new MenigaWebException("[No error message provided]", -1));
                    }
                }
            }
        }
    });
    return new MTask<>(tcs.getTask(), tcs);
}
Also used : TaskCompletionSource(com.meniga.sdk.providers.tasks.TaskCompletionSource) MenigaWebException(com.meniga.sdk.webservices.MenigaWebException) MTask(com.meniga.sdk.helpers.MTask) MenigaWebException(com.meniga.sdk.webservices.MenigaWebException)

Aggregations

MTask (com.meniga.sdk.helpers.MTask)2 TaskCompletionSource (com.meniga.sdk.providers.tasks.TaskCompletionSource)2 MenigaTransactionPage (com.meniga.sdk.models.transactions.MenigaTransactionPage)1 MenigaWebException (com.meniga.sdk.webservices.MenigaWebException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1