mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
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
This commit is contained in:
committed by
Facebook GitHub Bot
parent
6d822b4ca3
commit
3ef5e7a9a6
-133
@@ -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<Integer> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
+117
@@ -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<Int>) {
|
||||
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<OnIntArrayChangedEvent>(surfaceId, viewId) {
|
||||
override fun getEventName() = "onIntArrayChanged"
|
||||
|
||||
override fun getEventData() = payload
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user