Search in sources :

Example 1 with ReadableMapKeySetIterator

use of com.facebook.react.bridge.ReadableMapKeySetIterator in project react-native-navigation by wix.

the class BundleConverter method toBundle.

public static Bundle toBundle(ReadableMap map) {
    Bundle bundle = new Bundle();
    ReadableMapKeySetIterator it = map.keySetIterator();
    while (it.hasNextKey()) {
        String key = it.nextKey();
        switch(map.getType(key)) {
            case Null:
                break;
            case Boolean:
                bundle.putBoolean(key, map.getBoolean(key));
                break;
            case Number:
                putNumber(bundle, map, key);
                break;
            case String:
                bundle.putString(key, map.getString(key));
                break;
            case Map:
                bundle.putBundle(key, toBundle(map.getMap(key)));
                break;
            case Array:
                bundle.putBundle(key, toBundle(map.getArray(key)));
                break;
            default:
                break;
        }
    }
    return bundle;
}
Also used : ReadableMapKeySetIterator(com.facebook.react.bridge.ReadableMapKeySetIterator) Bundle(android.os.Bundle)

Example 2 with ReadableMapKeySetIterator

use of com.facebook.react.bridge.ReadableMapKeySetIterator in project react-native-fcm by evollu.

the class ReactNativeJson method convertMapToJson.

public static JSONObject convertMapToJson(ReadableMap readableMap) throws JSONException {
    JSONObject object = new JSONObject();
    ReadableMapKeySetIterator iterator = readableMap.keySetIterator();
    while (iterator.hasNextKey()) {
        String key = iterator.nextKey();
        switch(readableMap.getType(key)) {
            case Null:
                object.put(key, JSONObject.NULL);
                break;
            case Boolean:
                object.put(key, readableMap.getBoolean(key));
                break;
            case Number:
                object.put(key, readableMap.getDouble(key));
                break;
            case String:
                object.put(key, readableMap.getString(key));
                break;
            case Map:
                object.put(key, convertMapToJson(readableMap.getMap(key)));
                break;
            case Array:
                object.put(key, convertArrayToJson(readableMap.getArray(key)));
                break;
        }
    }
    return object;
}
Also used : ReadableMapKeySetIterator(com.facebook.react.bridge.ReadableMapKeySetIterator) JSONObject(org.json.JSONObject)

Example 3 with ReadableMapKeySetIterator

use of com.facebook.react.bridge.ReadableMapKeySetIterator in project react-native-push-notification by zo0r.

the class RNPushNotificationAttributes method matches.

/**
 * User to find notifications:
 * <p>
 * https://github.com/facebook/react-native/blob/master/Libraries/PushNotificationIOS/RCTPushNotificationManager.m#L294
 *
 * @param userInfo map of fields to match
 * @return true all fields in userInfo object match, false otherwise
 */
public boolean matches(ReadableMap userInfo) {
    Bundle bundle = toBundle();
    ReadableMapKeySetIterator iterator = userInfo.keySetIterator();
    while (iterator.hasNextKey()) {
        String key = iterator.nextKey();
        if (!bundle.containsKey(key))
            return false;
        switch(userInfo.getType(key)) {
            case Null:
                {
                    if (bundle.get(key) != null)
                        return false;
                    break;
                }
            case Boolean:
                {
                    if (userInfo.getBoolean(key) != bundle.getBoolean(key))
                        return false;
                    break;
                }
            case Number:
                {
                    if ((userInfo.getDouble(key) != bundle.getDouble(key)) && (userInfo.getInt(key) != bundle.getInt(key)))
                        return false;
                    break;
                }
            case String:
                {
                    if (!userInfo.getString(key).equals(bundle.getString(key)))
                        return false;
                    break;
                }
            case Map:
                // there are no maps in the bundle
                return false;
            case Array:
                // there are no arrays in the bundle
                return false;
        }
    }
    return true;
}
Also used : ReadableMapKeySetIterator(com.facebook.react.bridge.ReadableMapKeySetIterator) Bundle(android.os.Bundle)

Example 4 with ReadableMapKeySetIterator

use of com.facebook.react.bridge.ReadableMapKeySetIterator in project gl-react-native by ProjectSeptemberInc.

the class GLCanvas method recSyncData.

private GLRenderData recSyncData(GLData data, HashMap<Uri, GLImage> images) {
    Map<Uri, GLImage> prevImages = this.images;
    GLShader shader = getShader(data.shader);
    if (shader == null || !shader.ensureCompile())
        return null;
    Map<String, Integer> uniformsInteger = new HashMap<>();
    Map<String, Float> uniformsFloat = new HashMap<>();
    Map<String, IntBuffer> uniformsIntBuffer = new HashMap<>();
    Map<String, FloatBuffer> uniformsFloatBuffer = new HashMap<>();
    Map<String, GLTexture> textures = new HashMap<>();
    List<GLRenderData> contextChildren = new ArrayList<>();
    List<GLRenderData> children = new ArrayList<>();
    for (GLData child : data.contextChildren) {
        GLRenderData node = recSyncData(child, images);
        if (node == null)
            return null;
        contextChildren.add(node);
    }
    for (GLData child : data.children) {
        GLRenderData node = recSyncData(child, images);
        if (node == null)
            return null;
        children.add(node);
    }
    Map<String, Integer> uniformTypes = shader.getUniformTypes();
    List<String> uniformNames = shader.getUniformNames();
    Map<String, Integer> uniformSizes = shader.getUniformSizes();
    int units = 0;
    ReadableMapKeySetIterator iterator = data.uniforms.keySetIterator();
    while (iterator.hasNextKey()) {
        String uniformName = iterator.nextKey();
        int type = uniformTypes.get(uniformName);
        int size = uniformSizes.get(uniformName);
        ReadableMap dataUniforms = data.uniforms;
        if (type == GL_SAMPLER_2D || type == GL_SAMPLER_CUBE) {
            uniformsInteger.put(uniformName, units++);
            if (dataUniforms.isNull(uniformName)) {
                GLTexture emptyTexture = new GLTexture(this);
                emptyTexture.setPixelsEmpty();
                textures.put(uniformName, emptyTexture);
            } else {
                ReadableMap value = null;
                try {
                    value = dataUniforms.getMap(uniformName);
                } catch (Exception e) {
                    shader.runtimeException("texture uniform '" + uniformName + "': you cannot directly give require('./img.png') " + "to gl-react, use resolveAssetSource(require('./img.png')) instead.");
                    return null;
                }
                String t = value.getString("type");
                if (t.equals("content")) {
                    int id = value.getInt("id");
                    if (id >= contentTextures.size()) {
                        resizeUniformContentTextures(id + 1);
                    }
                    textures.put(uniformName, contentTextures.get(id));
                } else if (t.equals("fbo")) {
                    int id = value.getInt("id");
                    GLFBO fbo = getFBO(id);
                    textures.put(uniformName, fbo.color.get(0));
                } else if (t.equals("uri")) {
                    final Uri src = srcResource(value);
                    if (src == null) {
                        shader.runtimeException("texture uniform '" + uniformName + "': Invalid uri format '" + value + "'");
                    }
                    GLImage image = images.get(src);
                    if (image == null) {
                        image = prevImages.get(src);
                        if (image != null)
                            images.put(src, image);
                    }
                    if (image == null) {
                        image = new GLImage(this, executorSupplier.forDecode(), new Runnable() {

                            public void run() {
                                onImageLoad(src);
                            }
                        });
                        image.setSrc(src);
                        images.put(src, image);
                    }
                    textures.put(uniformName, image.getTexture());
                } else {
                    shader.runtimeException("texture uniform '" + uniformName + "': Unexpected type '" + type + "'");
                }
            }
        } else {
            if (size == 1) {
                switch(type) {
                    case GL_INT:
                        uniformsInteger.put(uniformName, dataUniforms.getInt(uniformName));
                        break;
                    case GL_BOOL:
                        uniformsInteger.put(uniformName, dataUniforms.getBoolean(uniformName) ? 1 : 0);
                        break;
                    case GL_FLOAT:
                        uniformsFloat.put(uniformName, (float) dataUniforms.getDouble(uniformName));
                        break;
                    case GL_FLOAT_VEC2:
                    case GL_FLOAT_VEC3:
                    case GL_FLOAT_VEC4:
                    case GL_FLOAT_MAT2:
                    case GL_FLOAT_MAT3:
                    case GL_FLOAT_MAT4:
                        ReadableArray arr = dataUniforms.getArray(uniformName);
                        if (arraySizeForType(type) != arr.size()) {
                            shader.runtimeException("uniform '" + uniformName + "': Invalid array size: " + arr.size() + ". Expected: " + arraySizeForType(type));
                        }
                        uniformsFloatBuffer.put(uniformName, parseAsFloatArray(arr));
                        break;
                    case GL_INT_VEC2:
                    case GL_INT_VEC3:
                    case GL_INT_VEC4:
                    case GL_BOOL_VEC2:
                    case GL_BOOL_VEC3:
                    case GL_BOOL_VEC4:
                        ReadableArray arr2 = dataUniforms.getArray(uniformName);
                        if (arraySizeForType(type) != arr2.size()) {
                            shader.runtimeException("uniform '" + uniformName + "': Invalid array size: " + arr2.size() + ". Expected: " + arraySizeForType(type));
                        }
                        uniformsIntBuffer.put(uniformName, parseAsIntArray(arr2));
                        break;
                    default:
                        shader.runtimeException("uniform '" + uniformName + "': type not supported: " + type);
                }
            } else {
                ReadableArray array = dataUniforms.getArray(uniformName);
                if (size != array.size()) {
                    shader.runtimeException("uniform '" + uniformName + "': Invalid array size: " + array.size() + ". Expected: " + size);
                }
                for (int i = 0; i < size; i++) {
                    String name = uniformName + "[" + i + "]";
                    switch(type) {
                        case GL_INT:
                            uniformsInteger.put(name, array.getInt(i));
                            break;
                        case GL_BOOL:
                            uniformsInteger.put(name, array.getBoolean(i) ? 1 : 0);
                            break;
                        case GL_FLOAT:
                            uniformsFloat.put(name, (float) array.getDouble(i));
                            break;
                        case GL_FLOAT_VEC2:
                        case GL_FLOAT_VEC3:
                        case GL_FLOAT_VEC4:
                        case GL_FLOAT_MAT2:
                        case GL_FLOAT_MAT3:
                        case GL_FLOAT_MAT4:
                            ReadableArray arr = array.getArray(i);
                            if (arraySizeForType(type) != arr.size()) {
                                shader.runtimeException("uniform '" + name + "': Invalid array size: " + arr.size() + ". Expected: " + arraySizeForType(type));
                            }
                            uniformsFloatBuffer.put(name, parseAsFloatArray(arr));
                            break;
                        case GL_INT_VEC2:
                        case GL_INT_VEC3:
                        case GL_INT_VEC4:
                        case GL_BOOL_VEC2:
                        case GL_BOOL_VEC3:
                        case GL_BOOL_VEC4:
                            ReadableArray arr2 = array.getArray(i);
                            if (arraySizeForType(type) != arr2.size()) {
                                shader.runtimeException("uniform '" + name + "': Invalid array size: " + arr2.size() + ". Expected: " + arraySizeForType(type));
                            }
                            uniformsIntBuffer.put(name, parseAsIntArray(arr2));
                            break;
                        default:
                            shader.runtimeException("uniform '" + name + "': type not supported: " + type);
                    }
                }
            }
        }
    }
    int[] maxTextureUnits = new int[1];
    glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, maxTextureUnits, 0);
    if (units > maxTextureUnits[0]) {
        shader.runtimeException("Maximum number of texture reach. got " + units + " >= max " + maxTextureUnits);
    }
    for (String uniformName : uniformNames) {
        int size = uniformSizes.get(uniformName);
        if (size == 1) {
            if (!uniformsFloat.containsKey(uniformName) && !uniformsInteger.containsKey(uniformName) && !uniformsFloatBuffer.containsKey(uniformName) && !uniformsIntBuffer.containsKey(uniformName)) {
                shader.runtimeException("All defined uniforms must be provided. Missing '" + uniformName + "'");
            }
        } else {
            for (int i = 0; i < size; i++) {
                String name = uniformName + "[" + i + "]";
                if (!uniformsFloat.containsKey(name) && !uniformsInteger.containsKey(name) && !uniformsFloatBuffer.containsKey(name) && !uniformsIntBuffer.containsKey(name)) {
                    shader.runtimeException("All defined uniforms must be provided. Missing '" + name + "'");
                }
            }
        }
    }
    return new GLRenderData(shader, uniformsInteger, uniformsFloat, uniformsIntBuffer, uniformsFloatBuffer, textures, (int) (data.width * data.pixelRatio), (int) (data.height * data.pixelRatio), data.fboId, contextChildren, children);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) FloatBuffer(java.nio.FloatBuffer) Uri(android.net.Uri) ReadableMap(com.facebook.react.bridge.ReadableMap) ReadableArray(com.facebook.react.bridge.ReadableArray) GLException(android.opengl.GLException) ReadableMapKeySetIterator(com.facebook.react.bridge.ReadableMapKeySetIterator) IntBuffer(java.nio.IntBuffer)

Example 5 with ReadableMapKeySetIterator

use of com.facebook.react.bridge.ReadableMapKeySetIterator in project native-navigation by airbnb.

the class DefaultNavigationImplementation method mapEqual.

private static boolean mapEqual(ReadableMap a, ReadableMap b) {
    ReadableMapKeySetIterator iterator = b.keySetIterator();
    while (iterator.hasNextKey()) {
        String key = iterator.nextKey();
        if (!a.hasKey(key))
            return false;
        ReadableType type = b.getType(key);
        if (type != a.getType(key))
            return false;
        switch(type) {
            case Null:
                break;
            case Boolean:
                if (a.getBoolean(key) != b.getBoolean(key))
                    return false;
                break;
            case Number:
                if (a.getDouble(key) != b.getDouble(key))
                    return false;
                break;
            case String:
                if (!a.getString(key).equals(b.getString(key)))
                    return false;
                break;
            case Map:
                if (!mapEqual(a.getMap(key), b.getMap(key)))
                    return false;
                break;
            case Array:
                if (!arrayEqual(a.getArray(key), b.getArray(key)))
                    return false;
                break;
            default:
                Log.e(TAG, "Could not convert object with key: " + key + ".");
        }
    }
    return true;
}
Also used : ReadableMapKeySetIterator(com.facebook.react.bridge.ReadableMapKeySetIterator) ReadableType(com.facebook.react.bridge.ReadableType)

Aggregations

ReadableMapKeySetIterator (com.facebook.react.bridge.ReadableMapKeySetIterator)9 ReadableMap (com.facebook.react.bridge.ReadableMap)4 Bundle (android.os.Bundle)3 Uri (android.net.Uri)1 GLException (android.opengl.GLException)1 ReactMethod (com.facebook.react.bridge.ReactMethod)1 ReadableArray (com.facebook.react.bridge.ReadableArray)1 ReadableType (com.facebook.react.bridge.ReadableType)1 ShareOpenGraphAction (com.facebook.share.model.ShareOpenGraphAction)1 ShareOpenGraphObject (com.facebook.share.model.ShareOpenGraphObject)1 FirebaseMessaging (com.google.firebase.messaging.FirebaseMessaging)1 RemoteMessage (com.google.firebase.messaging.RemoteMessage)1 FloatBuffer (java.nio.FloatBuffer)1 IntBuffer (java.nio.IntBuffer)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 JSONObject (org.json.JSONObject)1