refactor: migrate MyLegacyViewManager to kotlin (#39014)

Summary:
Migrate MyLegacyViewManager to Kotlin (https://github.com/facebook/react-native/issues/38825)

## Changelog:

[INTERNAL] [CHANGED]  - Moved MyLegacyViewManager to Kotlin

<!-- Help reviewers and the release process by writing your own changelog entry.

Pick one each for the category and type tags:

[ANDROID|GENERAL|IOS|INTERNAL] [BREAKING|ADDED|CHANGED|DEPRECATED|REMOVED|FIXED|SECURITY] - Message

For more details, see:
https://reactnative.dev/contributing/changelogs-in-pull-requests

Pull Request resolved: https://github.com/facebook/react-native/pull/39014

Test Plan: Run `yarn && yarn android`

Reviewed By: rshest

Differential Revision: D48519780

Pulled By: cortinico

fbshipit-source-id: b43a2cfacad977c52b8552c464246b812de30855
This commit is contained in:
imalgrab
2023-08-22 08:11:49 -07:00
committed by Facebook GitHub Bot
parent 3c943bbe3a
commit 172e2d0c7e
2 changed files with 85 additions and 111 deletions
@@ -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<MyNativeView> {
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<String, Object> getExportedViewConstants() {
return Collections.singletonMap("PI", 3.14);
}
@Override
public final Map<String, Object> getExportedCustomBubblingEventTypeConstants() {
return new HashMap<>(
MapBuilder.<String, Object>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<String, Integer> getCommandsMap() {
return MapBuilder.of("changeBackgroundColor", COMMAND_CHANGE_BACKGROUND_COLOR);
}
}
@@ -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<MyNativeView>() {
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<String, Any> = mapOf("PI" to 3.14)
override fun getExportedCustomBubblingEventTypeConstants(): Map<String, Any> {
return MapBuilder.builder<String, Any>()
.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<String, Int> =
mapOf("changeBackgroundColor" to COMMAND_CHANGE_BACKGROUND_COLOR)
companion object {
const val REACT_CLASS = "RNTMyLegacyNativeView"
const val COMMAND_CHANGE_BACKGROUND_COLOR = 42
}
}