Skip to content

Commit

Permalink
Add react navigation
Browse files Browse the repository at this point in the history
  • Loading branch information
adityapawar1 committed Oct 5, 2024
1 parent 88417f4 commit 29b7ef4
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 23 deletions.
19 changes: 5 additions & 14 deletions App.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
// In App.js in a new project

import * as React from 'react';
import { Text, View } from 'react-native';
import { NavigationContainer } from '@react-navigation/native';
import { createNativeStackNavigator } from '@react-navigation/native-stack';
import HomeScreen from '@/app';
import QRCodeScanner from '@/components/QRCodeScanner/QRCodeScanner';
import TreeInfoPage from '@/components/TreeInfoPage/TreeInfoPage';

function HomeScreen() {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Home Screen</Text>
</View>
);
}

const Stack = createNativeStackNavigator();
const Stack = createNativeStackNavigator<RootStackParamList>();

function App() {
return (
Expand All @@ -31,8 +22,8 @@ function App() {
options={{ headerShown: false }}
/>
<Stack.Screen
name="Tree"
component={HomeScreen}
name="TreeInfoPage"
component={TreeInfoPage}
options={{ headerShown: false }}
/>
</Stack.Navigator>
Expand Down
23 changes: 23 additions & 0 deletions src/app/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { StyleSheet, Text, View } from 'react-native';
import { StatusBar } from 'expo-status-bar';
import Logo from '@/components/Logo';

// Dummy home screen for now? idk what to put here
export default function HomeScreen() {
return (
<View style={styles.container}>
<Logo />
<Text>Home Screen!</Text>
<StatusBar style="auto" />
</View>
);
}

const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
27 changes: 18 additions & 9 deletions src/components/QRCodeScanner/QRCodeScanner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ import {
CameraView,
useCameraPermissions,
} from 'expo-camera';
import { NativeStackScreenProps } from '@react-navigation/native-stack';
import styles from './styles';

export default function QRCodeScanner() {
type QRCodeScannerProps = NativeStackScreenProps<RootStackParamList, 'Scanner'>;

export default function QRCodeScanner({ navigation }: QRCodeScannerProps) {
const [permission, requestPermission] = useCameraPermissions();
const [disableScanner, setDisableScanner] = useState<boolean>(false);

Expand All @@ -22,13 +25,19 @@ export default function QRCodeScanner() {
// Disable scanning callback so we don't scan multiple times
setDisableScanner(true);

Alert.alert('YUHHHHH QR Code Scanned', data.data, [
{
text: 'OK',
// Enable scanner after 2 seconds of pressing OK
onPress: () => setTimeout(() => setDisableScanner(false), 2000),
},
]);
Alert.alert(
'YUHHHHH QR Code Scanned',
`Going to tree page with id: ${data.data}`,
[
{
text: 'OK',
// Enable scanner after 2 seconds of pressing OK
onPress: () => navigation.push('TreeInfoPage', { treeId: data.data }),
},
],
);

// setTimeout(() => setDisableScanner(false), 2000),
};

// Camera permissions are still loading.
Expand All @@ -38,7 +47,7 @@ export default function QRCodeScanner() {

// No perms :(
if (!permission.granted) {
return <Text>Bruh you didn't enable perms for the camera</Text>;
return <Text>Permission for camera not granted.</Text>;
}

return (
Expand Down
26 changes: 26 additions & 0 deletions src/components/TreeInfoPage/TreeInfoPage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Pressable, Text, View } from 'react-native';
import { StatusBar } from 'expo-status-bar';
import { NativeStackScreenProps } from '@react-navigation/native-stack';
import Logo from '@/components/Logo';
import styles from './styles';

type TreeInfoPageProps = NativeStackScreenProps<
RootStackParamList,
'TreeInfoPage'
>;

export default function TreeInfoPage({ route, navigation }: TreeInfoPageProps) {
// Just placeholder text for now to show that the tree ID is being passed
return (
<View style={styles.container}>
<Logo />
<Text>i found a tree</Text>
<Text>with an id</Text>
<Text>{route.params.treeId}</Text>
<Pressable onPress={() => navigation.push('Scanner')}>
<Text>Back to scanner</Text>
</Pressable>
<StatusBar style="auto" />
</View>
);
}
10 changes: 10 additions & 0 deletions src/components/TreeInfoPage/styles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { StyleSheet } from 'react-native';

export default StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
6 changes: 6 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Not sure where to keep this stuff
type RootStackParamList = {
Home: undefined;
Scanner: undefined;
TreeInfoPage: { treeId: string };
};

0 comments on commit 29b7ef4

Please sign in to comment.