Skip to content

Commit

Permalink
refactor: get rid of require cycle
Browse files Browse the repository at this point in the history
  • Loading branch information
wouterds committed Jul 5, 2023
1 parent 19a2553 commit ae831f7
Show file tree
Hide file tree
Showing 14 changed files with 59 additions and 83 deletions.
2 changes: 1 addition & 1 deletion src/components/Swimlane/PopularMovies/Item/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { NavigationProp, useNavigation } from '@react-navigation/native';
import Card from 'components/Card';
import Touchable from 'components/Touchable';
import { Touchable } from 'components/Touchable';
import { Route, RouteParams } from 'navigation';
import React, { memo } from 'react';
import { useMovie } from 'store/movies/hooks';
Expand Down
3 changes: 0 additions & 3 deletions src/components/Swimlane/PopularMovies/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ const PopularMoviesSwimlane = ({ hideTitle }: Props) => {
showsHorizontalScrollIndicator={false}
showsVerticalScrollIndicator={false}
data={isLoading && data.length === 0 ? new Array(6).fill(null) : data}
keyExtractor={(item, index) =>
`swimlane.popular-movies.${item?.id || `index-${index}`}`
}
renderItem={({ item }) => <Item id={item?.id} />}
/>
</TVFocusGuideView>
Expand Down
2 changes: 1 addition & 1 deletion src/components/Swimlane/PopularTVShows/Item/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { NavigationProp, useNavigation } from '@react-navigation/native';
import Card from 'components/Card';
import Touchable from 'components/Touchable';
import { Touchable } from 'components/Touchable';
import { Route, RouteParams } from 'navigation';
import React, { memo } from 'react';
import { useShow } from 'store/shows/hooks';
Expand Down
3 changes: 0 additions & 3 deletions src/components/Swimlane/PopularTVShows/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ const PopularTVShowsSwimlane = ({ hideTitle }: Props) => {
showsHorizontalScrollIndicator={false}
showsVerticalScrollIndicator={false}
data={isLoading && data.length === 0 ? new Array(6).fill(null) : data}
keyExtractor={(item, index) =>
`swimlane.popular-tv-shows.${item?.id || `index-${index}`}`
}
renderItem={({ item }) => <Item id={item?.id} />}
/>
</TVFocusGuideView>
Expand Down
2 changes: 1 addition & 1 deletion src/components/Swimlane/TopRatedMovies/Item/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { NavigationProp, useNavigation } from '@react-navigation/native';
import Card from 'components/Card';
import Touchable from 'components/Touchable';
import { Touchable } from 'components/Touchable';
import { Route, RouteParams } from 'navigation';
import React, { memo } from 'react';
import { useMovie } from 'store/movies/hooks';
Expand Down
3 changes: 0 additions & 3 deletions src/components/Swimlane/TopRatedMovies/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ const TopRatedMoviesSwimlane = ({ hideTitle }: Props) => {
showsHorizontalScrollIndicator={false}
showsVerticalScrollIndicator={false}
data={isLoading && data.length === 0 ? new Array(6).fill(null) : data}
keyExtractor={(item, index) =>
`swimlane.top-rated-movies.${item?.id || `index-${index}`}`
}
renderItem={({ item }) => <Item id={item?.id} />}
/>
</TVFocusGuideView>
Expand Down
2 changes: 1 addition & 1 deletion src/components/Swimlane/TopRatedTVShows/Item/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { NavigationProp, useNavigation } from '@react-navigation/native';
import Card from 'components/Card';
import Touchable from 'components/Touchable';
import { Touchable } from 'components/Touchable';
import { Route, RouteParams } from 'navigation';
import React, { memo } from 'react';
import { useShow } from 'store/shows/hooks';
Expand Down
3 changes: 0 additions & 3 deletions src/components/Swimlane/TopRatedTVShows/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ const TopRatedTVShowsSwimlane = ({ hideTitle }: Props) => {
showsHorizontalScrollIndicator={false}
showsVerticalScrollIndicator={false}
data={isLoading && data.length === 0 ? new Array(6).fill(null) : data}
keyExtractor={(item, index) =>
`swimlane.top-rated-tv-shows.${item?.id || `index-${index}`}`
}
renderItem={({ item }) => <Item id={item?.id} />}
/>
</TVFocusGuideView>
Expand Down
2 changes: 1 addition & 1 deletion src/components/Swimlane/TrendingToday/Item/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { NavigationProp, useNavigation } from '@react-navigation/native';
import Card from 'components/Card';
import Touchable from 'components/Touchable';
import { Touchable } from 'components/Touchable';
import { Route, RouteParams } from 'navigation';
import React, { memo } from 'react';
import { useMovie } from 'store/movies/hooks';
Expand Down
5 changes: 0 additions & 5 deletions src/components/Swimlane/TrendingToday/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@ const TrendingTodaySwimlane = ({ hideTitle, hasTVPreferredFocus }: Props) => {
showsHorizontalScrollIndicator={false}
showsVerticalScrollIndicator={false}
data={isLoading && data.length === 0 ? new Array(6).fill(null) : data}
keyExtractor={(item, index) =>
`swimlane.trending-today.${
item ? `${item?.type}:${item?.id}` : `index-${index}`
}`
}
renderItem={({ item, index }) => (
<Item
id={item?.id}
Expand Down
46 changes: 46 additions & 0 deletions src/components/Touchable/component.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import React, { forwardRef, LegacyRef, useCallback, useState } from 'react';
import {
NativeSyntheticEvent,
Platform,
TargetedEvent,
TouchableOpacity,
} from 'react-native';

import { TouchableContextProvider } from './context';
import { TouchableProps } from './types';

export const Touchable = forwardRef(
({ onFocus, onBlur, children, ...props }: TouchableProps, ref) => {
const [hasFocus, setHasFocus] = useState(false);

const onFocusProxy = useCallback(
(e: NativeSyntheticEvent<TargetedEvent>) => {
setHasFocus(true);
onFocus?.(e);
},
[onFocus],
);

const onBlurProxy = useCallback(
(e: NativeSyntheticEvent<TargetedEvent>) => {
setHasFocus(false);
onBlur?.(e);
},
[onBlur],
);

return (
<TouchableOpacity
{...props}
onFocus={onFocusProxy}
onBlur={onBlurProxy}
activeOpacity={props.activeOpacity || Platform.isTV ? 1 : 0.7}
tvParallaxProperties={{ enabled: false }}
ref={ref as LegacyRef<TouchableOpacity>}>
<TouchableContextProvider hasFocus={hasFocus}>
{children}
</TouchableContextProvider>
</TouchableOpacity>
);
},
);
61 changes: 1 addition & 60 deletions src/components/Touchable/index.tsx
Original file line number Diff line number Diff line change
@@ -1,62 +1,3 @@
import React, {
forwardRef,
LegacyRef,
memo,
useCallback,
useState,
} from 'react';
import {
NativeSyntheticEvent,
Platform,
TargetedEvent,
TouchableOpacity,
TouchableOpacityProps,
ViewProps,
} from 'react-native';

import { TouchableContextProvider } from './context';

interface Props extends TouchableOpacityProps, ViewProps {}

export type TouchableProps = Props;

const Touchable = forwardRef(
({ onFocus, onBlur, children, ...props }: Props, ref) => {
const [hasFocus, setHasFocus] = useState(false);

const onFocusProxy = useCallback(
(e: NativeSyntheticEvent<TargetedEvent>) => {
setHasFocus(true);
onFocus?.(e);
},
[onFocus],
);

const onBlurProxy = useCallback(
(e: NativeSyntheticEvent<TargetedEvent>) => {
setHasFocus(false);
onBlur?.(e);
},
[onBlur],
);

return (
<TouchableOpacity
{...props}
onFocus={onFocusProxy}
onBlur={onBlurProxy}
activeOpacity={props.activeOpacity || Platform.isTV ? 1 : 0.7}
tvParallaxProperties={{ enabled: false }}
ref={ref as LegacyRef<TouchableOpacity>}>
<TouchableContextProvider hasFocus={hasFocus}>
{children}
</TouchableContextProvider>
</TouchableOpacity>
);
},
);

export default memo(Touchable);

export * from './component';
export * from './useTouchable';
export * from './withTouchable';
5 changes: 5 additions & 0 deletions src/components/Touchable/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { TouchableOpacityProps, ViewProps } from 'react-native';

interface Props extends TouchableOpacityProps, ViewProps {}

export type TouchableProps = Props;
3 changes: 2 additions & 1 deletion src/components/Touchable/withTouchable.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { ComponentType, forwardRef } from 'react';

import Touchable, { TouchableProps } from './index';
import { Touchable } from './component';
import { TouchableProps } from './types';

export const withTouchable = <Props extends Record<string, unknown>>(
Component: ComponentType<Props>,
Expand Down

0 comments on commit ae831f7

Please sign in to comment.