react-native-macos/packages/react-native-codegen/buck_tests/java/InterfaceOnlyNativeComponen...

31 строка
985 B
Java
Исходник Обычный вид История

package com.facebook.react.uimanager;
import android.view.ViewGroup;
import com.facebook.react.viewmanagers.InterfaceOnlyNativeComponentViewManagerDelegate;
import com.facebook.react.viewmanagers.InterfaceOnlyNativeComponentViewManagerInterface;
public class InterfaceOnlyNativeComponentViewManager extends SimpleViewManager<ViewGroup>
implements InterfaceOnlyNativeComponentViewManagerInterface<ViewGroup> {
public static final String REACT_CLASS = "InterfaceOnlyNativeComponentView";
@Override
public String getName() {
return REACT_CLASS;
}
private void test() {
Use ViewManagerDelegate if provided instead of $$PropsSetter to update view props Summary: This diff introduces an interface `ViewManagerDelegate` and its base implementation `BaseViewManagerDelegate`, which is used as a parent class for all view manager delegates generated by the JS codegen. Before the changes in this diff, generated delegates didn't support setting the base view properties such as background color, rotation, opacity, etc. Now it's possible to do by using `BaseViewManagerDelegate.setProperty(...)`, and since all generated delegates extend BaseViewManagerDelegate, they can just call `super.setProperty(...)` for properties they don't want to handle. This diff also introduced a new method `ViewManager.getDelegate()`. This will allow view managers to return an instance of the delegate generated by JS and ensure that the view properties are set in a type-safe manner. If this method returns null (it does by default), we fall back to the default implementation of setting view properties using Java-generated `$$PropsSetter` classes. This is an example of an interface class generated by JS: ``` public interface RCTAxialGradientViewViewManagerInterface<T extends View> { void setColors(T view, Nullable ReadableArray value); void setLocations(T view, Nullable ReadableArray value); void setEndX(T view, Float value); void setEndY(T view, Float value); void setStartX(T view, Float value); void setStartY(T view, Float value); } ``` This is an example of a delegate class generated by JS: ``` public class RCTAxialGradientViewManagerDelegate<T extends View, U extends BaseViewManager<T, ? extends LayoutShadowNode> & RCTAxialGradientViewManagerInterface<T>> extends BaseViewManagerDelegate<T, U> { public RCTAxialGradientViewManagerDelegate(U viewManager) { super(viewManager); } Override public void setProperty(T view, String propName, Nullable Object value) { switch (propName) { case "colors": mViewManager.setColors(view, (ReadableArray) value); break; case "locations": mViewManager.setLocations(view, (ReadableArray) value); break; case "endX": mViewManager.setEndX(view, value == null ? Float.NaN : ((Double) value).floatValue()); break; case "endY": mViewManager.setEndY(view, value == null ? Float.NaN : ((Double) value).floatValue()); break; case "startX": mViewManager.setStartX(view, value == null ? Float.NaN : ((Double) value).floatValue()); break; case "startY": mViewManager.setStartY(view, value == null ? Float.NaN : ((Double) value).floatValue()); break; default: super.setProperty(view, propName, value); } } } ``` NOTE: What if a view manager, for instance ReactAxialGradientManager, wanted to add support for the borderRadius prop? In the old Java codegen, it would just need to create a method and annotate it with ReactProp (name = ViewProps.BORDER_RADIUS) and $$PropsSetter would call this method when a property with this name must be set. With the new JS codegen, borderRadius is a part of the basic view props, so setBorderRadius is not generated as a part of the ViewManagerInterface, so it’s not possible to set this value. I see two options: 1) add a method boolean setProperty (String propName, Object value) and let the view manager handle it in a non-type safe way (return true if it’s been handled). 2) Generate BaseViewManagerInterface which will include all basic view props and make BaseViewManager implement this interface, leaving all methods empty so that it stays compatible with the current implementation. Override these methods in a view manager that needs to handle a specific property in a custom way (so we would override setBorderRadius in ReactAxialGradientManager). Reviewed By: mdvacca Differential Revision: D16667686 fbshipit-source-id: 06a15a92f8af55640b7a53c5a34f40366d1be2a8
2019-08-12 16:32:20 +03:00
InterfaceOnlyNativeComponentViewManagerDelegate<
ViewGroup, InterfaceOnlyNativeComponentViewManager>
delegate = new InterfaceOnlyNativeComponentViewManagerDelegate<>(this);
}
@Override
public ViewGroup createViewInstance(ThemedReactContext context) {
throw new IllegalStateException();
}
@Override
Fix tests for JS codegen by renaming conflicting props Summary: ** Summary of failures encountered during the build ** Rule //fbandroid/java/com/facebook/catalyst/launcher:app_prod_debug FAILED because Command failed with exit code 1. stderr: /java/InterfaceOnlyNativeComponentViewManagerDelegate.java:18: error: reference to setAccessibilityHint is ambiguous mViewManager.setAccessibilityHint(view, value == null ? "" : (String) value); ^ both method setAccessibilityHint(T,java.lang.String) in com.facebook.react.uimanager.BaseViewManagerInterface and method setAccessibilityHint(T,java.lang.String) in com.facebook.react.viewmanagers.InterfaceOnlyNativeComponentViewManagerInterface match /java/StringPropNativeComponentViewManagerDelegate.java:18: error: reference to setAccessibilityHint is ambiguous mViewManager.setAccessibilityHint(view, value == null ? "" : (String) value); ^ both method setAccessibilityHint(T,java.lang.String) in com.facebook.react.uimanager.BaseViewManagerInterface and method setAccessibilityHint(T,java.lang.String) in com.facebook.react.viewmanagers.StringPropNativeComponentViewManagerInterface match /java/StringPropNativeComponentViewManagerDelegate.java:21: error: reference to setAccessibilityRole is ambiguous mViewManager.setAccessibilityRole(view, value == null ? null : (String) value); ^ both method setAccessibilityRole(T,java.lang.String) in com.facebook.react.uimanager.BaseViewManagerInterface and method setAccessibilityRole(T,java.lang.String) in com.facebook.react.viewmanagers.StringPropNativeComponentViewManagerInterface match Errors: 3. Warnings: 0. When running <javac_jar>. When building rule //xplat/js/react-native-github/packages/react-native-codegen:generated_components_java-codegen_testsAndroid. ``` Reviewed By: mdvacca Differential Revision: D17107929 fbshipit-source-id: 32bc553d450628c530e22cb13f305e3a3e0f45cd
2019-08-30 19:39:04 +03:00
public void setTitle(ViewGroup view, String value) {}
}