From c8b1c73bd45182cb37f3e97fd0aa2f368910ef2b Mon Sep 17 00:00:00 2001 From: Spencer Ahrens Date: Mon, 6 Mar 2017 21:34:32 -0800 Subject: [PATCH] Simplify scroll interaction stuff Reviewed By: angelahess Differential Revision: D4662787 fbshipit-source-id: 6e28f8cacd08601254e70f30ab98ee27bd08cc5a --- .../Lists/ViewabilityHelper.js | 24 ------------------- .../CustomComponents/Lists/VirtualizedList.js | 5 ++++ .../Lists/__tests__/ViewabilityHelper-test.js | 17 ++++--------- 3 files changed, 9 insertions(+), 37 deletions(-) diff --git a/Libraries/CustomComponents/Lists/ViewabilityHelper.js b/Libraries/CustomComponents/Lists/ViewabilityHelper.js index 8da31962478..31ce384ba97 100644 --- a/Libraries/CustomComponents/Lists/ViewabilityHelper.js +++ b/Libraries/CustomComponents/Lists/ViewabilityHelper.js @@ -42,15 +42,6 @@ export type ViewabilityConfig = {| * render. */ waitForInteraction?: boolean, - - /** - * Criteria to filter out certain scroll events so they don't count as interactions. By default, - * any non-zero scroll offset will be considered an interaction. - */ - scrollInteractionFilter?: {| - minimumOffset?: number, // scrolls with an offset less than this are ignored. - minimumElapsed?: number, // scrolls that happen before this are ignored. - |}, |}; /** @@ -74,10 +65,6 @@ class ViewabilityHelper { _viewableItems: Map = new Map(); constructor(config: ViewabilityConfig = {viewAreaCoveragePercentThreshold: 0}) { - invariant( - config.scrollInteractionFilter == null || config.waitForInteraction, - 'scrollInteractionFilter only works in conjunction with waitForInteraction', - ); this._config = config; } @@ -163,17 +150,6 @@ class ViewabilityHelper { this._lastUpdateTime = updateTime; } const updateElapsed = this._lastUpdateTime ? updateTime - this._lastUpdateTime : 0; - if (this._config.waitForInteraction && !this._hasInteracted && scrollOffset !== 0) { - const filter = this._config.scrollInteractionFilter; - if (filter) { - if ((filter.minimumOffset == null || scrollOffset >= filter.minimumOffset) && - (filter.minimumElapsed == null || updateElapsed >= filter.minimumElapsed)) { - this._hasInteracted = true; - } - } else { - this._hasInteracted = true; - } - } if (this._config.waitForInteraction && !this._hasInteracted) { return; } diff --git a/Libraries/CustomComponents/Lists/VirtualizedList.js b/Libraries/CustomComponents/Lists/VirtualizedList.js index 23a842c7c43..c532e1aadfe 100644 --- a/Libraries/CustomComponents/Lists/VirtualizedList.js +++ b/Libraries/CustomComponents/Lists/VirtualizedList.js @@ -415,6 +415,7 @@ class VirtualizedList extends React.PureComponent { onContentSizeChange: this._onContentSizeChange, onLayout: this._onLayout, onScroll: this._onScroll, + onScrollBeginDrag: this._onScrollBeginDrag, ref: this._captureScrollRef, scrollEventThrottle: 50, // TODO: Android support }, @@ -609,6 +610,10 @@ class VirtualizedList extends React.PureComponent { this._updateCellsToRenderBatcher.schedule(); }; + _onScrollBeginDrag = (e): void => { + this._viewabilityHelper.recordInteraction(); + this.props.onScrollBeginDrag && this.props.onScrollBeginDrag(e); + }; _updateCellsToRender = () => { const {data, disableVirtualization, getItemCount, onEndReachedThreshold} = this.props; this._updateViewableItems(data); diff --git a/Libraries/CustomComponents/Lists/__tests__/ViewabilityHelper-test.js b/Libraries/CustomComponents/Lists/__tests__/ViewabilityHelper-test.js index 2ad5afe9684..1d97105a7ad 100644 --- a/Libraries/CustomComponents/Lists/__tests__/ViewabilityHelper-test.js +++ b/Libraries/CustomComponents/Lists/__tests__/ViewabilityHelper-test.js @@ -316,14 +316,11 @@ describe('onUpdate', function() { ); it( - 'waitForInteraction blocks callback until scroll', + 'waitForInteraction blocks callback until interaction', function() { const helper = new ViewabilityHelper({ waitForInteraction: true, viewAreaCoveragePercentThreshold: 0, - scrollInteractionFilter: { - minimumOffset: 20, - }, }); rowFrames = { a: {y: 0, height: 200}, @@ -340,15 +337,9 @@ describe('onUpdate', function() { onViewableItemsChanged, ); expect(onViewableItemsChanged).not.toBeCalled(); - helper.onUpdate( - data.length, - 10, // not far enough to meet minimumOffset - 100, - getFrameMetrics, - createViewToken, - onViewableItemsChanged, - ); - expect(onViewableItemsChanged).not.toBeCalled(); + + helper.recordInteraction(); + helper.onUpdate( data.length, 20,