Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
pierpo committed Sep 13, 2024
1 parent 4fa7e85 commit 4de915b
Show file tree
Hide file tree
Showing 13 changed files with 40 additions and 19 deletions.
2 changes: 1 addition & 1 deletion packages/example/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ const TabNavigator = () => {
);
};

function App(): JSX.Element {
function App() {
useTVPanEvent();
const { height, width } = useWindowDimensions();
const areFontsLoaded = useFonts();
Expand Down
2 changes: 2 additions & 0 deletions packages/example/src/components/Menu/Menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ export const Menu = ({ state, navigation }: BottomTabBarProps) => {
return (
<Fragment key={route.key}>
<MenuItem
// @ts-expect-error TODO fix this type error
label={menuItems[route.name].label}
// @ts-expect-error TODO fix this type error
icon={menuItems[route.name].icon}
isMenuOpen={isMenuOpen}
isActive={state.index === index}
Expand Down
2 changes: 1 addition & 1 deletion packages/example/src/components/PanEvent/PanEvent.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getGridCoordinates, moveFocus } from './PanEvent.utils';

class PanEvent {
private orientation = undefined;
private orientation: string | undefined = undefined;
private lastIndex = 0;

reset = () => {
Expand Down
6 changes: 5 additions & 1 deletion packages/example/src/components/PanEvent/PanEvent.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import { SupportedKeys } from '../remote-control/SupportedKeys';
import PanEvent from './PanEvent';
import { EMIT_KEY_DOWN_INTERVAL, GRID_SIZE, NUMBER_OF_COLUMNS } from './PanEvent.constants';

export const getGridCoordinates = (x: number, y: number, panEvent: PanEvent): number => {
export const getGridCoordinates = (
x: number,
y: number,
panEvent: PanEvent,
): number | undefined => {
const gridElementSize = GRID_SIZE / NUMBER_OF_COLUMNS;

const xIndex = Math.floor((x + gridElementSize / 2) / gridElementSize);
Expand Down
5 changes: 4 additions & 1 deletion packages/example/src/components/VirtualizedSpatialGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { scaledPixels } from '../design-system/helpers/scaledPixels';
import { theme } from '../design-system/theme/theme';
import { Header } from '../modules/header/view/Header';
import { BottomArrow, TopArrow } from '../design-system/components/Arrows';
import { ProgramInfo } from '../modules/program/domain/programInfo';

const NUMBER_OF_ROWS_VISIBLE_ON_SCREEN = 2;
const NUMBER_OF_RENDERED_ROWS = NUMBER_OF_ROWS_VISIBLE_ON_SCREEN + 5;
Expand All @@ -16,7 +17,9 @@ const INFINITE_SCROLL_ROW_THRESHOLD = 2;

export const VirtualizedSpatialGrid = ({ containerStyle }: { containerStyle?: ViewStyle }) => {
const renderItem = useCallback(
({ item, index }) => <ProgramNode programInfo={item} label={index?.toString?.()} />,
({ item, index }: { item: ProgramInfo; index: number }) => (
<ProgramNode programInfo={item} label={index?.toString?.()} />
),
[],
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,18 @@ export default class CustomEventEmitter<Events extends Record<EventType, unknown
off = <Key extends keyof Events>(eventType: Key, handler?: Handler<Events[keyof Events]>) => {
this.handlers.set(
eventType,
// @ts-expect-error TODO fix the type error
this.handlers.get(eventType).filter((h) => h !== handler),
);
};

emit = <Key extends keyof Events>(eventType: Key, evt?: Events[Key]) => {
const eventTypeHandlers = this.handlers.get(eventType);
// @ts-expect-error TODO fix the type error
for (let index = eventTypeHandlers.length - 1; index >= 0; index--) {
// @ts-expect-error TODO fix the type error
const handler = eventTypeHandlers[index];
// @ts-expect-error TODO fix the type error
if (handler(evt)) {
return;
}
Expand Down
2 changes: 2 additions & 0 deletions packages/example/src/hooks/useKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ export const useKey = (key: SupportedKeys, callback: (pressedKey: SupportedKeys)
if (actualKey !== key) return;
return callback(key);
};
// @ts-expect-error TODO fix the type error
RemoteControlManager.addKeydownListener(remoteControlListener);
// @ts-expect-error TODO fix the type error
return () => RemoteControlManager.removeKeydownListener(remoteControlListener);
}, [key, callback]);
};
1 change: 1 addition & 0 deletions packages/example/src/modules/header/view/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ interface HeaderProps {
}

export const Header = ({ title, description, verticalSize }: HeaderProps) => {
// @ts-expect-error TODO fix type error
const imageSource = images[Math.floor(Math.random() * 9)];
return (
<SpatialNavigationNode orientation="horizontal">
Expand Down
4 changes: 3 additions & 1 deletion packages/example/src/modules/program/view/ProgramList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,10 @@ export const ProgramList = React.forwardRef<View, ProgramListProps>(
numberOfRenderedItems={WINDOW_SIZE}
numberOfItemsVisibleOnScreen={NUMBER_OF_ITEMS_VISIBLE_ON_SCREEN}
onEndReachedThresholdItemsNumber={NUMBER_OF_ITEMS_VISIBLE_ON_SCREEN}
// @ts-expect-error TODO change the type from ReactElement to ReactNode in the core
descendingArrow={isActive ? <LeftArrow /> : null}
descendingArrowContainerStyle={styles.leftArrowContainer}
// @ts-expect-error TODO change the type from ReactElement to ReactNode in the core
ascendingArrow={isActive ? <RightArrow /> : null}
ascendingArrowContainerStyle={styles.rightArrowContainer}
ref={(elementRef) => {
Expand All @@ -129,7 +131,7 @@ export const ProgramsRow = ({
containerStyle?: object;
variant?: 'normal' | 'variable-size';
listSize?: number;
parentRef?: MutableRefObject<SpatialNavigationVirtualizedListRef>;
parentRef?: MutableRefObject<SpatialNavigationVirtualizedListRef | null>;
data?: ProgramInfo[];
}) => {
const theme = useTheme();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { SpatialNavigationVirtualizedListRef } from '../../../../../lib/src/spat
type Props = {
title: string;
listSize?: number;
parentRef?: MutableRefObject<SpatialNavigationVirtualizedListRef>;
parentRef?: MutableRefObject<SpatialNavigationVirtualizedListRef | null>;
};

export const ProgramListWithTitle = ({ title, parentRef, listSize }: Props) => {
Expand Down
4 changes: 2 additions & 2 deletions packages/example/src/pages/AsynchronousContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { useState } from 'react';
import { ActivityIndicator } from 'react-native';
import styled from '@emotion/native';

function sleep(ms) {
function sleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}

Expand Down Expand Up @@ -49,7 +49,7 @@ export const AsynchronousContent = () => {
<StyledNavigationRow direction="horizontal">
<Button label="First button" />
<SpatialNavigationNode>
{shouldShow && <Button label="Second button (asynchronously showed)" />}
{shouldShow ? <Button label="Second button (asynchronously showed)" /> : <></>}
</SpatialNavigationNode>
<Button label="Third button" />
</StyledNavigationRow>
Expand Down
18 changes: 9 additions & 9 deletions packages/example/src/pages/GridWithLongNodesPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ import { BottomArrow, TopArrow } from '../design-system/components/Arrows';
const HEADER_SIZE = scaledPixels(400);

export const GridWithLongNodesPage = () => {
const firstItemRef = useRef<SpatialNavigationNodeRef>(null);
const lastItemRef = useRef<SpatialNavigationNodeRef>(null);
const parentRef = useRef<SpatialNavigationVirtualizedListRef>(null);
const firstItemRef = useRef<SpatialNavigationNodeRef | null>(null);
const lastItemRef = useRef<SpatialNavigationNodeRef | null>(null);
const parentRef = useRef<SpatialNavigationVirtualizedListRef | null>(null);

return (
<Page>
Expand Down Expand Up @@ -56,13 +56,13 @@ export const GridWithLongNodesPage = () => {
<Button
label="Go to first"
onSelect={() => {
parentRef.current.focus(0);
parentRef.current?.focus(0);
}}
/>
<Button
label="Go to last"
onSelect={() => {
parentRef.current.focus(999);
parentRef.current?.focus(999);
}}
/>
</Row>
Expand Down Expand Up @@ -118,14 +118,14 @@ const ButtonRow = ({
firstItemRef,
lastItemRef,
}: {
firstItemRef: MutableRefObject<SpatialNavigationNodeRef>;
lastItemRef: MutableRefObject<SpatialNavigationNodeRef>;
firstItemRef: MutableRefObject<SpatialNavigationNodeRef | null>;
lastItemRef: MutableRefObject<SpatialNavigationNodeRef | null>;
}) => {
return (
<SpatialNavigationNode orientation="horizontal">
<ListContainer>
<Button label="Go to first item" onSelect={() => firstItemRef.current.focus()} />
<Button label="Go to last item" onSelect={() => lastItemRef.current.focus()} />
<Button label="Go to first item" onSelect={() => firstItemRef.current?.focus()} />
<Button label="Go to last item" onSelect={() => lastItemRef.current?.focus()} />
</ListContainer>
</SpatialNavigationNode>
);
Expand Down
7 changes: 5 additions & 2 deletions packages/example/src/utils/throttle.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
export const throttle = (callback, delay) => {
export const throttle = (
callback: { (): void; (...arg0: unknown[]): void },
delay: number | undefined,
) => {
let wait = false;

return (...args) => {
return (...args: unknown[]) => {
if (wait) {
return;
}
Expand Down

0 comments on commit 4de915b

Please sign in to comment.