From dc1a498d3974680f2257efd8ff9ec3f989204c68 Mon Sep 17 00:00:00 2001 From: Nicola Corti Date: Fri, 18 Oct 2024 08:33:25 -0700 Subject: [PATCH] Undo breaking change on UIManager eventDispatcher accessor (#47088) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/47088 Whe migrating this interface to Kotlin we've subtly introduced a breaking change which is causing a lot of breakages in the ecosystem. This is forcing users to do: ``` // Before reactContext.getNativeModule(UIManagerModule::class.java)!!.eventDispatcher // After reactContext.getNativeModule(UIManagerModule::class.java)!!.getEventDispatcher() ``` This reverts this breaking change. Plus the method had a generic parameters which was completely unnecessary so I'm removing it. Changelog: [Android] [Fixed] - Undo breaking change on UIManager eventDispatcher accessor Reviewed By: cipolleschi Differential Revision: D64533594 fbshipit-source-id: c4f9a36993a22839fae90fb239f883305422ecec --- .../animated/NativeAnimatedNodesManager.java | 3 ++- .../com/facebook/react/bridge/UIManager.kt | 2 +- .../react/fabric/FabricUIManager.java | 1 - .../uimanager/ReactAccessibilityDelegate.java | 27 +++++++++---------- .../java/com/facebook/react/RootViewTest.kt | 2 +- .../NativeAnimatedNodeTraversalTest.kt | 2 +- .../fabric/events/TouchEventDispatchTest.kt | 2 +- .../facebook/testutils/fakes/FakeUIManager.kt | 5 ++-- 8 files changed, 21 insertions(+), 23 deletions(-) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/NativeAnimatedNodesManager.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/NativeAnimatedNodesManager.java index 6fd291bbbdfcec..b3f9c642f46553 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/NativeAnimatedNodesManager.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/NativeAnimatedNodesManager.java @@ -91,7 +91,8 @@ public void initializeEventListenerForUIManagerType(@UIManagerType final int uiM UIManager uiManager = UIManagerHelper.getUIManager(mReactApplicationContext, uiManagerType); if (uiManager != null) { - uiManager.getEventDispatcher().addListener(this); + EventDispatcher eventDispatcher = (EventDispatcher) uiManager.getEventDispatcher(); + eventDispatcher.addListener(this); if (uiManagerType == UIManagerType.FABRIC) { mEventListenerInitializedForFabric = true; } else { diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/UIManager.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/UIManager.kt index 6aab9810556cd2..f893f0ea209f8c 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/UIManager.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/UIManager.kt @@ -78,7 +78,7 @@ public interface UIManager : PerformanceCounter { public fun dispatchCommand(reactTag: Int, commandId: String, commandArgs: ReadableArray?) /** @return the [EventDispatcher] object that is used by this class. */ - public fun getEventDispatcher(): T + public val eventDispatcher: Any? /** * Used by native animated module to bypass the process of updating the values through the shadow diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/FabricUIManager.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/FabricUIManager.java index e73cdff47d3cb5..8ad955bc966e4c 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/FabricUIManager.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/FabricUIManager.java @@ -1025,7 +1025,6 @@ public void onHostResume() { @Override @NonNull - @SuppressWarnings("unchecked") public EventDispatcher getEventDispatcher() { return mEventDispatcher; } diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/ReactAccessibilityDelegate.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/ReactAccessibilityDelegate.java index 2152d87657c470..76b604715ceb34 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/ReactAccessibilityDelegate.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/ReactAccessibilityDelegate.java @@ -577,20 +577,19 @@ public boolean performAccessibilityAction(View host, int action, Bundle args) { UIManager uiManager = UIManagerHelper.getUIManager(reactContext, ViewUtil.getUIManagerType(reactTag)); if (uiManager != null) { - uiManager - .getEventDispatcher() - .dispatchEvent( - new Event(surfaceId, reactTag) { - @Override - public String getEventName() { - return TOP_ACCESSIBILITY_ACTION_EVENT; - } - - @Override - protected WritableMap getEventData() { - return event; - } - }); + EventDispatcher eventDispatcher = (EventDispatcher) uiManager.getEventDispatcher(); + eventDispatcher.dispatchEvent( + new Event(surfaceId, reactTag) { + @Override + public String getEventName() { + return TOP_ACCESSIBILITY_ACTION_EVENT; + } + + @Override + protected WritableMap getEventData() { + return event; + } + }); } } else { ReactSoftExceptionLogger.logSoftException( diff --git a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/RootViewTest.kt b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/RootViewTest.kt index c68e1bbedb1e78..4285a84fb127e6 100644 --- a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/RootViewTest.kt +++ b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/RootViewTest.kt @@ -92,7 +92,7 @@ class RootViewTest { val eventEmitterModuleMock = mock(RCTEventEmitter::class.java) whenever(catalystInstanceMock.getNativeModule(UIManagerModule::class.java)) .thenReturn(uiManager) - whenever(uiManager.getEventDispatcher()).thenReturn(eventDispatcher) + whenever(uiManager.eventDispatcher).thenReturn(eventDispatcher) // RootView IDs is React Native follow the 11, 21, 31, ... progression. val rootViewId = 11 diff --git a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/animated/NativeAnimatedNodeTraversalTest.kt b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/animated/NativeAnimatedNodeTraversalTest.kt index 5807188f02af1d..514f817f07d95a 100644 --- a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/animated/NativeAnimatedNodeTraversalTest.kt +++ b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/animated/NativeAnimatedNodeTraversalTest.kt @@ -86,7 +86,7 @@ class NativeAnimatedNodeTraversalTest { uiManagerMock = mock(UIManagerModule::class.java) eventDispatcherMock = mock(EventDispatcher::class.java) - whenever(uiManagerMock.getEventDispatcher()).thenAnswer { eventDispatcherMock } + whenever(uiManagerMock.eventDispatcher).thenAnswer { eventDispatcherMock } whenever(uiManagerMock.constants).thenAnswer { mapOf("customDirectEventTypes" to emptyMap()) } diff --git a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/fabric/events/TouchEventDispatchTest.kt b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/fabric/events/TouchEventDispatchTest.kt index 454982113c346d..4ecd8ef01de480 100644 --- a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/fabric/events/TouchEventDispatchTest.kt +++ b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/fabric/events/TouchEventDispatchTest.kt @@ -488,7 +488,7 @@ class TouchEventDispatchTest { spy(FabricUIManager(reactContext, viewManagerRegistry, batchEventDispatchedListener)) uiManager.initialize() - eventDispatcher = uiManager.getEventDispatcher() + eventDispatcher = uiManager.eventDispatcher // Ignore scheduled choreographer work val reactChoreographerMock = mock(ReactChoreographer::class.java) diff --git a/packages/react-native/ReactAndroid/src/test/java/com/facebook/testutils/fakes/FakeUIManager.kt b/packages/react-native/ReactAndroid/src/test/java/com/facebook/testutils/fakes/FakeUIManager.kt index 55f287820a5803..b37693bd51fc17 100644 --- a/packages/react-native/ReactAndroid/src/test/java/com/facebook/testutils/fakes/FakeUIManager.kt +++ b/packages/react-native/ReactAndroid/src/test/java/com/facebook/testutils/fakes/FakeUIManager.kt @@ -65,9 +65,8 @@ class FakeUIManager : UIManager, UIBlockViewResolver { error("Not yet implemented") } - override fun getEventDispatcher(): T { - error("Not yet implemented") - } + override val eventDispatcher: Any? + get() = TODO("Not yet implemented") override fun synchronouslyUpdateViewOnUIThread(reactTag: Int, props: ReadableMap?) { error("Not yet implemented")