Skip to content

Commit

Permalink
Nullsafe views/scroll module (#44532)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #44532

Changelog: [Internal]

Fix nullsafe errors in the module and add the annotation

Reviewed By: rshest

Differential Revision: D57218666

fbshipit-source-id: 9fc8c6d002bb2c4b53c0874a6d8c38fcf52b9e19
  • Loading branch information
Thomas Nardone authored and facebook-github-bot committed May 14, 2024
1 parent 6876775 commit 13dff7c
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import androidx.core.view.ViewCompat;
import com.facebook.common.logging.FLog;
import com.facebook.infer.annotation.Assertions;
import com.facebook.infer.annotation.Nullsafe;
import com.facebook.react.common.ReactConstants;
import com.facebook.react.common.build.ReactBuildConfig;
import com.facebook.react.modules.i18nmanager.I18nUtil;
Expand All @@ -55,6 +56,7 @@
import java.util.List;

/** Similar to {@link ReactScrollView} but only supports horizontal scrolling. */
@Nullsafe(Nullsafe.Mode.LOCAL)
public class ReactHorizontalScrollView extends HorizontalScrollView
implements ReactClippingViewGroup,
ViewGroup.OnHierarchyChangeListener,
Expand Down Expand Up @@ -109,7 +111,7 @@ public class ReactHorizontalScrollView extends HorizontalScrollView
private boolean mPagedArrowScrolling = false;
private int pendingContentOffsetX = UNSET_CONTENT_OFFSET;
private int pendingContentOffsetY = UNSET_CONTENT_OFFSET;
private StateWrapper mStateWrapper = null;
private @Nullable StateWrapper mStateWrapper = null;
private final ReactScrollViewScrollState mReactScrollViewScrollState;
private final ValueAnimator DEFAULT_FLING_ANIMATOR = ObjectAnimator.ofInt(this, "scrollX", 0, 0);
private PointerEvents mPointerEvents = PointerEvents.AUTO;
Expand Down Expand Up @@ -248,7 +250,7 @@ public void setSnapInterval(int snapInterval) {
mSnapInterval = snapInterval;
}

public void setSnapOffsets(List<Integer> snapOffsets) {
public void setSnapOffsets(@Nullable List<Integer> snapOffsets) {
mSnapOffsets = snapOffsets;
}

Expand All @@ -268,7 +270,7 @@ public void flashScrollIndicators() {
awakenScrollBars();
}

public void setOverflow(String overflow) {
public void setOverflow(@Nullable String overflow) {
mOverflow = overflow;
invalidate();
}
Expand Down Expand Up @@ -307,17 +309,10 @@ protected void onDraw(Canvas canvas) {
if (DEBUG_MODE) {
FLog.i(TAG, "onDraw[%d]", getId());
}

getDrawingRect(mRect);

switch (mOverflow) {
case ViewProps.VISIBLE:
break;
default:
canvas.clipRect(mRect);
break;
if (!ViewProps.VISIBLE.equals(mOverflow)) {
canvas.clipRect(mRect);
}

super.onDraw(canvas);
}

Expand Down Expand Up @@ -839,8 +834,10 @@ public void onChildViewAdded(View parent, View child) {
}

@Override
public void onChildViewRemoved(View parent, View child) {
mContentView.removeOnLayoutChangeListener(this);
public @Nullable void onChildViewRemoved(View parent, View child) {
if (mContentView != null) {
mContentView.removeOnLayoutChangeListener(this);
}
mContentView = null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import android.view.View;
import androidx.annotation.Nullable;
import androidx.core.view.ViewCompat;
import com.facebook.infer.annotation.Nullsafe;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.RetryableMountingLayerException;
Expand All @@ -36,6 +37,7 @@
* <p>Note that {@link ReactScrollView} and {@link ReactHorizontalScrollView} are exposed to JS as a
* single ScrollView component, configured via the {@code horizontal} boolean property.
*/
@Nullsafe(Nullsafe.Mode.LOCAL)
@ReactModule(name = ReactHorizontalScrollViewManager.REACT_CLASS)
public class ReactHorizontalScrollViewManager extends ViewGroupManager<ReactHorizontalScrollView>
implements ReactScrollViewCommandHelper.ScrollCommandHandler<ReactHorizontalScrollView> {
Expand Down Expand Up @@ -67,7 +69,7 @@ public ReactHorizontalScrollView createViewInstance(ThemedReactContext context)
}

@Override
public Object updateState(
public @Nullable Object updateState(
ReactHorizontalScrollView view, ReactStylesDiffMap props, StateWrapper stateWrapper) {
view.setStateWrapper(stateWrapper);
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import androidx.core.view.ViewCompat;
import com.facebook.common.logging.FLog;
import com.facebook.infer.annotation.Assertions;
import com.facebook.infer.annotation.Nullsafe;
import com.facebook.react.R;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.common.ReactConstants;
Expand Down Expand Up @@ -62,6 +63,7 @@
* <p>ReactScrollView only supports vertical scrolling. For horizontal scrolling, use {@link
* ReactHorizontalScrollView}.
*/
@Nullsafe(Nullsafe.Mode.LOCAL)
public class ReactScrollView extends ScrollView
implements ReactClippingViewGroup,
ViewGroup.OnHierarchyChangeListener,
Expand Down Expand Up @@ -111,7 +113,7 @@ public class ReactScrollView extends ScrollView
private @Nullable ReadableMap mCurrentContentOffset = null;
private int pendingContentOffsetX = UNSET_CONTENT_OFFSET;
private int pendingContentOffsetY = UNSET_CONTENT_OFFSET;
private StateWrapper mStateWrapper = null;
private @Nullable StateWrapper mStateWrapper = null;
private final ReactScrollViewScrollState mReactScrollViewScrollState =
new ReactScrollViewScrollState(ViewCompat.LAYOUT_DIRECTION_LTR);
private final ValueAnimator DEFAULT_FLING_ANIMATOR = ObjectAnimator.ofInt(this, "scrollY", 0, 0);
Expand Down Expand Up @@ -237,7 +239,7 @@ public void setSnapInterval(int snapInterval) {
mSnapInterval = snapInterval;
}

public void setSnapOffsets(List<Integer> snapOffsets) {
public void setSnapOffsets(@Nullable List<Integer> snapOffsets) {
mSnapOffsets = snapOffsets;
}

Expand All @@ -257,7 +259,7 @@ public void flashScrollIndicators() {
awakenScrollBars();
}

public void setOverflow(String overflow) {
public void setOverflow(@Nullable String overflow) {
mOverflow = overflow;
invalidate();
}
Expand Down Expand Up @@ -615,7 +617,7 @@ private boolean isScrollPerfLoggingEnabled() {
}

private int getMaxScrollY() {
int contentHeight = mContentView.getHeight();
int contentHeight = mContentView == null ? 0 : mContentView.getHeight();
int viewportHeight = getHeight() - getPaddingBottom() - getPaddingTop();
return Math.max(0, contentHeight - viewportHeight);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import androidx.annotation.Nullable;
import com.facebook.infer.annotation.Assertions;
import com.facebook.infer.annotation.Nullsafe;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.common.MapBuilder;
import com.facebook.react.uimanager.PixelUtil;
Expand All @@ -18,6 +19,7 @@
* Helper for view managers to handle commands like 'scrollTo'. Shared by {@link
* ReactScrollViewManager} and {@link ReactHorizontalScrollViewManager}.
*/
@Nullsafe(Nullsafe.Mode.LOCAL)
public class ReactScrollViewCommandHelper {

public static final int COMMAND_SCROLL_TO = 1;
Expand Down Expand Up @@ -70,16 +72,15 @@ public static <T> void receiveCommand(
@Nullable ReadableArray args) {
Assertions.assertNotNull(viewManager);
Assertions.assertNotNull(scrollView);
Assertions.assertNotNull(args);
switch (commandType) {
case COMMAND_SCROLL_TO:
{
scrollTo(viewManager, scrollView, args);
scrollTo(viewManager, scrollView, Assertions.assertNotNull(args));
return;
}
case COMMAND_SCROLL_TO_END:
{
scrollToEnd(viewManager, scrollView, args);
scrollToEnd(viewManager, scrollView, Assertions.assertNotNull(args));
return;
}
case COMMAND_FLASH_SCROLL_INDICATORS:
Expand All @@ -101,16 +102,15 @@ public static <T> void receiveCommand(
@Nullable ReadableArray args) {
Assertions.assertNotNull(viewManager);
Assertions.assertNotNull(scrollView);
Assertions.assertNotNull(args);
switch (commandType) {
case "scrollTo":
{
scrollTo(viewManager, scrollView, args);
scrollTo(viewManager, scrollView, Assertions.assertNotNull(args));
return;
}
case "scrollToEnd":
{
scrollToEnd(viewManager, scrollView, args);
scrollToEnd(viewManager, scrollView, Assertions.assertNotNull(args));
return;
}
case "flashScrollIndicators":
Expand All @@ -126,15 +126,15 @@ public static <T> void receiveCommand(
}

private static <T> void scrollTo(
ScrollCommandHandler<T> viewManager, T scrollView, @Nullable ReadableArray args) {
ScrollCommandHandler<T> viewManager, T scrollView, ReadableArray args) {
int destX = Math.round(PixelUtil.toPixelFromDIP(args.getDouble(0)));
int destY = Math.round(PixelUtil.toPixelFromDIP(args.getDouble(1)));
boolean animated = args.getBoolean(2);
viewManager.scrollTo(scrollView, new ScrollToCommandData(destX, destY, animated));
}

private static <T> void scrollToEnd(
ScrollCommandHandler<T> viewManager, T scrollView, @Nullable ReadableArray args) {
ScrollCommandHandler<T> viewManager, T scrollView, ReadableArray args) {
boolean animated = args.getBoolean(0);
viewManager.scrollToEnd(scrollView, new ScrollToEndCommandData(animated));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import android.view.View;
import androidx.annotation.Nullable;
import androidx.core.view.ViewCompat;
import com.facebook.infer.annotation.Nullsafe;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.RetryableMountingLayerException;
Expand Down Expand Up @@ -39,6 +40,7 @@
* <p>Note that {@link ReactScrollView} and {@link ReactHorizontalScrollView} are exposed to JS as a
* single ScrollView component, configured via the {@code horizontal} boolean property.
*/
@Nullsafe(Nullsafe.Mode.LOCAL)
@ReactModule(name = ReactScrollViewManager.REACT_CLASS)
public class ReactScrollViewManager extends ViewGroupManager<ReactScrollView>
implements ReactScrollViewCommandHelper.ScrollCommandHandler<ReactScrollView> {
Expand Down Expand Up @@ -341,7 +343,7 @@ public void setMaintainVisibleContentPosition(ReactScrollView view, ReadableMap
}

@Override
public Object updateState(
public @Nullable Object updateState(
ReactScrollView view, ReactStylesDiffMap props, StateWrapper stateWrapper) {
view.setStateWrapper(stateWrapper);
return null;
Expand Down

0 comments on commit 13dff7c

Please sign in to comment.