Example usage for com.facebook.react.bridge Arguments createArray

List of usage examples for com.facebook.react.bridge Arguments createArray

Introduction

In this page you can find the example usage for com.facebook.react.bridge Arguments createArray.

Prototype

public static WritableArray createArray() 

Source Link

Document

This method should be used when you need to stub out creating NativeArrays in unit tests.

Usage

From source file:co.rewen.statex.StateXModule.java

License:Open Source License

/**
 * Given an array of keys, this returns a map of (key, value) pairs for the keys found, and
 * (key, null) for the keys that haven't been found.
 *//*from   www  . j a  v  a 2  s  .co  m*/
@ReactMethod
public void multiGet(final ReadableArray keys, final Callback callback) {
    if (keys == null) {
        callback.invoke(AsyncStorageErrorUtil.getInvalidKeyError(null), null);
        return;
    }

    new GuardedAsyncTask<Void, Void>(getReactApplicationContext()) {
        @Override
        protected void doInBackgroundGuarded(Void... params) {
            if (!ensureDatabase()) {
                callback.invoke(AsyncStorageErrorUtil.getDBError(null), null);
                return;
            }

            String[] columns = { KEY_COLUMN, VALUE_COLUMN };
            HashSet<String> keysRemaining = SetBuilder.newHashSet();
            WritableArray data = Arguments.createArray();
            for (int keyStart = 0; keyStart < keys.size(); keyStart += MAX_SQL_KEYS) {
                int keyCount = Math.min(keys.size() - keyStart, MAX_SQL_KEYS);
                Cursor cursor = mStateXDatabaseSupplier.get().query(TABLE_STATE, columns,
                        AsyncLocalStorageUtil.buildKeySelection(keyCount),
                        AsyncLocalStorageUtil.buildKeySelectionArgs(keys, keyStart, keyCount), null, null,
                        null);
                keysRemaining.clear();
                try {
                    if (cursor.getCount() != keys.size()) {
                        // some keys have not been found - insert them with null into the final array
                        for (int keyIndex = keyStart; keyIndex < keyStart + keyCount; keyIndex++) {
                            keysRemaining.add(keys.getString(keyIndex));
                        }
                    }

                    if (cursor.moveToFirst()) {
                        do {
                            WritableArray row = Arguments.createArray();
                            row.pushString(cursor.getString(0));
                            row.pushString(cursor.getString(1));
                            data.pushArray(row);
                            keysRemaining.remove(cursor.getString(0));
                        } while (cursor.moveToNext());
                    }
                } catch (Exception e) {
                    FLog.w(ReactConstants.TAG, e.getMessage(), e);
                    callback.invoke(AsyncStorageErrorUtil.getError(null, e.getMessage()), null);
                    return;
                } finally {
                    cursor.close();
                }

                for (String key : keysRemaining) {
                    WritableArray row = Arguments.createArray();
                    row.pushString(key);
                    row.pushNull();
                    data.pushArray(row);
                }
                keysRemaining.clear();
            }

            callback.invoke(null, data);
        }
    }.execute();
}

From source file:co.rewen.statex.StateXModule.java

License:Open Source License

/**
 * Returns an array with all keys from the database.
 */// www  .  j a  va2s.c om
@ReactMethod
public void getAllKeys(final Callback callback) {
    new GuardedAsyncTask<Void, Void>(getReactApplicationContext()) {
        @Override
        protected void doInBackgroundGuarded(Void... params) {
            if (!ensureDatabase()) {
                callback.invoke(AsyncStorageErrorUtil.getDBError(null), null);
                return;
            }
            WritableArray data = Arguments.createArray();
            String[] columns = { KEY_COLUMN };
            Cursor cursor = mStateXDatabaseSupplier.get().query(TABLE_STATE, columns, null, null, null, null,
                    null);
            try {
                if (cursor.moveToFirst()) {
                    do {
                        data.pushString(cursor.getString(0));
                    } while (cursor.moveToNext());
                }
            } catch (Exception e) {
                FLog.w(ReactConstants.TAG, e.getMessage(), e);
                callback.invoke(AsyncStorageErrorUtil.getError(null, e.getMessage()), null);
                return;
            } finally {
                cursor.close();
            }
            callback.invoke(null, data);
        }
    }.execute();
}

From source file:com.amazonaws.reactnative.core.AWSRNClientMarshaller.java

License:Open Source License

public static WritableArray jsonToReact(final JSONArray jsonArray) throws JSONException {
    final WritableArray writableArray = Arguments.createArray();
    for (int i = 0; i < jsonArray.length(); i++) {
        final Object value = jsonArray.get(i);
        if (value instanceof Float || value instanceof Double) {
            writableArray.pushDouble(jsonArray.getDouble(i));
        } else if (value instanceof Number) {
            writableArray.pushInt(jsonArray.getInt(i));
        } else if (value instanceof String) {
            writableArray.pushString(jsonArray.getString(i));
        } else if (value instanceof Boolean) {
            writableArray.pushBoolean(jsonArray.getBoolean(i));
        } else if (value instanceof JSONObject) {
            writableArray.pushMap(jsonToReact(jsonArray.getJSONObject(i)));
        } else if (value instanceof JSONArray) {
            writableArray.pushArray(jsonToReact(jsonArray.getJSONArray(i)));
        } else if (value == JSONObject.NULL) {
            writableArray.pushNull();/*from   w ww  .ja va2  s.  co m*/
        }
    }
    return writableArray;
}

From source file:com.auth0.lock.react.bridge.UserProfileBridge.java

License:Open Source License

public WritableMap toMap() {
    WritableMap profileMap = null;// w  ww  .  j a v a  2  s  . c o m
    if (profile != null) {
        profileMap = Arguments.createMap();
        profileMap.putString(EMAIL_KEY, profile.getEmail());
        profileMap.putString(ID_KEY, profile.getId());
        profileMap.putString(NAME_KEY, profile.getName());
        profileMap.putString(NICKNAME_KEY, profile.getNickname());
        if (profile.getCreatedAt() != null) {
            profileMap.putString(CREATED_AT_KEY, formatter.format(profile.getCreatedAt()));
        }
        profileMap.putString(PICTURE_KEY, profile.getPictureURL());
        Map<String, Object> info = new HashMap<>(profile.getExtraInfo());
        put("userMetadata", info.remove("user_metadata"), profileMap);
        put("appMetadata", info.remove("app_metadata"), profileMap);
        for (Map.Entry<String, Object> entry : info.entrySet()) {
            put(entry.getKey(), entry.getValue(), profileMap);
        }
        final WritableArray identities = Arguments.createArray();
        for (UserIdentity identity : profile.getIdentities()) {
            add(identity, identities);
        }
        profileMap.putArray("identities", identities);
    }
    return profileMap;
}

From source file:com.auth0.lock.react.bridge.UserProfileBridge.java

License:Open Source License

private void put(String key, List<?> list, WritableMap into) {
    if (list == null || list.isEmpty()) {
        return;//from   w  w  w  .  ja  v a2  s .  c om
    }

    final WritableArray array = Arguments.createArray();
    for (Object item : list) {
        if (item instanceof String) {
            array.pushString((String) item);
        }
        if (item instanceof Integer) {
            array.pushInt((Integer) item);
        }
        if (item instanceof Boolean) {
            array.pushBoolean((Boolean) item);
        }
        if (item instanceof Double) {
            array.pushDouble((Double) item);
        }
        if (item instanceof Date) {
            array.pushString(formatter.format(item));
        }
    }
    into.putArray(key, array);
}

From source file:com.boundlessgeo.spatialconnect.jsbridge.RNSpatialConnect.java

License:Apache License

private WritableArray convertArrayToArrayList(List list) {
    WritableArray writableArray = Arguments.createArray();

    if (list.size() < 1) {
        return writableArray;
    }/* w w  w .  j  av  a  2  s  . c o  m*/

    Object firstObject = list.get(0);
    if (firstObject == null) {
        for (int i = 0; i < list.size(); i++) {
            writableArray.pushNull();
        }
    } else if (firstObject instanceof Boolean) {
        for (Object object : list) {
            writableArray.pushBoolean((boolean) object);
        }
    } else if (firstObject instanceof Double) {
        for (Object object : list) {
            writableArray.pushDouble((double) object);
        }
    } else if (firstObject instanceof Integer) {
        for (Object object : list) {
            writableArray.pushInt((int) object);
        }
    } else if (firstObject instanceof Long) {
        for (Object object : list) {
            writableArray.pushDouble((long) object);
        }
    } else if (firstObject instanceof String) {
        for (Object object : list) {
            writableArray.pushString((String) object);
        }
    } else if (firstObject instanceof Map) {
        for (Object object : list) {
            writableArray.pushMap(convertHashMapToMap((Map) object));
        }
    } else if (firstObject instanceof List) {
        for (Object object : list) {
            writableArray.pushArray(convertArrayToArrayList((List) object));
        }
    }

    return writableArray;
}

From source file:com.boundlessgeo.spatialconnect.jsbridge.SCBridge.java

License:Apache License

/**
 * Handles the {@link BridgeCommand#DATASERVICE_ACTIVESTORESLIST} command.
 *//*w  w w  .j  a va  2s .c o m*/
private void handleActiveStoresList() {
    Log.d(LOG_TAG, "Handling DATASERVICE_ACTIVESTORESLIST message");
    List<SCDataStore> stores = sc.getDataService().getActiveStores();
    WritableMap eventPayload = Arguments.createMap();
    WritableArray storesArray = Arguments.createArray();
    for (SCDataStore store : stores) {
        storesArray.pushMap(getStoreMap(store));
    }
    eventPayload.putArray("stores", storesArray);
    sendEvent("storesList", eventPayload);
}

From source file:com.boundlessgeo.spatialconnect.jsbridge.SCBridge.java

License:Apache License

/**
 * Handles the {@link BridgeCommand#DATASERVICE_FORMSLIST} command.
 *//*  w ww .  ja  v a 2 s.c  om*/
private void handleFormsList() {
    Log.d(LOG_TAG, "Handling DATASERVICE_FORMSLIST message");
    List<SCFormConfig> formConfigs = sc.getDataService().getDefaultStore().getFormConfigs();
    WritableMap eventPayload = Arguments.createMap();
    WritableArray formsArray = Arguments.createArray();
    for (SCFormConfig config : formConfigs) {
        formsArray.pushMap(getFormMap(config));
    }
    eventPayload.putArray("forms", formsArray);
    sendEvent("formsList", eventPayload);
}

From source file:com.boundlessgeo.spatialconnect.jsbridge.SCBridge.java

License:Apache License

private WritableMap getFormMap(SCFormConfig formConfig) {
    WritableMap params = Arguments.createMap();
    params.putString("id", formConfig.getId());
    params.putString("name", formConfig.getName());
    params.putString("display_name", formConfig.getDisplayName());
    params.putString("layer_name", formConfig.getLayerName());
    WritableArray fields = Arguments.createArray();
    // for each field, create a WriteableMap with all the SCFormField params
    for (SCFormField field : formConfig.getFields()) {
        WritableMap fieldMap = Arguments.createMap();
        fieldMap.putString("id", field.getId());
        fieldMap.putString("key", field.getKey());
        fieldMap.putString("label", field.getLabel());
        if (field.isRequired() != null) {
            fieldMap.putBoolean("is_required", field.isRequired());
        }// w ww.  j  a  v a 2 s.co  m
        if (field.getPosition() != null) {
            fieldMap.putInt("order", field.getPosition());
        }
        if (field.getType() != null) {
            fieldMap.putString("type", field.getType());
        }
        if (field.getInitialValue() != null) {
            fieldMap.putString("initial_value", String.valueOf(field.getInitialValue()));
        }
        if (field.getMaximum() != null) {
            fieldMap.putDouble("minimum", Double.valueOf(String.valueOf(field.getMinimum())));
        }
        if (field.getMaximum() != null) {
            fieldMap.putDouble("maximum", Double.valueOf(String.valueOf(field.getMaximum())));
        }
        if (field.isExclusiveMaximum() != null) {
            fieldMap.putBoolean("exclusive_maximum", field.isExclusiveMaximum());
        }
        if (field.isExclusiveMinimum() != null) {
            fieldMap.putBoolean("exclusive_minimum", field.isExclusiveMinimum());
        }
        if (field.isInteger() != null) {
            fieldMap.putBoolean("is_integer", field.isInteger());
        }
        if (field.getMaximumLength() != null) {
            fieldMap.putInt("maximum_length", field.getMaximumLength());
        }
        if (field.getMinimumLength() != null) {
            fieldMap.putInt("minimum_length", field.getMinimumLength());
        }
        if (field.getPattern() != null) {
            fieldMap.putString("pattern", field.getPattern());
        }
        fields.pushMap(fieldMap);
    }
    params.putArray("fields", fields);
    return params;
}

From source file:com.geniem.rnble.RNBLEModule.java

License:Open Source License

@ReactMethod
public void discoverServices(final String peripheralUuid, ReadableArray uuids) {
    Log.d(TAG, "discoverServices");
    WritableArray filteredServiceUuids = Arguments.createArray();

    if (bluetoothGatt != null && this.discoveredServices != null && uuids != null && uuids.size() > 0) {
        //filter discovered services
        for (BluetoothGattService service : this.discoveredServices) {
            String uuid = service.getUuid().toString();
            for (int i = 0; i < uuids.size(); i++) {
                if (uuid.equalsIgnoreCase(uuids.getString(i))) {
                    filteredServiceUuids.pushString(toNobleUuid(uuid));
                }/* www.j ava2 s . co m*/
            }
        }
    } else if (uuids == null || uuids.size() == 0) {
        //if no uuids are requested return all discovered service uuids
        for (BluetoothGattService service : this.discoveredServices) {
            String uuid = service.getUuid().toString();
            filteredServiceUuids.pushString(toNobleUuid(uuid));
        }
    }

    WritableMap params = Arguments.createMap();
    params.putString("peripheralUuid", peripheralUuid);
    params.putArray("serviceUuids", filteredServiceUuids);

    this.sendEvent("ble.servicesDiscover", params);
}