From 48dcfa1718d088c44f925814de59ca6ad35d44f2 Mon Sep 17 00:00:00 2001 From: Lulu Wu Date: Thu, 5 Oct 2023 07:04:57 -0700 Subject: [PATCH] Fix new arch example not render in RNTester (#39810) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/39810 Two issues will be fixed: - Bridgeless has lazy view manager loading by default so the React Package that provides view managers must implement ViewManagerOnDemandReactPackage, we might could refactor the design of package classes later - ThemedReactContext should **NOT** be used directly to call function ```getJSModule```, since it doesn't overrides ```getJSModule``` for Bridgeless, we can use it's internal variable ```meactApplicationContext``` which should be an instance of BridgelessReactContext Reviewed By: cortinico Differential Revision: D49912656 fbshipit-source-id: a0bdd717612398e8d7a6f36d36dba241a3b06bd7 --- .../react/uiapp/RNTesterReactHostDelegate.kt | 19 ++++++++++++++++++- .../uiapp/component/MyLegacyViewManager.kt | 4 ++-- .../react/uiapp/component/MyNativeView.kt | 6 +++--- 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/packages/rn-tester/android/app/src/main/java/com/facebook/react/uiapp/RNTesterReactHostDelegate.kt b/packages/rn-tester/android/app/src/main/java/com/facebook/react/uiapp/RNTesterReactHostDelegate.kt index aa0111fec1328f..b7b2f13ebf086d 100644 --- a/packages/rn-tester/android/app/src/main/java/com/facebook/react/uiapp/RNTesterReactHostDelegate.kt +++ b/packages/rn-tester/android/app/src/main/java/com/facebook/react/uiapp/RNTesterReactHostDelegate.kt @@ -14,6 +14,7 @@ import com.facebook.react.JSEngineResolutionAlgorithm import com.facebook.react.ReactPackage import com.facebook.react.ReactPackageTurboModuleManagerDelegate import com.facebook.react.TurboReactPackage +import com.facebook.react.ViewManagerOnDemandReactPackage import com.facebook.react.bridge.JSBundleLoader import com.facebook.react.bridge.NativeModule import com.facebook.react.bridge.ReactApplicationContext @@ -108,7 +109,7 @@ class RNTesterReactHostDelegate internal constructor(context: Context) : ReactHo } } }, - object : ReactPackage { + object : ViewManagerOnDemandReactPackage, ReactPackage { override fun createNativeModules( reactContext: ReactApplicationContext ): List = emptyList() @@ -117,6 +118,22 @@ class RNTesterReactHostDelegate internal constructor(context: Context) : ReactHo reactContext: ReactApplicationContext ): List> = listOf(MyNativeViewManager(), MyLegacyViewManager(reactContext)) + + override fun getViewManagerNames( + reactContext: ReactApplicationContext + ): Collection = + listOf(MyNativeViewManager.REACT_CLASS, MyLegacyViewManager.REACT_CLASS) + + override fun createViewManager( + reactContext: ReactApplicationContext, + viewManagerName: String + ): ViewManager<*, *>? { + return when (viewManagerName) { + MyNativeViewManager.REACT_CLASS -> MyNativeViewManager() + MyLegacyViewManager.REACT_CLASS -> MyLegacyViewManager(reactContext) + else -> null + } + } }) } } diff --git a/packages/rn-tester/android/app/src/main/java/com/facebook/react/uiapp/component/MyLegacyViewManager.kt b/packages/rn-tester/android/app/src/main/java/com/facebook/react/uiapp/component/MyLegacyViewManager.kt index 5845e85af5390f..1ff6b782db7b5c 100644 --- a/packages/rn-tester/android/app/src/main/java/com/facebook/react/uiapp/component/MyLegacyViewManager.kt +++ b/packages/rn-tester/android/app/src/main/java/com/facebook/react/uiapp/component/MyLegacyViewManager.kt @@ -26,8 +26,8 @@ internal class MyLegacyViewManager(reactContext: ReactApplicationContext) : override fun getName(): String = REACT_CLASS - override fun createViewInstance(reactContext: ThemedReactContext): MyNativeView = - MyNativeView(reactContext).apply { setBackgroundColor(Color.RED) } + override fun createViewInstance(themedReactContext: ThemedReactContext): MyNativeView = + MyNativeView(themedReactContext).apply { setBackgroundColor(Color.RED) } @ReactProp(name = ViewProps.OPACITY, defaultFloat = 1f) override fun setOpacity(view: MyNativeView, opacity: Float) { diff --git a/packages/rn-tester/android/app/src/main/java/com/facebook/react/uiapp/component/MyNativeView.kt b/packages/rn-tester/android/app/src/main/java/com/facebook/react/uiapp/component/MyNativeView.kt index 78584e08f89d69..9f8a89a3e15238 100644 --- a/packages/rn-tester/android/app/src/main/java/com/facebook/react/uiapp/component/MyNativeView.kt +++ b/packages/rn-tester/android/app/src/main/java/com/facebook/react/uiapp/component/MyNativeView.kt @@ -7,7 +7,6 @@ package com.facebook.react.uiapp.component -import android.content.Context import android.graphics.Color import android.graphics.drawable.GradientDrawable import android.view.View @@ -15,13 +14,15 @@ import com.facebook.react.bridge.Arguments import com.facebook.react.bridge.ReactContext import com.facebook.react.bridge.WritableArray import com.facebook.react.bridge.WritableMap +import com.facebook.react.uimanager.ThemedReactContext import com.facebook.react.uimanager.UIManagerHelper import com.facebook.react.uimanager.events.Event import com.facebook.react.uimanager.events.RCTEventEmitter -class MyNativeView(context: Context) : View(context) { +class MyNativeView(context: ThemedReactContext) : View(context) { private var currentColor = 0 private var background: GradientDrawable = GradientDrawable() + private var reactContext: ReactContext = context.getReactApplicationContext() override fun setBackgroundColor(color: Int) { if (color != currentColor) { @@ -51,7 +52,6 @@ class MyNativeView(context: Context) : View(context) { Color.colorToHSV(color, hsv) event.putMap("backgroundColor", backgroundColor) - val reactContext = context as ReactContext reactContext.getJSModule(RCTEventEmitter::class.java).receiveEvent(id, "onColorChanged", event) }