Skip to content

Commit 00105b8

Browse files
mateoguzmanafacebook-github-bot
authored andcommitted
Migrate OnLayoutEvent to Kotlin (facebook#50082)
Summary: Migrate com.facebook.react.uimanager.OnLayoutEvent to Kotlin. ## Changelog: [INTERNAL] - Migrate com.facebook.react.uimanager.OnLayoutEvent to Kotlin Pull Request resolved: facebook#50082 Test Plan: ```bash yarn test-android yarn android ``` Reviewed By: cortinico Differential Revision: D71385027 Pulled By: javache fbshipit-source-id: 19985a494a68e63a2e3404ce2972c03161d61527
1 parent 2259db6 commit 00105b8

File tree

4 files changed

+175
-95
lines changed

4 files changed

+175
-95
lines changed

packages/react-native/ReactAndroid/api/ReactAndroid.api

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3970,16 +3970,19 @@ public class com/facebook/react/uimanager/NativeViewHierarchyOptimizer {
39703970
public fun onBatchComplete ()V
39713971
}
39723972

3973-
public class com/facebook/react/uimanager/OnLayoutEvent : com/facebook/react/uimanager/events/Event {
3974-
protected fun getEventData ()Lcom/facebook/react/bridge/WritableMap;
3973+
public final class com/facebook/react/uimanager/OnLayoutEvent : com/facebook/react/uimanager/events/Event {
3974+
public static final field Companion Lcom/facebook/react/uimanager/OnLayoutEvent$Companion;
39753975
public fun getEventName ()Ljava/lang/String;
3976-
protected fun init (IIIII)V
3977-
protected fun init (IIIIII)V
3978-
public static fun obtain (IIIII)Lcom/facebook/react/uimanager/OnLayoutEvent;
3979-
public static fun obtain (IIIIII)Lcom/facebook/react/uimanager/OnLayoutEvent;
3976+
public static final fun obtain (IIIII)Lcom/facebook/react/uimanager/OnLayoutEvent;
3977+
public static final fun obtain (IIIIII)Lcom/facebook/react/uimanager/OnLayoutEvent;
39803978
public fun onDispose ()V
39813979
}
39823980

3981+
public final class com/facebook/react/uimanager/OnLayoutEvent$Companion {
3982+
public final fun obtain (IIIII)Lcom/facebook/react/uimanager/OnLayoutEvent;
3983+
public final fun obtain (IIIIII)Lcom/facebook/react/uimanager/OnLayoutEvent;
3984+
}
3985+
39833986
public final class com/facebook/react/uimanager/PixelUtil {
39843987
public static final field INSTANCE Lcom/facebook/react/uimanager/PixelUtil;
39853988
public final fun dpToPx (D)F

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/OnLayoutEvent.java

Lines changed: 0 additions & 89 deletions
This file was deleted.
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
package com.facebook.react.uimanager
9+
10+
import androidx.core.util.Pools.SynchronizedPool
11+
import com.facebook.react.bridge.Arguments
12+
import com.facebook.react.bridge.WritableMap
13+
import com.facebook.react.common.annotations.VisibleForTesting
14+
import com.facebook.react.common.annotations.internal.LegacyArchitecture
15+
import com.facebook.react.common.annotations.internal.LegacyArchitectureLogLevel
16+
import com.facebook.react.common.annotations.internal.LegacyArchitectureLogger
17+
import com.facebook.react.uimanager.PixelUtil.toDIPFromPixel
18+
import com.facebook.react.uimanager.events.Event
19+
20+
/** Event used to notify JS component about changes of its position or dimensions. */
21+
@LegacyArchitecture
22+
public class OnLayoutEvent private constructor() : Event<OnLayoutEvent>() {
23+
@VisibleForTesting internal var x: Int = 0
24+
@VisibleForTesting internal var y: Int = 0
25+
@VisibleForTesting internal var width: Int = 0
26+
@VisibleForTesting internal var height: Int = 0
27+
28+
override fun onDispose() {
29+
EVENTS_POOL.release(this)
30+
}
31+
32+
protected fun init(surfaceId: Int, viewTag: Int, x: Int, y: Int, width: Int, height: Int) {
33+
super.init(surfaceId, viewTag)
34+
this.x = x
35+
this.y = y
36+
this.width = width
37+
this.height = height
38+
}
39+
40+
override fun getEventName(): String = "topLayout"
41+
42+
override fun getEventData(): WritableMap? {
43+
val layout =
44+
Arguments.createMap().apply {
45+
putDouble("x", toDIPFromPixel(x.toFloat()).toDouble())
46+
putDouble("y", toDIPFromPixel(y.toFloat()).toDouble())
47+
putDouble("width", toDIPFromPixel(width.toFloat()).toDouble())
48+
putDouble("height", toDIPFromPixel(height.toFloat()).toDouble())
49+
}
50+
51+
val event =
52+
Arguments.createMap().apply {
53+
putMap("layout", layout)
54+
putInt("target", viewTag)
55+
}
56+
57+
return event
58+
}
59+
60+
public companion object {
61+
init {
62+
LegacyArchitectureLogger.assertWhenLegacyArchitectureMinifyingEnabled(
63+
"OnLayoutEvent", LegacyArchitectureLogLevel.WARNING)
64+
}
65+
66+
private val EVENTS_POOL: SynchronizedPool<OnLayoutEvent> = SynchronizedPool<OnLayoutEvent>(20)
67+
68+
@Deprecated(
69+
"Use `obtain(surfaceId, viewTag, x, y, width, height)` instead.",
70+
ReplaceWith("obtain(surfaceId, viewTag, x, y, width, height)"))
71+
@JvmStatic
72+
public fun obtain(viewTag: Int, x: Int, y: Int, width: Int, height: Int): OnLayoutEvent {
73+
return obtain(-1, viewTag, x, y, width, height)
74+
}
75+
76+
@JvmStatic
77+
public fun obtain(
78+
surfaceId: Int,
79+
viewTag: Int,
80+
x: Int,
81+
y: Int,
82+
width: Int,
83+
height: Int
84+
): OnLayoutEvent {
85+
var event: OnLayoutEvent? = EVENTS_POOL.acquire()
86+
if (event == null) {
87+
event = OnLayoutEvent()
88+
}
89+
event.init(surfaceId, viewTag, x, y, width, height)
90+
return event
91+
}
92+
}
93+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
package com.facebook.react.uimanager
9+
10+
import com.facebook.react.common.SystemClock
11+
import org.assertj.core.api.Assertions.assertThat
12+
import org.junit.After
13+
import org.junit.Before
14+
import org.junit.Test
15+
import org.junit.runner.RunWith
16+
import org.mockito.MockedStatic
17+
import org.mockito.Mockito.mockStatic
18+
import org.robolectric.RobolectricTestRunner
19+
20+
@RunWith(RobolectricTestRunner::class)
21+
class OnLayoutEventTest {
22+
private lateinit var systemClock: MockedStatic<SystemClock>
23+
24+
@Before
25+
fun setup() {
26+
val ts = SystemClock.uptimeMillis()
27+
systemClock = mockStatic(SystemClock::class.java)
28+
systemClock.`when`<Long> { SystemClock.uptimeMillis() }.thenReturn(ts)
29+
}
30+
31+
@After
32+
fun tearDown() {
33+
systemClock.close()
34+
}
35+
36+
@Test
37+
fun testObtain_shouldReturnEventWithCorrectValues() {
38+
val surfaceId = 1
39+
val viewTag = 2
40+
val x = 10
41+
val y = 20
42+
val width = 100
43+
val height = 200
44+
45+
val event = OnLayoutEvent.obtain(surfaceId, viewTag, x, y, width, height)
46+
47+
assertThat(event).isNotNull
48+
assertThat(event.viewTag).isEqualTo(viewTag)
49+
assertThat(event.x).isEqualTo(x)
50+
assertThat(event.y).isEqualTo(y)
51+
assertThat(event.width).isEqualTo(width)
52+
assertThat(event.height).isEqualTo(height)
53+
}
54+
55+
@Test
56+
fun testGetEventName_shouldReturnCorrectEventName() {
57+
val event = OnLayoutEvent.obtain(1, 1, 10, 20, 100, 200)
58+
59+
assertThat(event.eventName).isEqualTo("topLayout")
60+
}
61+
62+
@Test
63+
fun testInit_shouldCorrectlyInitializeValues() {
64+
val event = OnLayoutEvent.obtain(1, 1, 10, 20, 100, 200)
65+
66+
assertThat(event.surfaceId).isEqualTo(1)
67+
assertThat(event.viewTag).isEqualTo(1)
68+
assertThat(event.x).isEqualTo(10)
69+
assertThat(event.y).isEqualTo(20)
70+
assertThat(event.width).isEqualTo(100)
71+
assertThat(event.height).isEqualTo(200)
72+
}
73+
}

0 commit comments

Comments
 (0)