Last active
March 12, 2018 16:55
-
-
Save weeravit/1172734db5f8952546aa631be4631cf3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import React from 'react'; | |
| import {Alert, StyleSheet, Text, View} from 'react-native'; | |
| import {BarCodeScanner, Permissions} from 'expo'; | |
| export default class App extends React.Component { | |
| state = { | |
| hasCameraPermission: null, | |
| isQRScanable: true | |
| } | |
| async componentWillMount() { | |
| const {status} = await Permissions.askAsync(Permissions.CAMERA); | |
| this.setState({hasCameraPermission: status === 'granted'}); | |
| } | |
| render() { | |
| const {hasCameraPermission, isQRScanable} = this.state; | |
| if (hasCameraPermission === null) { | |
| return <Text>Requesting for camera permission</Text>; | |
| } else if (hasCameraPermission === false) { | |
| return <Text>No access to camera</Text>; | |
| } else if (!isQRScanable) { | |
| return ( | |
| <View style={{flex: 1}}> | |
| <View style={{flex: 0.2, justifyContent: 'center', alignItems: 'center'}}> | |
| <Text style={{fontSize: 25}}>HELLO QR SCANNER</Text> | |
| </View> | |
| <View style={{flex: 0.6, backgroundColor: '#000000'}}/> | |
| <View style={{flex: 0.2}}/> | |
| </View> | |
| ); | |
| } else { | |
| return ( | |
| <View style={{flex: 1}}> | |
| <View style={{flex: 0.2, justifyContent: 'center', alignItems: 'center'}}> | |
| <Text style={{fontSize: 25}}>HELLO QR SCANNER</Text> | |
| </View> | |
| <View style={{flex: 0.6}}> | |
| <BarCodeScanner | |
| onBarCodeRead={this._handleBarCodeRead} | |
| style={StyleSheet.absoluteFill} | |
| /> | |
| </View> | |
| <View style={{flex: 0.2}}/> | |
| </View> | |
| ); | |
| } | |
| } | |
| _handleBarCodeRead = ({type, data}) => { | |
| // หลังจากที่มันอ่าน data ได้ ให้ handle api ตรงนี้ต่อเลย | |
| this.disableQRScanner() | |
| Alert.alert( | |
| 'Alert Title', | |
| data, | |
| [ | |
| {text: 'OK', onPress: () => this.enableQRScanner()}, | |
| ], | |
| {cancelable: false} | |
| ) | |
| } | |
| disableQRScanner() { | |
| this.setState({ | |
| ...this.state, | |
| isQRScanable: false | |
| }) | |
| } | |
| enableQRScanner() { | |
| this.setState({ | |
| ...this.state, | |
| isQRScanable: true | |
| }) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment