-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.tsx
More file actions
230 lines (220 loc) · 6.29 KB
/
Copy pathApp.tsx
File metadata and controls
230 lines (220 loc) · 6.29 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
//
// Top-level component accessed in the project (equivalent to a main function)
//
import React, { useEffect, useState } from 'react';
import { SafeAreaView, StatusBar } from 'react-native';
import AsyncStorage from '@react-native-async-storage/async-storage';
import { appStyles } from './src/styles.js';
import MainPage from './src/pages/MainPage';
import CameraPage from './src/pages/CameraPage';
import Structure from './src/types/Structure';
import CameraRollPage from './src/pages/CameraRollPage';
import Loading from './src/components/Loading';
import EditPage from './src/pages/EditPage';
import PreviousStructuresPage from './src/pages/PreviousStructuresPage';
// 2 default structures added to previously saved structures to begin with
const defaultStructures = [
{
structure: {
isDfa: false,
states: [
{
id: 0,
name: 'a',
isStart: true,
isFinal: false,
},
{
id: 1,
name: 'b',
isStart: false,
isFinal: false,
},
{
id: 2,
name: 'c',
isStart: false,
isFinal: false,
},
{
id: 3,
name: 'd',
isStart: false,
isFinal: true,
},
],
transitions: [
{ id: 0, start: 0, end: 1, token: '1' },
{ id: 1, start: 0, end: 2, token: 'ε' },
{ id: 2, start: 0, end: 3, token: '1' },
{ id: 3, start: 1, end: 3, token: '0' },
{ id: 4, start: 1, end: 3, token: '1' },
{ id: 5, start: 2, end: 3, token: 'ε' },
{ id: 6, start: 3, end: 3, token: '0' },
],
},
type: 'nfa',
},
{
structure: {
isDfa: true,
states: [
{
id: 0,
name: 'q0',
isStart: true,
isFinal: false,
},
{
id: 1,
name: 'q1',
isStart: false,
isFinal: true,
},
{
id: 2,
name: 'q2',
isStart: false,
isFinal: true,
},
{
id: 3,
name: 'q3',
isStart: false,
isFinal: false,
},
{
id: 4,
name: 'q4',
isStart: false,
isFinal: true,
},
{
id: 5,
name: 'q5',
isStart: false,
isFinal: false,
},
{
id: 6,
name: 'q6',
isStart: false,
isFinal: false,
},
],
transitions: [
{ id: 0, start: 0, end: 3, token: '0' },
{ id: 1, start: 0, end: 1, token: '1' },
{ id: 2, start: 1, end: 2, token: '0' },
{ id: 3, start: 1, end: 5, token: '1' },
{ id: 4, start: 2, end: 2, token: '0' },
{ id: 5, start: 2, end: 5, token: '1' },
{ id: 6, start: 3, end: 0, token: '0' },
{ id: 7, start: 3, end: 4, token: '1' },
{ id: 8, start: 4, end: 2, token: '0' },
{ id: 9, start: 4, end: 5, token: '1' },
{ id: 10, start: 5, end: 5, token: '0' },
{ id: 11, start: 5, end: 5, token: '1' },
{ id: 12, start: 6, end: 6, token: '0' },
{ id: 13, start: 6, end: 6, token: '1' },
],
},
type: 'nfa',
},
];
type PageProps = {
pageNumber: number;
setPageNumber: (newPageNumber: number) => void;
structure: Structure;
setStructure: (newStructure: Structure) => void;
setIsLoading: (newIsLoading: boolean) => void;
savedStructure: Structure;
setSavedStructure: (newStructure: Structure) => void;
};
// Handles which page to display
const Page = (props: PageProps) => {
switch (props.pageNumber) {
case 0:
return (
<MainPage
setPageNumber={props.setPageNumber}
structure={props.structure}
setStructure={props.setStructure}
savedStructure={props.savedStructure}
setSavedStructure={props.setSavedStructure}
/>
);
case 1:
return (
<CameraPage
setPageNumber={props.setPageNumber}
setStructure={props.setStructure}
setIsLoading={props.setIsLoading}
/>
);
case 2:
return (
<CameraRollPage
setPageNumber={props.setPageNumber}
setIsLoading={props.setIsLoading}
setStructure={props.setStructure}
/>
);
case 3:
return (
<EditPage
setPageNumber={props.setPageNumber}
structure={props.structure}
setStructure={props.setStructure}
setSavedStructure={props.setSavedStructure}
/>
);
case 4:
return (
<PreviousStructuresPage
setPageNumber={props.setPageNumber}
setStructure={props.setStructure}
/>
);
default:
return <></>;
}
};
function App(): JSX.Element {
const [pageNumber, setPageNumber] = useState(0);
const [structure, setStructure] = useState<Structure>(defaultStructures[0]);
const [isLoading, setIsLoading] = useState(false);
const [savedStructure, setSavedStructure] = useState<Structure>(defaultStructures[0]);
useEffect(() => {
const setDefaultPreviousStructures = async () => { // Async function used in useEffect, so must pre-define, then call
try {
const previousStructures = await AsyncStorage.getItem('previous-structures'); // Get previous structures
if (previousStructures === null) { // If no exist, first time opening the app, so set them to be the default structures.
// This will only happen the very first time the app is opened, as AsyncStorage persits over hard app resets.
await AsyncStorage.setItem('previous-structures', JSON.stringify(defaultStructures));
}
} catch (error) {
console.error(error);
}
};
setDefaultPreviousStructures();
}, []);
return (
<>
<SafeAreaView style={appStyles.container}>
<StatusBar barStyle={'dark-content'} />
<Page
pageNumber={pageNumber}
setPageNumber={setPageNumber}
structure={structure}
setStructure={setStructure}
setIsLoading={setIsLoading}
savedStructure={savedStructure}
setSavedStructure={setSavedStructure}
/>
</SafeAreaView>
{isLoading ? <Loading /> : <></>}
</>
);
}
export default App;