// @flow import * as React from 'react'; import {StyleSheet, View} from "react-native"; import {Button, Text, withTheme} from 'react-native-paper'; import {BarCodeScanner} from "expo-barcode-scanner"; import {Camera} from 'expo-camera'; import URLHandler from "../../utils/URLHandler"; import {Linking} from "expo"; import AlertDialog from "../../components/Dialogs/AlertDialog"; import i18n from 'i18n-js'; import CustomTabBar from "../../components/Tabbar/CustomTabBar"; type Props = {}; type State = { hasPermission: boolean, scanned: boolean, dialogVisible: boolean, dialogTitle: string, dialogMessage: string, }; class ScannerScreen extends React.Component { state = { hasPermission: false, scanned: false, dialogVisible: false, dialogTitle: "", dialogMessage: "", }; constructor() { super(); } componentDidMount() { this.requestPermissions(); } requestPermissions = () => Camera.requestPermissionsAsync().then(this.updatePermissionStatus); updatePermissionStatus = ({status}) => this.setState({hasPermission: status === "granted"}); handleCodeScanned = ({type, data}) => { if (!URLHandler.isUrlValid(data)) this.showErrorDialog(); else { this.setState({scanned: true}); Linking.openURL(data); } }; getPermissionScreen() { return {i18n.t("scannerScreen.errorPermission")} } getOverlay() { return ( ); } showHelpDialog = () => { this.setState({ dialogVisible: true, scanned: true, dialogTitle: i18n.t("scannerScreen.helpTitle"), dialogMessage: i18n.t("scannerScreen.helpMessage"), }); }; showErrorDialog() { this.setState({ dialogVisible: true, scanned: true, dialogTitle: i18n.t("scannerScreen.errorTitle"), dialogMessage: i18n.t("scannerScreen.errorMessage"), }); } onDialogDismiss = () => this.setState({ dialogVisible: false, scanned: false, }); getScanner() { return ( {this.getOverlay()} ); } render() { return ( {this.state.hasPermission ? this.getScanner() : this.getPermissionScreen() } ); } } const borderOffset = '10%'; const overlayBoxStyle = { position: 'absolute', width: 25, height: 25, }; const overlayLineStyle = { position: 'absolute', backgroundColor: "#fff", borderRadius: 2, }; const overlayHorizontalLineStyle = { ...overlayLineStyle, width: '100%', height: 5, }; const overlayVerticalLineStyle = { ...overlayLineStyle, height: '100%', width: 5, }; const overlayBackground = { backgroundColor: "rgba(0,0,0,0.47)", position: "absolute", }; const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', }, cameraContainer: { marginTop: 'auto', marginBottom: 'auto', aspectRatio: 1, width: '100%', }, button: { position: 'absolute', bottom: 5, width: '80%', left: '10%' }, overlayTopLeft: { ...overlayBoxStyle, top: borderOffset, left: borderOffset, }, overlayTopRight: { ...overlayBoxStyle, top: borderOffset, right: borderOffset, }, overlayBottomLeft: { ...overlayBoxStyle, bottom: borderOffset, left: borderOffset, }, overlayBottomRight: { ...overlayBoxStyle, bottom: borderOffset, right: borderOffset, }, }); export default withTheme(ScannerScreen);