-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
237 lines (210 loc) · 6.73 KB
/
App.js
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
import React, { Component } from 'react';
import { Alert, View, Text, Vibration, StyleSheet, ActivityIndicator, Button } from 'react-native';
import { Camera, BarCodeScanner, Permissions } from 'expo';
import { Ionicons } from '@expo/vector-icons';
class Scanner extends Component {
constructor(props) {
super(props);
this.state = {
hasCameraPermission: null,
type: Camera.Constants.Type.back,
};
this.onBarCodeRead = this.onBarCodeRead.bind(this);
}
async componentWillMount() {
const { status } = await Permissions.askAsync(Permissions.CAMERA);
await this.setState({hasCameraPermission: status === 'granted'});
this.resetScanner();
}
onBarCodeRead({ type, data }) {
if ((type === this.state.scannedItem.type && data === this.state.scannedItem.data) || data === null) {
return;
}
if (type != this.props.codeType) {
return;
}
this.setState({ scannedItem: { data, type } });
this.props.onBarCodeScanned(data);
this.resetScanner();
}
resetScanner() {
this.scannedCode = null;
this.setState({
scannedItem: {
type: null,
data: null
}
});
}
render() {
const { hasCameraPermission } = this.state;
if (hasCameraPermission === null) {
return <Text>Requesting for camera permission</Text>;
}
if (hasCameraPermission === false) {
return <Text>No access to camera</Text>;
}
return (
<View style={{ flex: 1 }}>
<Text style={styles.scanScreenMessage}>{this.props.prompt}</Text>
<View style={{ flex: 1 }}>
<BarCodeScanner
onBarCodeScanned={this.onBarCodeRead}
style={StyleSheet.absoluteFill}
/>
</View>
</View>
);
}
}
export default class ExpoScanner extends Component {
constructor(props) {
super(props);
// this.onBarCodeRead = this.onBarCodeRead.bind(this);
// this.renderMessage = this.renderMessage.bind(this);
// this.scannedCode = null;
this.state = {
// mode: "ticket",
'ticket': null,
'key': null,
'saveError': null
};
// this.onTicketScanned = this.onTicketScanned.bind(this);
// this.onKeyScanned = this.onKeyScanned.bind(this);
}
renderAlert(title, message) {
Alert.alert(
title,
message,
[
{ text: 'OK', onPress: () => this.resetScanner() },
],
{ cancelable: true }
);
}
//onTicketScanned(data) {
onTicketScanned = (data) => {
Vibration.vibrate(100);
console.log('ticket', data);
this.setState({ticket: data})
}
// onKeyScanned(data) {
onKeyScanned = (data) => {
Vibration.vibrate(100);
console.log('key', data);
this.setState({key: data}, this.doSave);
}
reset = () => {
this.setState({ticket: null, key: null, saveError: null});
}
doSave = () => {
const {ticket, key} = this.state;
console.log(this.state);
this.setState({'saveError': null});
// const url = 'https://postb.in/TMdThJ8N'
//const url = 'http://kac.lan:8000/api/v1/bind_token'
const url = 'http://192.168.1.10:8000/api/v1/bind_token'
fetch(`${url}?ticket_code=${ticket}&token=${key}`, {
method: 'POST',
// headers: {
// Accept: 'application/json',
// 'Content-Type': 'application/json',
// },
// body: JSON.stringify({
// firstParam: 'yourValue',
// secondParam: 'yourOtherValue',
// }),
})
.then(function(response) { return response.json(); })
.then((data) => {
if (data.status != 'ok') {
throw data.detail
}
Vibration.vibrate(100);
this.reset();
}).catch((error) => {
Vibration.vibrate(400);
console.log(error);
this.setState({'saveError': error})
});
}
// onBarCodeRead({ type, data }) {
// if ((type === this.state.scannedItem.type && data === this.state.scannedItem.data) || data === null) {
// return;
// }
// Vibration.vibrate();
// this.setState({ scannedItem: { data, type } });
// if (type == BarCodeScanner.Constants.BarCodeType.ean13) {
// // Do something for EAN
// console.log(`EAN scanned: ${data}`);
// this.resetScanner();
// this.props.navigation.navigate('YOUR_NEXT_SCREEN', { ean: data });
// } else if (type == BarCodeScanner.Constants.BarCodeType.qr) {
// // Do samething for QRCode
// console.log(`QRCode scanned: ${data}`);
// this.resetScanner();
// } else {
// this.renderAlert(
// 'This barcode is not supported.',
// `${type} : ${data}`,
// );
// }
// }
// renderMessage() {
// if (this.state.scannedItem && this.state.scannedItem.type) {
// const { type, data } = this.state.scannedItem;
// return (
// <Text style={styles.scanScreenMessage}>
// {`Scanned \n ${type} \n ${data}`}
// </Text>
// );
// }
// return <Text style={styles.scanScreenMessage}>Focus the barcode to scan.</Text>;
// }
render() {
const {ticket, key, saveError} = this.state;
let el = null
let bgColor = '#fff';
if (!ticket) {
el = <Scanner prompt="Scan ticket" onBarCodeScanned={this.onTicketScanned} codeType={BarCodeScanner.Constants.BarCodeType.qr} />
bgColor = '#f90';
} else if (ticket && !key) {
el = <Scanner prompt="Scan key" onBarCodeScanned={this.onKeyScanned} codeType={BarCodeScanner.Constants.BarCodeType.ean8} />
bgColor = '#9f0';
} else if (saveError) {
bgColor = '#f99';
el = <View style={{flex:1,flexDirection: 'column', alignItems:'center', justifyContent: 'center', padding: 10}}>
<View style={{flex:1,flexDirection: 'row', alignItems:'center', justifyContent: 'center', padding: 10}}>
<Ionicons name="md-alert" size={32} color="red" />
<Text>{saveError.toString()}</Text>
</View>
<View style={{flex:1,flexDirection: 'row', alignItems:'center', justifyContent: 'center', padding: 10}}>
<Button onPress={this.doSave} title="Try again" style={{margin: 10}}/>
<Button onPress={this.reset} title="Scan another" style={{margin: 10}}/>
</View>
</View>
} else {
el = <View style={{flex:1,flexDirection: 'row', alignItems:'center', justifyContent: 'center', padding: 10}}>
<ActivityIndicator size="large" color="#0000ff" style={{paddingRight: 10}}/>
<Text>Saving...</Text>
</View>
}
return <View style={{...styles.container, backgroundColor:bgColor}}>
{el}
</View>
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
paddingTop: 32,
backgroundColor: '#fff',
},
scanScreenMessage: {
fontSize: 40,
// color: 'white',
textAlign: 'center',
alignItems: 'center',
justifyContent: 'center'
}
});