2016-04-01 20:53:54 +00:00
|
|
|
/**
|
2017-03-24 21:18:39 +00:00
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
2016-04-01 20:53:54 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*
|
|
|
|
* @providesModule ViewabilityHelper
|
|
|
|
* @flow
|
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
2017-03-02 01:52:52 +00:00
|
|
|
const invariant = require('fbjs/lib/invariant');
|
Better ListView - FlatList
Summary:
We really need a better list view - so here it is!
Main changes from existing `ListView`:
* Items are "virtualized" to limit memory - that is, items outside of the render window are unmounted and their memory is reclaimed. This means that instance state is not preserved when items scroll out of the render window.
* No `DataSource` - just a simple `data` prop of shape `Array<any>`. By default, they are expected to be of the shape `{key: string}` but a custom `rowExtractor` function can be provided for different shapes, e.g. graphql data where you want to map `id` to `key`. Note the underlying `VirtualizedList` is much more flexible.
* Fancy `scrollTo` functionality: `scrollToEnd`, `scrollToIndex`, and `scrollToItem` in addition to the normal `scrollToOffset`.
* Built-in pull to refresh support - set set the `onRefresh` and `refreshing` props.
* Rendering additional rows is usually done with low priority, after any interactions/animations complete, unless we're about to run out of rendered content. This should help apps feel more responsive.
* Component props replace render functions, e.g. `ItemComponent: ReactClass<{item: Item, index: number}>` replaces `renderRow: (...) => React.Element<*>`
* Supports dynamic items automatically by using `onLayout`, or `getItemLayout` can be provided for a perf boost and smoother `scrollToIndex` and scroll bar behavior.
* Visibility callback replaced with more powerful viewability callback and works in vertical and horizontal mode on at least Android and iOS, but probably other platforms as well. Extra power comes from the `viewablePercentThreshold` that lets the client decide when an item should be considered viewable.
Demo:
https://www.facebook.com/groups/576288835853049/permalink/753923058089625/
Reviewed By: yungsters
Differential Revision: D4412469
fbshipit-source-id: e2d891490bf76fe14df49294ecddf78a58adcf23
2017-02-04 18:25:32 +00:00
|
|
|
|
2017-02-28 10:08:58 +00:00
|
|
|
export type ViewToken = {item: any, key: string, index: ?number, isViewable: boolean, section?: any};
|
Better ListView - FlatList
Summary:
We really need a better list view - so here it is!
Main changes from existing `ListView`:
* Items are "virtualized" to limit memory - that is, items outside of the render window are unmounted and their memory is reclaimed. This means that instance state is not preserved when items scroll out of the render window.
* No `DataSource` - just a simple `data` prop of shape `Array<any>`. By default, they are expected to be of the shape `{key: string}` but a custom `rowExtractor` function can be provided for different shapes, e.g. graphql data where you want to map `id` to `key`. Note the underlying `VirtualizedList` is much more flexible.
* Fancy `scrollTo` functionality: `scrollToEnd`, `scrollToIndex`, and `scrollToItem` in addition to the normal `scrollToOffset`.
* Built-in pull to refresh support - set set the `onRefresh` and `refreshing` props.
* Rendering additional rows is usually done with low priority, after any interactions/animations complete, unless we're about to run out of rendered content. This should help apps feel more responsive.
* Component props replace render functions, e.g. `ItemComponent: ReactClass<{item: Item, index: number}>` replaces `renderRow: (...) => React.Element<*>`
* Supports dynamic items automatically by using `onLayout`, or `getItemLayout` can be provided for a perf boost and smoother `scrollToIndex` and scroll bar behavior.
* Visibility callback replaced with more powerful viewability callback and works in vertical and horizontal mode on at least Android and iOS, but probably other platforms as well. Extra power comes from the `viewablePercentThreshold` that lets the client decide when an item should be considered viewable.
Demo:
https://www.facebook.com/groups/576288835853049/permalink/753923058089625/
Reviewed By: yungsters
Differential Revision: D4412469
fbshipit-source-id: e2d891490bf76fe14df49294ecddf78a58adcf23
2017-02-04 18:25:32 +00:00
|
|
|
|
2017-02-28 10:08:53 +00:00
|
|
|
export type ViewabilityConfig = {|
|
2017-02-22 01:05:25 +00:00
|
|
|
/**
|
|
|
|
* Minimum amount of time (in milliseconds) that an item must be physically viewable before the
|
|
|
|
* viewability callback will be fired. A high number means that scrolling through content without
|
|
|
|
* stopping will not mark the content as viewable.
|
|
|
|
*/
|
2017-02-28 10:08:53 +00:00
|
|
|
minimumViewTime?: number,
|
2017-02-22 01:05:25 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Percent of viewport that must be covered for a partially occluded item to count as
|
|
|
|
* "viewable", 0-100. Fully visible items are always considered viewable. A value of 0 means
|
|
|
|
* that a single pixel in the viewport makes the item viewable, and a value of 100 means that
|
|
|
|
* an item must be either entirely visible or cover the entire viewport to count as viewable.
|
|
|
|
*/
|
|
|
|
viewAreaCoveragePercentThreshold?: number,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Similar to `viewAreaPercentThreshold`, but considers the percent of the item that is visible,
|
|
|
|
* rather than the fraction of the viewable area it covers.
|
|
|
|
*/
|
2017-02-27 09:05:38 +00:00
|
|
|
itemVisiblePercentThreshold?: number,
|
2017-02-22 01:05:25 +00:00
|
|
|
|
|
|
|
/**
|
2017-02-28 10:08:53 +00:00
|
|
|
* Nothing is considered viewable until the user scrolls or `recordInteraction` is called after
|
|
|
|
* render.
|
2017-02-22 01:05:25 +00:00
|
|
|
*/
|
|
|
|
waitForInteraction?: boolean,
|
2017-02-28 10:08:53 +00:00
|
|
|
|};
|
2017-02-22 01:05:25 +00:00
|
|
|
|
2016-04-01 20:53:54 +00:00
|
|
|
/**
|
2017-03-01 17:11:59 +00:00
|
|
|
* A Utility class for calculating viewable items based on current metrics like scroll position and
|
|
|
|
* layout.
|
|
|
|
*
|
2017-02-28 10:08:53 +00:00
|
|
|
* An item is said to be in a "viewable" state when any of the following
|
2017-03-16 23:36:57 +00:00
|
|
|
* is true for longer than `minimumViewTime` milliseconds (after an interaction if `waitForInteraction`
|
2017-02-28 10:08:53 +00:00
|
|
|
* is true):
|
|
|
|
*
|
|
|
|
* - Occupying >= `viewAreaCoveragePercentThreshold` of the view area XOR fraction of the item
|
|
|
|
* visible in the view area >= `itemVisiblePercentThreshold`.
|
2016-04-01 20:53:54 +00:00
|
|
|
* - Entirely visible on screen
|
|
|
|
*/
|
2017-02-22 01:05:25 +00:00
|
|
|
class ViewabilityHelper {
|
|
|
|
_config: ViewabilityConfig;
|
2017-02-28 10:08:53 +00:00
|
|
|
_hasInteracted: boolean = false;
|
|
|
|
_lastUpdateTime: number = 0;
|
|
|
|
_timers: Set<number> = new Set();
|
|
|
|
_viewableIndices: Array<number> = [];
|
2017-02-28 10:08:58 +00:00
|
|
|
_viewableItems: Map<string, ViewToken> = new Map();
|
2017-02-22 01:05:25 +00:00
|
|
|
|
|
|
|
constructor(config: ViewabilityConfig = {viewAreaCoveragePercentThreshold: 0}) {
|
|
|
|
this._config = config;
|
|
|
|
}
|
|
|
|
|
2017-03-01 17:11:59 +00:00
|
|
|
/**
|
|
|
|
* Cleanup, e.g. on unmount. Clears any pending timers.
|
|
|
|
*/
|
2017-02-28 10:08:53 +00:00
|
|
|
dispose() {
|
|
|
|
this._timers.forEach(clearTimeout);
|
2017-02-22 01:05:25 +00:00
|
|
|
}
|
|
|
|
|
2017-03-01 17:11:59 +00:00
|
|
|
/**
|
|
|
|
* Determines which items are viewable based on the current metrics and config.
|
|
|
|
*/
|
Better ListView - FlatList
Summary:
We really need a better list view - so here it is!
Main changes from existing `ListView`:
* Items are "virtualized" to limit memory - that is, items outside of the render window are unmounted and their memory is reclaimed. This means that instance state is not preserved when items scroll out of the render window.
* No `DataSource` - just a simple `data` prop of shape `Array<any>`. By default, they are expected to be of the shape `{key: string}` but a custom `rowExtractor` function can be provided for different shapes, e.g. graphql data where you want to map `id` to `key`. Note the underlying `VirtualizedList` is much more flexible.
* Fancy `scrollTo` functionality: `scrollToEnd`, `scrollToIndex`, and `scrollToItem` in addition to the normal `scrollToOffset`.
* Built-in pull to refresh support - set set the `onRefresh` and `refreshing` props.
* Rendering additional rows is usually done with low priority, after any interactions/animations complete, unless we're about to run out of rendered content. This should help apps feel more responsive.
* Component props replace render functions, e.g. `ItemComponent: ReactClass<{item: Item, index: number}>` replaces `renderRow: (...) => React.Element<*>`
* Supports dynamic items automatically by using `onLayout`, or `getItemLayout` can be provided for a perf boost and smoother `scrollToIndex` and scroll bar behavior.
* Visibility callback replaced with more powerful viewability callback and works in vertical and horizontal mode on at least Android and iOS, but probably other platforms as well. Extra power comes from the `viewablePercentThreshold` that lets the client decide when an item should be considered viewable.
Demo:
https://www.facebook.com/groups/576288835853049/permalink/753923058089625/
Reviewed By: yungsters
Differential Revision: D4412469
fbshipit-source-id: e2d891490bf76fe14df49294ecddf78a58adcf23
2017-02-04 18:25:32 +00:00
|
|
|
computeViewableItems(
|
|
|
|
itemCount: number,
|
|
|
|
scrollOffset: number,
|
|
|
|
viewportHeight: number,
|
|
|
|
getFrameMetrics: (index: number) => ?{length: number, offset: number},
|
|
|
|
renderRange?: {first: number, last: number}, // Optional optimization to reduce the scan size
|
2016-04-01 20:53:54 +00:00
|
|
|
): Array<number> {
|
2017-02-27 09:05:38 +00:00
|
|
|
const {itemVisiblePercentThreshold, viewAreaCoveragePercentThreshold} = this._config;
|
2017-02-22 01:05:25 +00:00
|
|
|
const viewAreaMode = viewAreaCoveragePercentThreshold != null;
|
|
|
|
const viewablePercentThreshold = viewAreaMode ?
|
|
|
|
viewAreaCoveragePercentThreshold :
|
2017-02-27 09:05:38 +00:00
|
|
|
itemVisiblePercentThreshold;
|
2017-02-22 01:05:25 +00:00
|
|
|
invariant(
|
|
|
|
viewablePercentThreshold != null &&
|
2017-02-27 09:05:38 +00:00
|
|
|
(itemVisiblePercentThreshold != null) !== (viewAreaCoveragePercentThreshold != null),
|
|
|
|
'Must set exactly one of itemVisiblePercentThreshold or viewAreaCoveragePercentThreshold',
|
2017-02-22 01:05:25 +00:00
|
|
|
);
|
Better ListView - FlatList
Summary:
We really need a better list view - so here it is!
Main changes from existing `ListView`:
* Items are "virtualized" to limit memory - that is, items outside of the render window are unmounted and their memory is reclaimed. This means that instance state is not preserved when items scroll out of the render window.
* No `DataSource` - just a simple `data` prop of shape `Array<any>`. By default, they are expected to be of the shape `{key: string}` but a custom `rowExtractor` function can be provided for different shapes, e.g. graphql data where you want to map `id` to `key`. Note the underlying `VirtualizedList` is much more flexible.
* Fancy `scrollTo` functionality: `scrollToEnd`, `scrollToIndex`, and `scrollToItem` in addition to the normal `scrollToOffset`.
* Built-in pull to refresh support - set set the `onRefresh` and `refreshing` props.
* Rendering additional rows is usually done with low priority, after any interactions/animations complete, unless we're about to run out of rendered content. This should help apps feel more responsive.
* Component props replace render functions, e.g. `ItemComponent: ReactClass<{item: Item, index: number}>` replaces `renderRow: (...) => React.Element<*>`
* Supports dynamic items automatically by using `onLayout`, or `getItemLayout` can be provided for a perf boost and smoother `scrollToIndex` and scroll bar behavior.
* Visibility callback replaced with more powerful viewability callback and works in vertical and horizontal mode on at least Android and iOS, but probably other platforms as well. Extra power comes from the `viewablePercentThreshold` that lets the client decide when an item should be considered viewable.
Demo:
https://www.facebook.com/groups/576288835853049/permalink/753923058089625/
Reviewed By: yungsters
Differential Revision: D4412469
fbshipit-source-id: e2d891490bf76fe14df49294ecddf78a58adcf23
2017-02-04 18:25:32 +00:00
|
|
|
const viewableIndices = [];
|
|
|
|
if (itemCount === 0) {
|
|
|
|
return viewableIndices;
|
|
|
|
}
|
2016-05-19 21:44:39 +00:00
|
|
|
let firstVisible = -1;
|
Better ListView - FlatList
Summary:
We really need a better list view - so here it is!
Main changes from existing `ListView`:
* Items are "virtualized" to limit memory - that is, items outside of the render window are unmounted and their memory is reclaimed. This means that instance state is not preserved when items scroll out of the render window.
* No `DataSource` - just a simple `data` prop of shape `Array<any>`. By default, they are expected to be of the shape `{key: string}` but a custom `rowExtractor` function can be provided for different shapes, e.g. graphql data where you want to map `id` to `key`. Note the underlying `VirtualizedList` is much more flexible.
* Fancy `scrollTo` functionality: `scrollToEnd`, `scrollToIndex`, and `scrollToItem` in addition to the normal `scrollToOffset`.
* Built-in pull to refresh support - set set the `onRefresh` and `refreshing` props.
* Rendering additional rows is usually done with low priority, after any interactions/animations complete, unless we're about to run out of rendered content. This should help apps feel more responsive.
* Component props replace render functions, e.g. `ItemComponent: ReactClass<{item: Item, index: number}>` replaces `renderRow: (...) => React.Element<*>`
* Supports dynamic items automatically by using `onLayout`, or `getItemLayout` can be provided for a perf boost and smoother `scrollToIndex` and scroll bar behavior.
* Visibility callback replaced with more powerful viewability callback and works in vertical and horizontal mode on at least Android and iOS, but probably other platforms as well. Extra power comes from the `viewablePercentThreshold` that lets the client decide when an item should be considered viewable.
Demo:
https://www.facebook.com/groups/576288835853049/permalink/753923058089625/
Reviewed By: yungsters
Differential Revision: D4412469
fbshipit-source-id: e2d891490bf76fe14df49294ecddf78a58adcf23
2017-02-04 18:25:32 +00:00
|
|
|
const {first, last} = renderRange || {first: 0, last: itemCount - 1};
|
|
|
|
invariant(
|
|
|
|
last < itemCount,
|
|
|
|
'Invalid render range ' + JSON.stringify({renderRange, itemCount})
|
|
|
|
);
|
|
|
|
for (let idx = first; idx <= last; idx++) {
|
|
|
|
const metrics = getFrameMetrics(idx);
|
|
|
|
if (!metrics) {
|
2016-04-01 20:53:54 +00:00
|
|
|
continue;
|
|
|
|
}
|
Better ListView - FlatList
Summary:
We really need a better list view - so here it is!
Main changes from existing `ListView`:
* Items are "virtualized" to limit memory - that is, items outside of the render window are unmounted and their memory is reclaimed. This means that instance state is not preserved when items scroll out of the render window.
* No `DataSource` - just a simple `data` prop of shape `Array<any>`. By default, they are expected to be of the shape `{key: string}` but a custom `rowExtractor` function can be provided for different shapes, e.g. graphql data where you want to map `id` to `key`. Note the underlying `VirtualizedList` is much more flexible.
* Fancy `scrollTo` functionality: `scrollToEnd`, `scrollToIndex`, and `scrollToItem` in addition to the normal `scrollToOffset`.
* Built-in pull to refresh support - set set the `onRefresh` and `refreshing` props.
* Rendering additional rows is usually done with low priority, after any interactions/animations complete, unless we're about to run out of rendered content. This should help apps feel more responsive.
* Component props replace render functions, e.g. `ItemComponent: ReactClass<{item: Item, index: number}>` replaces `renderRow: (...) => React.Element<*>`
* Supports dynamic items automatically by using `onLayout`, or `getItemLayout` can be provided for a perf boost and smoother `scrollToIndex` and scroll bar behavior.
* Visibility callback replaced with more powerful viewability callback and works in vertical and horizontal mode on at least Android and iOS, but probably other platforms as well. Extra power comes from the `viewablePercentThreshold` that lets the client decide when an item should be considered viewable.
Demo:
https://www.facebook.com/groups/576288835853049/permalink/753923058089625/
Reviewed By: yungsters
Differential Revision: D4412469
fbshipit-source-id: e2d891490bf76fe14df49294ecddf78a58adcf23
2017-02-04 18:25:32 +00:00
|
|
|
const top = metrics.offset - scrollOffset;
|
|
|
|
const bottom = top + metrics.length;
|
2016-04-01 20:53:54 +00:00
|
|
|
if ((top < viewportHeight) && (bottom > 0)) {
|
|
|
|
firstVisible = idx;
|
|
|
|
if (_isViewable(
|
2017-02-22 01:05:25 +00:00
|
|
|
viewAreaMode,
|
2016-04-01 20:53:54 +00:00
|
|
|
viewablePercentThreshold,
|
|
|
|
top,
|
|
|
|
bottom,
|
2017-02-22 01:05:25 +00:00
|
|
|
viewportHeight,
|
|
|
|
metrics.length,
|
2016-04-01 20:53:54 +00:00
|
|
|
)) {
|
Better ListView - FlatList
Summary:
We really need a better list view - so here it is!
Main changes from existing `ListView`:
* Items are "virtualized" to limit memory - that is, items outside of the render window are unmounted and their memory is reclaimed. This means that instance state is not preserved when items scroll out of the render window.
* No `DataSource` - just a simple `data` prop of shape `Array<any>`. By default, they are expected to be of the shape `{key: string}` but a custom `rowExtractor` function can be provided for different shapes, e.g. graphql data where you want to map `id` to `key`. Note the underlying `VirtualizedList` is much more flexible.
* Fancy `scrollTo` functionality: `scrollToEnd`, `scrollToIndex`, and `scrollToItem` in addition to the normal `scrollToOffset`.
* Built-in pull to refresh support - set set the `onRefresh` and `refreshing` props.
* Rendering additional rows is usually done with low priority, after any interactions/animations complete, unless we're about to run out of rendered content. This should help apps feel more responsive.
* Component props replace render functions, e.g. `ItemComponent: ReactClass<{item: Item, index: number}>` replaces `renderRow: (...) => React.Element<*>`
* Supports dynamic items automatically by using `onLayout`, or `getItemLayout` can be provided for a perf boost and smoother `scrollToIndex` and scroll bar behavior.
* Visibility callback replaced with more powerful viewability callback and works in vertical and horizontal mode on at least Android and iOS, but probably other platforms as well. Extra power comes from the `viewablePercentThreshold` that lets the client decide when an item should be considered viewable.
Demo:
https://www.facebook.com/groups/576288835853049/permalink/753923058089625/
Reviewed By: yungsters
Differential Revision: D4412469
fbshipit-source-id: e2d891490bf76fe14df49294ecddf78a58adcf23
2017-02-04 18:25:32 +00:00
|
|
|
viewableIndices.push(idx);
|
2016-04-01 20:53:54 +00:00
|
|
|
}
|
|
|
|
} else if (firstVisible >= 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
Better ListView - FlatList
Summary:
We really need a better list view - so here it is!
Main changes from existing `ListView`:
* Items are "virtualized" to limit memory - that is, items outside of the render window are unmounted and their memory is reclaimed. This means that instance state is not preserved when items scroll out of the render window.
* No `DataSource` - just a simple `data` prop of shape `Array<any>`. By default, they are expected to be of the shape `{key: string}` but a custom `rowExtractor` function can be provided for different shapes, e.g. graphql data where you want to map `id` to `key`. Note the underlying `VirtualizedList` is much more flexible.
* Fancy `scrollTo` functionality: `scrollToEnd`, `scrollToIndex`, and `scrollToItem` in addition to the normal `scrollToOffset`.
* Built-in pull to refresh support - set set the `onRefresh` and `refreshing` props.
* Rendering additional rows is usually done with low priority, after any interactions/animations complete, unless we're about to run out of rendered content. This should help apps feel more responsive.
* Component props replace render functions, e.g. `ItemComponent: ReactClass<{item: Item, index: number}>` replaces `renderRow: (...) => React.Element<*>`
* Supports dynamic items automatically by using `onLayout`, or `getItemLayout` can be provided for a perf boost and smoother `scrollToIndex` and scroll bar behavior.
* Visibility callback replaced with more powerful viewability callback and works in vertical and horizontal mode on at least Android and iOS, but probably other platforms as well. Extra power comes from the `viewablePercentThreshold` that lets the client decide when an item should be considered viewable.
Demo:
https://www.facebook.com/groups/576288835853049/permalink/753923058089625/
Reviewed By: yungsters
Differential Revision: D4412469
fbshipit-source-id: e2d891490bf76fe14df49294ecddf78a58adcf23
2017-02-04 18:25:32 +00:00
|
|
|
return viewableIndices;
|
2017-02-22 01:05:25 +00:00
|
|
|
}
|
|
|
|
|
2017-03-01 17:11:59 +00:00
|
|
|
/**
|
|
|
|
* Figures out which items are viewable and how that has changed from before and calls
|
|
|
|
* `onViewableItemsChanged` as appropriate.
|
|
|
|
*/
|
2017-02-22 01:05:25 +00:00
|
|
|
onUpdate(
|
|
|
|
itemCount: number,
|
|
|
|
scrollOffset: number,
|
|
|
|
viewportHeight: number,
|
|
|
|
getFrameMetrics: (index: number) => ?{length: number, offset: number},
|
2017-02-28 10:08:58 +00:00
|
|
|
createViewToken: (index: number, isViewable: boolean) => ViewToken,
|
|
|
|
onViewableItemsChanged: ({viewableItems: Array<ViewToken>, changed: Array<ViewToken>}) => void,
|
2017-02-22 01:05:25 +00:00
|
|
|
renderRange?: {first: number, last: number}, // Optional optimization to reduce the scan size
|
|
|
|
): void {
|
2017-02-28 10:08:53 +00:00
|
|
|
const updateTime = Date.now();
|
2017-03-16 23:36:57 +00:00
|
|
|
if (this._lastUpdateTime === 0 && itemCount > 0 && getFrameMetrics(0)) {
|
2017-02-28 10:08:53 +00:00
|
|
|
// Only count updates after the first item is rendered and has a frame.
|
|
|
|
this._lastUpdateTime = updateTime;
|
|
|
|
}
|
|
|
|
const updateElapsed = this._lastUpdateTime ? updateTime - this._lastUpdateTime : 0;
|
|
|
|
if (this._config.waitForInteraction && !this._hasInteracted) {
|
|
|
|
return;
|
|
|
|
}
|
2017-02-22 01:05:25 +00:00
|
|
|
let viewableIndices = [];
|
|
|
|
if (itemCount) {
|
|
|
|
viewableIndices = this.computeViewableItems(
|
|
|
|
itemCount,
|
|
|
|
scrollOffset,
|
|
|
|
viewportHeight,
|
|
|
|
getFrameMetrics,
|
|
|
|
renderRange,
|
|
|
|
);
|
|
|
|
}
|
2017-02-28 10:08:53 +00:00
|
|
|
if (this._viewableIndices.length === viewableIndices.length &&
|
|
|
|
this._viewableIndices.every((v, ii) => v === viewableIndices[ii])) {
|
|
|
|
// We might get a lot of scroll events where visibility doesn't change and we don't want to do
|
|
|
|
// extra work in those cases.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this._viewableIndices = viewableIndices;
|
|
|
|
this._lastUpdateTime = updateTime;
|
2017-03-16 23:36:57 +00:00
|
|
|
if (this._config.minimumViewTime && updateElapsed < this._config.minimumViewTime) {
|
2017-02-28 10:08:53 +00:00
|
|
|
const handle = setTimeout(
|
|
|
|
() => {
|
|
|
|
this._timers.delete(handle);
|
2017-02-28 10:08:58 +00:00
|
|
|
this._onUpdateSync(viewableIndices, onViewableItemsChanged, createViewToken);
|
2017-02-28 10:08:53 +00:00
|
|
|
},
|
2017-03-16 23:36:57 +00:00
|
|
|
this._config.minimumViewTime,
|
2017-02-28 10:08:53 +00:00
|
|
|
);
|
|
|
|
this._timers.add(handle);
|
|
|
|
} else {
|
2017-02-28 10:08:58 +00:00
|
|
|
this._onUpdateSync(viewableIndices, onViewableItemsChanged, createViewToken);
|
2017-02-28 10:08:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-01 17:11:59 +00:00
|
|
|
/**
|
|
|
|
* Records that an interaction has happened even if there has been no scroll.
|
|
|
|
*/
|
2017-02-28 10:08:53 +00:00
|
|
|
recordInteraction() {
|
|
|
|
this._hasInteracted = true;
|
|
|
|
}
|
|
|
|
|
2017-02-28 10:08:58 +00:00
|
|
|
_onUpdateSync(viewableIndicesToCheck, onViewableItemsChanged, createViewToken) {
|
2017-02-28 10:08:53 +00:00
|
|
|
// Filter out indices that have gone out of view since this call was scheduled.
|
|
|
|
viewableIndicesToCheck = viewableIndicesToCheck.filter(
|
|
|
|
(ii) => this._viewableIndices.includes(ii)
|
|
|
|
);
|
2017-02-22 01:05:25 +00:00
|
|
|
const prevItems = this._viewableItems;
|
|
|
|
const nextItems = new Map(
|
2017-02-28 10:08:53 +00:00
|
|
|
viewableIndicesToCheck.map(ii => {
|
2017-02-28 10:08:58 +00:00
|
|
|
const viewable = createViewToken(ii, true);
|
2017-02-22 01:05:25 +00:00
|
|
|
return [viewable.key, viewable];
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
const changed = [];
|
|
|
|
for (const [key, viewable] of nextItems) {
|
|
|
|
if (!prevItems.has(key)) {
|
|
|
|
changed.push(viewable);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (const [key, viewable] of prevItems) {
|
|
|
|
if (!nextItems.has(key)) {
|
|
|
|
changed.push({...viewable, isViewable: false});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (changed.length > 0) {
|
|
|
|
this._viewableItems = nextItems;
|
2017-02-28 10:08:53 +00:00
|
|
|
onViewableItemsChanged({viewableItems: Array.from(nextItems.values()), changed});
|
2017-02-22 01:05:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-04-01 20:53:54 +00:00
|
|
|
|
Better ListView - FlatList
Summary:
We really need a better list view - so here it is!
Main changes from existing `ListView`:
* Items are "virtualized" to limit memory - that is, items outside of the render window are unmounted and their memory is reclaimed. This means that instance state is not preserved when items scroll out of the render window.
* No `DataSource` - just a simple `data` prop of shape `Array<any>`. By default, they are expected to be of the shape `{key: string}` but a custom `rowExtractor` function can be provided for different shapes, e.g. graphql data where you want to map `id` to `key`. Note the underlying `VirtualizedList` is much more flexible.
* Fancy `scrollTo` functionality: `scrollToEnd`, `scrollToIndex`, and `scrollToItem` in addition to the normal `scrollToOffset`.
* Built-in pull to refresh support - set set the `onRefresh` and `refreshing` props.
* Rendering additional rows is usually done with low priority, after any interactions/animations complete, unless we're about to run out of rendered content. This should help apps feel more responsive.
* Component props replace render functions, e.g. `ItemComponent: ReactClass<{item: Item, index: number}>` replaces `renderRow: (...) => React.Element<*>`
* Supports dynamic items automatically by using `onLayout`, or `getItemLayout` can be provided for a perf boost and smoother `scrollToIndex` and scroll bar behavior.
* Visibility callback replaced with more powerful viewability callback and works in vertical and horizontal mode on at least Android and iOS, but probably other platforms as well. Extra power comes from the `viewablePercentThreshold` that lets the client decide when an item should be considered viewable.
Demo:
https://www.facebook.com/groups/576288835853049/permalink/753923058089625/
Reviewed By: yungsters
Differential Revision: D4412469
fbshipit-source-id: e2d891490bf76fe14df49294ecddf78a58adcf23
2017-02-04 18:25:32 +00:00
|
|
|
|
2016-04-01 20:53:54 +00:00
|
|
|
function _isViewable(
|
2017-02-22 01:05:25 +00:00
|
|
|
viewAreaMode: boolean,
|
2016-04-01 20:53:54 +00:00
|
|
|
viewablePercentThreshold: number,
|
|
|
|
top: number,
|
|
|
|
bottom: number,
|
2017-02-22 01:05:25 +00:00
|
|
|
viewportHeight: number,
|
|
|
|
itemLength: number,
|
2016-04-01 20:53:54 +00:00
|
|
|
): bool {
|
2017-02-22 01:05:25 +00:00
|
|
|
if (_isEntirelyVisible(top, bottom, viewportHeight)) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
const pixels = _getPixelsVisible(top, bottom, viewportHeight);
|
|
|
|
const percent = 100 * (viewAreaMode ? pixels / viewportHeight : pixels / itemLength);
|
|
|
|
return percent >= viewablePercentThreshold;
|
|
|
|
}
|
2016-04-01 20:53:54 +00:00
|
|
|
}
|
|
|
|
|
2017-02-22 01:05:25 +00:00
|
|
|
function _getPixelsVisible(
|
2016-04-01 20:53:54 +00:00
|
|
|
top: number,
|
|
|
|
bottom: number,
|
|
|
|
viewportHeight: number
|
|
|
|
): number {
|
2017-02-22 01:05:25 +00:00
|
|
|
const visibleHeight = Math.min(bottom, viewportHeight) - Math.max(top, 0);
|
|
|
|
return Math.max(0, visibleHeight);
|
2016-04-01 20:53:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function _isEntirelyVisible(
|
|
|
|
top: number,
|
|
|
|
bottom: number,
|
|
|
|
viewportHeight: number
|
|
|
|
): bool {
|
|
|
|
return top >= 0 && bottom <= viewportHeight && bottom > top;
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = ViewabilityHelper;
|