From 7ee891b90d8335464f18a8a5b56643bce86942c8 Mon Sep 17 00:00:00 2001 From: DiegoOTdC Date: Mon, 17 Aug 2020 10:17:18 +0200 Subject: [PATCH 01/10] add expo's BarCodeScanner to new BarcodeScanner screen, navigate to it from homescreen onPress 'Scan Barcode' --- App.js | 2 ++ package-lock.json | 9 ++++++ package.json | 3 +- src/screens/BarcodeScanner/index.js | 46 +++++++++++++++++++++++++++++ src/screens/HomeScreen/index.js | 2 +- 5 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 src/screens/BarcodeScanner/index.js diff --git a/App.js b/App.js index d174bdc..c0236ed 100644 --- a/App.js +++ b/App.js @@ -4,6 +4,7 @@ import store from "./src/store"; import { NavigationContainer } from "@react-navigation/native"; import { createStackNavigator } from "@react-navigation/stack"; import HomeScreen from "./src/screens/HomeScreen"; +import BarcodeScanner from "./src/screens/BarcodeScanner"; import Camera from "./src/screens/Camera"; import Preview from "./src/screens/Preview"; import Recipes from "./src/screens/Recipes"; @@ -33,6 +34,7 @@ function App() { + diff --git a/package-lock.json b/package-lock.json index 0cadd27..1a9f37f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3495,6 +3495,15 @@ "url-parse": "^1.4.4" } }, + "expo-barcode-scanner": { + "version": "8.2.1", + "resolved": "https://registry.npmjs.org/expo-barcode-scanner/-/expo-barcode-scanner-8.2.1.tgz", + "integrity": "sha512-cmsXLoWThqYtfWpsFOg7KCLgJsYOHbj9LG1wpFG86xSdJlQrfZRQ1GzrA4eT06rp8FdGs9nLJPh8/10AH9EZoA==", + "requires": { + "lodash": "^4.6.0", + "prop-types": "^15.6.0" + } + }, "expo-camera": { "version": "8.3.1", "resolved": "https://registry.npmjs.org/expo-camera/-/expo-camera-8.3.1.tgz", diff --git a/package.json b/package.json index c4849c4..f767d88 100644 --- a/package.json +++ b/package.json @@ -39,7 +39,8 @@ "react-native-web": "~0.11.7", "react-redux": "^7.2.1", "redux": "^4.0.5", - "redux-thunk": "^2.3.0" + "redux-thunk": "^2.3.0", + "expo-barcode-scanner": "~8.2.1" }, "devDependencies": { "@babel/core": "^7.8.6", diff --git a/src/screens/BarcodeScanner/index.js b/src/screens/BarcodeScanner/index.js new file mode 100644 index 0000000..a7153a2 --- /dev/null +++ b/src/screens/BarcodeScanner/index.js @@ -0,0 +1,46 @@ +import React, { useState, useEffect } from "react"; +import { Text, View, StyleSheet, Button } from "react-native"; +import { BarCodeScanner } from "expo-barcode-scanner"; + +export default function BarcodeScanner() { + const [hasPermission, setHasPermission] = useState(null); + const [scanned, setScanned] = useState(false); + + useEffect(() => { + (async () => { + const { status } = await BarCodeScanner.requestPermissionsAsync(); + setHasPermission(status === "granted"); + })(); + }, []); + + const handleBarCodeScanned = ({ type, data }) => { + setScanned(true); + alert(`Bar code with type ${type} and data ${data} has been scanned!`); + }; + + if (hasPermission === null) { + return Requesting for camera permission; + } + if (hasPermission === false) { + return No access to camera; + } + + return ( + + + + {scanned && ( +