convert DebuggingOverlayManager to Kotlin (#43818)

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

convert Java to Kotlin: `react/views/debuggingoverlay/DebuggingOverlayManager.java`

Changelog:
[Internal] internal

Reviewed By: makovkastar

Differential Revision: D55662140

fbshipit-source-id: 6ebb5806be153a6427b9d1f35bbdda5e4cf98d11
This commit is contained in:
Alan Lee
2024-04-03 23:42:44 -07:00
committed by Facebook GitHub Bot
parent 49c40812ca
commit 76a29fc8e7
3 changed files with 156 additions and 166 deletions
@@ -5880,7 +5880,8 @@ public final class com/facebook/react/views/debuggingoverlay/DebuggingOverlay :
public final fun setTraceUpdates (Ljava/util/List;)V
}
public class com/facebook/react/views/debuggingoverlay/DebuggingOverlayManager : com/facebook/react/uimanager/SimpleViewManager {
public final class com/facebook/react/views/debuggingoverlay/DebuggingOverlayManager : com/facebook/react/uimanager/SimpleViewManager {
public static final field Companion Lcom/facebook/react/views/debuggingoverlay/DebuggingOverlayManager$Companion;
public static final field REACT_CLASS Ljava/lang/String;
public fun <init> ()V
public synthetic fun createViewInstance (Lcom/facebook/react/uimanager/ThemedReactContext;)Landroid/view/View;
@@ -5897,6 +5898,9 @@ public class com/facebook/react/views/debuggingoverlay/DebuggingOverlayManager$$
public fun setProperty (Lcom/facebook/react/views/debuggingoverlay/DebuggingOverlayManager;Lcom/facebook/react/views/debuggingoverlay/DebuggingOverlay;Ljava/lang/String;Ljava/lang/Object;)V
}
public final class com/facebook/react/views/debuggingoverlay/DebuggingOverlayManager$Companion {
}
public final class com/facebook/react/views/debuggingoverlay/TraceUpdate {
public fun <init> (ILandroid/graphics/RectF;I)V
public final fun getColor ()I
@@ -1,165 +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.views.debuggingoverlay;
import android.graphics.RectF;
import androidx.annotation.Nullable;
import com.facebook.infer.annotation.Nullsafe;
import com.facebook.react.bridge.NoSuchKeyException;
import com.facebook.react.bridge.ReactNoCrashSoftException;
import com.facebook.react.bridge.ReactSoftExceptionLogger;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.UnexpectedNativeTypeException;
import com.facebook.react.module.annotations.ReactModule;
import com.facebook.react.uimanager.PixelUtil;
import com.facebook.react.uimanager.SimpleViewManager;
import com.facebook.react.uimanager.ThemedReactContext;
import java.util.ArrayList;
import java.util.List;
@Nullsafe(Nullsafe.Mode.LOCAL)
@ReactModule(name = DebuggingOverlayManager.REACT_CLASS)
public class DebuggingOverlayManager extends SimpleViewManager<DebuggingOverlay> {
public static final String REACT_CLASS = "DebuggingOverlay";
public DebuggingOverlayManager() {}
@Override
public void receiveCommand(
DebuggingOverlay view, String commandId, @Nullable ReadableArray args) {
switch (commandId) {
case "highlightTraceUpdates":
{
if (args == null) {
break;
}
ReadableArray providedTraceUpdates = args.getArray(0);
List<TraceUpdate> formattedTraceUpdates = new ArrayList<>();
boolean successfullyParsedPayload = true;
for (int i = 0; i < providedTraceUpdates.size(); i++) {
ReadableMap traceUpdate = providedTraceUpdates.getMap(i);
ReadableMap serializedRectangle = traceUpdate.getMap("rectangle");
if (serializedRectangle == null) {
ReactSoftExceptionLogger.logSoftException(
REACT_CLASS,
new ReactNoCrashSoftException(
"Unexpected payload for highlighting trace updates: rectangle field is"
+ " null"));
successfullyParsedPayload = false;
break;
}
int id = traceUpdate.getInt("id");
int color = traceUpdate.getInt("color");
try {
float left = (float) serializedRectangle.getDouble("x");
float top = (float) serializedRectangle.getDouble("y");
float right = (float) (left + serializedRectangle.getDouble("width"));
float bottom = (float) (top + serializedRectangle.getDouble("height"));
RectF rectangle =
new RectF(
PixelUtil.toPixelFromDIP(left),
PixelUtil.toPixelFromDIP(top),
PixelUtil.toPixelFromDIP(right),
PixelUtil.toPixelFromDIP(bottom));
formattedTraceUpdates.add(new TraceUpdate(id, rectangle, color));
} catch (NoSuchKeyException | UnexpectedNativeTypeException e) {
ReactSoftExceptionLogger.logSoftException(
REACT_CLASS,
new ReactNoCrashSoftException(
"Unexpected payload for highlighting trace updates: rectangle field should"
+ " have x, y, width, height fields"));
successfullyParsedPayload = false;
break;
}
}
if (successfullyParsedPayload) {
view.setTraceUpdates(formattedTraceUpdates);
}
break;
}
case "highlightElements":
{
if (args == null) {
return;
}
ReadableArray providedElements = args.getArray(0);
List<RectF> elementsRectangles = new ArrayList<>();
boolean successfullyParsedPayload = true;
for (int i = 0; i < providedElements.size(); i++) {
ReadableMap element = providedElements.getMap(i);
try {
float left = (float) element.getDouble("x");
float top = (float) element.getDouble("y");
float right = (float) (left + element.getDouble("width"));
float bottom = (float) (top + element.getDouble("height"));
RectF rect =
new RectF(
PixelUtil.toPixelFromDIP(left),
PixelUtil.toPixelFromDIP(top),
PixelUtil.toPixelFromDIP(right),
PixelUtil.toPixelFromDIP(bottom));
elementsRectangles.add(rect);
} catch (NoSuchKeyException | UnexpectedNativeTypeException e) {
ReactSoftExceptionLogger.logSoftException(
REACT_CLASS,
new ReactNoCrashSoftException(
"Unexpected payload for highlighting elements: every element should have x,"
+ " y, width, height fields"));
successfullyParsedPayload = false;
break;
}
}
if (successfullyParsedPayload) {
view.setHighlightedElementsRectangles(elementsRectangles);
}
break;
}
case "clearElementsHighlights":
view.clearElementsHighlights();
break;
default:
ReactSoftExceptionLogger.logSoftException(
REACT_CLASS,
new ReactNoCrashSoftException(
"Received unexpected command in DebuggingOverlayManager"));
}
}
@Override
public DebuggingOverlay createViewInstance(ThemedReactContext context) {
return new DebuggingOverlay(context);
}
@Override
public String getName() {
return REACT_CLASS;
}
}
@@ -0,0 +1,151 @@
/*
* 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.views.debuggingoverlay
import android.graphics.RectF
import com.facebook.react.bridge.NoSuchKeyException
import com.facebook.react.bridge.ReactNoCrashSoftException
import com.facebook.react.bridge.ReactSoftExceptionLogger
import com.facebook.react.bridge.ReadableArray
import com.facebook.react.bridge.UnexpectedNativeTypeException
import com.facebook.react.module.annotations.ReactModule
import com.facebook.react.uimanager.PixelUtil
import com.facebook.react.uimanager.SimpleViewManager
import com.facebook.react.uimanager.ThemedReactContext
@ReactModule(name = DebuggingOverlayManager.REACT_CLASS)
public class DebuggingOverlayManager : SimpleViewManager<DebuggingOverlay>() {
override fun receiveCommand(view: DebuggingOverlay, commandId: String, args: ReadableArray?) {
when (commandId) {
"highlightTraceUpdates" -> {
if (args == null) {
return
}
val providedTraceUpdates = args.getArray(0)
val formattedTraceUpdates = mutableListOf<TraceUpdate>()
var successfullyParsedPayload = true
for (i in 0 until providedTraceUpdates.size()) {
val traceUpdate = providedTraceUpdates.getMap(i)
val serializedRectangle = traceUpdate.getMap("rectangle")
if (serializedRectangle == null) {
ReactSoftExceptionLogger.logSoftException(
REACT_CLASS,
ReactNoCrashSoftException(
"Unexpected payload for highlighting trace updates: rectangle field is" +
" null"))
successfullyParsedPayload = false
break
}
val id = traceUpdate.getInt("id")
val color = traceUpdate.getInt("color")
try {
val left = serializedRectangle.getDouble("x").toFloat()
val top = serializedRectangle.getDouble("y").toFloat()
val right = (left + serializedRectangle.getDouble("width")).toFloat()
val bottom = (top + serializedRectangle.getDouble("height")).toFloat()
val rectangle =
RectF(
PixelUtil.toPixelFromDIP(left),
PixelUtil.toPixelFromDIP(top),
PixelUtil.toPixelFromDIP(right),
PixelUtil.toPixelFromDIP(bottom))
formattedTraceUpdates.add(TraceUpdate(id, rectangle, color))
} catch (ex: Exception) {
when (ex) {
is NoSuchKeyException,
is UnexpectedNativeTypeException -> {
ReactSoftExceptionLogger.logSoftException(
REACT_CLASS,
ReactNoCrashSoftException(
"Unexpected payload for highlighting trace updates: rectangle field should" +
" have x, y, width, height fields"))
successfullyParsedPayload = false
}
else -> throw ex
}
}
}
if (successfullyParsedPayload) {
view.setTraceUpdates(formattedTraceUpdates)
}
}
"highlightElements" -> {
if (args == null) {
return
}
val providedElements = args.getArray(0)
val elementsRectangles = mutableListOf<RectF>()
var successfullyParsedPayload = true
for (i in 0 until providedElements.size()) {
val element = providedElements.getMap(i)
try {
val left = element.getDouble("x").toFloat()
val top = element.getDouble("y").toFloat()
val right = (left + element.getDouble("width")).toFloat()
val bottom = (top + element.getDouble("height")).toFloat()
val rect =
RectF(
PixelUtil.toPixelFromDIP(left),
PixelUtil.toPixelFromDIP(top),
PixelUtil.toPixelFromDIP(right),
PixelUtil.toPixelFromDIP(bottom))
elementsRectangles.add(rect)
} catch (ex: Exception) {
when (ex) {
is NoSuchKeyException,
is UnexpectedNativeTypeException -> {
ReactSoftExceptionLogger.logSoftException(
REACT_CLASS,
ReactNoCrashSoftException(
"Unexpected payload for highlighting elements: every element should have x," +
" y, width, height fields"))
successfullyParsedPayload = false
}
else -> throw ex
}
}
}
if (successfullyParsedPayload) {
view.setHighlightedElementsRectangles(elementsRectangles)
}
}
"clearElementsHighlights" -> {
view.clearElementsHighlights()
}
else -> {
ReactSoftExceptionLogger.logSoftException(
REACT_CLASS,
ReactNoCrashSoftException("Received unexpected command in DebuggingOverlayManager"))
}
}
}
public override fun createViewInstance(context: ThemedReactContext): DebuggingOverlay {
return DebuggingOverlay(context)
}
public override fun getName(): String {
return REACT_CLASS
}
public companion object {
public const val REACT_CLASS: String = "DebuggingOverlay"
}
}