From 4d55b253054c746bdfd9b4ba29a3e07239ae5959 Mon Sep 17 00:00:00 2001 From: Pierre Poupin Date: Fri, 13 Sep 2024 14:14:46 +0200 Subject: [PATCH] chore: refactor example components part 2 (#151) * chore(example): fix weird size behaviours * chore(example): take out isItemLarge function * chore(example): extract itemSize function * chore(example): pass list size as props --- .../example/src/design-system/theme/sizes.ts | 2 +- .../src/modules/program/view/Program.tsx | 2 +- .../src/modules/program/view/ProgramList.tsx | 43 ++++++++++++------- .../program/view/ProgramListWithTitle.tsx | 9 ++-- packages/example/src/pages/Home.tsx | 10 ++++- 5 files changed, 42 insertions(+), 24 deletions(-) diff --git a/packages/example/src/design-system/theme/sizes.ts b/packages/example/src/design-system/theme/sizes.ts index d6ff0286..6fcef321 100644 --- a/packages/example/src/design-system/theme/sizes.ts +++ b/packages/example/src/design-system/theme/sizes.ts @@ -2,7 +2,7 @@ import { scaledPixels } from '../helpers/scaledPixels'; export const sizes = { program: { - landscape: { width: scaledPixels(250), height: scaledPixels(200) }, + landscape: { width: scaledPixels(450), height: scaledPixels(200) }, portrait: { width: scaledPixels(200), height: scaledPixels(250) }, long: { width: scaledPixels(416), height: scaledPixels(250) }, }, diff --git a/packages/example/src/modules/program/view/Program.tsx b/packages/example/src/modules/program/view/Program.tsx index 464a80fe..78b40cc5 100644 --- a/packages/example/src/modules/program/view/Program.tsx +++ b/packages/example/src/modules/program/view/Program.tsx @@ -51,7 +51,7 @@ const ProgramContainer = styled(Animated.View)<{ height: theme.sizes.program.portrait.height, // Height is the same for both variants width: variant === 'landscape' - ? theme.sizes.program.landscape.width * 2 + ? theme.sizes.program.landscape.width : theme.sizes.program.portrait.width, overflow: 'hidden', borderRadius: 20, diff --git a/packages/example/src/modules/program/view/ProgramList.tsx b/packages/example/src/modules/program/view/ProgramList.tsx index d4ea4115..c900034e 100644 --- a/packages/example/src/modules/program/view/ProgramList.tsx +++ b/packages/example/src/modules/program/view/ProgramList.tsx @@ -21,28 +21,32 @@ const NUMBER_OF_ITEMS_VISIBLE_ON_SCREEN = 7; const WINDOW_SIZE = NUMBER_OF_ITEMS_VISIBLE_ON_SCREEN + 8; const ROW_PADDING = scaledPixels(70); +const GAP_BETWEEN_ELEMENTS = scaledPixels(30); + type ProgramListProps = { orientation?: 'vertical' | 'horizontal'; containerStyle?: object; listRef?: MutableRefObject; data?: ProgramInfo[]; + listSize?: number; variant?: 'normal' | 'variable-size'; }; +const isItemLarge = (item: { id: string }) => { + return parseInt(item.id, 10) % 2 === 0; // Arbitrary condition to decide size +}; + export const ProgramList = ({ orientation = 'horizontal', containerStyle, listRef, data, variant = 'normal', + listSize = 1000, }: ProgramListProps) => { const navigation = useNavigation>(); const theme = useTheme(); - const isItemLarge = useCallback((item: ProgramInfo) => { - return parseInt(item.id, 10) % 2 === 0; // Arbitrary condition to decide size - }, []); - const renderItem = useCallback( ({ item, index }: { item: ProgramInfo; index: number }) => ( ), - [navigation, isItemLarge, variant], + [navigation, variant], ); - const programInfos = useMemo( - () => data ?? getPrograms(variant === 'variable-size' ? 10 : 1000), - [data, variant], + const programInfos = useMemo(() => data ?? getPrograms(listSize), [data, listSize]); + + const itemSize = useMemo( + () => { + if (variant === 'normal') { + return theme.sizes.program.portrait.width + GAP_BETWEEN_ELEMENTS; + } + + return (item: ProgramInfo) => + isItemLarge(item) + ? theme.sizes.program.landscape.width + GAP_BETWEEN_ELEMENTS + : theme.sizes.program.portrait.width + GAP_BETWEEN_ELEMENTS; + }, // Default item size for "normal" + [theme.sizes.program.landscape.width, theme.sizes.program.portrait.width, variant], ); return ( @@ -68,14 +83,7 @@ export const ProgramList = ({ orientation={orientation} data={programInfos} renderItem={renderItem} - itemSize={ - variant === 'variable-size' - ? (item: ProgramInfo) => - isItemLarge(item) - ? theme.sizes.program.landscape.width * 2 + 45 - : theme.sizes.program.portrait.width + 30 - : theme.sizes.program.portrait.width + 30 // Default item size for "normal" - } + itemSize={itemSize} numberOfRenderedItems={WINDOW_SIZE} numberOfItemsVisibleOnScreen={NUMBER_OF_ITEMS_VISIBLE_ON_SCREEN} onEndReachedThresholdItemsNumber={NUMBER_OF_ITEMS_VISIBLE_ON_SCREEN} @@ -95,10 +103,12 @@ export const ProgramsRow = ({ containerStyle, listRef, variant = 'normal', + listSize, }: { containerStyle?: object; listRef?: MutableRefObject; variant?: 'normal' | 'variable-size'; + listSize?: number; }) => { const theme = useTheme(); return ( @@ -109,6 +119,7 @@ export const ProgramsRow = ({ }} listRef={listRef} variant={variant} + listSize={listSize} /> ); }; diff --git a/packages/example/src/modules/program/view/ProgramListWithTitle.tsx b/packages/example/src/modules/program/view/ProgramListWithTitle.tsx index ee016bca..4eca6db0 100644 --- a/packages/example/src/modules/program/view/ProgramListWithTitle.tsx +++ b/packages/example/src/modules/program/view/ProgramListWithTitle.tsx @@ -8,28 +8,29 @@ import { SpatialNavigationVirtualizedListRef } from '../../../../../lib/src/spat type Props = { title: string; listRef?: MutableRefObject; + listSize?: number; }; -export const ProgramListWithTitle = ({ title, listRef }: Props) => { +export const ProgramListWithTitle = ({ title, listRef, listSize }: Props) => { return ( {title} - + ); }; -export const ProgramListWithTitleAndVariableSizes = ({ title }: Props) => { +export const ProgramListWithTitleAndVariableSizes = ({ title, listSize }: Props) => { return ( {title} - + ); }; diff --git a/packages/example/src/pages/Home.tsx b/packages/example/src/pages/Home.tsx index c5e4bb00..26263341 100644 --- a/packages/example/src/pages/Home.tsx +++ b/packages/example/src/pages/Home.tsx @@ -35,9 +35,15 @@ export const Home = () => { - + - +