1import { PermissionStatus, createPermissionHook, UnavailabilityError, } from 'expo-modules-core'; 2import * as React from 'react'; 3import { Platform } from 'react-native'; 4import ExpoBarCodeScannerModule from './ExpoBarCodeScannerModule'; 5import ExpoBarCodeScannerView from './ExpoBarCodeScannerView'; 6const { BarCodeType, Type } = ExpoBarCodeScannerModule; 7const EVENT_THROTTLE_MS = 500; 8export { PermissionStatus }; 9export class BarCodeScanner extends React.Component { 10 lastEvents = {}; 11 lastEventsTimes = {}; 12 static Constants = { 13 BarCodeType, 14 Type, 15 }; 16 static ConversionTables = { 17 type: Type, 18 }; 19 static defaultProps = { 20 type: Type.back, 21 barCodeTypes: Object.values(BarCodeType), 22 }; 23 static async getPermissionsAsync() { 24 return ExpoBarCodeScannerModule.getPermissionsAsync(); 25 } 26 static async requestPermissionsAsync() { 27 return ExpoBarCodeScannerModule.requestPermissionsAsync(); 28 } 29 // @needsAudit 30 /** 31 * Check or request permissions for the barcode scanner. 32 * This uses both `requestPermissionAsync` and `getPermissionsAsync` to interact with the permissions. 33 * 34 * @example 35 * ```ts 36 * const [status, requestPermission] = BarCodeScanner.usePermissions(); 37 * ``` 38 */ 39 static usePermissions = createPermissionHook({ 40 getMethod: BarCodeScanner.getPermissionsAsync, 41 requestMethod: BarCodeScanner.requestPermissionsAsync, 42 }); 43 static async scanFromURLAsync(url, barCodeTypes = Object.values(BarCodeType)) { 44 if (!ExpoBarCodeScannerModule.scanFromURLAsync) { 45 throw new UnavailabilityError('expo-barcode-scanner', 'scanFromURLAsync'); 46 } 47 if (Array.isArray(barCodeTypes) && !barCodeTypes.length) { 48 throw new Error('No barCodeTypes specified; provide at least one barCodeType for scanner'); 49 } 50 if (Platform.OS === 'ios') { 51 if (Array.isArray(barCodeTypes) && !barCodeTypes.includes(BarCodeType.qr)) { 52 // Only QR type is supported on iOS, fail if one tries to use other types 53 throw new Error('Only QR type is supported by scanFromURLAsync() on iOS'); 54 } 55 // on iOS use only supported QR type 56 return await ExpoBarCodeScannerModule.scanFromURLAsync(url, [BarCodeType.qr]); 57 } 58 // On other platforms, if barCodeTypes is not provided, use all available types 59 return await ExpoBarCodeScannerModule.scanFromURLAsync(url, barCodeTypes); 60 } 61 render() { 62 const nativeProps = this.convertNativeProps(this.props); 63 const { onBarCodeScanned } = this.props; 64 return (React.createElement(ExpoBarCodeScannerView, { ...nativeProps, onBarCodeScanned: this.onObjectDetected(onBarCodeScanned) })); 65 } 66 onObjectDetected = (callback) => ({ nativeEvent }) => { 67 const { type } = nativeEvent; 68 if (this.lastEvents[type] && 69 this.lastEventsTimes[type] && 70 JSON.stringify(nativeEvent) === this.lastEvents[type] && 71 Date.now() - this.lastEventsTimes[type] < EVENT_THROTTLE_MS) { 72 return; 73 } 74 if (callback) { 75 callback(nativeEvent); 76 this.lastEventsTimes[type] = new Date(); 77 this.lastEvents[type] = JSON.stringify(nativeEvent); 78 } 79 }; 80 convertNativeProps(props) { 81 const nativeProps = {}; 82 for (const [key, value] of Object.entries(props)) { 83 if (typeof value === 'string' && BarCodeScanner.ConversionTables[key]) { 84 nativeProps[key] = BarCodeScanner.ConversionTables[key][value]; 85 } 86 else { 87 nativeProps[key] = value; 88 } 89 } 90 return nativeProps; 91 } 92} 93export const { Constants, getPermissionsAsync, requestPermissionsAsync, usePermissions } = BarCodeScanner; 94//# sourceMappingURL=BarCodeScanner.js.map