Skip to content

Commit

Permalink
Migrate ScrollEvent to RCTModernEventEmitter
Browse files Browse the repository at this point in the history
Summary:
Motivation: perf, simplicity, adhering to new SurfaceMountingManager APIs available to us. Backwards-compatible with events sent through old system or Fabric, to Fabric or non-Fabric Views.

Changelog: [Changed][Android] Old Native method to create ScrollEvent has been deprecated and will be removed at some point in the (distant) future

Reviewed By: mdvacca

Differential Revision: D26027105

fbshipit-source-id: b9dba5b56c2bfed3b8fc4488c54b271b85ab5fa0
  • Loading branch information
JoshuaGross authored and facebook-github-bot committed Jan 23, 2021
1 parent 708038d commit 62f0dee
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@

public class SurfaceMountingManager {
public static final String TAG = SurfaceMountingManager.class.getSimpleName();

private static final boolean SHOW_CHANGED_VIEW_HIERARCHIES = ReactBuildConfig.DEBUG && false;
private static final long KEEPALIVE_MILLISECONDS = 1000;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,42 @@ public static ReactContext getReactContext(View view) {
return (ReactContext) context;
}

/**
* @return Get the ThemedReactContext associated with a View, if possible, and then call
* getSurfaceId on it. See above (getReactContext) for additional context.
*/
public static int getSurfaceId(View view) {
int reactTag = view.getId();

// In non-Fabric we don't have (or use) SurfaceId
if (getUIManagerType(reactTag) == UIManagerType.DEFAULT) {
return -1;
}

Context context = view.getContext();
if (!(context instanceof ThemedReactContext) && context instanceof ContextWrapper) {
context = ((ContextWrapper) context).getBaseContext();
}

int surfaceId = getSurfaceId(context);

// All Fabric-managed Views (should) have a ThemedReactContext attached.
if (surfaceId == -1) {
ReactSoftException.logSoftException(
"UIManagerHelper",
new IllegalStateException(
"Fabric View [" + reactTag + "] does not have SurfaceId associated with it"));
}
return surfaceId;
}

public static int getSurfaceId(Context context) {
if (context instanceof ThemedReactContext) {
return ((ThemedReactContext) context).getSurfaceId();
}
return -1;
}

/**
* @return the default padding used by Android EditText's. This method returns the padding in an
* array to avoid extra classloading during hot-path of RN Android.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,11 @@ private static void emitScrollEvent(
}

ReactContext reactContext = (ReactContext) scrollView.getContext();
int surfaceId = UIManagerHelper.getSurfaceId(reactContext);
UIManagerHelper.getEventDispatcherForReactTag(reactContext, scrollView.getId())
.dispatchEvent(
ScrollEvent.obtain(
surfaceId,
scrollView.getId(),
scrollEventType,
scrollView.getScrollX(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.facebook.react.uimanager.PixelUtil;
import com.facebook.react.uimanager.events.Event;
import com.facebook.react.uimanager.events.RCTEventEmitter;
import com.facebook.react.uimanager.events.RCTModernEventEmitter;

/** A event dispatched from a ScrollView scrolling. */
public class ScrollEvent extends Event<ScrollEvent> {
Expand All @@ -33,6 +34,7 @@ public class ScrollEvent extends Event<ScrollEvent> {
private @Nullable ScrollEventType mScrollEventType;

public static ScrollEvent obtain(
int surfaceId,
int viewTag,
ScrollEventType scrollEventType,
int scrollX,
Expand All @@ -48,6 +50,7 @@ public static ScrollEvent obtain(
event = new ScrollEvent();
}
event.init(
surfaceId,
viewTag,
scrollEventType,
scrollX,
Expand All @@ -69,6 +72,7 @@ public void onDispose() {
private ScrollEvent() {}

private void init(
int surfaceId,
int viewTag,
ScrollEventType scrollEventType,
int scrollX,
Expand All @@ -79,7 +83,7 @@ private void init(
int contentHeight,
int scrollViewWidth,
int scrollViewHeight) {
super.init(viewTag);
super.init(surfaceId, viewTag);
mScrollEventType = scrollEventType;
mScrollX = scrollX;
mScrollY = scrollY;
Expand Down Expand Up @@ -116,6 +120,12 @@ public void dispatch(RCTEventEmitter rctEventEmitter) {
rctEventEmitter.receiveEvent(getViewTag(), getEventName(), serializeEventData());
}

@Override
public void dispatchV2(RCTModernEventEmitter rctEventEmitter) {
rctEventEmitter.receiveEvent(
getSurfaceId(), getViewTag(), getEventName(), serializeEventData());
}

private WritableMap serializeEventData() {
WritableMap contentInset = Arguments.createMap();
contentInset.putDouble("top", 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1131,8 +1131,10 @@ public ReactScrollWatcher(ReactEditText editText) {
@Override
public void onScrollChanged(int horiz, int vert, int oldHoriz, int oldVert) {
if (mPreviousHoriz != horiz || mPreviousVert != vert) {
int surfaceId = UIManagerHelper.getSurfaceId(mReactEditText);
ScrollEvent event =
ScrollEvent.obtain(
surfaceId,
mReactEditText.getId(),
ScrollEventType.SCROLL,
horiz,
Expand Down

0 comments on commit 62f0dee

Please sign in to comment.