Skip to content

Commit 78c9671

Browse files
cortinicofacebook-github-bot
authored andcommitted
Migrate ThemedReactContext to Kotlin (#52309)
Summary: Pull Request resolved: #52309 This is another class going from Java to Kotlin. Thish should have no breaking changes, but I'll keep an eye to see if this is disruptive for users in the ecosystem. I also haven't removed any of the Deprecated method, which can be cleaned up afterwards. Changelog: [Android] [Changed] - Migrate ThemedReactContext to Kotlin Reviewed By: javache Differential Revision: D77374236 fbshipit-source-id: d1787b21897b01c45bbf841fdda00972e0be58db
1 parent d4bf1b7 commit 78c9671

File tree

3 files changed

+157
-208
lines changed

3 files changed

+157
-208
lines changed

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4218,10 +4218,11 @@ public abstract interface class com/facebook/react/uimanager/StateWrapper {
42184218
public abstract fun updateState (Lcom/facebook/react/bridge/WritableMap;)V
42194219
}
42204220

4221-
public class com/facebook/react/uimanager/ThemedReactContext : com/facebook/react/bridge/ReactContext {
4221+
public final class com/facebook/react/uimanager/ThemedReactContext : com/facebook/react/bridge/ReactContext {
42224222
public fun <init> (Lcom/facebook/react/bridge/ReactApplicationContext;Landroid/content/Context;)V
42234223
public fun <init> (Lcom/facebook/react/bridge/ReactApplicationContext;Landroid/content/Context;Ljava/lang/String;)V
42244224
public fun <init> (Lcom/facebook/react/bridge/ReactApplicationContext;Landroid/content/Context;Ljava/lang/String;I)V
4225+
public synthetic fun <init> (Lcom/facebook/react/bridge/ReactApplicationContext;Landroid/content/Context;Ljava/lang/String;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
42254226
public fun addLifecycleEventListener (Lcom/facebook/react/bridge/LifecycleEventListener;)V
42264227
public fun destroy ()V
42274228
public fun getCatalystInstance ()Lcom/facebook/react/bridge/CatalystInstance;
@@ -4230,14 +4231,14 @@ public class com/facebook/react/uimanager/ThemedReactContext : com/facebook/reac
42304231
public fun getJSCallInvokerHolder ()Lcom/facebook/react/turbomodule/core/interfaces/CallInvokerHolder;
42314232
public fun getJSModule (Ljava/lang/Class;)Lcom/facebook/react/bridge/JavaScriptModule;
42324233
public fun getJavaScriptContextHolder ()Lcom/facebook/react/bridge/JavaScriptContextHolder;
4233-
public fun getModuleName ()Ljava/lang/String;
4234+
public final fun getModuleName ()Ljava/lang/String;
42344235
public fun getNativeModule (Ljava/lang/Class;)Lcom/facebook/react/bridge/NativeModule;
42354236
public fun getNativeModule (Ljava/lang/String;)Lcom/facebook/react/bridge/NativeModule;
42364237
public fun getNativeModules ()Ljava/util/Collection;
4237-
public fun getReactApplicationContext ()Lcom/facebook/react/bridge/ReactApplicationContext;
4238+
public final fun getReactApplicationContext ()Lcom/facebook/react/bridge/ReactApplicationContext;
42384239
public fun getSourceURL ()Ljava/lang/String;
4239-
public fun getSurfaceID ()Ljava/lang/String;
4240-
public fun getSurfaceId ()I
4240+
public final fun getSurfaceID ()Ljava/lang/String;
4241+
public final fun getSurfaceId ()I
42414242
public fun handleException (Ljava/lang/Exception;)V
42424243
public fun hasActiveCatalystInstance ()Z
42434244
public fun hasActiveReactInstance ()Z

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

Lines changed: 0 additions & 203 deletions
This file was deleted.
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
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+
@file:Suppress("DEPRECATION")
9+
10+
package com.facebook.react.uimanager
11+
12+
import android.app.Activity
13+
import android.content.Context
14+
import com.facebook.react.bridge.Callback
15+
import com.facebook.react.bridge.CatalystInstance
16+
import com.facebook.react.bridge.JavaScriptContextHolder
17+
import com.facebook.react.bridge.JavaScriptModule
18+
import com.facebook.react.bridge.LifecycleEventListener
19+
import com.facebook.react.bridge.NativeModule
20+
import com.facebook.react.bridge.ReactApplicationContext
21+
import com.facebook.react.bridge.ReactContext
22+
import com.facebook.react.bridge.UIManager
23+
import com.facebook.react.common.annotations.internal.LegacyArchitecture
24+
import com.facebook.react.turbomodule.core.interfaces.CallInvokerHolder
25+
26+
/**
27+
* Wraps [ReactContext] with the base [Context] passed into the constructor. It provides also a way
28+
* to start activities using the viewContext to which RN native views belong. It delegates lifecycle
29+
* listener registration to the original instance of [ReactContext] which is supposed to receive the
30+
* lifecycle events. At the same time we disallow receiving lifecycle events for this wrapper
31+
* instances. TODO: T7538544 Rename ThemedReactContext to be in alignment with name of
32+
* ReactApplicationContext
33+
*
34+
* @property moduleName A [String] that represents the module name of the js application that is
35+
* being rendered with this [ThemedReactContext]
36+
*/
37+
public class ThemedReactContext(
38+
public val reactApplicationContext: ReactApplicationContext,
39+
base: Context,
40+
public val moduleName: String?,
41+
public val surfaceId: Int
42+
) : ReactContext(base) {
43+
44+
@Deprecated("This constructor is deprecated and you should not be using it.")
45+
public constructor(
46+
reactApplicationContext: ReactApplicationContext,
47+
base: Context,
48+
moduleName: String? = null
49+
) : this(reactApplicationContext, base, moduleName, -1)
50+
51+
@Deprecated("This constructor is deprecated and you should not be using it.")
52+
public constructor(
53+
reactApplicationContext: ReactApplicationContext,
54+
base: Context
55+
) : this(reactApplicationContext, base, null, -1)
56+
57+
init {
58+
initializeFromOther(reactApplicationContext)
59+
}
60+
61+
override fun addLifecycleEventListener(listener: LifecycleEventListener) {
62+
reactApplicationContext.addLifecycleEventListener(listener)
63+
}
64+
65+
override fun removeLifecycleEventListener(listener: LifecycleEventListener) {
66+
reactApplicationContext.removeLifecycleEventListener(listener)
67+
}
68+
69+
override fun hasCurrentActivity(): Boolean = reactApplicationContext.hasCurrentActivity()
70+
71+
override fun getCurrentActivity(): Activity? = reactApplicationContext.getCurrentActivity()
72+
73+
override fun <T : JavaScriptModule> getJSModule(jsInterface: Class<T>): T =
74+
reactApplicationContext.getJSModule<T>(jsInterface)
75+
76+
override fun <T : NativeModule> hasNativeModule(nativeModuleInterface: Class<T>): Boolean =
77+
reactApplicationContext.hasNativeModule<T>(nativeModuleInterface)
78+
79+
override fun getNativeModules(): MutableCollection<NativeModule?>? =
80+
reactApplicationContext.getNativeModules()
81+
82+
override fun <T : NativeModule> getNativeModule(nativeModuleInterface: Class<T>): T? =
83+
reactApplicationContext.getNativeModule<T>(nativeModuleInterface)
84+
85+
override fun getNativeModule(moduleName: String): NativeModule? =
86+
reactApplicationContext.getNativeModule(moduleName)
87+
88+
@Deprecated(
89+
"This method is deprecated and will be removed once the Legacy Architecture is removed")
90+
@LegacyArchitecture
91+
override fun getCatalystInstance(): CatalystInstance? =
92+
reactApplicationContext.getCatalystInstance()
93+
94+
@Deprecated(
95+
"This API has been deprecated due to naming consideration, please use hasActiveReactInstance() instead",
96+
ReplaceWith("hasActiveReactInstance()"))
97+
@LegacyArchitecture
98+
override fun hasActiveCatalystInstance(): Boolean =
99+
reactApplicationContext.hasActiveCatalystInstance()
100+
101+
override fun hasActiveReactInstance(): Boolean =
102+
reactApplicationContext.hasActiveCatalystInstance()
103+
104+
@Deprecated(
105+
"This API has been deprecated due to naming consideration, please use hasReactInstance() instead",
106+
ReplaceWith("hasReactInstance()"))
107+
@LegacyArchitecture
108+
override fun hasCatalystInstance(): Boolean = reactApplicationContext.hasCatalystInstance()
109+
110+
override fun hasReactInstance(): Boolean = reactApplicationContext.hasReactInstance()
111+
112+
override fun destroy() {
113+
reactApplicationContext.destroy()
114+
}
115+
116+
/**
117+
* This is misnamed but has some uses out in the wild. It will be deleted in a future release of
118+
* RN.
119+
*
120+
* @return a [String] that represents the module name of the js application that is being rendered
121+
* with this [ThemedReactContext]
122+
*/
123+
@Deprecated(
124+
"Do not depend on this method. It will be removed in a future release of React Native.")
125+
public fun getSurfaceID(): String? = moduleName
126+
127+
override fun handleException(e: Exception?) {
128+
reactApplicationContext.handleException(e)
129+
}
130+
131+
@Deprecated(
132+
"You should not invoke isBridgeless and let your code depend on this check. This function will be removed in the future.")
133+
override fun isBridgeless(): Boolean = reactApplicationContext.isBridgeless()
134+
135+
override fun getJavaScriptContextHolder(): JavaScriptContextHolder? =
136+
reactApplicationContext.getJavaScriptContextHolder()
137+
138+
override fun getJSCallInvokerHolder(): CallInvokerHolder? =
139+
reactApplicationContext.getJSCallInvokerHolder()
140+
141+
@Deprecated(
142+
"This method is deprecated, please use UIManagerHelper.getUIManager() instead.",
143+
ReplaceWith("UIManagerHelper.getUIManager()"))
144+
override fun getFabricUIManager(): UIManager? = reactApplicationContext.getFabricUIManager()
145+
146+
override fun getSourceURL(): String? = reactApplicationContext.getSourceURL()
147+
148+
override fun registerSegment(segmentId: Int, path: String?, callback: Callback?) {
149+
reactApplicationContext.registerSegment(segmentId, path, callback)
150+
}
151+
}

0 commit comments

Comments
 (0)