Example usage for com.facebook.react.bridge WritableMap putInt

List of usage examples for com.facebook.react.bridge WritableMap putInt

Introduction

In this page you can find the example usage for com.facebook.react.bridge WritableMap putInt.

Prototype

void putInt(@NonNull String key, int value);

Source Link

Usage

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

License:Open Source License

public static WritableMap jsonToReact(final JSONObject jsonObject) throws JSONException {
    final WritableMap writableMap = Arguments.createMap();
    final Iterator iterator = jsonObject.keys();
    while (iterator.hasNext()) {
        final String key = (String) iterator.next();
        final Object value = jsonObject.get(key);
        if (value instanceof Float || value instanceof Double) {
            writableMap.putDouble(key, jsonObject.getDouble(key));
        } else if (value instanceof Number) {
            writableMap.putInt(key, jsonObject.getInt(key));
        } else if (value instanceof String) {
            writableMap.putString(key, jsonObject.getString(key));
        } else if (value instanceof JSONObject) {
            writableMap.putMap(key, jsonToReact(jsonObject.getJSONObject(key)));
        } else if (value instanceof JSONArray) {
            writableMap.putArray(key, jsonToReact(jsonObject.getJSONArray(key)));
        } else if (value instanceof Boolean) {
            writableMap.putBoolean(key, jsonObject.getBoolean(key));
        } else if (value == JSONObject.NULL) {
            writableMap.putNull(key);/*from  w  w w  .  ja va2s  .c om*/
        }
    }
    return writableMap;
}

From source file:com.amazonaws.reactnative.s3.AWSRNS3TransferUtility.java

License:Open Source License

private void subscribe(TransferObserver observer) {
    observer.setTransferListener(new TransferListener() {
        @Override/*from  w w  w  .j a  v  a  2s  .c o  m*/
        public void onStateChanged(final int id, final TransferState state) {
            final WritableMap map = Arguments.createMap();
            final String uuid = transferIDMap.get(id);
            final Map<String, Object> req = requestMap.get(uuid);
            if (!(boolean) req.get(COMPLETIONHANDLER)) {
                return;
            }
            map.putString(REQUESTID, uuid);
            if (state == TransferState.CANCELED) {
                map.putString(ERROR, "canceled");
            } else if (state == TransferState.FAILED) {
                map.putString(ERROR, "failed");
            } else if (state == TransferState.COMPLETED) {
                final WritableMap request = Arguments.createMap();
                TransferObserver observer = null;
                try {
                    observer = transferUtility.getTransferById(id);
                } catch (final AmazonClientException e) {
                    throw e;
                }
                request.putString(BUCKET, observer.getBucket());
                request.putString(KEY, observer.getKey());
                request.putString(LOCATION, observer.getAbsoluteFilePath());
                map.putMap(REQUEST, request);
                removeTransferFromMap(uuid, id);
            }
            if ((state.equals(TransferState.CANCELED) || state.equals(TransferState.FAILED)
                    || state.equals(TransferState.COMPLETED)) && (map.hasKey(ERROR) || map.hasKey(REQUEST))) {
                sendEvent(getReactApplicationContext(), "CompletionHandlerEvent", map);
            }
        }

        @Override
        public void onProgressChanged(final int id, final long bytesCurrent, final long bytesTotal) {
            final WritableMap map = Arguments.createMap();
            final String uuid = transferIDMap.get(id);
            final Map<String, Object> req = requestMap.get(uuid);
            map.putString(TYPE, (String) req.get(TYPE));
            map.putString(REQUESTID, uuid);
            map.putDouble(COMPLETEDUNITCOUNT, (double) bytesCurrent);
            map.putDouble(TOTALUNITCOUNT, (double) bytesTotal);
            if (bytesTotal == 0) {
                map.putDouble(FRACTIONCOMPLETED, 0);
            } else {
                map.putDouble(FRACTIONCOMPLETED, ((double) bytesCurrent / (double) bytesTotal));
            }
            sendEvent(getReactApplicationContext(), "ProgressEventUtility", map);
        }

        @Override
        public void onError(final int id, final Exception ex) {
            final WritableMap map = Arguments.createMap();
            final WritableMap errorMap = Arguments.createMap();
            final String uuid = transferIDMap.get(id);
            map.putString(REQUESTID, uuid);
            errorMap.putString(ERROR, ex.toString());
            errorMap.putString(DESCRIPTION, ex.getLocalizedMessage());
            errorMap.putInt(CODE, ex.hashCode());
            map.putMap(ERROR, errorMap);
            sendEvent(getReactApplicationContext(), "ProgressEventUtility", map);
        }
    });
}

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

License:Open Source License

private void put(String key, Object value, WritableMap map) {
    if (value instanceof String) {
        map.putString(key, (String) value);
    }//  w ww  .j a  v a 2  s .  c  o m
    if (value instanceof Integer) {
        map.putInt(key, (Integer) value);
    }
    if (value instanceof Boolean) {
        map.putBoolean(key, (Boolean) value);
    }
    if (value instanceof Double) {
        map.putDouble(key, (Double) value);
    }
    if (value instanceof Date) {
        map.putString(key, formatter.format(value));
    }
    if (value instanceof Map) {
        //noinspection unchecked
        put(key, (Map) value, map);
    }
    if (value instanceof List) {
        put(key, (List) value, map);
    }
}

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

License:Apache License

private WritableMap writePayloadToMap(WritableMap response, Object value) {
    if (value instanceof Boolean) {
        response.putBoolean("payload", (Boolean) value);
    } else if (value instanceof Integer) {
        response.putInt("payload", (Integer) value);
    } else if (value instanceof String) {
        response.putString("payload", (String) value);
    } else if (value instanceof HashMap) {
        // if we upgrade react, we can just `writeMap` instead of having to convert it
        response.putMap("payload", convertHashMapToMap((Map<String, Object>) value));
    }/*from   w  ww .j  a  va2  s .c  om*/

    return response;
}

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

License:Apache License

private WritableMap convertHashMapToMap(Map<String, Object> hashMap) {
    WritableMap writableMap = Arguments.createMap();

    for (Map.Entry<String, Object> entry : hashMap.entrySet()) {
        String key = entry.getKey();
        Object value = entry.getValue();

        if (value == null) {
            writableMap.putNull(key);//from  www  . ja v  a2s  . c om
        } else if (value instanceof Boolean) {
            writableMap.putBoolean(key, (Boolean) value);
        } else if (value instanceof Double) {
            writableMap.putDouble(key, (Double) value);
        } else if (value instanceof Integer) {
            writableMap.putInt(key, (Integer) value);
        } else if (value instanceof Long) {
            writableMap.putDouble(key, (Long) value);
        } else if (value instanceof String) {
            writableMap.putString(key, (String) value);
        } else if (value instanceof Map) {
            writableMap.putMap(key, convertHashMapToMap((Map) value));
        } else if (value instanceof List) {
            writableMap.putArray(key, convertArrayToArrayList((List) value));
        }
    }

    return writableMap;
}

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

License:Apache License

private WritableMap getStoreMap(SCDataStore store) {
    WritableMap params = Arguments.createMap();
    params.putString("storeId", store.getStoreId());
    params.putString("name", store.getName());
    params.putString("type", store.getType());
    params.putInt("version", store.getVersion());
    params.putString("key", store.getKey());
    return params;
}

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  w w  .  j a va 2  s .c  o 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.boundlessgeo.spatialconnect.jsbridge.SCBridge.java

License:Apache License

private static WritableMap convertJsonToMap(JSONObject jsonObject) throws JSONException {
    WritableMap map = new WritableNativeMap();

    Iterator<String> iterator = jsonObject.keys();
    while (iterator.hasNext()) {
        String key = iterator.next();
        Object value = jsonObject.get(key);
        if (value instanceof JSONObject) {
            map.putMap(key, convertJsonToMap((JSONObject) value));
        } else if (value instanceof JSONArray) {
            map.putArray(key, convertJsonToArray((JSONArray) value));
        } else if (value instanceof Boolean) {
            map.putBoolean(key, (Boolean) value);
        } else if (value instanceof Integer) {
            map.putInt(key, (Integer) value);
        } else if (value instanceof Double) {
            map.putDouble(key, (Double) value);
        } else if (value instanceof String) {
            map.putString(key, (String) value);
        } else {//from   ww  w  . j ava2  s  .  c om
            map.putString(key, value.toString());
        }
    }
    return map;
}

From source file:com.dylanvann.cameraroll.CameraRollManager.java

License:Open Source License

private static void putAlbums(ContentResolver resolver, Cursor cursor, WritableMap response) {
    WritableArray albums = new WritableNativeArray();
    int bucketIdIndex = cursor.getColumnIndex(Video.VideoColumns.BUCKET_ID);
    int bucketNameIndex = cursor.getColumnIndex(Video.VideoColumns.BUCKET_DISPLAY_NAME);
    int idIndex = cursor.getColumnIndex(FileColumns._ID);
    int mimeTypeIndex = cursor.getColumnIndex(FileColumns.MIME_TYPE);
    int mediaTypeIndex = cursor.getColumnIndex(FileColumns.MEDIA_TYPE);
    int dateModifiedIndex = cursor.getColumnIndex(FileColumns.DATE_MODIFIED);
    int widthIndex = IS_JELLY_BEAN_OR_LATER ? cursor.getColumnIndex(FileColumns.WIDTH) : -1;
    int heightIndex = IS_JELLY_BEAN_OR_LATER ? cursor.getColumnIndex(FileColumns.HEIGHT) : -1;
    HashMap<String, WritableMap> albumsMap = new HashMap<>();
    String assetCountKey = "assetCount";
    if (cursor.moveToFirst()) {
        {/*w ww.ja v a  2  s.  co m*/
            WritableMap album = new WritableNativeMap();
            album.putInt(assetCountKey, cursor.getCount());
            WritableArray previewAssets = new WritableNativeArray();
            WritableMap asset = new WritableNativeMap();
            putAssetInfo(resolver, cursor, asset, mediaTypeIndex, idIndex, widthIndex, heightIndex,
                    mimeTypeIndex, dateModifiedIndex);
            previewAssets.pushMap(asset);
            album.putArray("previewAssets", previewAssets);
            albumsMap.put("-1", album);
        }

        while (cursor.moveToNext()) {
            String albumId = cursor.getString(bucketIdIndex);
            if (!albumsMap.containsKey(albumId)) {
                WritableMap album = new WritableNativeMap();
                String albumName = cursor.getString(bucketNameIndex);
                album.putString("id", albumId);
                album.putString("title", albumName);
                album.putInt(assetCountKey, 1);
                WritableArray previewAssets = new WritableNativeArray();
                WritableMap asset = new WritableNativeMap();
                putAssetInfo(resolver, cursor, asset, mediaTypeIndex, idIndex, widthIndex, heightIndex,
                        mimeTypeIndex, dateModifiedIndex);
                previewAssets.pushMap(asset);
                album.putArray("previewAssets", previewAssets);
                albumsMap.put(albumId, album);
            } else {
                WritableMap album = albumsMap.get(albumId);
                int count = album.getInt(assetCountKey);
                album.putInt(assetCountKey, count + 1);
            }
        }
        Collection<WritableMap> albumsCollection = albumsMap.values();
        for (WritableMap album : albumsCollection) {
            albums.pushMap(album);
        }
    }
    response.putArray("albums", albums);
}

From source file:com.dzhyun.sdk.DzhChannel.java

License:Open Source License

@ReactMethod
public void connect(final String url, final int id) {
    OkHttpClient client = new OkHttpClient();

    client.setConnectTimeout(10, TimeUnit.SECONDS);
    client.setWriteTimeout(10, TimeUnit.SECONDS);
    // Disable timeouts for read
    client.setReadTimeout(0, TimeUnit.MINUTES);

    Request request = new Request.Builder().tag(id).url(url).build();

    WebSocketCall.create(client, request).enqueue(new WebSocketListener() {

        @Override//from  www. jav a  2s.  co m
        public void onOpen(WebSocket webSocket, Response response) {
            mWebSocketConnections.put(id, webSocket);
            WritableMap params = Arguments.createMap();
            params.putInt("id", id);
            sendEvent("dzhChannelOpen", params);
        }

        @Override
        public void onClose(int code, String reason) {
            WritableMap params = Arguments.createMap();
            params.putInt("id", id);
            params.putInt("code", code);
            params.putString("reason", reason);
            sendEvent("dzhChannelClosed", params);
        }

        @Override
        public void onFailure(IOException e, Response response) {
            notifyWebSocketFailed(id, e.getMessage());
        }

        @Override
        public void onPong(Buffer buffer) {
        }

        @Override
        public void onMessage(BufferedSource bufferedSource, WebSocket.PayloadType payloadType) {
            String message;
            if (payloadType == WebSocket.PayloadType.BINARY) {

                try {
                    message = Pb2Json.toJson(bufferedSource.readByteArray());
                    bufferedSource.close();
                } catch (IOException e) {
                    FLog.e(ReactConstants.TAG, "decode pb failed " + id, e);
                    return;
                }

            } else {
                try {
                    message = bufferedSource.readUtf8();
                } catch (IOException e) {
                    notifyWebSocketFailed(id, e.getMessage());
                    return;
                }
                try {
                    bufferedSource.close();
                } catch (IOException e) {
                    FLog.e(ReactConstants.TAG, "Could not close BufferedSource for WebSocket id " + id, e);
                }

            }

            WritableMap params = Arguments.createMap();
            params.putInt("id", id);
            params.putString("data", message);
            sendEvent("dzhChannelMessage", params);
        }
    });

    // Trigger shutdown of the dispatcher's executor so this process can exit cleanly
    client.getDispatcher().getExecutorService().shutdown();
}