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