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);
}
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);
}
Aggregations