From c12e0dac159c8936a3f9e146215de3694ac88c67 Mon Sep 17 00:00:00 2001 From: Jonathan Moskovich <48201295+jonathanmos@users.noreply.github.com> Date: Thu, 30 Nov 2023 12:57:00 +0200 Subject: [PATCH] Remove recursive calculation of alpha --- .../reactnative/sessionreplay/ColorUtils.kt | 20 ++----------------- .../sessionreplay/ReactViewGroupMapper.kt | 8 ++------ .../sessionreplay/ColorUtilsTest.kt | 8 ++++---- 3 files changed, 8 insertions(+), 28 deletions(-) diff --git a/packages/react-native-session-replay/android/src/main/kotlin/com/datadog/reactnative/sessionreplay/ColorUtils.kt b/packages/react-native-session-replay/android/src/main/kotlin/com/datadog/reactnative/sessionreplay/ColorUtils.kt index 768768ebf..d07b9ea37 100644 --- a/packages/react-native-session-replay/android/src/main/kotlin/com/datadog/reactnative/sessionreplay/ColorUtils.kt +++ b/packages/react-native-session-replay/android/src/main/kotlin/com/datadog/reactnative/sessionreplay/ColorUtils.kt @@ -6,8 +6,6 @@ package com.datadog.reactnative.sessionreplay -import android.view.View -import android.view.ViewGroup import androidx.annotation.VisibleForTesting private const val HEX_COLOR_INCLUDING_ALPHA_LENGTH: Int = 8 @@ -16,25 +14,11 @@ internal fun resolveBackgroundColorAsHex(backgroundColor: Int): String { val colorHexString = Integer.toHexString(backgroundColor) // reorder the hex string from argb to rgba - return "#${reorderRGBAtoARGB(colorHexString)}" -} - -internal fun resolveOpacity(view: View, currentOpacity: Float): Float { - return if (view.alpha == 0f) { - 0f - } else { - val combinedOpacity = view.alpha * currentOpacity - - if (view.parent != null && view.parent is ViewGroup) { - resolveOpacity(view.parent as ViewGroup, combinedOpacity) - } else { - combinedOpacity - } - } + return "#${reorderARGBtoRGBA(colorHexString)}" } @VisibleForTesting -internal fun reorderRGBAtoARGB(hexString: String): String { +internal fun reorderARGBtoRGBA(hexString: String): String { return if (hexString.length == HEX_COLOR_INCLUDING_ALPHA_LENGTH) { hexString.substring(2, 8) + hexString.substring(0, 2) } else { diff --git a/packages/react-native-session-replay/android/src/main/kotlin/com/datadog/reactnative/sessionreplay/ReactViewGroupMapper.kt b/packages/react-native-session-replay/android/src/main/kotlin/com/datadog/reactnative/sessionreplay/ReactViewGroupMapper.kt index 6e5acc402..fbef968ff 100644 --- a/packages/react-native-session-replay/android/src/main/kotlin/com/datadog/reactnative/sessionreplay/ReactViewGroupMapper.kt +++ b/packages/react-native-session-replay/android/src/main/kotlin/com/datadog/reactnative/sessionreplay/ReactViewGroupMapper.kt @@ -35,11 +35,7 @@ internal class ReactViewGroupMapper : val backgroundDrawable = view.background // view.alpha is the value of the opacity prop on the js side - val opacity = resolveOpacity(view, 1f) - - if (opacity == 0f) { - return emptyList() - } + val opacity = view.alpha val (shapeStyle, border) = if (backgroundDrawable is ReactViewBackgroundDrawable) { @@ -52,7 +48,7 @@ internal class ReactViewGroupMapper : } else { backgroundDrawable?.resolveShapeStyleAndBorder(opacity) ?: (null to null) } - + return listOf( MobileSegment.Wireframe.ShapeWireframe( resolveViewId(view), diff --git a/packages/react-native-session-replay/android/src/test/kotlin/com/datadog/reactnative/sessionreplay/ColorUtilsTest.kt b/packages/react-native-session-replay/android/src/test/kotlin/com/datadog/reactnative/sessionreplay/ColorUtilsTest.kt index 18ad97bbf..2a3f890b4 100644 --- a/packages/react-native-session-replay/android/src/test/kotlin/com/datadog/reactnative/sessionreplay/ColorUtilsTest.kt +++ b/packages/react-native-session-replay/android/src/test/kotlin/com/datadog/reactnative/sessionreplay/ColorUtilsTest.kt @@ -47,24 +47,24 @@ internal class ColorUtilsTest { } @Test - fun `M reverse alpha to end W reverseRGBAtoARGB { hex color with alpha }`() { + fun `M reverse alpha to end W reorderARGBtoRGBA { hex color with alpha }`() { // Given val hexString = "FF006666" // When - val reversedString = reorderRGBAtoARGB(hexString) + val reversedString = reorderARGBtoRGBA(hexString) // Then assertThat(reversedString).isEqualTo("006666FF") } @Test - fun `M return original string W reverseRGBAtoARGB { hex color without alpha }`() { + fun `M return original string W reorderARGBtoRGBA { hex color without alpha }`() { // Given val hexString = "006666" // When - val reversedString = reorderRGBAtoARGB(hexString) + val reversedString = reorderARGBtoRGBA(hexString) // Then assertThat(reversedString).isEqualTo("006666")