Skip to content

Commit

Permalink
chore(app): improve code structure
Browse files Browse the repository at this point in the history
  • Loading branch information
JeremyDolle committed May 28, 2024
1 parent 0a5d00f commit c3dd3b3
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 42 deletions.
2 changes: 1 addition & 1 deletion template/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { ThemeProvider } from '@/theme';
import ApplicationNavigator from './navigators/Application';
import './translations';

const queryClient = new QueryClient();
export const queryClient = new QueryClient();

export const storage = new MMKV();

Expand Down
8 changes: 1 addition & 7 deletions template/src/components/molecules/Brand/Brand.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type Props = {
mode?: 'contain' | 'cover' | 'stretch' | 'repeat' | 'center';
};

function Brand({ height, width, mode }: Props) {
function Brand({ height = 200, width = 200, mode = 'contain' }: Props) {
const { layout } = useTheme();

if (!isImageSourcePropType(LogoLight) || !isImageSourcePropType(LogoDark)) {
Expand All @@ -33,10 +33,4 @@ function Brand({ height, width, mode }: Props) {
);
}

Brand.defaultProps = {
height: 200,
width: 200,
mode: 'contain',
};

export default Brand;
20 changes: 14 additions & 6 deletions template/src/components/template/SafeScreen/SafeScreen.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,33 @@
import { SafeAreaView, StatusBar } from 'react-native';
import { StatusBar, View } from 'react-native';
import type { PropsWithChildren } from 'react';
import { useSafeAreaInsets } from 'react-native-safe-area-context';

import { useTheme } from '@/theme';

import type { PropsWithChildren } from 'react';

function SafeScreen({ children }: PropsWithChildren) {
const { layout, variant, navigationTheme } = useTheme();
const insets = useSafeAreaInsets();

return (
<SafeAreaView
<View
style={[
layout.flex_1,
{ backgroundColor: navigationTheme.colors.background },
{
backgroundColor: navigationTheme.colors.background,
// Paddings to handle safe area
paddingTop: insets.top,
paddingBottom: insets.bottom,
paddingLeft: insets.left,
paddingRight: insets.right,
},
]}
>
<StatusBar
barStyle={variant === 'dark' ? 'light-content' : 'dark-content'}
backgroundColor={navigationTheme.colors.background}
/>
{children}
</SafeAreaView>
</View>
);
}

Expand Down
19 changes: 11 additions & 8 deletions template/src/navigators/Application.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
import { createStackNavigator } from '@react-navigation/stack';
import { NavigationContainer } from '@react-navigation/native';
import { SafeAreaProvider } from 'react-native-safe-area-context';

import { Example, Startup } from '@/screens';
import { useTheme } from '@/theme';

import type { ApplicationStackParamList } from '@/types/navigation';
import type { RootStackParamList } from '@/types/navigation';

const Stack = createStackNavigator<ApplicationStackParamList>();
const Stack = createStackNavigator<RootStackParamList>();

function ApplicationNavigator() {
const { variant, navigationTheme } = useTheme();

return (
<NavigationContainer theme={navigationTheme}>
<Stack.Navigator key={variant} screenOptions={{ headerShown: false }}>
<Stack.Screen name="Startup" component={Startup} />
<Stack.Screen name="Example" component={Example} />
</Stack.Navigator>
</NavigationContainer>
<SafeAreaProvider>
<NavigationContainer theme={navigationTheme}>
<Stack.Navigator key={variant} screenOptions={{ headerShown: false }}>
<Stack.Screen name="Startup" component={Startup} />
<Stack.Screen name="Example" component={Example} />
</Stack.Navigator>
</NavigationContainer>
</SafeAreaProvider>
);
}

Expand Down
33 changes: 19 additions & 14 deletions template/src/screens/Example/Example.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { ThemeProvider } from '@/theme';
import i18n from '@/translations';

import { SafeAreaProvider } from 'react-native-safe-area-context';
import Example from './Example';

describe('Example screen should render correctly', () => {
Expand All @@ -28,13 +29,15 @@ describe('Example screen should render correctly', () => {

test('the user change the language', () => {
const component = (
<ThemeProvider storage={storage}>
<I18nextProvider i18n={i18n}>
<QueryClientProvider client={queryClient}>
<Example />
</QueryClientProvider>
</I18nextProvider>
</ThemeProvider>
<SafeAreaProvider>
<ThemeProvider storage={storage}>
<I18nextProvider i18n={i18n}>
<QueryClientProvider client={queryClient}>
<Example />
</QueryClientProvider>
</I18nextProvider>
</ThemeProvider>
</SafeAreaProvider>
);

render(component);
Expand All @@ -50,13 +53,15 @@ describe('Example screen should render correctly', () => {

test('the user change the theme', () => {
const component = (
<ThemeProvider storage={storage}>
<I18nextProvider i18n={i18n}>
<QueryClientProvider client={queryClient}>
<Example />
</QueryClientProvider>
</I18nextProvider>
</ThemeProvider>
<SafeAreaProvider>
<ThemeProvider storage={storage}>
<I18nextProvider i18n={i18n}>
<QueryClientProvider client={queryClient}>
<Example />
</QueryClientProvider>
</I18nextProvider>
</ThemeProvider>
</SafeAreaProvider>
);

render(component);
Expand Down
17 changes: 11 additions & 6 deletions template/src/screens/Startup/Startup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ import { useEffect } from 'react';
import { ActivityIndicator, Text, View } from 'react-native';
import { useQuery } from '@tanstack/react-query';
import { useTranslation } from 'react-i18next';
import { CommonActions } from '@react-navigation/native';

import { useTheme } from '@/theme';
import { Brand } from '@/components/molecules';
import { SafeScreen } from '@/components/template';

import type { ApplicationScreenProps } from '@/types/navigation';
import type { RootScreenProps } from '@/types/navigation';

function Startup({ navigation }: ApplicationScreenProps) {
function Startup({ navigation }: RootScreenProps<'Startup'>) {
const { layout, gutters, fonts } = useTheme();
const { t } = useTranslation(['startup']);

Expand All @@ -21,10 +22,14 @@ function Startup({ navigation }: ApplicationScreenProps) {
});

useEffect(() => {
navigation.reset({
index: 0,
routes: [{ name: 'Example' }],
});
if (isSuccess) {
navigation.dispatch(
CommonActions.reset({
index: 0,
routes: [{ name: 'Example' }],
}),
);
}
}, [isSuccess]);

return (
Expand Down

0 comments on commit c3dd3b3

Please sign in to comment.