1 | import { PermissionStatus, createPermissionHook, UnavailabilityError, } from 'expo-modules-core';
|
2 | import * as React from 'react';
|
3 | import { Platform } from 'react-native';
|
4 | import ExpoBarCodeScannerModule from './ExpoBarCodeScannerModule';
|
5 | import ExpoBarCodeScannerView from './ExpoBarCodeScannerView';
|
6 | const { BarCodeType, Type } = ExpoBarCodeScannerModule;
|
7 | const EVENT_THROTTLE_MS = 500;
|
8 | export class BarCodeScanner extends React.Component {
|
9 | lastEvents = {};
|
10 | lastEventsTimes = {};
|
11 | static Constants = {
|
12 | BarCodeType,
|
13 | Type,
|
14 | };
|
15 | static ConversionTables = {
|
16 | type: Type,
|
17 | };
|
18 | static defaultProps = {
|
19 | type: Type.back,
|
20 | barCodeTypes: Object.values(BarCodeType),
|
21 | };
|
22 |
|
23 | |
24 |
|
25 |
|
26 |
|
27 | static async getPermissionsAsync() {
|
28 | return ExpoBarCodeScannerModule.getPermissionsAsync();
|
29 | }
|
30 |
|
31 | |
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 | static async requestPermissionsAsync() {
|
38 | return ExpoBarCodeScannerModule.requestPermissionsAsync();
|
39 | }
|
40 |
|
41 | |
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 | static usePermissions = createPermissionHook({
|
51 | getMethod: BarCodeScanner.getPermissionsAsync,
|
52 | requestMethod: BarCodeScanner.requestPermissionsAsync,
|
53 | });
|
54 |
|
55 | |
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 | static async scanFromURLAsync(url, barCodeTypes = Object.values(BarCodeType)) {
|
66 | if (!ExpoBarCodeScannerModule.scanFromURLAsync) {
|
67 | throw new UnavailabilityError('expo-barcode-scanner', 'scanFromURLAsync');
|
68 | }
|
69 | if (Array.isArray(barCodeTypes) && !barCodeTypes.length) {
|
70 | throw new Error('No barCodeTypes specified; provide at least one barCodeType for scanner');
|
71 | }
|
72 | if (Platform.OS === 'ios') {
|
73 | if (Array.isArray(barCodeTypes) && !barCodeTypes.includes(BarCodeType.qr)) {
|
74 |
|
75 | throw new Error('Only QR type is supported by scanFromURLAsync() on iOS');
|
76 | }
|
77 |
|
78 | return await ExpoBarCodeScannerModule.scanFromURLAsync(url, [BarCodeType.qr]);
|
79 | }
|
80 |
|
81 | return await ExpoBarCodeScannerModule.scanFromURLAsync(url, barCodeTypes);
|
82 | }
|
83 | render() {
|
84 | const nativeProps = this.convertNativeProps(this.props);
|
85 | const { onBarCodeScanned } = this.props;
|
86 | return (React.createElement(ExpoBarCodeScannerView, { ...nativeProps, onBarCodeScanned: this.onObjectDetected(onBarCodeScanned) }));
|
87 | }
|
88 | onObjectDetected = (callback) => ({ nativeEvent }) => {
|
89 | const { type } = nativeEvent;
|
90 | if (this.lastEvents[type] &&
|
91 | this.lastEventsTimes[type] &&
|
92 | JSON.stringify(nativeEvent) === this.lastEvents[type] &&
|
93 | Date.now() - this.lastEventsTimes[type] < EVENT_THROTTLE_MS) {
|
94 | return;
|
95 | }
|
96 | if (callback) {
|
97 | callback(nativeEvent);
|
98 | this.lastEventsTimes[type] = new Date();
|
99 | this.lastEvents[type] = JSON.stringify(nativeEvent);
|
100 | }
|
101 | };
|
102 | convertNativeProps(props) {
|
103 | const nativeProps = {};
|
104 | for (const [key, value] of Object.entries(props)) {
|
105 | if (typeof value === 'string' && BarCodeScanner.ConversionTables[key]) {
|
106 | nativeProps[key] = BarCodeScanner.ConversionTables[key][value];
|
107 | }
|
108 | else {
|
109 | nativeProps[key] = value;
|
110 | }
|
111 | }
|
112 | return nativeProps;
|
113 | }
|
114 | }
|
115 | export { PermissionStatus };
|
116 | export const { Constants, getPermissionsAsync, requestPermissionsAsync, usePermissions, scanFromURLAsync, } = BarCodeScanner;
|
117 |
|
\ | No newline at end of file |