From 3242de08c530291963510a9c6b93f857764a5882 Mon Sep 17 00:00:00 2001 From: fabriziobertoglio1987 Date: Mon, 26 Sep 2022 17:03:01 +0800 Subject: [PATCH] adapting #26444 to solve issue #30373 - Import improvements from PR #26444 _maybeCallOnEndReached: https://github.com/facebook/react-native/pull/26444/files#diff-7481ec8ed5532798b075df637e805a8e439807aa2ce671208c24068c286361e8L1374-R1413 https://github.com/facebook/react-native/blob/2d3f6ca9801ef92b446458a2efc795db4ec17021/Libraries/Lists/VirtualizedList.js#L1372-L1414 - Additional check _hasDoneFirstScroll for maybeCallOnEndReached (added in onScroll callback) - Add improved logic start/endPositionReached and isScrollingForward - Add threeshold instead of 2 - Use default threeshold 30 for iOS - Add other improvements from method _maybeCallOnEndReached --- Libraries/Lists/VirtualizedList.js | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/Libraries/Lists/VirtualizedList.js b/Libraries/Lists/VirtualizedList.js index c88485babc616d..e761fcadf14660 100644 --- a/Libraries/Lists/VirtualizedList.js +++ b/Libraries/Lists/VirtualizedList.js @@ -53,6 +53,7 @@ const ViewabilityHelper = require('./ViewabilityHelper'); const invariant = require('invariant'); const ON_END_REACHED_EPSILON = 0.001; +const MIN_START_POSITION_THRESHOLD = 2; let _usedIndexForKey = false; let _keylessItemComponentName: string = ''; @@ -1329,22 +1330,19 @@ class VirtualizedList extends React.PureComponent { inverted && enabledTalkbackCompatibleInvertedList; let distanceFromEnd; - let endPositionReached; let startPositionReached; let isScrollingForward = dOffset < 0; + const threeshold = onEndReachedThresholdOrDefault(onEndReachedThreshold); // in case of inverted flatlist with talkback enabled // replace 2 with threeshold - const THRESHOLD = Platform.OS === 'android' ? 2 : 30; if (talkbackCompatibility && this._hasTriggeredInitialScrollToIndex) { distanceFromEnd = offset; startPositionReached = - Math.abs(visibleLength + offset - contentLength) < THRESHOLD; - endPositionReached = Math.abs(offset) < THRESHOLD; + Math.abs(visibleLength + offset - contentLength) < + MIN_START_POSITION_THRESHOLD; } else { distanceFromEnd = contentLength - visibleLength - offset; - endPositionReached = - Math.abs(visibleLength + offset - contentLength) < THRESHOLD; - startPositionReached = Math.abs(offset) < THRESHOLD; + startPositionReached = Math.abs(offset) < MIN_START_POSITION_THRESHOLD; } if ( @@ -1370,9 +1368,8 @@ class VirtualizedList extends React.PureComponent { return; } - // $FlowFixMe - const minimumDistanceFromEnd = onEndReachedThreshold * visibleLength; - if (distanceFromEnd >= minimumDistanceFromEnd) { + const minimumDistanceFromEnd = threeshold * visibleLength; + if (distanceFromEnd > minimumDistanceFromEnd) { return; } @@ -1399,9 +1396,11 @@ class VirtualizedList extends React.PureComponent { inverted && enabledTalkbackCompatibleInvertedList ) { + /* console.error( 'The prop enabledTalkbackCompatibleInvertedList can not be used with initialScrollIndex.', ); + */ } if ( width > 0 &&