Skip to content

Commit

Permalink
chore: refactor example components (#150)
Browse files Browse the repository at this point in the history
* chore(example): remove duplicated component

* chore(example): remove dead props

* chore(example): add missing memo

* chore(example): refactor duplicate Program and ProgramLandscape components into one

* chore(example): refactor duplicate ProgramList
  • Loading branch information
pierpo authored Sep 13, 2024
1 parent d6fbe20 commit 7242ba8
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 311 deletions.
46 changes: 0 additions & 46 deletions packages/example/src/modules/program/view/LongProgram.tsx

This file was deleted.

53 changes: 31 additions & 22 deletions packages/example/src/modules/program/view/Program.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type ProgramProps = {
isFocused?: boolean;
programInfo: ProgramInfo;
label?: string;
variant?: 'portrait' | 'landscape';
};

const Label = React.memo(({ label }: { label: string }) => {
Expand All @@ -17,33 +18,41 @@ const Label = React.memo(({ label }: { label: string }) => {
Label.displayName = 'Label';

export const Program = React.memo(
React.forwardRef<View, ProgramProps>(({ isFocused = false, programInfo, label }, ref) => {
const imageSource = programInfo.image;
React.forwardRef<View, ProgramProps>(
({ isFocused = false, programInfo, label, variant = 'portrait' }, ref) => {
const imageSource = programInfo.image;
const scaleAnimation = useFocusAnimation(isFocused);

const scaleAnimation = useFocusAnimation(isFocused);

return (
<ProgramContainer
style={scaleAnimation} // Apply the animated scale transform
ref={ref}
isFocused={isFocused}
>
<ProgramImage source={imageSource} accessible />
{label ? (
<Overlay>
<Label label={label} />
</Overlay>
) : null}
</ProgramContainer>
);
}),
return (
<ProgramContainer
style={scaleAnimation} // Apply the animated scale transform
ref={ref}
isFocused={isFocused}
variant={variant}
>
<ProgramImage source={imageSource} accessible />
{label ? (
<Overlay>
<Label label={label} />
</Overlay>
) : null}
</ProgramContainer>
);
},
),
);

Program.displayName = 'Program';

const ProgramContainer = styled(Animated.View)<{ isFocused: boolean }>(({ isFocused, theme }) => ({
height: theme.sizes.program.portrait.height,
width: theme.sizes.program.portrait.width,
const ProgramContainer = styled(Animated.View)<{
isFocused: boolean;
variant: 'portrait' | 'landscape';
}>(({ isFocused, variant, theme }) => ({
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.portrait.width,
overflow: 'hidden',
borderRadius: 20,
borderColor: isFocused ? theme.colors.primary.light : 'transparent',
Expand Down
61 changes: 0 additions & 61 deletions packages/example/src/modules/program/view/ProgramLandscape.tsx

This file was deleted.

48 changes: 35 additions & 13 deletions packages/example/src/modules/program/view/ProgramList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,42 +21,61 @@ const NUMBER_OF_ITEMS_VISIBLE_ON_SCREEN = 7;
const WINDOW_SIZE = NUMBER_OF_ITEMS_VISIBLE_ON_SCREEN + 8;
const ROW_PADDING = scaledPixels(70);

type ProgramListProps = {
orientation?: 'vertical' | 'horizontal';
containerStyle?: object;
listRef?: MutableRefObject<SpatialNavigationVirtualizedListRef>;
data?: ProgramInfo[];
variant?: 'normal' | 'variable-size';
};

export const ProgramList = ({
orientation,
orientation = 'horizontal',
containerStyle,
listRef,
data,
}: {
orientation?: 'vertical' | 'horizontal';
containerStyle?: object;
listRef: MutableRefObject<SpatialNavigationVirtualizedListRef>;
data?: ProgramInfo[];
}) => {
variant = 'normal',
}: ProgramListProps) => {
const navigation = useNavigation<NativeStackNavigationProp<RootStackParamList>>();
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 }) => (
<ProgramNode
programInfo={item}
onSelect={() => navigation.push('ProgramDetail', { programInfo: item })}
label={index.toString()}
variant={variant === 'variable-size' && isItemLarge(item) ? 'landscape' : 'portrait'}
/>
),
[navigation],
[navigation, isItemLarge, variant],
);
const theme = useTheme();

const programInfos = useMemo(() => getPrograms(1000), []);
const programInfos = useMemo(
() => data ?? getPrograms(variant === 'variable-size' ? 10 : 1000),
[data, variant],
);

return (
<SpatialNavigationNode>
{({ isActive }) => (
<Container isActive={isActive} style={containerStyle}>
<SpatialNavigationVirtualizedList
orientation={orientation}
data={data ?? programInfos}
data={programInfos}
renderItem={renderItem}
itemSize={theme.sizes.program.portrait.width + 30}
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"
}
numberOfRenderedItems={WINDOW_SIZE}
numberOfItemsVisibleOnScreen={NUMBER_OF_ITEMS_VISIBLE_ON_SCREEN}
onEndReachedThresholdItemsNumber={NUMBER_OF_ITEMS_VISIBLE_ON_SCREEN}
Expand All @@ -75,9 +94,11 @@ export const ProgramList = ({
export const ProgramsRow = ({
containerStyle,
listRef,
variant = 'normal',
}: {
containerStyle?: object;
listRef: MutableRefObject<SpatialNavigationVirtualizedListRef>;
listRef?: MutableRefObject<SpatialNavigationVirtualizedListRef>;
variant?: 'normal' | 'variable-size';
}) => {
const theme = useTheme();
return (
Expand All @@ -87,6 +108,7 @@ export const ProgramsRow = ({
height: theme.sizes.program.portrait.height + ROW_PADDING,
}}
listRef={listRef}
variant={variant}
/>
);
};
Expand Down
120 changes: 0 additions & 120 deletions packages/example/src/modules/program/view/ProgramListVariableSize.tsx

This file was deleted.

Loading

0 comments on commit 7242ba8

Please sign in to comment.