Java Code Examples for com.facebook.react.bridge.ReadableType#Number

The following examples show how to use com.facebook.react.bridge.ReadableType#Number . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JSStackTrace.java    From react-native-GPay with MIT License 6 votes vote down vote up
public static String format(String message, ReadableArray stack) {
  StringBuilder stringBuilder = new StringBuilder(message).append(", stack:\n");
  for (int i = 0; i < stack.size(); i++) {
    ReadableMap frame = stack.getMap(i);
    stringBuilder
      .append(frame.getString("methodName"))
      .append("@")
      .append(stackFrameToModuleId(frame))
      .append(frame.getInt("lineNumber"));
    if (frame.hasKey("column") &&
      !frame.isNull("column") &&
      frame.getType("column") == ReadableType.Number) {
      stringBuilder
        .append(":")
        .append(frame.getInt("column"));
    }
    stringBuilder.append("\n");
  }
  return stringBuilder.toString();
}
 
Example 2
Source File: SQLiteAndroidDatabase.java    From react-native-sqlite-storage with MIT License 6 votes vote down vote up
private void bindArgsToStatement(SQLiteStatement myStatement, ReadableArray sqlArgs) {
    if (sqlArgs == null)
        return;

    for (int i = 0; i < sqlArgs.size(); i++) {
        ReadableType type = sqlArgs.getType(i);
        if (type == ReadableType.Number) {
            double tmp = sqlArgs.getDouble(i);
            if (tmp == (long) tmp) {
                myStatement.bindLong(i + 1, (long) tmp);
            } else {
                myStatement.bindDouble(i + 1, tmp);
            }
        } else if (sqlArgs.isNull(i)) {
            myStatement.bindNull(i + 1);
        } else {
            myStatement.bindString(i + 1, sqlArgs.getString(i));
        }
    }
}
 
Example 3
Source File: SQLitePlugin.java    From react-native-sqlite-storage with MIT License 6 votes vote down vote up
private void bindArgsToStatement(SQLiteStatement myStatement, ReadableArray sqlArgs) {
    for (int i = 0; i < sqlArgs.size(); i++) {
        ReadableType type = sqlArgs.getType(i);
        if (type == ReadableType.Number){
            double tmp = sqlArgs.getDouble(i);
            if (tmp == (long) tmp) {
                myStatement.bindLong(i + 1, (long) tmp);
            } else {
                myStatement.bindDouble(i + 1, tmp);
            }
        } else if (sqlArgs.isNull(i)) {
            myStatement.bindNull(i + 1);
        } else {
            myStatement.bindString(i + 1, SQLitePluginConverter.getString(sqlArgs,i,""));
        }
    }
}
 
Example 4
Source File: SQLiteManager.java    From react-native-android-sqlite with MIT License 6 votes vote down vote up
public void exec(final String sql, final ReadableArray values) {
	SQLiteStatement statement = Assertions.assertNotNull(db).compileStatement(sql);

	db.beginTransaction();
	try {
		for (int i=0; i < values.size(); i++) {
			if (values.getType(i) == ReadableType.Number) {
				statement.bindLong(i+1, values.getInt(i));
			} else {
				statement.bindString(i+1, values.getString(i));
			}
		}

		statement.execute();
		db.setTransactionSuccessful();
	} catch (Exception e) {
		throw e;
	} finally {
		db.endTransaction();
	}
}
 
Example 5
Source File: ReactNativeSmooch.java    From react-native-smooch with MIT License 6 votes vote down vote up
private Map<String, Object> getUserProperties(ReadableMap properties) {
    ReadableMapKeySetIterator iterator = properties.keySetIterator();
    Map<String, Object> userProperties = new HashMap<>();

    while (iterator.hasNextKey()) {
        String key = iterator.nextKey();
        ReadableType type = properties.getType(key);
        if (type == ReadableType.Boolean) {
            userProperties.put(key, properties.getBoolean(key));
        } else if (type == ReadableType.Number) {
            userProperties.put(key, properties.getDouble(key));
        } else if (type == ReadableType.String) {
            userProperties.put(key, properties.getString(key));
        }
    }

    return userProperties;
}
 
Example 6
Source File: ReactDrawerLayoutManager_1d0b39_t.java    From coming with MIT License 6 votes vote down vote up
@ReactProp(name = "drawerPosition")
public void setDrawerPosition(ReactDrawerLayout view, Dynamic drawerPosition) {
  if (drawerPosition.isNull()) {
    view.setDrawerPosition(Gravity.START);
  } else if (drawerPosition.getType() == ReadableType.Number) {
    final int drawerPositionNum = drawerPosition.asInt();

    if (Gravity.START == drawerPositionNum || Gravity.END == drawerPositionNum) {
      view.setDrawerPosition(drawerPositionNum);
    } else {
      throw new JSApplicationIllegalArgumentException("Unknown drawerPosition " + drawerPositionNum);
    }
  } else if (drawerPosition.getType() == ReadableType.String) {
    final String drawerPositionStr = drawerPosition.asString();

    if (drawerPositionStr.equals("left")) {
      view.setDrawerPosition(Gravity.START);
    } else if (drawerPositionStr.equals("right")) {
      view.setDrawerPosition(Gravity.END);
    } else {
      throw new JSApplicationIllegalArgumentException("drawerPosition must be 'left' or 'right', received" + drawerPositionStr);
    }
  } else {
    throw new JSApplicationIllegalArgumentException("drawerPosition must be a string or int");
  }
}
 
Example 7
Source File: FrescoBasedReactTextInlineImageShadowNode.java    From react-native-GPay with MIT License 5 votes vote down vote up
/**
 * Besides width/height, all other layout props on inline images are ignored
 */
@Override
public void setWidth(Dynamic width) {
  if (width.getType() == ReadableType.Number) {
    mWidth = (float) width.asDouble();
  } else {
    throw new JSApplicationIllegalArgumentException(
        "Inline images must not have percentage based width");
  }
}
 
Example 8
Source File: FrescoBasedReactTextInlineImageShadowNode.java    From react-native-GPay with MIT License 5 votes vote down vote up
@Override
public void setHeight(Dynamic height) {
  if (height.getType() == ReadableType.Number) {
    mHeight = (float) height.asDouble();
  } else {
    throw new JSApplicationIllegalArgumentException(
        "Inline images must not have percentage based height");
  }
}
 
Example 9
Source File: NavigatorModule.java    From native-navigation with MIT License 5 votes vote down vote up
/**
 * Returns the result_code from the ReadableMap payload or RESULT_OK if none found. <p> Throws
 * IllegalArgumentException if the resultCode is not a number.
 */
private static int getResultCodeFromPayload(ReadableMap payload) {
  if (payload == null) {
    return Activity.RESULT_OK;
  }
  if (!payload.hasKey(RESULT_CODE)) {
    return Activity.RESULT_OK;
  }
  if (payload.getType(RESULT_CODE) != ReadableType.Number) {
    throw new IllegalArgumentException("Found non-integer resultCode.");
  }
  return payload.getInt(RESULT_CODE);
}
 
Example 10
Source File: CheckoutModule.java    From react-native-square-reader-sdk with Apache License 2.0 4 votes vote down vote up
static private boolean validateJSCheckoutParams(ReadableMap jsCheckoutParams, StringBuilder paramError) {
    // check types of all parameters
    if (!jsCheckoutParams.hasKey("amountMoney") || jsCheckoutParams.getType("amountMoney") != ReadableType.Map) {
        paramError.append("'amountMoney' is missing or not an object");
        return false;
    } else if (jsCheckoutParams.hasKey("skipReceipt") && jsCheckoutParams.getType("skipReceipt") != ReadableType.Boolean) {
        paramError.append("'skipReceipt' is not a boolean");
        return false;
    } else if (jsCheckoutParams.hasKey("collectSignature") && jsCheckoutParams.getType("collectSignature") != ReadableType.Boolean) {
        paramError.append("'collectSignature' is not a boolean");
        return false;
    } else if (jsCheckoutParams.hasKey("allowSplitTender") && jsCheckoutParams.getType("allowSplitTender") != ReadableType.Boolean) {
        paramError.append("'allowSplitTender' is not a boolean");
        return false;
    } else if (jsCheckoutParams.hasKey("delayCapture") && jsCheckoutParams.getType("delayCapture") != ReadableType.Boolean) {
        paramError.append("'delayCapture' is not a boolean");
        return false;
    } else if (jsCheckoutParams.hasKey("note") && jsCheckoutParams.getType("note") != ReadableType.String) {
        paramError.append("'note' is not a string");
        return false;
    } else if (jsCheckoutParams.hasKey("tipSettings") && jsCheckoutParams.getType("tipSettings") != ReadableType.Map) {
        paramError.append("'tipSettings' is not an object");
        return false;
    } else if (jsCheckoutParams.hasKey("additionalPaymentTypes") && jsCheckoutParams.getType("additionalPaymentTypes") != ReadableType.Array) {
        paramError.append("'additionalPaymentTypes' is not an array");
        return false;
    }

    // check amountMoney
    ReadableMap amountMoney = jsCheckoutParams.getMap("amountMoney");
    if (!amountMoney.hasKey("amount") || amountMoney.getType("amount") != ReadableType.Number) {
        paramError.append("'amount' is not an integer");
        return false;
    }
    if (amountMoney.hasKey("currencyCode") && amountMoney.getType("currencyCode") != ReadableType.String) {
        paramError.append("'currencyCode' is not a String");
        return false;
    }
    if (amountMoney.hasKey("currencyCode")) {
        try {
            CurrencyCode.valueOf(amountMoney.getString("currencyCode"));
        } catch (IllegalArgumentException ex) {
            paramError.append("failed to parse 'currencyCode'");
            return false;
        }
    }

    if (jsCheckoutParams.hasKey("tipSettings")) {
        // check tipSettings
        ReadableMap tipSettings = jsCheckoutParams.getMap("tipSettings");
        if (tipSettings.hasKey("showCustomTipField") && tipSettings.getType("showCustomTipField") != ReadableType.Boolean) {
            paramError.append("'showCustomTipField' is not a boolean");
            return false;
        } else if (tipSettings.hasKey("showSeparateTipScreen") && tipSettings.getType("showSeparateTipScreen") != ReadableType.Boolean) {
            paramError.append("'showSeparateTipScreen' is not a boolean");
            return false;
        } else if (tipSettings.hasKey("tipPercentages") && tipSettings.getType("tipPercentages") != ReadableType.Array) {
            paramError.append("'tipPercentages' is not an array");
            return false;
        }
    }

    return true;
}
 
Example 11
Source File: SQLitePlugin.java    From react-native-sqlite-storage with MIT License 4 votes vote down vote up
/**
 * Get rows results from query cursor.
 *
 * @return results in string form
 */
private WritableMap executeSqlStatementNDK(String query, ReadableArray queryArgs,
                                           CallbackContext cbc) throws Exception {
    WritableMap rowsResult = Arguments.createMap();

    boolean hasRows;

    SQLiteStatement myStatement = null;
    try {
        try {
            myStatement = mydb.prepareStatement(query);
            if (queryArgs != null) {
                for (int i = 0; i < queryArgs.size(); ++i) {
                    ReadableType type = queryArgs.getType(i);
                    if (type == ReadableType.Number){
                        double tmp = queryArgs.getDouble(i);
                        if (tmp == (long) tmp) {
                            myStatement.bindLong(i + 1, (long) tmp);
                        } else {
                            myStatement.bindDouble(i + 1, tmp);
                        }
                    } else if (queryArgs.isNull(i)) {
                        myStatement.bindNull(i + 1);
                    } else {
                        myStatement.bindTextNativeString(i + 1, SQLitePluginConverter.getString(queryArgs,i,""));
                    }
                }
            }

            hasRows = myStatement.step();
        } catch (Exception ex) {
            FLog.e(TAG, "SQLitePlugin.executeSql[Batch]() failed", ex);
            throw ex;
        }

        // If query result has rows
        if (hasRows) {
            WritableArray rowsArrayResult = Arguments.createArray();
            String key;
            int colCount = myStatement.getColumnCount();

            // Build up JSON result object for each row
            do {
                WritableMap row = Arguments.createMap();
                for (int i = 0; i < colCount; ++i) {
                    key = myStatement.getColumnName(i);

                    switch (myStatement.getColumnType(i)) {
                        case SQLColumnType.NULL:
                            row.putNull(key);
                            break;

                        case SQLColumnType.REAL:
                            row.putDouble(key, myStatement.getColumnDouble(i));
                            break;

                        case SQLColumnType.INTEGER:
                            row.putDouble(key, myStatement.getColumnLong(i));
                            break;

                        case SQLColumnType.BLOB:
                        case SQLColumnType.TEXT:
                        default:
                            row.putString(key, myStatement.getColumnTextNativeString(i));
                    }

                }

                rowsArrayResult.pushMap(row);
            } while (myStatement.step());

            rowsResult.putArray("rows", rowsArrayResult);
        }
    } finally {
        if (myStatement != null) {
            myStatement.dispose();
        }
    }

    return rowsResult;
}
 
Example 12
Source File: SQLiteManager.java    From react-native-android-sqlite with MIT License 4 votes vote down vote up
public WritableArray query(final String sql, final ReadableArray values) {
	WritableArray data = Arguments.createArray();

	// FLog.w(ReactConstants.TAG, "values.size()=%s", Integer.toString(values.size()));

	String[] args = new String[values.size()];
	// FLog.w(ReactConstants.TAG, "sqlitemanager.query.args.length=%d", args.length);

	for ( int i=0; i < values.size(); i++) {
		if (values.getType(i) == ReadableType.Number) {
			args[i] = Integer.toString(values.getInt(i));
		} else {
			args[i] = values.getString(i);
		}
	}

	Cursor cursor = db.rawQuery(sql, args);

	try {
		if (cursor.moveToFirst()) {
			do {
				WritableMap item = Arguments.createMap();
				for (int i=0; i < cursor.getColumnCount(); i++) {
					switch( cursor.getType(i) ) {
						case Cursor.FIELD_TYPE_INTEGER:
							item.putInt(cursor.getColumnName(i), cursor.getInt(i));
							break;
						default:
							item.putString(cursor.getColumnName(i), cursor.getString(i));
							break;
					}
				}

				data.pushMap(item);
			} while (cursor.moveToNext());
		}
	} catch (Exception e) {
		throw e;
	} finally {
		cursor.close();
	}

	return data;
}