-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.tsx
More file actions
258 lines (213 loc) · 6.42 KB
/
Copy pathApp.tsx
File metadata and controls
258 lines (213 loc) · 6.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
//import * as React from 'react';
import {StatusBar} from 'expo-status-bar'
import React from 'react'
import {StyleSheet, Text, View, TouchableOpacity, SafeAreaView, Alert, Button, TextInput} from 'react-native'
import { NavigationContainer } from '@react-navigation/native';
import { createNativeStackNavigator } from '@react-navigation/native-stack';
import {Camera} from 'expo-camera'
import { BarCodeScanner } from 'expo-barcode-scanner';
import fdaapi from './FDAAPI.tsx'
import database from './Database.tsx'
// this is for the log in screen
function HomeScreen({ navigation }) {
const [username, setUsername] = React.useState(null)
return (
<SafeAreaView>
<Text> Enter Username</Text>
<TextInput
style={styles.input}
placeholder="e.g scuterez"
onChangeText = {(val) => setUsername(val)}
/>
<Button
title="Log In"
// enter username and then send it to database
onPress={() => {navigation.navigate('Home'); database.functions.login(username);}}
/>
</SafeAreaView>
);
}
//this is for the screen after the log in where the user chooses to put in a new recipe or search for an existing one
function DetailsScreen({navigation}) {
return (
<SafeAreaView style={styles.container}>
<View>
<Button
title="Start New Recipe"
onPress={() => navigation.navigate('Camera')}
/>
</View>
<View>
<Button
title="Saved Recipes"
color="#f194ff"
onPress={() => navigation.navigate('Recipes Saved')}
/>
</View>
</SafeAreaView>
);
}
function RecipesSavedScreen() {
const [SavedRecipe, setSavedRecipe] = React.useState(null)
return (
<SafeAreaView>
<Text> Enter Recipe Name </Text>
<TextInput
style={styles.input}
placeholder="e.g Spaghetti"
onChangeText = {(val) => setSavedRecipe(val)}
//need to make the app find the recipe to see if it already exist and then print out the information
/>
<Button
title="Find Recipe"
onPress = {()=> database.functions.readRecipe(SavedRecipe)}
/>
<Button
title="Load Recipe"
onPress = {()=> Alert.alert(database.functions.getRecipe()) }
/>
<Text> {database.functions.getRecipe()} </Text>
</SafeAreaView>
);
}
function CameraScreen() {
const [startCamera,setStartCamera] = React.useState(false);
const [scanned, setScanned] = React.useState(false);
const [text, setText] = React.useState('Not yet scanned')
const [newRecipe, setNewRecipe] = React.useState(null)
const [servingsize, setServingsize] = React.useState(null)
// this is for camera permission and to start the camera when its pressed
const __startCamera = async () => {
const {status} = await BarCodeScanner.requestPermissionsAsync()
if (status === 'granted') {
// start the camera
setStartCamera(true)
} else {
Alert.alert('Access denied')
}
}
// this is to scan and get the number
const handleBarCodeScanned = ({ type, data }) => {
setScanned(true);
setText(data)
barcode = data.substring(1, data.length)
fdaapi.functions.parseBarcode(barcode);
}
return (
<View style={styles.container}>
{startCamera ? (
<View style={styles.container}>
<Button
title = "Upload Recipe"
onPress = {()=>database.functions.uploadRecipe()}
/>
<View style={styles.barcodebox}>
<BarCodeScanner
onBarCodeScanned={scanned ? undefined : handleBarCodeScanned}
style={{ height: 400, width: 400 }} />
</View>
{scanned && <Button
title='Scan again?'
onPress={() => setScanned(false)} color='#dc143c' />}
<Text> Serving Size </Text>
<TextInput
//keyboardType = 'numeric'
style={styles.input}
//the number they put is what we send to multiply by to get total calories
placeholder="Number of Servings"
onChangeText = {(val) => setServingsize(val)}
/>
<Button
title = "Go"
onPress = {()=> {database.functions.addIngredient(fdaapi.functions.getItemName(),fdaapi.functions.getCalories(Number(servingsize))); fdaapi.functions.clear();}}
/>
<Text> {servingsize} of this item contains {fdaapi.functions.getCalories(Number(servingsize))} </Text>
</View>
) : (
<View
style={{
flex: 1,
backgroundColor: '#fff',
justifyContent: 'center',
alignItems: 'center'
}}
>
<Text> Name of Recipe </Text>
<TextInput
style={styles.input}
//name to save into recipes
placeholder="Enter name of recipe"
onChangeText = {(val) => {setNewRecipe(val); database.functions.addRecipe(newRecipe);}}
/>
<TouchableOpacity
onPress={__startCamera}
style={{
width: 200,
borderRadius: 1,
backgroundColor: '#000',
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center',
height: 50
}}
>
<Text
style={{
color: '#fff',
fontWeight: 'bold',
textAlign: 'center'
}}
>
Add Ingredient
</Text>
</TouchableOpacity>
</View>
)}
<StatusBar style="auto" />
</View>
)
}
const Stack = createNativeStackNavigator();
export default function App() {
return (
<NavigationContainer>
<Stack.Navigator initialRouteName="Log In">
<Stack.Screen name="Log In" component={HomeScreen} />
<Stack.Screen name="Home" component={DetailsScreen} />
<Stack.Screen name ="Camera" component ={CameraScreen}/>
<Stack.Screen name = "Recipes Saved" component = {RecipesSavedScreen}/>
</Stack.Navigator>
</NavigationContainer>
);
}
const styles = StyleSheet.create({
input: {
height: 40,
margin: 12,
borderWidth: 1,
padding: 10,
},
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center'
},
maintext: {
fontSize: 20,
margin: 20,
},
barcodebox: {
alignItems: 'center',
justifyContent: 'center',
height: 400,
width: 350,
overflow: 'hidden',
borderRadius: 2,
backgroundColor: '#000'
},
space: {
width: 20,
height: 20,
}
})