Permalink
Browse files

Convert remaining viewmanager to @ReactProp.

Differential Revision: D2481816

committer: Service User <svcscm@fb.com>
  • Loading branch information...
kmagiera authored and facebook-github-bot-7 committed Sep 25, 2015
1 parent 0fb3d8d commit 656126a2f1c8a1170faab61e1eeda8545b3ce944
@@ -54,8 +54,8 @@ public boolean onInterceptTouchEvent(MotionEvent ev) {
setDrawerProperties();
}
/* package */ void setDrawerWidth(int drawerWidth) {
mDrawerWidth = (int) PixelUtil.toPixelFromDIP((float) drawerWidth);
/* package */ void setDrawerWidth(int drawerWidthInPx) {
mDrawerWidth = drawerWidthInPx;
setDrawerProperties();
}
@@ -21,10 +21,10 @@
import com.facebook.react.bridge.JSApplicationIllegalArgumentException;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.common.MapBuilder;
import com.facebook.react.uimanager.CatalystStylesDiffMap;
import com.facebook.react.uimanager.PixelUtil;
import com.facebook.react.uimanager.ReactProp;
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.uimanager.UIManagerModule;
import com.facebook.react.uimanager.UIProp;
import com.facebook.react.uimanager.ViewGroupManager;
import com.facebook.react.uimanager.events.EventDispatcher;
import com.facebook.react.views.drawer.events.DrawerClosedEvent;
@@ -42,11 +42,6 @@
public static final int OPEN_DRAWER = 1;
public static final int CLOSE_DRAWER = 2;
@UIProp(UIProp.Type.NUMBER)
public static final String PROP_DRAWER_POSITION = "drawerPosition";
@UIProp(UIProp.Type.NUMBER)
public static final String PROP_DRAWER_WIDTH = "drawerWidth";
@Override
public String getName() {
return REACT_CLASS;
@@ -65,22 +60,20 @@ protected ReactDrawerLayout createViewInstance(ThemedReactContext context) {
return new ReactDrawerLayout(context);
}
@Override
public void updateView(ReactDrawerLayout view, CatalystStylesDiffMap props) {
super.updateView(view, props);
if (props.hasKey(PROP_DRAWER_POSITION)) {
int drawerPosition = props.getInt(PROP_DRAWER_POSITION, -1);
if (Gravity.START == drawerPosition || Gravity.END == drawerPosition) {
view.setDrawerPosition(drawerPosition);
} else {
throw new JSApplicationIllegalArgumentException("Unknown drawerPosition " + drawerPosition);
}
@ReactProp(name = "drawerPosition", defaultInt = Gravity.START)
public void setDrawerPosition(ReactDrawerLayout view, int drawerPosition) {
if (Gravity.START == drawerPosition || Gravity.END == drawerPosition) {
view.setDrawerPosition(drawerPosition);
} else {
throw new JSApplicationIllegalArgumentException("Unknown drawerPosition " + drawerPosition);
}
}
if (props.hasKey(PROP_DRAWER_WIDTH)) {
view.setDrawerWidth(props.getInt(PROP_DRAWER_WIDTH, ReactDrawerLayout.DEFAULT_DRAWER_WIDTH));
}
@ReactProp(name = "drawerWidth", defaultFloat = Float.NaN)
public void getDrawerWidth(ReactDrawerLayout view, float width) {
int widthInPx = Float.isNaN(width) ?
ReactDrawerLayout.DEFAULT_DRAWER_WIDTH : Math.round(PixelUtil.toPixelFromDIP(width));
view.setDrawerWidth(widthInPx);
}
@Override
@@ -15,10 +15,9 @@
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.controller.AbstractDraweeControllerBuilder;
import com.facebook.react.uimanager.CatalystStylesDiffMap;
import com.facebook.react.uimanager.ReactProp;
import com.facebook.react.uimanager.SimpleViewManager;
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.uimanager.UIProp;
import com.facebook.react.uimanager.ViewProps;
public class ReactImageManager extends SimpleViewManager<ReactImageView> {
@@ -30,15 +29,6 @@ public String getName() {
return REACT_CLASS;
}
// In JS this is Image.props.source.uri
@UIProp(UIProp.Type.STRING)
public static final String PROP_SRC = "src";
@UIProp(UIProp.Type.NUMBER)
public static final String PROP_BORDER_RADIUS = "borderRadius";
@UIProp(UIProp.Type.STRING)
public static final String PROP_RESIZE_MODE = ViewProps.RESIZE_MODE;
private static final String PROP_TINT_COLOR = "tintColor";
private final @Nullable AbstractDraweeControllerBuilder mDraweeControllerBuilder;
private final @Nullable Object mCallerContext;
@@ -63,27 +53,34 @@ public ReactImageView createViewInstance(ThemedReactContext context) {
mCallerContext);
}
@Override
public void updateView(final ReactImageView view, final CatalystStylesDiffMap props) {
super.updateView(view, props);
// In JS this is Image.props.source.uri
@ReactProp(name = "src")
public void setSource(ReactImageView view, @Nullable String source) {
view.setSource(source);
}
if (props.hasKey(PROP_RESIZE_MODE)) {
view.setScaleType(ImageResizeMode.toScaleType(props.getString(PROP_RESIZE_MODE)));
}
if (props.hasKey(PROP_SRC)) {
view.setSource(props.getString(PROP_SRC));
}
if (props.hasKey(PROP_BORDER_RADIUS)) {
view.setBorderRadius(props.getFloat(PROP_BORDER_RADIUS, 0.0f));
}
if (props.hasKey(PROP_TINT_COLOR)) {
if (props.isNull(PROP_TINT_COLOR)) {
view.clearColorFilter();
} else {
final int tintColor = props.getInt(PROP_TINT_COLOR, Color.TRANSPARENT);
view.setColorFilter(tintColor);
}
@ReactProp(name = "borderRadius")
public void setBorderRadius(ReactImageView view, float borderRadius) {
view.setBorderRadius(borderRadius);
}
@ReactProp(name = ViewProps.RESIZE_MODE)
public void setResizeMode(ReactImageView view, @Nullable String resizeMode) {
view.setScaleType(ImageResizeMode.toScaleType(resizeMode));
}
@ReactProp(name = "tintColor", customType = "Color")
public void setTintColor(ReactImageView view, @Nullable Integer tintColor) {
if (tintColor == null) {
view.clearColorFilter();
} else {
view.setColorFilter(tintColor);
}
}
@Override
protected void onAfterUpdateTransaction(ReactImageView view) {
super.onAfterUpdateTransaction(view);
view.maybeUpdateView();
}
}
@@ -16,21 +16,21 @@
import android.widget.ProgressBar;
import com.facebook.react.bridge.JSApplicationIllegalArgumentException;
import com.facebook.react.uimanager.BaseViewPropertyApplicator;
import com.facebook.react.uimanager.CatalystStylesDiffMap;
import com.facebook.react.uimanager.BaseViewManager;
import com.facebook.react.uimanager.ReactProp;
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.uimanager.UIProp;
import com.facebook.react.uimanager.ViewManager;
/**
* Manages instances of ProgressBar. ProgressBar is wrapped in a FrameLayout because the style of
* the ProgressBar can only be set in the constructor; whenever the style of a ProgressBar changes,
* we have to drop the existing ProgressBar (if there is one) and create a new one with the style
* given.
*/
public class ReactProgressBarViewManager extends ViewManager<FrameLayout, ProgressBarShadowNode> {
public class ReactProgressBarViewManager extends
BaseViewManager<FrameLayout, ProgressBarShadowNode> {
@UIProp(UIProp.Type.STRING) public static final String PROP_STYLE = "styleAttr";
/* package */ static final String PROP_STYLE = "styleAttr";
/* package */ static final String REACT_CLASS = "AndroidProgressBar";
/* package */ static final String DEFAULT_STYLE = "Large";
@@ -45,18 +45,15 @@ protected FrameLayout createViewInstance(ThemedReactContext context) {
return new FrameLayout(context);
}
@Override
public void updateView(FrameLayout view, CatalystStylesDiffMap props) {
BaseViewPropertyApplicator.applyCommonViewProperties(view, props);
if (props.hasKey(PROP_STYLE)) {
final int style = getStyleFromString(props.getString(PROP_STYLE));
view.removeAllViews();
view.addView(
new ProgressBar(view.getContext(), null, style),
new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
}
@ReactProp(name = PROP_STYLE)
public void setStyle(FrameLayout view, @Nullable String styleName) {
final int style = getStyleFromString(styleName);
view.removeAllViews();
view.addView(
new ProgressBar(view.getContext(), null, style),
new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
}
@Override
@@ -15,9 +15,8 @@
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.common.MapBuilder;
import com.facebook.react.uimanager.CatalystStylesDiffMap;
import com.facebook.react.uimanager.ReactProp;
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.uimanager.UIProp;
import com.facebook.react.uimanager.ViewGroupManager;
import com.facebook.react.views.view.ReactClippingViewGroupHelper;
@@ -33,11 +32,6 @@
private static final String REACT_CLASS = "RCTScrollView";
@UIProp(UIProp.Type.BOOLEAN) public static final String PROP_SHOWS_VERTICAL_SCROLL_INDICATOR =
"showsVerticalScrollIndicator";
@UIProp(UIProp.Type.BOOLEAN) public static final String PROP_SHOWS_HORIZONTAL_SCROLL_INDICATOR =
"showsHorizontalScrollIndicator";
@Override
public String getName() {
return REACT_CLASS;
@@ -48,20 +42,19 @@ public ReactScrollView createViewInstance(ThemedReactContext context) {
return new ReactScrollView(context);
}
@Override
public void updateView(ReactScrollView scrollView, CatalystStylesDiffMap props) {
super.updateView(scrollView, props);
if (props.hasKey(PROP_SHOWS_VERTICAL_SCROLL_INDICATOR)) {
scrollView.setVerticalScrollBarEnabled(
props.getBoolean(PROP_SHOWS_VERTICAL_SCROLL_INDICATOR, true));
}
if (props.hasKey(PROP_SHOWS_HORIZONTAL_SCROLL_INDICATOR)) {
scrollView.setHorizontalScrollBarEnabled(
props.getBoolean(PROP_SHOWS_HORIZONTAL_SCROLL_INDICATOR, true));
}
ReactClippingViewGroupHelper.applyRemoveClippedSubviewsProperty(scrollView, props);
@ReactProp(name = "showsVerticalScrollIndicator")
public void setShowsVerticalScrollIndicator(ReactScrollView view, boolean value) {
view.setVerticalScrollBarEnabled(value);
}
@ReactProp(name = "showsHorizontalScrollIndicator")
public void setShowsHorizontalScrollIndicator(ReactScrollView view, boolean value) {
view.setHorizontalScrollBarEnabled(value);
}
@ReactProp(name = ReactClippingViewGroupHelper.PROP_REMOVE_CLIPPED_SUBVIEWS)
public void setRemoveClippedSubviews(ReactScrollView view, boolean removeClippedSubviews) {
view.setRemoveClippedSubviews(removeClippedSubviews);
}
@Override
@@ -18,13 +18,11 @@
import com.facebook.csslayout.CSSNode;
import com.facebook.csslayout.MeasureOutput;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.uimanager.CatalystStylesDiffMap;
import com.facebook.react.uimanager.ReactProp;
import com.facebook.react.uimanager.ReactShadowNode;
import com.facebook.react.uimanager.SimpleViewManager;
import com.facebook.react.uimanager.UIManagerModule;
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.uimanager.UIProp;
import com.facebook.react.uimanager.ViewProps;
/**
@@ -33,8 +31,6 @@
public class ReactSwitchManager extends SimpleViewManager<ReactSwitch> {
private static final String REACT_CLASS = "AndroidSwitch";
@UIProp(UIProp.Type.BOOLEAN) public static final String PROP_ENABLED = ViewProps.ENABLED;
@UIProp(UIProp.Type.BOOLEAN) public static final String PROP_ON = ViewProps.ON;
private static class ReactSwitchShadowNode extends ReactShadowNode implements
CSSNode.MeasureFunction {
@@ -97,19 +93,18 @@ protected ReactSwitch createViewInstance(ThemedReactContext context) {
return view;
}
@Override
public void updateView(ReactSwitch view, CatalystStylesDiffMap props) {
super.updateView(view, props);
if (props.hasKey(PROP_ENABLED)) {
view.setEnabled(props.getBoolean(PROP_ENABLED, true));
}
if (props.hasKey(PROP_ON)) {
// we set the checked change listener to null and then restore it so that we don't fire an
// onChange event to JS when JS itself is updating the value of the switch
view.setOnCheckedChangeListener(null);
view.setOn(props.getBoolean(PROP_ON, false));
view.setOnCheckedChangeListener(ON_CHECKED_CHANGE_LISTENER);
}
@ReactProp(name = ViewProps.ENABLED, defaultBoolean = true)
public void setEnabled(ReactSwitch view, boolean enabled) {
view.setEnabled(enabled);
}
@ReactProp(name = ViewProps.ON)
public void setOn(ReactSwitch view, boolean on) {
// we set the checked change listener to null and then restore it so that we don't fire an
// onChange event to JS when JS itself is updating the value of the switch
view.setOnCheckedChangeListener(null);
view.setOn(on);
view.setOnCheckedChangeListener(ON_CHECKED_CHANGE_LISTENER);
}
@Override
Oops, something went wrong.

1 comment on commit 656126a

@brentvatne

This comment has been minimized.

Show comment
Hide comment
@brentvatne

brentvatne Sep 25, 2015

Collaborator

Neat, this is a lot closer to how native modules work on iOS now. I'm curious if that was the primary goal or if you had some other need for this @kmagiera :)

Collaborator

brentvatne commented on 656126a Sep 25, 2015

Neat, this is a lot closer to how native modules work on iOS now. I'm curious if that was the primary goal or if you had some other need for this @kmagiera :)

Please sign in to comment.