Skip to content

Commit

Permalink
Add debug logging capabilities to ReactHorizontalScrollView
Browse files Browse the repository at this point in the history
Summary:
When debugging this class a lot, I found it helpful to have these logs and it would have been nice if they were here already - I had to rewrite these several times.

Changelog: [Internal]

Reviewed By: mdvacca

Differential Revision: D26836318

fbshipit-source-id: 08eb9ae19923fc593d1aba031586a02a193d6b2d
  • Loading branch information
JoshuaGross authored and facebook-github-bot committed Mar 6, 2021
1 parent 365e124 commit 8e2fa86
Showing 1 changed file with 60 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.bridge.WritableNativeMap;
import com.facebook.react.common.ReactConstants;
import com.facebook.react.common.build.ReactBuildConfig;
import com.facebook.react.modules.i18nmanager.I18nUtil;
import com.facebook.react.uimanager.FabricViewStateManager;
import com.facebook.react.uimanager.MeasureSpecAssertions;
Expand All @@ -50,6 +51,9 @@
public class ReactHorizontalScrollView extends HorizontalScrollView
implements ReactClippingViewGroup, FabricViewStateManager.HasFabricViewStateManager {

private static boolean DEBUG_MODE = false && ReactBuildConfig.DEBUG;
private static String TAG = ReactHorizontalScrollView.class.getSimpleName();

private static @Nullable Field sScrollerField;
private static boolean sTriedToGetScrollerField = false;
private static final String CONTENT_OFFSET_LEFT = "contentOffsetLeft";
Expand Down Expand Up @@ -141,7 +145,7 @@ private OverScroller getOverScrollerFromParent() {
sScrollerField.setAccessible(true);
} catch (NoSuchFieldException e) {
FLog.w(
ReactConstants.TAG,
TAG,
"Failed to get mScroller field for HorizontalScrollView! "
+ "This app will exhibit the bounce-back scrolling bug :(");
}
Expand All @@ -154,7 +158,7 @@ private OverScroller getOverScrollerFromParent() {
scroller = (OverScroller) scrollerValue;
} else {
FLog.w(
ReactConstants.TAG,
TAG,
"Failed to cast mScroller field in HorizontalScrollView (probably due to OEM changes to AOSP)! "
+ "This app will exhibit the bounce-back scrolling bug :(");
scroller = null;
Expand Down Expand Up @@ -238,6 +242,10 @@ public void setOverflow(String overflow) {

@Override
protected void onDraw(Canvas canvas) {
if (DEBUG_MODE) {
FLog.i(TAG, "onDraw[%d]", getId());
}

getDrawingRect(mRect);

switch (mOverflow) {
Expand All @@ -255,12 +263,27 @@ protected void onDraw(Canvas canvas) {
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
MeasureSpecAssertions.assertExplicitMeasureSpec(widthMeasureSpec, heightMeasureSpec);

setMeasuredDimension(
MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.getSize(heightMeasureSpec));
int measuredWidth = MeasureSpec.getSize(widthMeasureSpec);
int measuredHeight = MeasureSpec.getSize(heightMeasureSpec);

if (DEBUG_MODE) {
FLog.i(
TAG,
"onMeasure[%d] measured width: %d measured height: %d",
getId(),
measuredWidth,
measuredHeight);
}

setMeasuredDimension(measuredWidth, measuredHeight);
}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
if (DEBUG_MODE) {
FLog.i(TAG, "onLayout[%d] l %d t %d r %d b %d", getId(), l, t, r, b);
}

// Call with the present values in order to re-layout if necessary
// If a "pending" value has been set, we restore that value.
// That value gets cleared by reactScrollTo.
Expand Down Expand Up @@ -344,6 +367,10 @@ private void scrollToChild(View child) {

@Override
protected void onScrollChanged(int x, int y, int oldX, int oldY) {
if (DEBUG_MODE) {
FLog.i(TAG, "onScrollChanged[%d] x %d y %d oldx %d oldy %d", getId(), x, y, oldX, oldY);
}

super.onScrollChanged(x, y, oldX, oldY);

mActivelyScrolling = true;
Expand Down Expand Up @@ -573,6 +600,17 @@ public void setEndFillColor(int color) {

@Override
protected void onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY) {
if (DEBUG_MODE) {
FLog.i(
TAG,
"onOverScrolled[%d] scrollX %d scrollY %d clampedX %d clampedY %d",
getId(),
scrollX,
scrollY,
clampedX,
clampedY);
}

if (mScroller != null) {
// FB SCROLLVIEW CHANGE

Expand Down Expand Up @@ -978,6 +1016,10 @@ public void setBorderStyle(@Nullable String style) {
* scroll view and state. Calling raw `smoothScrollTo` doesn't update state.
*/
public void reactSmoothScrollTo(int x, int y) {
if (DEBUG_MODE) {
FLog.i(TAG, "reactSmoothScrollTo[%d] x %d y %d", getId(), x, y);
}

// `smoothScrollTo` contains some logic that, if called multiple times in a short amount of
// time, will treat all calls as part of the same animation and will not lengthen the duration
// of the animation. This means that, for example, if the user is scrolling rapidly, multiple
Expand Down Expand Up @@ -1034,6 +1076,10 @@ public void onAnimationRepeat(Animator animator) {}
* scroll view and state. Calling raw `reactScrollTo` doesn't update state.
*/
public void reactScrollTo(int x, int y) {
if (DEBUG_MODE) {
FLog.i(TAG, "reactScrollTo[%d] x %d y %d", getId(), x, y);
}

scrollTo(x, y);
updateStateOnScroll(x, y);
setPendingContentOffsets(x, y);
Expand Down Expand Up @@ -1080,6 +1126,16 @@ private void updateStateOnScroll(final int scrollX, final int scrollY) {
fabricScrollX = scrollX;
}

if (DEBUG_MODE) {
FLog.i(
TAG,
"updateStateOnScroll[%d] scrollX %d scrollY %d fabricScrollX",
getId(),
scrollX,
scrollY,
fabricScrollX);
}

mFabricViewStateManager.setState(
new FabricViewStateManager.StateUpdateCallback() {
@Override
Expand Down

0 comments on commit 8e2fa86

Please sign in to comment.