diff --git a/packages/rn-tester/android/app/src/main/java/com/facebook/react/uiapp/component/MyLegacyViewManager.java b/packages/rn-tester/android/app/src/main/java/com/facebook/react/uiapp/component/MyLegacyViewManager.java deleted file mode 100644 index e2f34691472..00000000000 --- a/packages/rn-tester/android/app/src/main/java/com/facebook/react/uiapp/component/MyLegacyViewManager.java +++ /dev/null @@ -1,111 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -package com.facebook.react.uiapp.component; - -import android.graphics.Color; -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; -import com.facebook.react.bridge.ReactApplicationContext; -import com.facebook.react.bridge.ReadableArray; -import com.facebook.react.common.MapBuilder; -import com.facebook.react.module.annotations.ReactModule; -import com.facebook.react.uimanager.SimpleViewManager; -import com.facebook.react.uimanager.ThemedReactContext; -import com.facebook.react.uimanager.ViewProps; -import com.facebook.react.uimanager.annotations.ReactProp; -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; - -/** Legacy View manager (non Fabric compatible) for {@link MyNativeView} components. */ -@ReactModule(name = MyLegacyViewManager.REACT_CLASS) -public class MyLegacyViewManager extends SimpleViewManager { - - public static final String REACT_CLASS = "RNTMyLegacyNativeView"; - private static final int COMMAND_CHANGE_BACKGROUND_COLOR = 42; - private final ReactApplicationContext mCallerContext; - - public MyLegacyViewManager(ReactApplicationContext reactContext) { - mCallerContext = reactContext; - } - - @NonNull - @Override - public String getName() { - return REACT_CLASS; - } - - @NonNull - @Override - protected MyNativeView createViewInstance(@NonNull ThemedReactContext reactContext) { - MyNativeView view = new MyNativeView(reactContext); - view.setBackgroundColor(Color.RED); - return view; - } - - @Override - @ReactProp(name = ViewProps.OPACITY, defaultFloat = 1.f) - public void setOpacity(@NonNull MyNativeView view, float opacity) { - super.setOpacity(view, opacity); - } - - @ReactProp(name = ViewProps.COLOR) - public void setColor(@NonNull MyNativeView view, @Nullable String color) { - view.setBackgroundColor(Color.parseColor(color)); - } - - @ReactProp(name = "cornerRadius") - public void setCornerRadius(@NonNull MyNativeView view, @Nullable float cornerRadius) { - view.setCornerRadius(cornerRadius); - } - - @Override - public final Map getExportedViewConstants() { - return Collections.singletonMap("PI", 3.14); - } - - @Override - public final Map getExportedCustomBubblingEventTypeConstants() { - return new HashMap<>( - MapBuilder.builder() - .put( - "onColorChanged", - MapBuilder.of( - "phasedRegistrationNames", - MapBuilder.of( - "bubbled", "onColorChanged", "captured", "onColorChangedCapture"))) - .build()); - } - - @Override - public void receiveCommand( - @NonNull MyNativeView view, String commandId, @Nullable ReadableArray args) { - if (commandId.contentEquals("changeBackgroundColor")) { - int sentColor = Color.parseColor(args.getString(0)); - view.setBackgroundColor(sentColor); - } - } - - @SuppressWarnings("deprecation") // We intentionally want to test against the legacy API here. - @Override - public void receiveCommand( - @NonNull MyNativeView view, int commandId, @Nullable ReadableArray args) { - switch (commandId) { - case COMMAND_CHANGE_BACKGROUND_COLOR: - int sentColor = Color.parseColor(args.getString(0)); - view.setBackgroundColor(sentColor); - break; - } - } - - @Nullable - @Override - public Map getCommandsMap() { - return MapBuilder.of("changeBackgroundColor", COMMAND_CHANGE_BACKGROUND_COLOR); - } -} diff --git a/packages/rn-tester/android/app/src/main/java/com/facebook/react/uiapp/component/MyLegacyViewManager.kt b/packages/rn-tester/android/app/src/main/java/com/facebook/react/uiapp/component/MyLegacyViewManager.kt new file mode 100644 index 00000000000..5845e85af53 --- /dev/null +++ b/packages/rn-tester/android/app/src/main/java/com/facebook/react/uiapp/component/MyLegacyViewManager.kt @@ -0,0 +1,85 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +package com.facebook.react.uiapp.component + +import android.graphics.Color +import com.facebook.react.bridge.ReactApplicationContext +import com.facebook.react.bridge.ReadableArray +import com.facebook.react.common.MapBuilder +import com.facebook.react.module.annotations.ReactModule +import com.facebook.react.uimanager.SimpleViewManager +import com.facebook.react.uimanager.ThemedReactContext +import com.facebook.react.uimanager.ViewProps +import com.facebook.react.uimanager.annotations.ReactProp + +/** Legacy View manager (non Fabric compatible) for {@link MyNativeView} components. */ +@ReactModule(name = MyLegacyViewManager.REACT_CLASS) +internal class MyLegacyViewManager(reactContext: ReactApplicationContext) : + SimpleViewManager() { + + private val callerContext: ReactApplicationContext = reactContext + + override fun getName(): String = REACT_CLASS + + override fun createViewInstance(reactContext: ThemedReactContext): MyNativeView = + MyNativeView(reactContext).apply { setBackgroundColor(Color.RED) } + + @ReactProp(name = ViewProps.OPACITY, defaultFloat = 1f) + override fun setOpacity(view: MyNativeView, opacity: Float) { + super.setOpacity(view, opacity) + } + + @ReactProp(name = ViewProps.COLOR) + fun setColor(view: MyNativeView, color: String) { + view.setBackgroundColor(Color.parseColor(color)) + } + + @ReactProp(name = "cornerRadius") + fun setCornerRadius(view: MyNativeView, cornerRadius: Float) { + if (cornerRadius !== null) { + view.setCornerRadius(cornerRadius) + } + } + + override fun getExportedViewConstants(): Map = mapOf("PI" to 3.14) + + override fun getExportedCustomBubblingEventTypeConstants(): Map { + return MapBuilder.builder() + .put( + "onColorChanged", + MapBuilder.of( + "phasedRegistrationNames", + MapBuilder.of("bubbled", "onColorChanged", "captured", "onColorChangedCapture"))) + .build() + } + + override fun receiveCommand(view: MyNativeView, commandId: String, args: ReadableArray?) { + if (commandId.contentEquals("changeBackgroundColor")) { + val sentColor: Int = Color.parseColor(args?.getString(0)) + view.setBackgroundColor(sentColor) + } + } + + @Suppress("DEPRECATION") // We intentionally want to test against the legacy API here. + override fun receiveCommand(view: MyNativeView, commandId: Int, args: ReadableArray?) { + when (commandId) { + COMMAND_CHANGE_BACKGROUND_COLOR -> { + val sentColor: Int = Color.parseColor(args?.getString(0)) + view.setBackgroundColor(sentColor) + } + } + } + + override fun getCommandsMap(): Map = + mapOf("changeBackgroundColor" to COMMAND_CHANGE_BACKGROUND_COLOR) + + companion object { + const val REACT_CLASS = "RNTMyLegacyNativeView" + const val COMMAND_CHANGE_BACKGROUND_COLOR = 42 + } +}