|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +package com.facebook.react.views.scroll; |
| 9 | + |
| 10 | +import android.graphics.Rect; |
| 11 | +import android.view.View; |
| 12 | +import android.view.ViewGroup; |
| 13 | +import androidx.annotation.Nullable; |
| 14 | +import com.facebook.infer.annotation.Assertions; |
| 15 | +import com.facebook.react.bridge.ReactContext; |
| 16 | +import com.facebook.react.bridge.ReadableMap; |
| 17 | +import com.facebook.react.bridge.UIManager; |
| 18 | +import com.facebook.react.bridge.UIManagerListener; |
| 19 | +import com.facebook.react.bridge.UiThreadUtil; |
| 20 | +import com.facebook.react.uimanager.UIManagerHelper; |
| 21 | +import com.facebook.react.uimanager.common.ViewUtil; |
| 22 | +import com.facebook.react.views.scroll.ReactScrollViewHelper.HasSmoothScroll; |
| 23 | +import com.facebook.react.views.view.ReactViewGroup; |
| 24 | +import java.lang.ref.WeakReference; |
| 25 | + |
| 26 | +/** |
| 27 | + * Manage state for the maintainVisibleContentPosition prop. |
| 28 | + * |
| 29 | + * <p>This uses UIManager to listen to updates and capture position of items before and after |
| 30 | + * layout. |
| 31 | + */ |
| 32 | +public class MaintainVisibleScrollPositionHelper<ScrollViewT extends ViewGroup & HasSmoothScroll> |
| 33 | + implements UIManagerListener { |
| 34 | + private final ScrollViewT mScrollView; |
| 35 | + private final boolean mHorizontal; |
| 36 | + private @Nullable Config mConfig; |
| 37 | + private @Nullable WeakReference<View> mFirstVisibleView = null; |
| 38 | + private @Nullable Rect mPrevFirstVisibleFrame = null; |
| 39 | + private boolean mListening = false; |
| 40 | + |
| 41 | + public static class Config { |
| 42 | + public final int minIndexForVisible; |
| 43 | + public final @Nullable Integer autoScrollToTopThreshold; |
| 44 | + |
| 45 | + Config(int minIndexForVisible, @Nullable Integer autoScrollToTopThreshold) { |
| 46 | + this.minIndexForVisible = minIndexForVisible; |
| 47 | + this.autoScrollToTopThreshold = autoScrollToTopThreshold; |
| 48 | + } |
| 49 | + |
| 50 | + static Config fromReadableMap(ReadableMap value) { |
| 51 | + int minIndexForVisible = value.getInt("minIndexForVisible"); |
| 52 | + Integer autoScrollToTopThreshold = |
| 53 | + value.hasKey("autoscrollToTopThreshold") |
| 54 | + ? value.getInt("autoscrollToTopThreshold") |
| 55 | + : null; |
| 56 | + return new Config(minIndexForVisible, autoScrollToTopThreshold); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + public MaintainVisibleScrollPositionHelper(ScrollViewT scrollView, boolean horizontal) { |
| 61 | + mScrollView = scrollView; |
| 62 | + mHorizontal = horizontal; |
| 63 | + } |
| 64 | + |
| 65 | + public void setConfig(@Nullable Config config) { |
| 66 | + mConfig = config; |
| 67 | + } |
| 68 | + |
| 69 | + /** Start listening to view hierarchy updates. Should be called when this is created. */ |
| 70 | + public void start() { |
| 71 | + if (mListening) { |
| 72 | + return; |
| 73 | + } |
| 74 | + mListening = true; |
| 75 | + getUIManagerModule().addUIManagerEventListener(this); |
| 76 | + } |
| 77 | + |
| 78 | + /** Stop listening to view hierarchy updates. Should be called before this is destroyed. */ |
| 79 | + public void stop() { |
| 80 | + if (!mListening) { |
| 81 | + return; |
| 82 | + } |
| 83 | + mListening = false; |
| 84 | + getUIManagerModule().removeUIManagerEventListener(this); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Update the scroll position of the managed ScrollView. This should be called after layout has |
| 89 | + * been updated. |
| 90 | + */ |
| 91 | + public void updateScrollPosition() { |
| 92 | + if (mConfig == null || mFirstVisibleView == null || mPrevFirstVisibleFrame == null) { |
| 93 | + return; |
| 94 | + } |
| 95 | + |
| 96 | + View firstVisibleView = mFirstVisibleView.get(); |
| 97 | + Rect newFrame = new Rect(); |
| 98 | + firstVisibleView.getHitRect(newFrame); |
| 99 | + |
| 100 | + if (mHorizontal) { |
| 101 | + int deltaX = newFrame.left - mPrevFirstVisibleFrame.left; |
| 102 | + if (deltaX != 0) { |
| 103 | + int scrollX = mScrollView.getScrollX(); |
| 104 | + mScrollView.scrollTo(scrollX + deltaX, mScrollView.getScrollY()); |
| 105 | + mPrevFirstVisibleFrame = newFrame; |
| 106 | + if (mConfig.autoScrollToTopThreshold != null |
| 107 | + && scrollX <= mConfig.autoScrollToTopThreshold) { |
| 108 | + mScrollView.reactSmoothScrollTo(0, mScrollView.getScrollY()); |
| 109 | + } |
| 110 | + } |
| 111 | + } else { |
| 112 | + int deltaY = newFrame.top - mPrevFirstVisibleFrame.top; |
| 113 | + if (deltaY != 0) { |
| 114 | + int scrollY = mScrollView.getScrollY(); |
| 115 | + mScrollView.scrollTo(mScrollView.getScrollX(), scrollY + deltaY); |
| 116 | + mPrevFirstVisibleFrame = newFrame; |
| 117 | + if (mConfig.autoScrollToTopThreshold != null |
| 118 | + && scrollY <= mConfig.autoScrollToTopThreshold) { |
| 119 | + mScrollView.reactSmoothScrollTo(mScrollView.getScrollX(), 0); |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + private @Nullable ReactViewGroup getContentView() { |
| 126 | + return (ReactViewGroup) mScrollView.getChildAt(0); |
| 127 | + } |
| 128 | + |
| 129 | + private UIManager getUIManagerModule() { |
| 130 | + return Assertions.assertNotNull( |
| 131 | + UIManagerHelper.getUIManager( |
| 132 | + (ReactContext) mScrollView.getContext(), |
| 133 | + ViewUtil.getUIManagerType(mScrollView.getId()))); |
| 134 | + } |
| 135 | + |
| 136 | + private void computeTargetView() { |
| 137 | + if (mConfig == null) { |
| 138 | + return; |
| 139 | + } |
| 140 | + ReactViewGroup contentView = getContentView(); |
| 141 | + if (contentView == null) { |
| 142 | + return; |
| 143 | + } |
| 144 | + |
| 145 | + int currentScroll = mHorizontal ? mScrollView.getScrollX() : mScrollView.getScrollY(); |
| 146 | + for (int i = mConfig.minIndexForVisible; i < contentView.getChildCount(); i++) { |
| 147 | + View child = contentView.getChildAt(i); |
| 148 | + float position = mHorizontal ? child.getX() : child.getY(); |
| 149 | + if (position > currentScroll || i == contentView.getChildCount() - 1) { |
| 150 | + mFirstVisibleView = new WeakReference<>(child); |
| 151 | + Rect frame = new Rect(); |
| 152 | + child.getHitRect(frame); |
| 153 | + mPrevFirstVisibleFrame = frame; |
| 154 | + break; |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + // UIManagerListener |
| 160 | + |
| 161 | + @Override |
| 162 | + public void willDispatchViewUpdates(final UIManager uiManager) { |
| 163 | + UiThreadUtil.runOnUiThread( |
| 164 | + new Runnable() { |
| 165 | + @Override |
| 166 | + public void run() { |
| 167 | + computeTargetView(); |
| 168 | + } |
| 169 | + }); |
| 170 | + } |
| 171 | + |
| 172 | + @Override |
| 173 | + public void didDispatchMountItems(UIManager uiManager) { |
| 174 | + // noop |
| 175 | + } |
| 176 | + |
| 177 | + @Override |
| 178 | + public void didScheduleMountItems(UIManager uiManager) { |
| 179 | + // noop |
| 180 | + } |
| 181 | +} |
0 commit comments