-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathApp.tsx
198 lines (184 loc) · 4.77 KB
/
App.tsx
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
import * as React from "react";
import {
Text,
View,
Button,
Linking,
StyleSheet,
TouchableOpacity,
ActivityIndicator,
} from "react-native";
import RNPickerSelect from "react-native-picker-select";
import { Camera } from "expo-camera";
import * as FileSystem from "expo-file-system";
import extractTextFromImage from "./api/extractTextFromImage";
import createRepl from "./api/createRepl";
import {
getMainFileForLanguage,
SupportedLanguage,
LANGUAGES,
} from "./api/languages";
export default function App() {
const [hasPermission, setHasPermission] = React.useState<boolean | null>(
null
);
const [type, setType] = React.useState(Camera.Constants.Type.back);
const [processingImage, setProcessingImage] = React.useState<boolean>(false);
const [imageText, setImageText] = React.useState<string>("");
const [language, setLanguage] = React.useState<string>("python3");
let camera: Camera | null = null;
React.useEffect(() => {
(async () => {
const { status } = await Camera.requestPermissionsAsync();
setHasPermission(status === "granted");
})();
}, []);
if (hasPermission === null) {
return <View />;
}
if (hasPermission === false) {
return <Text>Give me camera pls :(</Text>;
}
if (processingImage) {
return (
<View style={styles.loadingContainer}>
<ActivityIndicator size="large" />
</View>
);
}
if (imageText) {
return (
<View style={styles.imageTextContainer}>
<Text style={styles.imageTextHeader}>Received:</Text>
<Text style={styles.imageText}>{imageText}</Text>
<Text style={styles.languageSelectText}>Select a language:</Text>
<View style={styles.languageSelector}>
<RNPickerSelect
onValueChange={(value) => {
setLanguage(value);
}}
placeholder={{}}
items={LANGUAGES}
/>
</View>
<Button
title="Create Repl"
onPress={async () => {
const name = getMainFileForLanguage(language as SupportedLanguage);
const files = [
{
name,
content: imageText,
},
];
const repl = await createRepl({ files, language });
if (repl) {
// url is of the form /@username/slug
const url = `https://repl.it${repl.url}`;
await Linking.openURL(url);
}
}}
/>
<Button title="Try Again" onPress={() => setImageText("")} />
</View>
);
}
return (
<View style={styles.container}>
<Camera
ref={(ref) => {
camera = ref;
}}
style={styles.cameraContainer}
type={type}
>
<View style={styles.cameraView}>
<TouchableOpacity
style={styles.flipButtonContainer}
onPress={() => {
setType(
type === Camera.Constants.Type.back
? Camera.Constants.Type.front
: Camera.Constants.Type.back
);
}}
>
<Text style={styles.flipButtonText}> Flip </Text>
</TouchableOpacity>
<TouchableOpacity
style={styles.captureButton}
onPress={async () => {
const { uri } = await camera!.takePictureAsync();
setProcessingImage(true);
const encoded = await FileSystem.readAsStringAsync(uri, {
encoding: FileSystem.EncodingType.Base64,
});
const content = await extractTextFromImage(encoded);
if (content) {
setImageText(content);
}
setProcessingImage(false);
}}
></TouchableOpacity>
</View>
</Camera>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
},
cameraContainer: {
flex: 1,
},
cameraView: {
flex: 1,
flexDirection: "row",
justifyContent: "center",
alignItems: "flex-end",
backgroundColor: "transparent",
},
loadingContainer: {
flex: 1,
justifyContent: "center",
alignItems: "center",
},
imageTextContainer: {
flex: 1,
justifyContent: "center",
alignItems: "center",
},
imageTextHeader: {
fontSize: 30,
marginBottom: 20,
},
imageText: {
fontSize: 20,
marginBottom: 20,
},
languageSelector: {
marginBottom: 20,
},
languageSelectText: {
fontSize: 20,
marginBottom: 15,
},
captureButton: {
width: 70,
height: 70,
borderRadius: 35,
borderWidth: 5,
borderColor: "#FFF",
marginBottom: 50,
},
flipButtonContainer: {
position: "absolute",
left: 20,
bottom: 30,
},
flipButtonText: {
fontSize: 32,
color: "white",
},
});