11import { flattenStyles } from "@mendix/piw-native-utils-internal" ;
22import { ValueStatus } from "mendix" ;
3- import { Component , createElement } from "react" ;
3+ import { createElement , ReactElement , useMemo } from "react" ;
44import { View } from "react-native" ;
5- import { RNCamera } from "react-native-camera" ;
5+ import { Camera , useCodeScanner , Code , useCameraDevice } from "react-native-vision -camera" ;
66import BarcodeMask from "react-native-barcode-mask" ;
77
88import { BarcodeScannerProps } from "../typings/BarcodeScannerProps" ;
@@ -11,55 +11,48 @@ import { executeAction } from "@mendix/piw-utils-internal";
1111
1212export type Props = BarcodeScannerProps < BarcodeScannerStyle > ;
1313
14- export class BarcodeScanner extends Component < Props > {
15- private readonly styles = flattenStyles ( defaultBarcodeScannerStyle , this . props . style ) ;
16- private readonly onBarCodeReadHandler = throttle ( this . onBarCodeRead . bind ( this ) , 2000 ) ;
14+ export function BarcodeScanner ( props : Props ) : ReactElement {
15+ const device = useCameraDevice ( "back" ) ;
1716
18- render ( ) : JSX . Element {
19- return (
20- < View style = { this . styles . container } >
21- < RNCamera
22- testID = { this . props . name }
17+ const styles = useMemo ( ( ) => flattenStyles ( defaultBarcodeScannerStyle , props . style ) , [ props . style ] ) ;
18+
19+ const codeScanner = useCodeScanner ( {
20+ codeTypes : [ "ean-13" , "qr" , "aztec" , "codabar" , "code-128" , "data-matrix" ] ,
21+ onCodeScanned : ( codes : Code [ ] ) => {
22+ if ( props . barcode . status !== ValueStatus . Available || codes . length === 0 || ! codes [ 0 ] . value ) {
23+ return ;
24+ }
25+ const { value } = codes [ 0 ] ;
26+
27+ if ( value !== props . barcode . value ) {
28+ props . barcode . setValue ( value ) ;
29+ }
30+ executeAction ( props . onDetect ) ;
31+ }
32+ } ) ;
33+
34+ return (
35+ < View style = { styles . container } >
36+ { device && (
37+ < Camera
38+ testID = { props . name }
2339 style = { { flex : 1 , justifyContent : "center" , alignItems : "center" } }
24- captureAudio = { false }
25- onBarCodeRead = { this . onBarCodeReadHandler }
40+ audio = { false }
41+ isActive = { true }
42+ device = { device }
43+ codeScanner = { codeScanner }
2644 >
27- { this . props . showMask && (
45+ { props . showMask && (
2846 < BarcodeMask
29- edgeColor = { this . styles . mask . color }
30- width = { this . styles . mask . width }
31- height = { this . styles . mask . height }
32- backgroundColor = { this . styles . mask . backgroundColor }
33- showAnimatedLine = { this . props . showAnimatedLine }
47+ edgeColor = { styles . mask . color }
48+ width = { styles . mask . width }
49+ height = { styles . mask . height }
50+ backgroundColor = { styles . mask . backgroundColor }
51+ showAnimatedLine = { props . showAnimatedLine }
3452 />
3553 ) }
36- </ RNCamera >
37- </ View >
38- ) ;
39- }
40-
41- private onBarCodeRead ( event : { data : string } ) : void {
42- if ( this . props . barcode . status !== ValueStatus . Available || ! event . data ) {
43- return ;
44- }
45-
46- if ( event . data !== this . props . barcode . value ) {
47- this . props . barcode . setValue ( event . data ) ;
48- }
49-
50- executeAction ( this . props . onDetect ) ;
51- }
52- }
53-
54- export function throttle < F extends ( ...params : any [ ] ) => void > ( fn : F , threshold : number ) : F {
55- let wait = false ;
56- return function invokeFn ( this : any , ...args : any [ ] ) {
57- if ( ! wait ) {
58- fn ( ...args ) ;
59- wait = true ;
60- setTimeout ( ( ) => {
61- wait = false ;
62- } , threshold ) ;
63- }
64- } as F ;
54+ </ Camera >
55+ ) }
56+ </ View >
57+ ) ;
6558}
0 commit comments