From 3ef5e7a9a6b29dfe122d9878c5d85319dc2b8fc5 Mon Sep 17 00:00:00 2001 From: Bogusz Kaszowski Date: Fri, 11 Aug 2023 15:34:55 -0700 Subject: [PATCH] migrate MyNativeView to Kotlin (#38951) Summary: This PR converts MyNativeView into Kotlin as requested in https://github.com/facebook/react-native/issues/38825 . ## Changelog: [INTERNAL] [CHANGED] - Migrate MyNativeView to Kotlin Pull Request resolved: https://github.com/facebook/react-native/pull/38951 Test Plan: 1. run `yarn android` 2. Check whether RN Tester runs as expected Reviewed By: philIip, bvanderhoof Differential Revision: D48281685 Pulled By: mdvacca fbshipit-source-id: e57d6ab9418b3b1df630de13591af4011bd126b0 --- .../react/uiapp/component/MyNativeView.java | 133 ------------------ .../react/uiapp/component/MyNativeView.kt | 117 +++++++++++++++ 2 files changed, 117 insertions(+), 133 deletions(-) delete mode 100644 packages/rn-tester/android/app/src/main/java/com/facebook/react/uiapp/component/MyNativeView.java create mode 100644 packages/rn-tester/android/app/src/main/java/com/facebook/react/uiapp/component/MyNativeView.kt diff --git a/packages/rn-tester/android/app/src/main/java/com/facebook/react/uiapp/component/MyNativeView.java b/packages/rn-tester/android/app/src/main/java/com/facebook/react/uiapp/component/MyNativeView.java deleted file mode 100644 index 1a3992536e0..00000000000 --- a/packages/rn-tester/android/app/src/main/java/com/facebook/react/uiapp/component/MyNativeView.java +++ /dev/null @@ -1,133 +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.content.Context; -import android.graphics.Color; -import android.graphics.drawable.GradientDrawable; -import android.view.View; -import com.facebook.react.bridge.Arguments; -import com.facebook.react.bridge.ReactContext; -import com.facebook.react.bridge.WritableArray; -import com.facebook.react.bridge.WritableMap; -import com.facebook.react.uimanager.UIManagerHelper; -import com.facebook.react.uimanager.events.Event; -import com.facebook.react.uimanager.events.EventDispatcher; -import com.facebook.react.uimanager.events.RCTEventEmitter; -import java.util.List; - -class MyNativeView extends View { - - private int currentColor = 0; - private GradientDrawable background; - - public MyNativeView(Context context) { - super(context); - background = new GradientDrawable(); - } - - @Override - public void setBackgroundColor(int color) { - if (color != currentColor) { - background.setColor(color); - currentColor = color; - emitNativeEvent(color); - setBackground(background); - } - } - - public void setCornerRadius(float cornerRadius) { - background.setCornerRadius(cornerRadius); - setBackground(background); - } - - private void emitNativeEvent(int color) { - WritableMap event = Arguments.createMap(); - WritableMap backgroundColor = Arguments.createMap(); - float[] hsv = new float[3]; - Color.colorToHSV(color, hsv); - backgroundColor.putDouble("hue", hsv[0]); - backgroundColor.putDouble("saturation", hsv[1]); - backgroundColor.putDouble("brightness", hsv[2]); - backgroundColor.putDouble("alpha", Color.alpha(color)); - event.putMap("backgroundColor", backgroundColor); - ReactContext reactContext = (ReactContext) getContext(); - reactContext.getJSModule(RCTEventEmitter.class).receiveEvent(getId(), "onColorChanged", event); - } - - void emitOnArrayChangedEvent(List ints) { - WritableMap payload = Arguments.createMap(); - - WritableArray newIntArray = Arguments.createArray(); - WritableArray newBoolArray = Arguments.createArray(); - WritableArray newFloatArray = Arguments.createArray(); - WritableArray newDoubleArray = Arguments.createArray(); - WritableArray newYesNoArray = Arguments.createArray(); - WritableArray newStringArray = Arguments.createArray(); - WritableArray newObjectArray = Arguments.createArray(); - WritableArray newArrayArray = Arguments.createArray(); - - for (Integer i : ints) { - newIntArray.pushInt(i * 2); - newBoolArray.pushBoolean(i % 2 == 1); - newFloatArray.pushDouble(i * 3.14); - newDoubleArray.pushDouble(i / 3.14); - newYesNoArray.pushString(i % 2 == 1 ? "yep" : "nope"); - newStringArray.pushString(i.toString()); - - WritableMap latLon = Arguments.createMap(); - latLon.putDouble("lat", -1.0 * i); - latLon.putDouble("lon", 2.0 * i); - newObjectArray.pushMap(latLon); - - WritableArray innerArray = Arguments.createArray(); - innerArray.pushInt(i); - innerArray.pushInt(i); - innerArray.pushInt(i); - newArrayArray.pushArray(innerArray); - } - - payload.putArray("values", newIntArray); - payload.putArray("boolValues", newBoolArray); - payload.putArray("floats", newFloatArray); - payload.putArray("doubles", newDoubleArray); - payload.putArray("yesNos", newYesNoArray); - payload.putArray("strings", newStringArray); - payload.putArray("latLons", newObjectArray); - payload.putArray("multiArrays", newArrayArray); - - ReactContext reactContext = (ReactContext) getContext(); - int surfaceId = UIManagerHelper.getSurfaceId(reactContext); - EventDispatcher eventDispatcher = - UIManagerHelper.getEventDispatcherForReactTag(reactContext, getId()); - Event event = new OnIntArrayChangedEvent(surfaceId, getId(), payload); - - if (eventDispatcher != null) { - eventDispatcher.dispatchEvent(event); - } - } - - class OnIntArrayChangedEvent extends Event { - private WritableMap mPayload; - - public OnIntArrayChangedEvent(int surfaceId, int viewId, WritableMap payload) { - super(surfaceId, viewId); - this.mPayload = payload; - } - - @Override - public String getEventName() { - return "onIntArrayChanged"; - } - - @Override - protected WritableMap getEventData() { - return mPayload; - } - } -} diff --git a/packages/rn-tester/android/app/src/main/java/com/facebook/react/uiapp/component/MyNativeView.kt b/packages/rn-tester/android/app/src/main/java/com/facebook/react/uiapp/component/MyNativeView.kt new file mode 100644 index 00000000000..78584e08f89 --- /dev/null +++ b/packages/rn-tester/android/app/src/main/java/com/facebook/react/uiapp/component/MyNativeView.kt @@ -0,0 +1,117 @@ +/* + * 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.content.Context +import android.graphics.Color +import android.graphics.drawable.GradientDrawable +import android.view.View +import com.facebook.react.bridge.Arguments +import com.facebook.react.bridge.ReactContext +import com.facebook.react.bridge.WritableArray +import com.facebook.react.bridge.WritableMap +import com.facebook.react.uimanager.UIManagerHelper +import com.facebook.react.uimanager.events.Event +import com.facebook.react.uimanager.events.RCTEventEmitter + +class MyNativeView(context: Context) : View(context) { + private var currentColor = 0 + private var background: GradientDrawable = GradientDrawable() + + override fun setBackgroundColor(color: Int) { + if (color != currentColor) { + background.setColor(color) + currentColor = color + emitNativeEvent(color) + setBackground(background) + } + } + + fun setCornerRadius(cornerRadius: Float) { + background.setCornerRadius(cornerRadius) + setBackground(background) + } + + private fun emitNativeEvent(color: Int) { + val event = Arguments.createMap() + val hsv = FloatArray(3) + val backgroundColor = + Arguments.createMap().apply { + putDouble("hue", hsv[0].toDouble()) + putDouble("saturation", hsv[1].toDouble()) + putDouble("brightness", hsv[2].toDouble()) + putDouble("alpha", Color.alpha(color).toDouble()) + } + + Color.colorToHSV(color, hsv) + event.putMap("backgroundColor", backgroundColor) + + val reactContext = context as ReactContext + reactContext.getJSModule(RCTEventEmitter::class.java).receiveEvent(id, "onColorChanged", event) + } + + fun emitOnArrayChangedEvent(ints: List) { + val newIntArray = Arguments.createArray() + val newBoolArray = Arguments.createArray() + val newFloatArray = Arguments.createArray() + val newDoubleArray = Arguments.createArray() + val newYesNoArray = Arguments.createArray() + val newStringArray = Arguments.createArray() + val newObjectArray = Arguments.createArray() + val newArrayArray = Arguments.createArray() + + for (i in ints) { + newIntArray.pushInt(i * 2) + newBoolArray.pushBoolean(i % 2 == 1) + newFloatArray.pushDouble(i * 3.14) + newDoubleArray.pushDouble(i / 3.14) + newYesNoArray.pushString(if (i % 2 == 1) "yep" else "nope") + newStringArray.pushString(i.toString()) + + val latLon = Arguments.createMap() + latLon.putDouble("lat", -1.0 * i) + latLon.putDouble("lon", 2.0 * i) + newObjectArray.pushMap(latLon) + + val innerArray: WritableArray = Arguments.createArray() + innerArray.pushInt(i) + innerArray.pushInt(i) + innerArray.pushInt(i) + newArrayArray.pushArray(innerArray) + } + + val payload = + Arguments.createMap().apply { + putArray("values", newIntArray) + putArray("boolValues", newBoolArray) + putArray("floats", newFloatArray) + putArray("doubles", newDoubleArray) + putArray("yesNos", newYesNoArray) + putArray("strings", newStringArray) + putArray("latLons", newObjectArray) + putArray("multiArrays", newArrayArray) + } + + val reactContext = context as ReactContext + val surfaceId = UIManagerHelper.getSurfaceId(reactContext) + val eventDispatcher = UIManagerHelper.getEventDispatcherForReactTag(reactContext, id) + val event = OnIntArrayChangedEvent(surfaceId, id, payload) + + eventDispatcher?.dispatchEvent(event) + } + + inner class OnIntArrayChangedEvent( + surfaceId: Int, + viewId: Int, + private val payload: WritableMap + ) : Event(surfaceId, viewId) { + override fun getEventName() = "onIntArrayChanged" + + override fun getEventData() = payload + } +}