Search in sources :

Example 1 with MethodCall

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());
}
Also used : Context(android.content.Context) Realm(io.realm.Realm) RxWebSocketCallback(chat.rocket.android_ddp.rx.RxWebSocketCallback) RealmResults(io.realm.RealmResults) DDPClientCallback(chat.rocket.android_ddp.DDPClientCallback) LogIfError(chat.rocket.android.helper.LogIfError) List(java.util.List) JSONObject(org.json.JSONObject) RealmHelper(chat.rocket.persistence.realm.RealmHelper) SyncState(chat.rocket.core.SyncState) DDPClientRef(chat.rocket.android.service.DDPClientRef) CheckSum(chat.rocket.android.helper.CheckSum) MethodCall(chat.rocket.persistence.realm.models.internal.MethodCall) DDPClientCallback(chat.rocket.android_ddp.DDPClientCallback) RxWebSocketCallback(chat.rocket.android_ddp.rx.RxWebSocketCallback) MethodCall(chat.rocket.persistence.realm.models.internal.MethodCall) LogIfError(chat.rocket.android.helper.LogIfError) JSONObject(org.json.JSONObject)

Aggregations

Context (android.content.Context)1 CheckSum (chat.rocket.android.helper.CheckSum)1 LogIfError (chat.rocket.android.helper.LogIfError)1 DDPClientRef (chat.rocket.android.service.DDPClientRef)1 DDPClientCallback (chat.rocket.android_ddp.DDPClientCallback)1 RxWebSocketCallback (chat.rocket.android_ddp.rx.RxWebSocketCallback)1 SyncState (chat.rocket.core.SyncState)1 RealmHelper (chat.rocket.persistence.realm.RealmHelper)1 MethodCall (chat.rocket.persistence.realm.models.internal.MethodCall)1 Realm (io.realm.Realm)1 RealmResults (io.realm.RealmResults)1 List (java.util.List)1 JSONObject (org.json.JSONObject)1