Skip to content

Commit

Permalink
Moving lists to separate files
Browse files Browse the repository at this point in the history
  • Loading branch information
vishalnarkhede committed Jan 21, 2021
1 parent c5ebd69 commit ba30c74
Show file tree
Hide file tree
Showing 3 changed files with 181 additions and 167 deletions.
88 changes: 88 additions & 0 deletions src/Lists/FlatList.tsx
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;
}
}}
/>
);
}
);
89 changes: 89 additions & 0 deletions src/Lists/ScrollView.tsx
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;
}
}}
/>
);
}
);
171 changes: 4 additions & 167 deletions src/index.android.tsx
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 };

0 comments on commit ba30c74

Please sign in to comment.