Skip to content

Commit

Permalink
Migrate lookup of EventDispatcher to not depend on UIManagerModule
Browse files Browse the repository at this point in the history
Summary:
This diff migrates all the lookups of EventDispatcher to not depend on UIManagerModule anymore.
This refactor is necessary because:
- Users running in Fabric / Venice should not load on the UIManagerModule class
- D25858934 will introduce a change that will break all of these callsites

In the migration I'm relying on the method UIManagerHelper.getEventDispatcherFromReactTag() that returns the correct EventDispatcher for a reactTag.

I'm planning to land this change early in the week (to catch potential errors in alpha / beta versions)

As a followup we need to deprecate and prevent developers to continue using getNativeModule(UIManagerModule.class) moving forward. That will be part of another diff

changelog: [internal] internal

Reviewed By: JoshuaGross

Differential Revision: D25858933

fbshipit-source-id: e26c99759307517b5bef483274fe0e0d71bb4c6c
  • Loading branch information
mdvacca authored and facebook-github-bot committed Jan 11, 2021
1 parent 6a9525c commit 5348a98
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,7 @@ public void run() {
/**
* Test that the mentions input has colors displayed correctly. Removed for being flaky in open
* source, December 2016 public void testMetionsInputColors() throws Throwable { EventDispatcher
* eventDispatcher =
* getReactContext().getNativeModule(UIManagerModule.class).getEventDispatcher(); ReactEditText
* eventDispatcher = UIManagerHelper.getEventEmitterForReactTag(reactContext, tag); ReactEditText
* reactEditText = getViewByTestId("tokenizedInput"); String newText = "#Things and more #things";
* int contentWidth = reactEditText.getWidth(); int contentHeight = reactEditText.getHeight(); int
* start = 0; int count = newText.length();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import com.facebook.react.module.annotations.ReactModule;
import com.facebook.react.uimanager.PixelUtil;
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.uimanager.UIManagerModule;
import com.facebook.react.uimanager.UIManagerHelper;
import com.facebook.react.uimanager.ViewGroupManager;
import com.facebook.react.uimanager.ViewManagerDelegate;
import com.facebook.react.uimanager.annotations.ReactProp;
Expand Down Expand Up @@ -56,12 +56,13 @@ public ReactDrawerLayoutManager() {

@Override
protected void addEventEmitters(ThemedReactContext reactContext, ReactDrawerLayout view) {
UIManagerModule uiManager = reactContext.getNativeModule(UIManagerModule.class);
if (uiManager == null) {
EventDispatcher eventDispatcher =
UIManagerHelper.getEventDispatcherForReactTag(reactContext, view.getId());
if (eventDispatcher == null) {
return;
}

view.addDrawerListener(new DrawerEventEmitter(view, uiManager.getEventDispatcher()));
view.addDrawerListener(new DrawerEventEmitter(view, eventDispatcher));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ public void onShow(DialogInterface dialog) {
dispatcher.dispatchEvent(new ShowEvent(view.getId()));
}
});
view.setEventDispatcher(dispatcher);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,10 @@ protected void setHardwareAccelerated(boolean hardwareAccelerated) {
mPropertyRequiresNewDialog = true;
}

void setEventDispatcher(EventDispatcher eventDispatcher) {
mHostView.setEventDispatcher(eventDispatcher);
}

@Override
public void onHostResume() {
// We show the dialog again when the host resumes
Expand Down Expand Up @@ -393,6 +397,7 @@ static class DialogRootViewGroup extends ReactViewGroup
private boolean hasAdjustedSize = false;
private int viewWidth;
private int viewHeight;
private EventDispatcher mEventDispatcher;

private final FabricViewStateManager mFabricViewStateManager = new FabricViewStateManager();

Expand All @@ -402,6 +407,10 @@ public DialogRootViewGroup(Context context) {
super(context);
}

private void setEventDispatcher(EventDispatcher eventDispatcher) {
mEventDispatcher = eventDispatcher;
}

@Override
protected void onSizeChanged(final int w, final int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
Expand Down Expand Up @@ -494,13 +503,13 @@ private ReactContext getReactContext() {

@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
mJSTouchDispatcher.handleTouchEvent(event, getEventDispatcher());
mJSTouchDispatcher.handleTouchEvent(event, mEventDispatcher);
return super.onInterceptTouchEvent(event);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
mJSTouchDispatcher.handleTouchEvent(event, getEventDispatcher());
mJSTouchDispatcher.handleTouchEvent(event, mEventDispatcher);
super.onTouchEvent(event);
// In case when there is no children interested in handling touch event, we return true from
// the root view in order to receive subsequent events related to that gesture
Expand All @@ -509,7 +518,7 @@ public boolean onTouchEvent(MotionEvent event) {

@Override
public void onChildStartedNativeGesture(MotionEvent androidEvent) {
mJSTouchDispatcher.onChildStartedNativeGesture(androidEvent, getEventDispatcher());
mJSTouchDispatcher.onChildStartedNativeGesture(androidEvent, mEventDispatcher);
}

@Override
Expand All @@ -518,11 +527,6 @@ public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
// even when some other view disallow that
}

private EventDispatcher getEventDispatcher() {
ReactContext reactContext = getReactContext();
return reactContext.getNativeModule(UIManagerModule.class).getEventDispatcher();
}

@Override
public FabricViewStateManager getFabricViewStateManager() {
return mFabricViewStateManager;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@
import com.facebook.react.uimanager.PixelUtil;
import com.facebook.react.uimanager.SimpleViewManager;
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.uimanager.UIManagerModule;
import com.facebook.react.uimanager.UIManagerHelper;
import com.facebook.react.uimanager.ViewManagerDelegate;
import com.facebook.react.uimanager.ViewProps;
import com.facebook.react.uimanager.annotations.ReactProp;
import com.facebook.react.uimanager.events.EventDispatcher;
import com.facebook.react.viewmanagers.SliderManagerDelegate;
import com.facebook.react.viewmanagers.SliderManagerInterface;
import com.facebook.yoga.YogaMeasureFunction;
Expand Down Expand Up @@ -95,16 +96,13 @@ public long measure(
@Override
public void onProgressChanged(SeekBar seekbar, int progress, boolean fromUser) {
ReactContext reactContext = (ReactContext) seekbar.getContext();
UIManagerModule uiManager = reactContext.getNativeModule(UIManagerModule.class);

if (uiManager != null) {
uiManager
.getEventDispatcher()
.dispatchEvent(
new ReactSliderEvent(
seekbar.getId(),
((ReactSlider) seekbar).toRealProgress(progress),
fromUser));
EventDispatcher eventDispatcher =
UIManagerHelper.getEventDispatcherForReactTag(reactContext, seekbar.getId());

if (eventDispatcher != null) {
eventDispatcher.dispatchEvent(
new ReactSliderEvent(
seekbar.getId(), ((ReactSlider) seekbar).toRealProgress(progress), fromUser));
}
}

Expand All @@ -114,15 +112,14 @@ public void onStartTrackingTouch(SeekBar seekbar) {}
@Override
public void onStopTrackingTouch(SeekBar seekbar) {
ReactContext reactContext = (ReactContext) seekbar.getContext();
UIManagerModule uiManager = reactContext.getNativeModule(UIManagerModule.class);

if (uiManager != null) {
uiManager
.getEventDispatcher()
.dispatchEvent(
new ReactSlidingCompleteEvent(
seekbar.getId(),
((ReactSlider) seekbar).toRealProgress(seekbar.getProgress())));
EventDispatcher eventDispatcher =
UIManagerHelper.getEventDispatcherForReactTag(reactContext, seekbar.getId());

if (eventDispatcher != null) {
eventDispatcher.dispatchEvent(
new ReactSlidingCompleteEvent(
seekbar.getId(),
((ReactSlider) seekbar).toRealProgress(seekbar.getProgress())));
}
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import com.facebook.react.common.build.ReactBuildConfig;
import com.facebook.react.uimanager.FabricViewStateManager;
import com.facebook.react.uimanager.UIManagerModule;
import com.facebook.react.uimanager.events.EventDispatcher;
import com.facebook.react.views.text.CustomLetterSpacingSpan;
import com.facebook.react.views.text.CustomLineHeightSpan;
import com.facebook.react.views.text.CustomStyleSpan;
Expand Down Expand Up @@ -119,6 +120,7 @@ public class ReactEditText extends AppCompatEditText
protected boolean mIsSettingTextFromState = false;

private static final KeyListener sKeyListener = QwertyKeyListener.getInstanceForFullKeyboard();
private @Nullable EventDispatcher mEventDispatcher;

public ReactEditText(Context context) {
super(context);
Expand Down Expand Up @@ -247,7 +249,8 @@ public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
InputConnection inputConnection = super.onCreateInputConnection(outAttrs);
if (inputConnection != null && mOnKeyPress) {
inputConnection =
new ReactEditTextInputConnectionWrapper(inputConnection, reactContext, this);
new ReactEditTextInputConnectionWrapper(
inputConnection, reactContext, this, mEventDispatcher);
}

if (isMultiline() && getBlurOnSubmit()) {
Expand Down Expand Up @@ -1043,6 +1046,10 @@ private void updateCachedSpannable(boolean resetStyles) {
TextLayoutManager.setCachedSpannabledForTag(getId(), sb);
}

void setEventDispatcher(@Nullable EventDispatcher eventDispatcher) {
mEventDispatcher = eventDispatcher;
}

/**
* This class will redirect *TextChanged calls to the listeners only in the case where the text is
* changed by the user, and not explicitly set by JS.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
import android.view.inputmethod.InputConnection;
import android.view.inputmethod.InputConnectionWrapper;
import androidx.annotation.Nullable;
import com.facebook.infer.annotation.Assertions;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.uimanager.UIManagerModule;
import com.facebook.react.uimanager.events.EventDispatcher;

/**
Expand Down Expand Up @@ -61,11 +59,12 @@ class ReactEditTextInputConnectionWrapper extends InputConnectionWrapper {
private @Nullable String mKey = null;

public ReactEditTextInputConnectionWrapper(
InputConnection target, final ReactContext reactContext, final ReactEditText editText) {
InputConnection target,
final ReactContext reactContext,
final ReactEditText editText,
EventDispatcher eventDispatcher) {
super(target, false);
mEventDispatcher =
Assertions.assertNotNull(reactContext.getNativeModule(UIManagerModule.class))
.getEventDispatcher();
mEventDispatcher = eventDispatcher;
mEditText = editText;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -968,6 +968,7 @@ public void afterTextChanged(Editable s) {}
@Override
protected void addEventEmitters(
final ThemedReactContext reactContext, final ReactEditText editText) {
editText.setEventDispatcher(getEventDispatcher(reactContext, editText));
editText.addTextChangedListener(new ReactTextInputTextWatcher(reactContext, editText));
editText.setOnFocusChangeListener(
new View.OnFocusChangeListener() {
Expand Down

0 comments on commit 5348a98

Please sign in to comment.