Permalink
Browse files

Add null checks for dynamics

Reviewed By: ahmedre

Differential Revision: D4400532

fbshipit-source-id: d6e503ca39fe1cf90baaf52348b6fa3130f76fb8
  • Loading branch information...
emilsjolander authored and facebook-github-bot committed Jan 11, 2017
1 parent 3f49e74 commit b15c042a21359a727f2c5d79721994473be83620
@@ -14,6 +14,7 @@
* pass one of multiple types down to the native layer.
*/
public interface Dynamic {
boolean isNull();
boolean asBoolean();
double asDouble();
int asInt();
@@ -42,6 +42,14 @@ public void recycle() {
sPool.release(this);
}
@Override
public boolean isNull() {
if (mArray == null) {
throw new IllegalStateException("This dynamic value has been recycled");
}
return mArray.isNull(mIndex);
}
@Override
public boolean asBoolean() {
if (mArray == null) {
@@ -42,6 +42,14 @@ public void recycle() {
sPool.release(this);
}
@Override
public boolean isNull() {
if (mMap == null || mName == null) {
throw new IllegalStateException("This dynamic value has been recycled");
}
return mMap.isNull(mName);
}
@Override
public boolean asBoolean() {
if (mMap == null || mName == null) {
@@ -45,16 +45,20 @@ private static float getDynamicAsFloat(Dynamic dynamic) {
return (float) PixelUtil.toPixelFromDIP(dynamic.asDouble());
}
private static boolean isNull(Dynamic d) {
return d == null || d.isNull();
}
@ReactProp(name = ViewProps.WIDTH)
public void setWidth(Dynamic width) {
if (isVirtual()) {
return;
}
if (width != null && dynamicIsPercent(width)) {
if (!isNull(width) && dynamicIsPercent(width)) {
setStyleWidthPercent(getDynamicAsPercent(width));
} else {
setStyleWidth(width == null ? YogaConstants.UNDEFINED : getDynamicAsFloat(width));
setStyleWidth(isNull(width) ? YogaConstants.UNDEFINED : getDynamicAsFloat(width));
}
width.recycle();
@@ -66,10 +70,10 @@ public void setMinWidth(Dynamic minWidth) {
return;
}
if (minWidth != null && dynamicIsPercent(minWidth)) {
if (!isNull(minWidth) && dynamicIsPercent(minWidth)) {
setStyleMinWidthPercent(getDynamicAsPercent(minWidth));
} else {
setStyleMinWidth(minWidth == null ? YogaConstants.UNDEFINED : getDynamicAsFloat(minWidth));
setStyleMinWidth(isNull(minWidth) ? YogaConstants.UNDEFINED : getDynamicAsFloat(minWidth));
}
minWidth.recycle();
@@ -81,10 +85,10 @@ public void setMaxWidth(Dynamic maxWidth) {
return;
}
if (maxWidth != null && dynamicIsPercent(maxWidth)) {
if (!isNull(maxWidth) && dynamicIsPercent(maxWidth)) {
setStyleMaxWidthPercent(getDynamicAsPercent(maxWidth));
} else {
setStyleMaxWidth(maxWidth == null ? YogaConstants.UNDEFINED : getDynamicAsFloat(maxWidth));
setStyleMaxWidth(isNull(maxWidth) ? YogaConstants.UNDEFINED : getDynamicAsFloat(maxWidth));
}
maxWidth.recycle();
@@ -96,10 +100,10 @@ public void setHeight(Dynamic height) {
return;
}
if (height != null && dynamicIsPercent(height)) {
if (!isNull(height) && dynamicIsPercent(height)) {
setStyleHeightPercent(getDynamicAsPercent(height));
} else {
setStyleHeight(height == null ? YogaConstants.UNDEFINED : getDynamicAsFloat(height));
setStyleHeight(isNull(height) ? YogaConstants.UNDEFINED : getDynamicAsFloat(height));
}
height.recycle();
@@ -111,10 +115,10 @@ public void setMinHeight(Dynamic minHeight) {
return;
}
if (minHeight != null && dynamicIsPercent(minHeight)) {
if (!isNull(minHeight) && dynamicIsPercent(minHeight)) {
setStyleMinHeightPercent(getDynamicAsPercent(minHeight));
} else {
setStyleMinHeight(minHeight == null ? YogaConstants.UNDEFINED : getDynamicAsFloat(minHeight));
setStyleMinHeight(isNull(minHeight) ? YogaConstants.UNDEFINED : getDynamicAsFloat(minHeight));
}
minHeight.recycle();
@@ -126,10 +130,10 @@ public void setMaxHeight(Dynamic maxHeight) {
return;
}
if (maxHeight != null && dynamicIsPercent(maxHeight)) {
if (!isNull(maxHeight) && dynamicIsPercent(maxHeight)) {
setStyleMaxHeightPercent(getDynamicAsPercent(maxHeight));
} else {
setStyleMaxHeight(maxHeight == null ? YogaConstants.UNDEFINED : getDynamicAsFloat(maxHeight));
setStyleMaxHeight(isNull(maxHeight) ? YogaConstants.UNDEFINED : getDynamicAsFloat(maxHeight));
}
maxHeight.recycle();
@@ -165,10 +169,10 @@ public void setFlexBasis(Dynamic flexBasis) {
return;
}
if (flexBasis != null && dynamicIsPercent(flexBasis)) {
if (!isNull(flexBasis) && dynamicIsPercent(flexBasis)) {
setFlexBasisPercent(getDynamicAsPercent(flexBasis));
} else {
setFlexBasis(flexBasis == null ? 0 : getDynamicAsFloat(flexBasis));
setFlexBasis(isNull(flexBasis) ? 0 : getDynamicAsFloat(flexBasis));
}
flexBasis.recycle();
@@ -254,12 +258,12 @@ public void setMargins(int index, Dynamic margin) {
return;
}
if (margin != null && dynamicIsPercent(margin)) {
if (!isNull(margin) && dynamicIsPercent(margin)) {
setMarginPercent(ViewProps.PADDING_MARGIN_SPACING_TYPES[index], getDynamicAsPercent(margin));
} else {
setMargin(
ViewProps.PADDING_MARGIN_SPACING_TYPES[index],
margin == null ? YogaConstants.UNDEFINED : getDynamicAsFloat(margin));
isNull(margin) ? YogaConstants.UNDEFINED : getDynamicAsFloat(margin));
}
margin.recycle();
@@ -279,13 +283,13 @@ public void setPaddings(int index, Dynamic padding) {
return;
}
if (padding != null && dynamicIsPercent(padding)) {
if (!isNull(padding) && dynamicIsPercent(padding)) {
setPaddingPercent(
ViewProps.PADDING_MARGIN_SPACING_TYPES[index], getDynamicAsPercent(padding));
} else {
setPadding(
ViewProps.PADDING_MARGIN_SPACING_TYPES[index],
padding == null ? YogaConstants.UNDEFINED : getDynamicAsFloat(padding));
isNull(padding) ? YogaConstants.UNDEFINED : getDynamicAsFloat(padding));
}
padding.recycle();
@@ -316,12 +320,12 @@ public void setPositionValues(int index, Dynamic position) {
return;
}
if (position != null && dynamicIsPercent(position)) {
if (!isNull(position) && dynamicIsPercent(position)) {
setPositionPercent(ViewProps.POSITION_SPACING_TYPES[index], getDynamicAsPercent(position));
} else {
setPosition(
ViewProps.POSITION_SPACING_TYPES[index],
position == null ? YogaConstants.UNDEFINED : getDynamicAsFloat(position));
isNull(position) ? YogaConstants.UNDEFINED : getDynamicAsFloat(position));
}
position.recycle();

0 comments on commit b15c042

Please sign in to comment.