use of chat.rocket.persistence.realm.models.internal.MethodCall in project Rocket.Chat.Android by RocketChat.
the class MethodCallObserver method onUpdateResults.
@Override
public void onUpdateResults(List<MethodCall> results) {
String digest = getDigestFor(results);
if (prevDigest == null) {
if (digest == null) {
return;
}
} else {
if (prevDigest.equals(digest)) {
return;
}
}
prevDigest = digest;
if (results == null || results.isEmpty()) {
return;
}
MethodCall call = results.get(0);
final String methodCallId = call.getMethodCallId();
final String methodName = call.getName();
final String params = call.getParamsJson();
final long timeout = call.getTimeout();
realmHelper.executeTransaction(realm -> realm.createOrUpdateObjectFromJson(MethodCall.class, new JSONObject().put(MethodCall.ID, methodCallId).put(MethodCall.SYNC_STATE, SyncState.SYNCING))).onSuccessTask(task -> ddpClientRef.get().rpc(methodCallId, methodName, params, timeout).onSuccessTask(_task -> realmHelper.executeTransaction(realm -> {
String json = _task.getResult().result;
return realm.createOrUpdateObjectFromJson(MethodCall.class, new JSONObject().put(MethodCall.ID, methodCallId).put(MethodCall.SYNC_STATE, SyncState.SYNCED).put(MethodCall.RESULT_JSON, json));
}))).continueWithTask(task -> {
if (task.isFaulted()) {
return realmHelper.executeTransaction(realm -> {
Exception exception = task.getError();
final String errMessage;
if (exception instanceof DDPClientCallback.RPC.Error) {
errMessage = ((DDPClientCallback.RPC.Error) exception).error.toString();
} else if (exception instanceof DDPClientCallback.RPC.Timeout) {
errMessage = "{\"message\": \"Connection Timeout\"}";
} else if (exception instanceof RxWebSocketCallback.Failure) {
errMessage = "{\"message\": \"Connection Failure\"}";
} else {
errMessage = exception.getMessage();
}
realm.createOrUpdateObjectFromJson(MethodCall.class, new JSONObject().put(MethodCall.ID, methodCallId).put(MethodCall.SYNC_STATE, SyncState.FAILED).put(MethodCall.RESULT_JSON, errMessage));
return null;
});
}
return task;
}).continueWith(new LogIfError());
}
Aggregations