Skip to content

Commit 4918b30

Browse files
fabriziocuccifacebook-github-bot
authored andcommitted
Kotlinify ViewManagerRegistry (facebook#51262)
Summary: Pull Request resolved: facebook#51262 As per title. Changelog: [Internal] Reviewed By: cortinico Differential Revision: D74571782 fbshipit-source-id: 4acec9b9c0801d863074b7a91fb116780f22929a
1 parent 7430ee0 commit 4918b30

File tree

3 files changed

+163
-193
lines changed

3 files changed

+163
-193
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4691,11 +4691,11 @@ public final class com/facebook/react/uimanager/ViewManagerRegistry : android/co
46914691
public fun <init> (Lcom/facebook/react/uimanager/ViewManagerResolver;)V
46924692
public fun <init> (Ljava/util/List;)V
46934693
public fun <init> (Ljava/util/Map;)V
4694-
public fun get (Ljava/lang/String;)Lcom/facebook/react/uimanager/ViewManager;
4695-
public fun invalidate ()V
4694+
public final fun get (Ljava/lang/String;)Lcom/facebook/react/uimanager/ViewManager;
4695+
public final fun invalidate ()V
46964696
public fun onConfigurationChanged (Landroid/content/res/Configuration;)V
46974697
public fun onLowMemory ()V
4698-
public fun onSurfaceStopped (I)V
4698+
public final fun onSurfaceStopped (I)V
46994699
public fun onTrimMemory (I)V
47004700
}
47014701

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

Lines changed: 0 additions & 190 deletions
This file was deleted.
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
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 android.content.ComponentCallbacks2
11+
import android.content.res.Configuration
12+
import com.facebook.react.bridge.UiThreadUtil
13+
14+
/**
15+
* Class that stores the mapping between native view name used in JS and the corresponding instance
16+
* of [ViewManager].
17+
*/
18+
public class ViewManagerRegistry : ComponentCallbacks2 {
19+
20+
private val viewManagers: MutableMap<String, ViewManager<*, *>>
21+
private val viewManagerResolver: ViewManagerResolver?
22+
23+
public constructor(viewManagerResolver: ViewManagerResolver) {
24+
this.viewManagers = mutableMapOf<String, ViewManager<*, *>>()
25+
this.viewManagerResolver = viewManagerResolver
26+
}
27+
28+
public constructor(viewManagerList: List<ViewManager<in Nothing, in Nothing>>) {
29+
viewManagers = viewManagerList.associateBy { it.name }.toMutableMap()
30+
viewManagerResolver = null
31+
}
32+
33+
public constructor(viewManagerMap: Map<String, ViewManager<*, *>>?) {
34+
viewManagers = viewManagerMap?.toMutableMap() ?: mutableMapOf<String, ViewManager<*, *>>()
35+
viewManagerResolver = null
36+
}
37+
38+
/**
39+
* @param className [String] that identifies the [ViewManager] inside the [ViewManagerRegistry].
40+
* @return the [ViewManager] registered to the className
41+
* @throws [IllegalViewOperationException] if there is no view manager registered for the
42+
* className.
43+
*/
44+
@Synchronized
45+
public fun get(className: String): ViewManager<*, *> {
46+
// 1. Try to get the manager without the prefix.
47+
viewManagers[className]?.let {
48+
return it
49+
}
50+
51+
// 2. Try to get the manager with the RCT prefix.
52+
val rctViewManagerName = "RCT$className"
53+
viewManagers[rctViewManagerName]?.let {
54+
return it
55+
}
56+
57+
if (viewManagerResolver != null) {
58+
59+
// 1. Try to get the manager without the prefix.
60+
val resolvedManager = getViewManagerFromResolver(className)
61+
if (resolvedManager != null) {
62+
return resolvedManager
63+
}
64+
65+
// 2. Try to get the manager with the RCT prefix.
66+
val rctResolvedManager = getViewManagerFromResolver(rctViewManagerName)
67+
if (rctResolvedManager != null) {
68+
return rctResolvedManager
69+
}
70+
71+
throw IllegalViewOperationException(
72+
"Can't find ViewManager '$className' nor '$rctViewManagerName' in ViewManagerRegistry, " +
73+
"existing names are: ${viewManagerResolver.getViewManagerNames()}")
74+
}
75+
76+
throw IllegalViewOperationException("No ViewManager found for class $className")
77+
}
78+
79+
private fun getViewManagerFromResolver(className: String): ViewManager<*, *>? {
80+
val viewManager = viewManagerResolver?.getViewManager(className)
81+
if (viewManager != null) {
82+
viewManagers[className] = viewManager
83+
}
84+
return viewManager
85+
}
86+
87+
/**
88+
* @param className [String] that identifies the [ViewManager] inside the [ViewManagerRegistry].
89+
* @return the [ViewManager] registered to the className or null if it does not exist
90+
*/
91+
@JvmName("getViewManagerIfExists")
92+
@Synchronized
93+
internal fun getViewManagerIfExists(className: String): ViewManager<*, *>? {
94+
viewManagers[className]?.let {
95+
return it
96+
}
97+
return viewManagerResolver?.let { getViewManagerFromResolver(className) }
98+
}
99+
100+
/** Send lifecycle signal to all ViewManagers that StopSurface has been called. */
101+
public fun onSurfaceStopped(surfaceId: Int) {
102+
val viewManagers: List<ViewManager<*, *>> =
103+
synchronized(this) { ArrayList(viewManagers.values) }
104+
105+
val runnable = {
106+
for (viewManager in viewManagers) {
107+
viewManager.onSurfaceStopped(surfaceId)
108+
}
109+
}
110+
111+
if (UiThreadUtil.isOnUiThread()) {
112+
runnable()
113+
} else {
114+
UiThreadUtil.runOnUiThread(runnable)
115+
}
116+
}
117+
118+
/** Called on instance destroy */
119+
public fun invalidate() {
120+
val viewManagers: List<ViewManager<*, *>> =
121+
synchronized(this) { ArrayList(viewManagers.values) }
122+
123+
val runnable = {
124+
for (viewManager in viewManagers) {
125+
viewManager.invalidate()
126+
}
127+
}
128+
129+
if (UiThreadUtil.isOnUiThread()) {
130+
runnable()
131+
} else {
132+
UiThreadUtil.runOnUiThread(runnable)
133+
}
134+
}
135+
136+
/** ComponentCallbacks2 method. */
137+
public override fun onTrimMemory(level: Int) {
138+
val viewManagers: List<ViewManager<*, *>> =
139+
synchronized(this) { ArrayList(viewManagers.values) }
140+
141+
val runnable = {
142+
for (viewManager in viewManagers) {
143+
viewManager.trimMemory()
144+
}
145+
}
146+
147+
if (UiThreadUtil.isOnUiThread()) {
148+
runnable()
149+
} else {
150+
UiThreadUtil.runOnUiThread(runnable)
151+
}
152+
}
153+
154+
/** ComponentCallbacks2 method. */
155+
public override fun onConfigurationChanged(newConfig: Configuration): Unit = Unit
156+
157+
/** ComponentCallbacks2 method. */
158+
@Deprecated("Overrides deprecated ComponentCallbacks2.onLowMemory()")
159+
public override fun onLowMemory(): Unit = onTrimMemory(ComponentCallbacks2.TRIM_MEMORY_BACKGROUND)
160+
}

0 commit comments

Comments
 (0)