Skip to content

Commit

Permalink
Create recomputeViewableItems method
Browse files Browse the repository at this point in the history
  • Loading branch information
Paduado committed Jul 31, 2024
1 parent 86f75b2 commit eec4cf7
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ and adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]

- Add recomputeViewableItems method
- https://github.com/Shopify/flash-list/pull/1296

## [1.7.0] - 2024-07-03

- Update internal dependency and fixture app to `react-native@0.72`.
Expand Down
8 changes: 8 additions & 0 deletions documentation/docs/fundamentals/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,14 @@ recordInteraction();

Tells the list an interaction has occurred, which should trigger viewability calculations, e.g. if `waitForInteractions` is true and the user has not scrolled. You should typically call `recordInteraction()` when user for example taps on an item or invokes a navigation action.

### `recomputeViewableItems()`

```tsx
recomputeViewableItems();
```

Retriggers viewability calculations. Useful to imperatively trigger viewability calculations.

### `scrollToEnd()`

```tsx
Expand Down
7 changes: 7 additions & 0 deletions src/FlashList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -870,6 +870,13 @@ class FlashList<T> extends React.PureComponent<
public recordInteraction = () => {
this.viewabilityManager.recordInteraction();
};

/**
* Retriggers viewability calculations. Useful to imperatively trigger viewability calculations.
*/
public recomputeViewableItems = () => {
this.viewabilityManager.recomputeViewableItems();
};
}

export default FlashList;
4 changes: 4 additions & 0 deletions src/viewability/ViewabilityHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ class ViewabilityHelper {
}
}

public clearLastReportedViewableIndices() {
this.lastReportedViewableIndices = [];
}

private isItemViewable(
index: number,
horizontal: boolean,
Expand Down
8 changes: 8 additions & 0 deletions src/viewability/ViewabilityManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ export default class ViewabilityManager<T> {
});
};

public recomputeViewableItems = () => {
this.viewabilityHelpers.forEach((viewabilityHelper) =>
viewabilityHelper.clearLastReportedViewableIndices()
);

this.updateViewableItems();
};

/**
* Creates a new `ViewabilityHelper` instance with `onViewableItemsChanged` callback and `ViewabilityConfig`
* @returns `ViewabilityHelper` instance
Expand Down

0 comments on commit eec4cf7

Please sign in to comment.