Skip to content

Commit 52dc502

Browse files
committed
feat: upgrade to react-native-vision-camera
1 parent 19e559c commit 52dc502

5 files changed

Lines changed: 58 additions & 72 deletions

File tree

configs/e2e/native_dependencies.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"react-native-svg": "15.11.1",
1010
"react-native-video": "6.10.0",
1111
"@react-native-async-storage/async-storage": "2.0.0",
12-
"react-native-camera": "3.40.0",
12+
"react-native-vision-camera": "4.7.1",
1313
"react-native-view-shot": "4.0.3",
1414
"react-native-blob-util": "0.21.2",
1515
"react-native-file-viewer-turbo": "0.6.0",

packages/pluggableWidgets/barcode-scanner-native/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"@mendix/piw-utils-internal": "*",
2424
"deprecated-react-native-prop-types": "^4.0.0",
2525
"react-native-barcode-mask": "^1.2.4",
26-
"react-native-camera": "3.40.0"
26+
"react-native-vision-camera": "4.7.1"
2727
},
2828
"devDependencies": {
2929
"@mendix/pluggable-widgets-tools": "*"
Lines changed: 40 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { flattenStyles } from "@mendix/piw-native-utils-internal";
22
import { ValueStatus } from "mendix";
3-
import { Component, createElement } from "react";
3+
import { createElement, ReactElement, useMemo } from "react";
44
import { View } from "react-native";
5-
import { RNCamera } from "react-native-camera";
5+
import { Camera, useCodeScanner, Code, useCameraDevice } from "react-native-vision-camera";
66
import BarcodeMask from "react-native-barcode-mask";
77

88
import { BarcodeScannerProps } from "../typings/BarcodeScannerProps";
@@ -11,55 +11,48 @@ import { executeAction } from "@mendix/piw-utils-internal";
1111

1212
export 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
}

patches/react-native-camera+3.40.0.patch

Lines changed: 0 additions & 20 deletions
This file was deleted.

pnpm-lock.yaml

Lines changed: 16 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)