-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c5ebd69
commit ba30c74
Showing
3 changed files
with
181 additions
and
167 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import React, { useEffect, useRef } from 'react'; | ||
import { | ||
NativeModules, | ||
FlatList, | ||
FlatListProps, | ||
Platform, | ||
findNodeHandle, | ||
} from 'react-native'; | ||
|
||
export const MvcpScrollViewManager = NativeModules.MvcpScrollViewManager; | ||
|
||
export type maintainVisibleContentPositionPropType = { | ||
autoscrollToTopThreshold?: number; | ||
minIndexForVisible: number; | ||
}; | ||
|
||
export type FlatListComponentPropType<T = any> = FlatListProps<T> & { | ||
maintainVisibleContentPosition: maintainVisibleContentPositionPropType; | ||
}; | ||
|
||
export default React.forwardRef( | ||
(props: FlatListComponentPropType, forwardedRef) => { | ||
const flRef = useRef<FlatList>(null); | ||
|
||
const { extraData, maintainVisibleContentPosition: mvcp } = props; | ||
const autoscrollToTopThreshold = useRef<number>(); | ||
const minIndexForVisible = useRef<number>(); | ||
|
||
useEffect(() => { | ||
let cleanupPromise: Promise<number> | undefined; | ||
const enableMaintainVisibleContentPosition = (): void => { | ||
if (!mvcp || Platform.OS !== 'android') return; | ||
|
||
if ( | ||
autoscrollToTopThreshold.current === mvcp.autoscrollToTopThreshold && | ||
minIndexForVisible.current === mvcp.minIndexForVisible | ||
) { | ||
// Don't do anythinig if the values haven't changed | ||
return; | ||
} | ||
|
||
autoscrollToTopThreshold.current = | ||
mvcp.autoscrollToTopThreshold || -Number.MAX_SAFE_INTEGER; | ||
minIndexForVisible.current = mvcp.minIndexForVisible || 0; | ||
|
||
if (flRef.current) { | ||
const viewTag = findNodeHandle(flRef.current); | ||
cleanupPromise = MvcpScrollViewManager.enableMaintainVisibleContentPosition( | ||
viewTag, | ||
autoscrollToTopThreshold.current, | ||
minIndexForVisible.current | ||
); | ||
} | ||
}; | ||
|
||
enableMaintainVisibleContentPosition(); | ||
|
||
return () => { | ||
if ( | ||
autoscrollToTopThreshold.current === mvcp.autoscrollToTopThreshold && | ||
minIndexForVisible.current === mvcp.minIndexForVisible | ||
) { | ||
// Don't do anythinig if the values haven't changed | ||
return; | ||
} | ||
|
||
cleanupPromise?.then((handle) => { | ||
MvcpScrollViewManager.disableMaintainVisibleContentPosition(handle); | ||
}); | ||
}; | ||
}, [extraData, mvcp]); | ||
|
||
return ( | ||
<FlatList | ||
{...props} | ||
ref={(ref) => { | ||
// @ts-ignore | ||
flRef.current = ref; | ||
if (typeof forwardedRef === 'function') { | ||
forwardedRef(ref); | ||
} else if (forwardedRef?.current) { | ||
forwardedRef.current = ref; | ||
} | ||
}} | ||
/> | ||
); | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import React, { useEffect, useRef } from 'react'; | ||
import { | ||
NativeModules, | ||
FlatList, | ||
Platform, | ||
findNodeHandle, | ||
ScrollViewProps, | ||
ScrollView, | ||
} from 'react-native'; | ||
|
||
export const MvcpScrollViewManager = NativeModules.MvcpScrollViewManager; | ||
|
||
export type maintainVisibleContentPositionPropType = { | ||
autoscrollToTopThreshold?: number; | ||
minIndexForVisible: number; | ||
}; | ||
|
||
export type ScrollViewComponentPropType = ScrollViewProps & { | ||
maintainVisibleContentPosition: maintainVisibleContentPositionPropType; | ||
}; | ||
|
||
export default React.forwardRef( | ||
(props: ScrollViewComponentPropType, forwardedRef) => { | ||
const flRef = useRef<FlatList>(null); | ||
|
||
const { maintainVisibleContentPosition: mvcp } = props; | ||
const autoscrollToTopThreshold = useRef<number>(); | ||
const minIndexForVisible = useRef<number>(); | ||
|
||
useEffect(() => { | ||
let cleanupPromise: Promise<number> | undefined; | ||
const enableMaintainVisibleContentPosition = (): void => { | ||
if (!mvcp || Platform.OS !== 'android') return; | ||
|
||
if ( | ||
autoscrollToTopThreshold.current === mvcp.autoscrollToTopThreshold && | ||
minIndexForVisible.current === mvcp.minIndexForVisible | ||
) { | ||
// Don't do anythinig if the values haven't changed | ||
return; | ||
} | ||
|
||
autoscrollToTopThreshold.current = | ||
mvcp.autoscrollToTopThreshold || -Number.MAX_SAFE_INTEGER; | ||
minIndexForVisible.current = mvcp.minIndexForVisible || 0; | ||
|
||
if (flRef.current) { | ||
const viewTag = findNodeHandle(flRef.current); | ||
cleanupPromise = MvcpScrollViewManager.enableMaintainVisibleContentPosition( | ||
viewTag, | ||
autoscrollToTopThreshold.current, | ||
minIndexForVisible.current | ||
); | ||
} | ||
}; | ||
|
||
enableMaintainVisibleContentPosition(); | ||
|
||
return () => { | ||
if ( | ||
autoscrollToTopThreshold.current === mvcp.autoscrollToTopThreshold && | ||
minIndexForVisible.current === mvcp.minIndexForVisible | ||
) { | ||
// Don't do anythinig if the values haven't changed | ||
return; | ||
} | ||
|
||
cleanupPromise?.then((handle) => { | ||
MvcpScrollViewManager.disableMaintainVisibleContentPosition(handle); | ||
}); | ||
}; | ||
}, [mvcp]); | ||
|
||
return ( | ||
<ScrollView | ||
{...props} | ||
ref={(ref) => { | ||
// @ts-ignore | ||
flRef.current = ref; | ||
if (typeof forwardedRef === 'function') { | ||
forwardedRef(ref); | ||
} else if (forwardedRef?.current) { | ||
forwardedRef.current = ref; | ||
} | ||
}} | ||
/> | ||
); | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,168 +1,5 @@ | ||
import React, { useEffect, useRef } from 'react'; | ||
import { | ||
NativeModules, | ||
FlatList, | ||
FlatListProps, | ||
Platform, | ||
findNodeHandle, | ||
ScrollViewProps, | ||
ScrollView, | ||
} from 'react-native'; | ||
import FlatList from './Lists/FlatList'; | ||
import ScrollView from './Lists/ScrollView'; | ||
|
||
export const MvcpScrollViewManager = NativeModules.MvcpScrollViewManager; | ||
|
||
export type maintainVisibleContentPositionPropType = { | ||
autoscrollToTopThreshold?: number; | ||
minIndexForVisible: number; | ||
}; | ||
|
||
export type FlatListComponentPropType<T = any> = FlatListProps<T> & { | ||
maintainVisibleContentPosition: maintainVisibleContentPositionPropType; | ||
}; | ||
|
||
export type ScrollViewComponentPropType = ScrollViewProps & { | ||
maintainVisibleContentPosition: maintainVisibleContentPositionPropType; | ||
}; | ||
|
||
const FlatListComponent = React.forwardRef( | ||
(props: FlatListComponentPropType, forwardedRef) => { | ||
const flRef = useRef<FlatList>(null); | ||
|
||
const { extraData, maintainVisibleContentPosition: mvcp } = props; | ||
const autoscrollToTopThreshold = useRef<number>(); | ||
const minIndexForVisible = useRef<number>(); | ||
|
||
useEffect(() => { | ||
let cleanupPromise: Promise<number> | undefined; | ||
const enableMaintainVisibleContentPosition = (): void => { | ||
if (!mvcp || Platform.OS !== 'android') return; | ||
|
||
if ( | ||
autoscrollToTopThreshold.current === mvcp.autoscrollToTopThreshold && | ||
minIndexForVisible.current === mvcp.minIndexForVisible | ||
) { | ||
// Don't do anythinig if the values haven't changed | ||
return; | ||
} | ||
|
||
autoscrollToTopThreshold.current = | ||
mvcp.autoscrollToTopThreshold || -Number.MAX_SAFE_INTEGER; | ||
minIndexForVisible.current = mvcp.minIndexForVisible || 0; | ||
|
||
if (flRef.current) { | ||
const viewTag = findNodeHandle(flRef.current); | ||
cleanupPromise = MvcpScrollViewManager.enableMaintainVisibleContentPosition( | ||
viewTag, | ||
autoscrollToTopThreshold.current, | ||
minIndexForVisible.current | ||
); | ||
} | ||
}; | ||
|
||
enableMaintainVisibleContentPosition(); | ||
|
||
return () => { | ||
if ( | ||
autoscrollToTopThreshold.current === mvcp.autoscrollToTopThreshold && | ||
minIndexForVisible.current === mvcp.minIndexForVisible | ||
) { | ||
// Don't do anythinig if the values haven't changed | ||
return; | ||
} | ||
|
||
cleanupPromise?.then((handle) => { | ||
MvcpScrollViewManager.disableMaintainVisibleContentPosition(handle); | ||
}); | ||
}; | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [extraData, mvcp]); | ||
|
||
return ( | ||
<FlatList | ||
{...props} | ||
ref={(ref) => { | ||
// @ts-ignore | ||
flRef.current = ref; | ||
if (typeof forwardedRef === 'function') { | ||
forwardedRef(ref); | ||
} else if (forwardedRef?.current) { | ||
forwardedRef.current = ref; | ||
} | ||
}} | ||
/> | ||
); | ||
} | ||
); | ||
|
||
const ScrollViewComponent = React.forwardRef( | ||
(props: ScrollViewComponentPropType, forwardedRef) => { | ||
const flRef = useRef<FlatList>(null); | ||
|
||
const { maintainVisibleContentPosition: mvcp } = props; | ||
const autoscrollToTopThreshold = useRef<number>(); | ||
const minIndexForVisible = useRef<number>(); | ||
|
||
useEffect(() => { | ||
let cleanupPromise: Promise<number> | undefined; | ||
const enableMaintainVisibleContentPosition = (): void => { | ||
if (!mvcp || Platform.OS !== 'android') return; | ||
|
||
if ( | ||
autoscrollToTopThreshold.current === mvcp.autoscrollToTopThreshold && | ||
minIndexForVisible.current === mvcp.minIndexForVisible | ||
) { | ||
// Don't do anythinig if the values haven't changed | ||
return; | ||
} | ||
|
||
autoscrollToTopThreshold.current = | ||
mvcp.autoscrollToTopThreshold || -Number.MAX_SAFE_INTEGER; | ||
minIndexForVisible.current = mvcp.minIndexForVisible || 0; | ||
|
||
if (flRef.current) { | ||
const viewTag = findNodeHandle(flRef.current); | ||
cleanupPromise = MvcpScrollViewManager.enableMaintainVisibleContentPosition( | ||
viewTag, | ||
autoscrollToTopThreshold.current, | ||
minIndexForVisible.current | ||
); | ||
} | ||
}; | ||
|
||
enableMaintainVisibleContentPosition(); | ||
|
||
return () => { | ||
if ( | ||
autoscrollToTopThreshold.current === mvcp.autoscrollToTopThreshold && | ||
minIndexForVisible.current === mvcp.minIndexForVisible | ||
) { | ||
// Don't do anythinig if the values haven't changed | ||
return; | ||
} | ||
|
||
cleanupPromise?.then((handle) => { | ||
MvcpScrollViewManager.disableMaintainVisibleContentPosition(handle); | ||
}); | ||
}; | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [mvcp]); | ||
|
||
return ( | ||
<ScrollView | ||
{...props} | ||
ref={(ref) => { | ||
// @ts-ignore | ||
flRef.current = ref; | ||
if (typeof forwardedRef === 'function') { | ||
forwardedRef(ref); | ||
} else if (forwardedRef?.current) { | ||
forwardedRef.current = ref; | ||
} | ||
}} | ||
/> | ||
); | ||
} | ||
); | ||
|
||
export { FlatListComponent as FlatList }; | ||
export { ScrollViewComponent as ScrollView }; | ||
export { FlatList }; | ||
export { ScrollView }; |