Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Boundary Processing #298

Merged
merged 11 commits into from
Feb 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/guides/sticky/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ _setRef(recycler) {
this._recyclerRef = recycler;
}
```
* If using `overrideRowRenderer`, keep in mind that upon scrolling to the very top or bottom of the content, stickies will be hidden. eg. If the first item in the list is given as sticky, scrolling to the top will display the original view and not the overridden view.

### 2) Props
* `stickyHeaderIndices` - An array of indices whose corresponding items need to be stuck to the top of the RecyclerListView once the items scroll off the top. Every subsequent sticky index view will push the previous sticky view off the top to take its place. Needs to be sorted ascending.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "recyclerlistview",
"version": "1.4.0-beta.sticky.15",
"version": "1.4.0-beta.sticky.16",
"description": "The listview that you need and deserve. It was built for performance, uses cell recycling to achieve smooth scrolling.",
"main": "dist/reactnative/index.js",
"types": "dist/reactnative/index.d.ts",
Expand Down
12 changes: 5 additions & 7 deletions src/core/RecyclerListView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,11 @@ import { TOnItemStatusChanged } from "./ViewabilityTracker";
import VirtualRenderer, { RenderStack, RenderStackItem, RenderStackParams } from "./VirtualRenderer";
import ItemAnimator, { BaseItemAnimator } from "./ItemAnimator";
import { DebugHandlers } from "..";

//#if [REACT-NATIVE]
import ScrollComponent from "../platform/reactnative/scrollcomponent/ScrollComponent";
import ViewRenderer from "../platform/reactnative/viewrenderer/ViewRenderer";
import { DefaultJSItemAnimator as DefaultItemAnimator } from "../platform/reactnative/itemanimators/defaultjsanimator/DefaultJSItemAnimator";
import { Platform } from "react-native";

const IS_WEB = !Platform || Platform.OS === "web";
//#endif

Expand Down Expand Up @@ -91,7 +89,6 @@ export interface RecyclerListViewProps {
onScroll?: (rawEvent: ScrollEvent, offsetX: number, offsetY: number) => void;
onRecreate?: (params: OnRecreateParams) => void;
onEndReached?: () => void;
onVisibleEndReached?: () => void;
onEndReachedThreshold?: number;
onVisibleIndexesChanged?: TOnItemStatusChanged;
onVisibleIndicesChanged?: TOnItemStatusChanged;
Expand Down Expand Up @@ -323,6 +320,10 @@ export default class RecyclerListView<P extends RecyclerListViewProps, S extends
return this._layout;
}

public getContentDimension(): Dimension {
return this._virtualRenderer.getLayoutDimension();
}

public render(): JSX.Element {
//TODO:Talha
// const {
Expand Down Expand Up @@ -571,7 +572,7 @@ export default class RecyclerListView<P extends RecyclerListViewProps, S extends
}

private _processOnEndReached(): void {
if ((this.props.onEndReached || this.props.onVisibleEndReached) && this._virtualRenderer) {
if (this.props.onEndReached && this._virtualRenderer) {
const layout = this._virtualRenderer.getLayoutDimension();
const viewabilityTracker = this._virtualRenderer.getViewabilityTracker();
if (viewabilityTracker) {
Expand All @@ -583,9 +584,6 @@ export default class RecyclerListView<P extends RecyclerListViewProps, S extends
this._onEndReachedCalled = true;
this.props.onEndReached();
}
if (this.props.onVisibleEndReached && endReachedMargin <= 0) {
this.props.onVisibleEndReached();
}
} else {
this._onEndReachedCalled = false;
}
Expand Down
51 changes: 32 additions & 19 deletions src/core/StickyContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,18 @@ export interface StickyContainerProps {
overrideRowRenderer?: (type: string | number | undefined, data: any, index: number, extendedState?: object) => JSX.Element | JSX.Element[] | null;
style?: StyleProp<ViewStyle>;
}
export interface StickyContainerState {
topVisible: boolean;
}
export interface RecyclerChild extends React.ReactElement<RecyclerListViewProps> {
ref: (recyclerRef: any) => {};
props: RecyclerListViewProps;
}
export default class StickyContainer<P extends StickyContainerProps, S extends StickyContainerState> extends React.Component<P, S> {
export default class StickyContainer<P extends StickyContainerProps> extends React.Component<P> {
public static propTypes = {};
private _recyclerRef: RecyclerListView<RecyclerListViewProps, RecyclerListViewState> | undefined = undefined;
private _dataProvider: DataProvider;
private _layoutProvider: BaseLayoutProvider;
private _extendedState: object | undefined;
private _rowRenderer: ((type: string | number, data: any, index: number, extendedState?: object) => JSX.Element | JSX.Element[] | null);
private _distanceFromWindow: number;

private _stickyHeaderRef: StickyHeader<StickyObjectProps, StickyObjectState> | null = null;
private _stickyFooterRef: StickyFooter<StickyObjectProps, StickyObjectState> | null = null;
Expand All @@ -46,15 +44,16 @@ export default class StickyContainer<P extends StickyContainerProps, S extends S
constructor(props: P, context?: any) {
super(props, context);
this._assertChildType();
const childProps: RecyclerListViewProps = this.props.children.props;
this._dataProvider = childProps.dataProvider;
const childProps: RecyclerListViewProps = props.children.props;
this._dataProvider = childProps.dataProvider;
this._layoutProvider = childProps.layoutProvider;
this._extendedState = childProps.extendedState;
this._rowRenderer = childProps.rowRenderer;
this.state = {
topVisible: false,
} as S;
this._distanceFromWindow = childProps.distanceFromWindow ? childProps.distanceFromWindow : 0;
}

public componentWillReceiveProps(newProps: P): void {
this._initParams(newProps);
}

public render(): JSX.Element {
Expand All @@ -64,7 +63,6 @@ export default class StickyContainer<P extends StickyContainerProps, S extends S
ref: this._getRecyclerRef,
onVisibleIndicesChanged: this._onVisibleIndicesChanged,
onScroll: this._onScroll,
onVisibleEndReached: this._onEndReached,
});
return (
<View style={this.props.style ? this.props.style : {flex: 1}}>
Expand All @@ -77,7 +75,9 @@ export default class StickyContainer<P extends StickyContainerProps, S extends S
getLayoutTypeForIndex={this._getLayoutTypeForIndex}
getExtendedState={this._getExtendedState}
getRLVRenderedSize={this._getRLVRenderedSize}
getContentDimension={this._getContentDimension}
getRowRenderer={this._getRowRenderer}
getDistanceFromWindow={this._getDistanceFromWindow}
overrideRowRenderer={this.props.overrideRowRenderer}/>
) : null}
{this.props.stickyFooterIndices ? (
Expand All @@ -88,7 +88,9 @@ export default class StickyContainer<P extends StickyContainerProps, S extends S
getLayoutTypeForIndex={this._getLayoutTypeForIndex}
getExtendedState={this._getExtendedState}
getRLVRenderedSize={this._getRLVRenderedSize}
getContentDimension={this._getContentDimension}
getRowRenderer={this._getRowRenderer}
getDistanceFromWindow={this._getDistanceFromWindow}
overrideRowRenderer={this.props.overrideRowRenderer}/>
) : null}
</View>
Expand Down Expand Up @@ -142,9 +144,6 @@ export default class StickyContainer<P extends StickyContainerProps, S extends S
private _onScroll = (rawEvent: ScrollEvent, offsetX: number, offsetY: number) => {
if (this._stickyHeaderRef) {
this._stickyHeaderRef.onScroll(offsetY);
if (offsetY === 0) {
this._stickyHeaderRef.onStartReached();
}
}
if (this._stickyFooterRef) {
this._stickyFooterRef.onScroll(offsetY);
Expand All @@ -154,12 +153,6 @@ export default class StickyContainer<P extends StickyContainerProps, S extends S
}
}

private _onEndReached = () => {
if (this._stickyFooterRef) {
this._stickyFooterRef.onEndReached();
}
}

private _assertChildType = (): void => {
if (React.Children.count(this.props.children) !== 1 || !this._isChildRecyclerInstance()) {
throw new CustomError(RecyclerListViewExceptions.wrongStickyChildTypeException);
Expand Down Expand Up @@ -204,6 +197,26 @@ export default class StickyContainer<P extends StickyContainerProps, S extends S
}
return undefined;
}

private _getContentDimension = (): Dimension | undefined => {
if (this._recyclerRef) {
return this._recyclerRef.getContentDimension();
}
return undefined;
}

private _getDistanceFromWindow = (): number => {
return this._distanceFromWindow;
}

private _initParams = (props: P) => {
const childProps: RecyclerListViewProps = props.children.props;
this._dataProvider = childProps.dataProvider;
this._layoutProvider = childProps.layoutProvider;
this._extendedState = childProps.extendedState;
this._rowRenderer = childProps.rowRenderer;
this._distanceFromWindow = childProps.distanceFromWindow ? childProps.distanceFromWindow : 0;
}
}

StickyContainer.propTypes = {
Expand Down
38 changes: 17 additions & 21 deletions src/core/sticky/StickyFooter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,24 @@ import StickyObject, {StickyObjectProps, StickyObjectState, StickyType} from "./
import BinarySearch, {ValueAndIndex} from "../../utils/BinarySearch";

export default class StickyFooter<P extends StickyObjectProps, S extends StickyObjectState> extends StickyObject<P, S> {
private _initialOnScroll: boolean = true;

constructor(props: P, context?: any) {
super(props, context);
}

public onEndReached(): void {
this._stickyViewVisible(false);
this.boundaryReached = true;
}

protected boundaryProcessing(): void {
if (this.boundaryReached && !this._initialOnScroll) {
this.boundaryReached = false;
this.onVisibleIndicesChanged(this.visibleIndices);
}
this._initialOnScroll = false;
}

protected initStickyParams(_offsetY: number): void {
protected initStickyParams(): void {
this.stickyType = StickyType.FOOTER;
this.stickyTypeMultiplier = -1;
this.containerPosition = {bottom: 0};
this.bounceScrolling = false;
}

protected calculateVisibleStickyIndex(
stickyIndices: number[] | undefined, _smallestVisibleIndex: number, largestVisibleIndex: number, _offsetY: number,
stickyIndices: number[] | undefined, _smallestVisibleIndex: number, largestVisibleIndex: number,
offsetY: number, distanceFromWindow: number, windowBound ?: number,
): void {
if (stickyIndices && largestVisibleIndex && !this.boundaryReached) {
if (largestVisibleIndex > stickyIndices[stickyIndices.length - 1]) {
if (stickyIndices && largestVisibleIndex) {
this.bounceScrolling = this.hasReachedBoundary(offsetY, distanceFromWindow, windowBound);
if (largestVisibleIndex > stickyIndices[stickyIndices.length - 1] || this.bounceScrolling) {
this.stickyVisiblity = false;
} else {
this.stickyVisiblity = true;
Expand All @@ -58,7 +46,15 @@ export default class StickyFooter<P extends StickyObjectProps, S extends StickyO
return -1 * (currentY + currentHeight);
}

protected getScrollY(offsetY: number, scrollableHeight: number): number | null {
return scrollableHeight ? -1 * (offsetY + scrollableHeight) : null;
protected getScrollY(offsetY: number, scrollableHeight: number): number | undefined {
return scrollableHeight ? -1 * (offsetY + scrollableHeight) : undefined;
}

protected hasReachedBoundary(offsetY: number, distanceFromWindow: number, windowBound?: number): boolean {
if (windowBound) {
const endReachedMargin = Math.round(offsetY - (windowBound + distanceFromWindow));
return endReachedMargin >= 0;
}
return false;
}
}
32 changes: 13 additions & 19 deletions src/core/sticky/StickyHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,22 @@ export default class StickyHeader<P extends StickyObjectProps, S extends StickyO
super(props, context);
}

public onStartReached(): void {
this._stickyViewVisible(false);
this.boundaryReached = true;
}

protected boundaryProcessing(): void {
if (this.boundaryReached) {
this.boundaryReached = false;
this.onVisibleIndicesChanged(this.visibleIndices);
}
}

protected initStickyParams(offsetY: number): void {
protected initStickyParams(): void {
this.stickyType = StickyType.HEADER;
this.stickyTypeMultiplier = 1;
this.containerPosition = {top: 0};
if (offsetY === 0) {
this.boundaryReached = true;
}

// Kept as true contrary to as in StickyFooter because in case of initialOffset not given, onScroll isn't called and boundaryProcessing isn't done.
// Default behaviour in that case will be sticky header hidden.
this.bounceScrolling = true;
}

protected calculateVisibleStickyIndex(
stickyIndices: number[] | undefined, smallestVisibleIndex: number, largestVisibleIndex: number, offsetY: number,
stickyIndices: number[] | undefined, smallestVisibleIndex: number, largestVisibleIndex: number, offsetY: number, distanceFromWindow: number,
): void {
if (stickyIndices && smallestVisibleIndex !== undefined) {
if (smallestVisibleIndex < stickyIndices[0] || offsetY === 0) {
this.bounceScrolling = this.hasReachedBoundary(offsetY, distanceFromWindow);
if (smallestVisibleIndex < stickyIndices[0] || this.bounceScrolling) {
this.stickyVisiblity = false;
} else {
this.stickyVisiblity = true;
Expand All @@ -58,7 +48,11 @@ export default class StickyHeader<P extends StickyObjectProps, S extends StickyO
return currentY;
}

protected getScrollY(offsetY: number, scrollableHeight: number): number | null {
protected getScrollY(offsetY: number, scrollableHeight: number): number | undefined {
return offsetY;
}

protected hasReachedBoundary(offsetY: number, distanceFromWindow: number, _windowBound?: number): boolean {
return offsetY <= distanceFromWindow;
}
}
Loading