-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.tsx
151 lines (137 loc) · 3.48 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
import { BarCodeScanner } from "expo-barcode-scanner";
import { StatusBar } from "expo-status-bar";
import { useEffect, useState } from "react";
import {
Image,
SafeAreaView,
StyleSheet,
Text,
TouchableOpacity,
View,
} from "react-native";
import { findProductByCode, IProduct } from "./services/api";
async function askForPermission() {
const { status } = await BarCodeScanner.requestPermissionsAsync();
return status === "granted";
}
export default function App() {
const [hasPermission, setHasPermission] = useState<boolean | null>(null);
const [scanning, setScanning] = useState(false);
const [product, setProduct] = useState<IProduct | null | undefined>(null);
useEffect(() => {
askForPermission().then(setHasPermission);
}, []);
function onBarCodeScanner(payload: { data: string }) {
// com o código de barras, podemos consultar a nossa API
const product = findProductByCode(payload.data);
setProduct(product);
setScanning(false);
// após o retorna da API, podemos salvar o produto em um state
}
if (hasPermission === null) {
return <Text>Requisitando permissão....</Text>;
}
if (hasPermission === false) {
return <Text>Sem acesso a camera</Text>;
}
return (
<SafeAreaView style={styles.container}>
<StatusBar style="auto" />
<Text style={styles.title}>Easy Barcode Scanner</Text>
<Text style={styles.subtitle}>
Encontre o preço dos itens que você precisa
</Text>
{product === undefined && (
<Text style={styles.subtitle}>Produto não encontrado :(</Text>
)}
{product && (
<View style={styles.productContainer}>
<Text style={styles.productPrice}>{product?.price}</Text>
<Text style={styles.productName}>{product?.name}</Text>
<View style={styles.productImageContainer}>
<Image
style={styles.productImage}
source={{
uri: product?.image,
}}
/>
</View>
</View>
)}
{scanning && (
<BarCodeScanner
onBarCodeScanned={onBarCodeScanner}
style={{
height: 300,
width: "100%",
}}
/>
)}
<TouchableOpacity style={styles.button}>
<Text
style={styles.buttonText}
onPress={() => {
setScanning((prev) => !prev);
setProduct(null);
}}
>
{scanning ? "Cancelar" : "Consultar Preço"}
</Text>
</TouchableOpacity>
</SafeAreaView>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#2CA58D",
alignItems: "center",
justifyContent: "space-around",
},
title: {
fontSize: 30,
fontWeight: "bold",
marginVertical: 40,
color: "#fff",
},
subtitle: {
fontSize: 20,
marginVertical: 20,
marginHorizontal: 10,
},
button: {
marginVertical: 20,
padding: 10,
backgroundColor: "#0A2342",
borderRadius: 10,
},
buttonText: {
color: "#fff",
fontSize: 20,
fontWeight: "bold",
},
productContainer: {
flex: 1,
alignItems: "center",
},
productPrice: {
fontSize: 42,
fontWeight: "bold",
color: "#0A2342",
},
productName: {
fontSize: 30,
fontWeight: "bold",
color: "#0A2342",
},
productImageContainer: {
marginVertical: 30,
width: 350,
height: 350,
},
productImage: {
width: "100%",
height: "100%",
resizeMode: "contain",
},
});