Skip to content

Commit

Permalink
feat: support for min index
Browse files Browse the repository at this point in the history
  • Loading branch information
friedolinfoerder committed Aug 24, 2022
1 parent 1dbbea3 commit 0218c71
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ public void setShiftOffset(double shiftOffset) {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
super.onLayoutChange(v, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom);
int shiftHeight = Math.min(bottom - oldBottom, (int)mShiftHeight);
int scrollWindowHeight = getHeight() - getPaddingBottom() - getPaddingTop();
if(mShiftHeight != 0 && mShiftOffset <= getScrollY() + scrollWindowHeight / 2) {
if(mShiftHeight != 0 && shiftHeight > 0 && mShiftOffset <= getScrollY() + scrollWindowHeight / 2) {
// correct
scrollTo(0, getScrollY() + (int)mShiftHeight);
scrollTo(0, getScrollY() + shiftHeight);
if(getOverScrollerFromParent() != null && !getOverScrollerFromParent().isFinished()) {
// get current directed velocity from scroller
int direction = getOverScrollerFromParent().getFinalY() - getOverScrollerFromParent().getStartY() > 0 ? 1 : -1;
Expand Down
3 changes: 2 additions & 1 deletion src/FlatList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import { FlatList as FlatListRN, LayoutChangeEvent, Platform, ScrollViewProps, S
import { ScrollView } from './ScrollView';
import { usePrerenderedData } from './hooks/usePrerenderedData';
import type { BidirectionalFlatListProps, FlatListType } from './types';
import { MIN_INDEX } from './config';

const maintainVisibleContentPosition = { minIndexForVisible: 1 };
const maintainVisibleContentPosition = { minIndexForVisible: MIN_INDEX };

const FlatListImpl = forwardRef<FlatListType, BidirectionalFlatListProps>((props, ref) => {
const renderScrollComponent = useCallback((props: ScrollViewProps) => {
Expand Down
1 change: 1 addition & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const MIN_INDEX = 1;
29 changes: 17 additions & 12 deletions src/hooks/usePrerenderedData.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { MutableRefObject, ReactNode, useCallback, useEffect, useRef, useState } from 'react';
import { FlatListProps, LayoutChangeEvent, View } from 'react-native';
import type { FlatListType, OnUpdateData, RenderItem } from '../types';
import { MIN_INDEX } from '../config';

type OnLayout = (options: {id: string; height: number}) => void;

Expand Down Expand Up @@ -41,13 +42,15 @@ export const usePrerenderedData = ({data, keyExtractor, renderItem, scrollRef, o
return p;
}, {});
const index = data.findIndex((d) => d === newD[0]);
const shift = {
height: newD.reduce((p, c, i) => p + heightsRef.current[keyExtractor(c, i)], 0),
offset: data.slice(0, index).reduce((p, c, i) => p + heightsRef.current[keyExtractor(c, i)], 0),
};
scrollRef.current?.shift(shift);
if(index > 0 && index <= MIN_INDEX) {
const shift = {
height: newD.reduce((p, c, i) => p + heightsRef.current[keyExtractor(c, i)], 0),
offset: data.slice(0, index).reduce((p, c, i) => p + heightsRef.current[keyExtractor(c, i)], 0),
};
scrollRef.current?.shift(shift);
onUpdateData?.({heights: heightsRef.current, ...shift});
}
setFinalData(data);
onUpdateData?.({heights: heightsRef.current, ...shift});
return;
}
setNewData(data.filter((d, i) => !heightsRef.current[keyExtractor(d, i)]));
Expand All @@ -67,14 +70,16 @@ export const usePrerenderedData = ({data, keyExtractor, renderItem, scrollRef, o
return;
}
const index = data.findIndex((d) => d === newData[0]);
const shift = {
height: newData.reduce((p, c, i) => p + heightsRef.current[keyExtractor(c, i)], 0),
offset: data.slice(0, index).reduce((p, c, i) => p + heightsRef.current[keyExtractor(c, i)], 0),
};
scrollRef.current?.shift(shift);
if(index > 0 && index <= MIN_INDEX) {
const shift = {
height: newData.reduce((p, c, i) => p + heightsRef.current[keyExtractor(c, i)], 0),
offset: data.slice(0, index).reduce((p, c, i) => p + heightsRef.current[keyExtractor(c, i)], 0),
};
scrollRef.current?.shift(shift);
onUpdateData?.({heights: heightsRef.current, ...shift});
}
setNewData([]);
setFinalData(data);
onUpdateData?.({heights: heightsRef.current, ...shift});
}, [data, keyExtractor, newData, onUpdateData, scrollRef]);

return {
Expand Down

0 comments on commit 0218c71

Please sign in to comment.