From 619af6448da1500ceb826095a7ef31a7262794c4 Mon Sep 17 00:00:00 2001 From: Arushi Kesarwani Date: Thu, 30 Nov 2023 21:24:54 -0800 Subject: [PATCH] Adding getFabricUIManager() APIs to ReactContext (#41728) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/41728 Adding APIs for `getFabricUIManager()` to ReactContext and it's subclasses. This will replace the `getJSIModule()` post JSI module deletion. Reviewed By: philIip Differential Revision: D51718430 fbshipit-source-id: c897ab0ee9e755e3fdb3d1e5629177818870f293 --- .../facebook/react/bridge/ReactContext.java | 20 +++++++++++++++++++ .../react/runtime/BridgelessReactContext.java | 6 ++++++ .../react/uimanager/ThemedReactContext.java | 9 +++++++++ 3 files changed, 35 insertions(+) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/ReactContext.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/ReactContext.java index b57513cb67216e..78c385ce157643 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/ReactContext.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/ReactContext.java @@ -26,6 +26,7 @@ import com.facebook.react.bridge.queue.ReactQueueConfiguration; import com.facebook.react.common.LifecycleState; import com.facebook.react.common.ReactConstants; +import com.facebook.react.common.annotations.DeprecatedInNewArchitecture; import java.lang.ref.WeakReference; import java.util.Collection; import java.util.concurrent.CopyOnWriteArraySet; @@ -554,6 +555,25 @@ public boolean isBridgeless() { return mCatalystInstance.getJSIModule(moduleType); } + @DeprecatedInNewArchitecture( + message = + "This method will be deprecated later as part of Stable APIs with bridge removal and not encouraged usage.") + /** + * Get the UIManager for Fabric from the CatalystInstance. + * + * @return The UIManager when CatalystInstance is active. + */ + public @Nullable UIManager getFabricUIManager() { + if (!hasActiveReactInstance()) { + throw new IllegalStateException( + "Unable to retrieve a UIManager if CatalystInstance is not active."); + } + UIManager uiManager = mCatalystInstance.getFabricUIManager(); + return uiManager != null + ? uiManager + : (UIManager) mCatalystInstance.getJSIModule(JSIModuleType.UIManager); + } + /** * Get the sourceURL for the JS bundle from the CatalystInstance. This method is needed for * compatibility with bridgeless mode, which has no CatalystInstance. diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/BridgelessReactContext.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/BridgelessReactContext.java index ad4b76d4b549dc..6e1994a3804d00 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/BridgelessReactContext.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/BridgelessReactContext.java @@ -21,6 +21,7 @@ import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.bridge.ReactNoCrashBridgeNotAllowedSoftException; import com.facebook.react.bridge.ReactSoftExceptionLogger; +import com.facebook.react.bridge.UIManager; import com.facebook.react.bridge.WritableNativeArray; import com.facebook.react.config.ReactFeatureFlags; import com.facebook.react.devsupport.interfaces.DevSupportManager; @@ -84,6 +85,11 @@ public void setSourceURL(String sourceURL) { + moduleType.name()); } + @Override + public @Nullable UIManager getFabricUIManager() { + return mReactHost.getUIManager(); + } + @Override public CatalystInstance getCatalystInstance() { ReactSoftExceptionLogger.logSoftExceptionVerbose( diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/ThemedReactContext.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/ThemedReactContext.java index b10d2d9b3c0681..eded794637a424 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/ThemedReactContext.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/ThemedReactContext.java @@ -15,6 +15,7 @@ import com.facebook.react.bridge.LifecycleEventListener; import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.bridge.ReactContext; +import com.facebook.react.bridge.UIManager; /** * Wraps {@link ReactContext} with the base {@link Context} passed into the constructor. It provides @@ -116,4 +117,12 @@ public JSIModule getJSIModule(JSIModuleType moduleType) { } return super.getJSIModule(moduleType); } + + @Override + public UIManager getFabricUIManager() { + if (isBridgeless()) { + return mReactApplicationContext.getFabricUIManager(); + } + return super.getFabricUIManager(); + } }