Skip to content

Commit

Permalink
rn-navio integration
Browse files Browse the repository at this point in the history
  • Loading branch information
kanzitelli committed Oct 1, 2022
1 parent c5c94d1 commit 5b93bbb
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 132 deletions.
27 changes: 17 additions & 10 deletions App.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
import 'expo-dev-client';
import React, {useCallback, useEffect, useState} from 'react';
import * as SplashScreen from 'expo-splash-screen';
import {LogBox} from 'react-native';
import * as SplashScreen from 'expo-splash-screen';
import {GestureHandlerRootView} from 'react-native-gesture-handler';

import {AppRoot} from './src/screens';
import {configureDesignSystem} from './src/utils/designSystem';
import {
configureDesignSystem,
getNavigationTheme,
getStatusBarBGColor,
getStatusBarStyle,
} from './src/utils/designSystem';
import {hydrateStores} from './src/stores';
import {initServices} from './src/services';
import {AppearanceProvider, SSProvider} from './src/utils/providers';
import {SSProvider} from './src/utils/providers';
import {StatusBar} from 'expo-status-bar';
import {useAppearance} from './src/utils/hooks';

LogBox.ignoreLogs(['Require']);

export default (): JSX.Element => {
useAppearance();
const [ready, setReady] = useState(false);

const startApp = useCallback(async () => {
const start = useCallback(async () => {
await SplashScreen.preventAutoHideAsync();

await hydrateStores();
Expand All @@ -27,16 +35,15 @@ export default (): JSX.Element => {
}, []);

useEffect(() => {
startApp();
}, [startApp]);
start();
}, [start]);

if (!ready) <></>;
if (!ready) return <></>;
return (
<GestureHandlerRootView style={{flex: 1}}>
<SSProvider>
<AppearanceProvider>
<AppRoot />
</AppearanceProvider>
<StatusBar style={getStatusBarStyle()} backgroundColor={getStatusBarBGColor()} />
<AppRoot navigationContainerProps={{theme: getNavigationTheme()}} />
</SSProvider>
</GestureHandlerRootView>
);
Expand Down
49 changes: 36 additions & 13 deletions src/screens/_screen-sample.tsx
Original file line number Diff line number Diff line change
@@ -1,42 +1,65 @@
import React from 'react';
import React, {useEffect} from 'react';
import {ScrollView} from 'react-native';
import {View} from 'react-native-ui-lib';
import {observer} from 'mobx-react';
import {useNavigation} from '@react-navigation/native';
import {NavioScreen} from 'rn-navio';

import {useServices} from '../services';
import {services, useServices} from '../services';
// import {useStores} from '../stores';
import {Section} from '../components/section';
import {BButton} from '../components/button';
import {useAppearance} from '../utils/hooks';

export type Props = {
type?: 'push' | 'show';
type?: 'push';
};
export const Example: React.FC<Props> = observer(({type = 'push'}) => {

export const Example: NavioScreen<Props> = observer(({type = 'push'}) => {
useAppearance(); // for Dark Mode
const {nav, t} = useServices();
const navigation = useNavigation();
const {t, navio} = useServices();
// const {ui} = useStores();

// State

// Methods
const push = () => nav.push('Example');
const show = () => nav.show('ExampleModal');
const goBack = async () => {
nav.pop();
const push = () => navio.push('Example', {type: 'push'});
const pushStack = () => navio.pushStack('ExampleStack');
const jumpTo = () => navio.jumpTo('PlaygroundTab');
const show = () => navio.show('ExampleModal');
const setRoot = () => navio.setRoot('Tabs');
const goBack = () => navio.pop();

// Start
useEffect(() => {
configureUI();
}, []);

// UI Methods
const configureUI = () => {
navigation.setOptions({});
};

// UI Methods

return (
<View flex bg-bgColor>
<ScrollView contentInsetAdjustmentBehavior="always">
<Section title={t.do('section.navigation.title')}>
<BButton marginV-s1 label={t.do('section.navigation.button.push')} onPress={push} />
<BButton marginV-s1 label={t.do('section.navigation.button.show')} onPress={show} />
<BButton marginV-s1 label={t.do('section.navigation.button.back')} onPress={goBack} />
<Section title={t.do('section.navio.title')}>
<BButton marginV-s1 label={t.do('section.navio.button.push')} onPress={push} />
<BButton marginV-s1 label={t.do('section.navio.button.push_stack')} onPress={pushStack} />
<BButton marginV-s1 label={t.do('section.navio.button.jump_to')} onPress={jumpTo} />
<BButton marginV-s1 label={t.do('section.navio.button.show')} onPress={show} />
<BButton marginV-s1 label={t.do('section.navio.button.back')} onPress={goBack} />
<BButton marginV-s1 label={'Set Root - Tabs'} onPress={setRoot} />
</Section>
</ScrollView>
</View>
);
});

Example.options = props => ({
headerBackTitleStyle: false,
title: `${services.t.do('example.title')} ${(props?.route?.params as Props)?.type ?? ''}`,
});
154 changes: 55 additions & 99 deletions src/screens/index.tsx
Original file line number Diff line number Diff line change
@@ -1,106 +1,62 @@
import pick from 'lodash/pick';
import {Navio} from 'rn-navio';

import {Main} from './main';
import {Playground} from './playground';
import {Settings} from './settings';
import {Example, Props as ExampleProps} from './_screen-sample';
import {ModalsInfo, ScreensInfo, TabsInfo} from '../services/navigation/types';
import {Root, Stack} from '../services/navigation/layouts';
import {screenDefaultOptions, tabBarDefaultOptions} from '../services/navigation/options';
import {services} from '../services';
import {Example} from './_screen-sample';

// Describe screens props here
// They will be also used for defining screens, tabs and modals names
type ScreenProps = {
Main: undefined;
Playground: undefined;
Settings: undefined;
Example: ExampleProps;
};
type ModalProps = {
ExampleModal: undefined;
};
type TabProps = {
MainTab: undefined;
PlaygroundTab: undefined;
SettingsTab: undefined;
};
export type ScreenAndModalProps = ModalProps & ScreenProps;
import {useAppearance} from '../utils/hooks';
import {screenDefaultOptions, tabDefaultOptions, getTabBarIcon} from '../utils/designSystem';

export type ScreenName = keyof ScreenProps;
export type ModalName = keyof ModalProps;
export type TabName = keyof TabProps;
// NAVIO
export const navio = Navio.build({
screens: {
Main,
Settings,
Example,
Playground: {
component: Playground,
options: () => ({
title: 'Playground',
}),
},
},
stacks: {
ExampleStack: ['Example'],
},
tabs: {
MainTab: {
stack: ['Main', 'Example'],
options: {
title: 'Home',
tabBarIcon: getTabBarIcon('MainTab'),
},
},
PlaygroundTab: {
stack: ['Playground'],
options: () => ({
title: 'Playground',
tabBarIcon: getTabBarIcon('PlaygroundTab'),
}),
},
SettingsTab: {
stack: ['Settings'],
options: () => ({
title: 'Settings',
tabBarIcon: getTabBarIcon('SettingsTab'),
}),
},
},
modals: {
ExampleModal: 'ExampleStack',
},
root: 'Tabs',
hooks: [useAppearance],
options: {
stack: screenDefaultOptions,
tab: tabDefaultOptions,
},
});

// Screens
const screens: ScreensInfo = {
Main: {
component: Main,
options: () => ({
title: services.t.do('home.title'),
...screenDefaultOptions(),
}),
},
Playground: {
component: Playground,
options: () => ({
title: 'Playground',
...screenDefaultOptions(),
}),
},
Settings: {
component: Settings,
options: () => ({
title: services.t.do('settings.title'),
...screenDefaultOptions(),
}),
},
Example: {
component: Example,
options: () => ({
title: services.t.do('example.title'),
...screenDefaultOptions(),
}),
},
};
const HomeStack = () => <Stack screens={pick(screens, ['Main', 'Example'])} />;
const PlaygroundStack = () => <Stack screens={pick(screens, ['Playground'])} />;
const SettingsStack = () => <Stack screens={pick(screens, ['Settings'])} />;
const ExampleStack = () => <Stack screens={pick(screens, ['Example'])} />;

// Tabs
const tabs: TabsInfo = {
MainTab: {
component: HomeStack,
options: () => ({
title: 'Home',
...tabBarDefaultOptions('MainTab'),
}),
},
PlaygroundTab: {
component: PlaygroundStack,
options: () => ({
title: 'Playground',
...tabBarDefaultOptions('PlaygroundTab'),
}),
},
SettingsTab: {
component: SettingsStack,
options: () => ({
title: 'Settings',
...tabBarDefaultOptions('SettingsTab'),
}),
},
};

// Modals
const modals: ModalsInfo = {
ExampleModal: {
component: ExampleStack,
options: () => ({
title: 'ExampleModal',
}),
},
};

// Root
export const AppRoot: React.FC = () => <Root tabs={tabs} modals={modals} />;
export const getNavio = () => navio;
export const AppRoot = navio.Root;
28 changes: 19 additions & 9 deletions src/screens/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,23 @@ import {Text, View} from 'react-native-ui-lib';
import Constants from 'expo-constants';
import * as Application from 'expo-application';
import {If} from '@kanzitelli/if-component';
import {useNavigation} from '@react-navigation/native';
import {observer} from 'mobx-react';
import {useNavigation} from '@react-navigation/native';
import {NavioScreen} from 'rn-navio';

import {useServices} from '../services';
import {services, useServices} from '../services';
import {useStores} from '../stores';
import {Section} from '../components/section';
import {BButton, HeaderButton} from '../components/button';
import {Reanimated2} from '../components/reanimated2';
import {Row} from '../components/row';
import {useAppearance} from '../utils/hooks';

export const Main: React.FC = observer(({}) => {
export const Main: NavioScreen = observer(({}) => {
useAppearance();
const navigation = useNavigation();
const {counter, ui} = useStores();
const {t, api, nav} = useServices();
const {t, api, navio} = useServices();

// State (local)
const [loading, setLoading] = useState(false);
Expand All @@ -39,8 +40,11 @@ export const Main: React.FC = observer(({}) => {
}, [api.counter, counter]);

// Methods
const push = () => nav.push('Example', {type: 'push'});
const show = () => nav.show('ExampleModal');
const push = () => navio.push('Example', {type: 'push'});
const pushStack = () => navio.pushStack('ExampleStack');
const jumpTo = () => navio.jumpTo('PlaygroundTab');
const show = () => navio.show('ExampleModal');
const setRoot = () => navio.setRoot('ExampleStack');

const handleCounterDec = () => counter.set('value', counter.value - 1);
const handleCounterInc = () => counter.set('value', counter.value + 1);
Expand Down Expand Up @@ -76,9 +80,12 @@ export const Main: React.FC = observer(({}) => {
</Text>
</Section>

<Section title={t.do('section.navigation.title')}>
<BButton marginV-s1 label={t.do('section.navigation.button.push')} onPress={push} />
<BButton marginV-s1 label={t.do('section.navigation.button.show')} onPress={show} />
<Section title={t.do('section.navio.title')}>
<BButton marginV-s1 label={t.do('section.navio.button.push')} onPress={push} />
<BButton marginV-s1 label={t.do('section.navio.button.push_stack')} onPress={pushStack} />
<BButton marginV-s1 label={t.do('section.navio.button.jump_to')} onPress={jumpTo} />
<BButton marginV-s1 label={t.do('section.navio.button.show')} onPress={show} />
<BButton marginV-s1 label={'Set Root - Stack'} onPress={setRoot} />
</Section>

<Section title="Reanimated 2">
Expand Down Expand Up @@ -115,3 +122,6 @@ export const Main: React.FC = observer(({}) => {
</View>
);
});
Main.options = () => ({
title: services.t.do('home.title'),
});
7 changes: 6 additions & 1 deletion src/screens/settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {ScrollView} from 'react-native';
import {Text, View, SegmentedControl, Colors} from 'react-native-ui-lib';
import {observer} from 'mobx-react';
import {useNavigation} from '@react-navigation/native';
import {NavioScreen} from 'rn-navio';

import {Section} from '../components/section';
import {Row} from '../components/row';
Expand All @@ -17,8 +18,9 @@ import {
import {useAppearance} from '../utils/hooks';
import {useStores} from '../stores';
import {HeaderButton} from '../components/button';
import {services} from '../services';

export const Settings: React.FC = observer(({}) => {
export const Settings: NavioScreen = observer(({}) => {
useAppearance();
const navigation = useNavigation();
const {ui} = useStores();
Expand Down Expand Up @@ -103,3 +105,6 @@ export const Settings: React.FC = observer(({}) => {
</View>
);
});
Settings.options = () => ({
title: services.t.do('settings.title'),
});

0 comments on commit 5b93bbb

Please sign in to comment.