From 7c4e9252939fb9e3599b8bb34b0d59ff6b3444ce Mon Sep 17 00:00:00 2001
From: Carlos Alvarez <cead22@gmail.com>
Date: Mon, 1 Apr 2024 13:06:09 -0700
Subject: [PATCH] Revert "Revert "feat: Upgrade VisionCamera to V4""

---
 ios/Podfile.lock                              |  4 +-
 package-lock.json                             | 15 +++++--
 package.json                                  |  2 +-
 .../react-native-vision-camera+2.16.8.patch   | 22 ----------
 .../NavigationAwareCamera/index.native.js     |  1 +
 .../step/IOURequestStepScan/index.native.js   | 43 ++++++++++---------
 6 files changed, 38 insertions(+), 49 deletions(-)
 delete mode 100644 patches/react-native-vision-camera+2.16.8.patch

diff --git a/ios/Podfile.lock b/ios/Podfile.lock
index b2995c35c9b..24ef0704be2 100644
--- a/ios/Podfile.lock
+++ b/ios/Podfile.lock
@@ -1415,7 +1415,7 @@ PODS:
     - SDWebImage/Core (~> 5.17)
   - SocketRocket (0.6.1)
   - Turf (2.7.0)
-  - VisionCamera (2.16.8):
+  - VisionCamera (4.0.0-beta.11):
     - React
     - React-callinvoker
     - React-Core
@@ -1920,7 +1920,7 @@ SPEC CHECKSUMS:
   SDWebImageWebPCoder: af09429398d99d524cae2fe00f6f0f6e491ed102
   SocketRocket: f32cd54efbe0f095c4d7594881e52619cfe80b17
   Turf: 13d1a92d969ca0311bbc26e8356cca178ce95da2
-  VisionCamera: 0a6794d1974aed5d653d0d0cb900493e2583e35a
+  VisionCamera: b6b6f46949eae83b71429c971162af337ef34fa3
   Yoga: e64aa65de36c0832d04e8c7bd614396c77a80047
 
 PODFILE CHECKSUM: a431c146e1501391834a2f299a74093bac53b530
diff --git a/package-lock.json b/package-lock.json
index aa494e6d8cc..210a192ac90 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -114,7 +114,7 @@
         "react-native-tab-view": "^3.5.2",
         "react-native-url-polyfill": "^2.0.0",
         "react-native-view-shot": "3.8.0",
-        "react-native-vision-camera": "2.16.8",
+        "react-native-vision-camera": "^4.0.0-beta.11",
         "react-native-web": "^0.19.9",
         "react-native-web-linear-gradient": "^1.1.2",
         "react-native-web-sound": "^0.1.3",
@@ -39769,11 +39769,18 @@
       }
     },
     "node_modules/react-native-vision-camera": {
-      "version": "2.16.8",
-      "license": "MIT",
+      "version": "4.0.0-beta.11",
+      "resolved": "https://registry.npmjs.org/react-native-vision-camera/-/react-native-vision-camera-4.0.0-beta.11.tgz",
+      "integrity": "sha512-cKg/nwT0q0H1ivEVG+PQt/QxhFgf/dd7SiMm7bCzlSCmt0T2tXyIdLsuY312iKBB2qasQZOYtzRsoQjipHQkDw==",
       "peerDependencies": {
         "react": "*",
-        "react-native": "*"
+        "react-native": "*",
+        "react-native-worklets-core": "*"
+      },
+      "peerDependenciesMeta": {
+        "react-native-worklets-core": {
+          "optional": true
+        }
       }
     },
     "node_modules/react-native-web": {
diff --git a/package.json b/package.json
index f7afb324afd..2305fa870ff 100644
--- a/package.json
+++ b/package.json
@@ -165,7 +165,7 @@
     "react-native-tab-view": "^3.5.2",
     "react-native-url-polyfill": "^2.0.0",
     "react-native-view-shot": "3.8.0",
-    "react-native-vision-camera": "2.16.8",
+    "react-native-vision-camera": "^4.0.0-beta.11",
     "react-native-web": "^0.19.9",
     "react-native-web-linear-gradient": "^1.1.2",
     "react-native-web-sound": "^0.1.3",
diff --git a/patches/react-native-vision-camera+2.16.8.patch b/patches/react-native-vision-camera+2.16.8.patch
deleted file mode 100644
index 3afc4573985..00000000000
--- a/patches/react-native-vision-camera+2.16.8.patch
+++ /dev/null
@@ -1,22 +0,0 @@
-diff --git a/node_modules/react-native-vision-camera/android/build.gradle b/node_modules/react-native-vision-camera/android/build.gradle
-index ddfa243..bafffc3 100644
---- a/node_modules/react-native-vision-camera/android/build.gradle
-+++ b/node_modules/react-native-vision-camera/android/build.gradle
-@@ -334,7 +334,7 @@ if (ENABLE_FRAME_PROCESSORS) {
-   def thirdPartyVersions = new Properties()
-   thirdPartyVersions.load(new FileInputStream(thirdPartyVersionsFile))
- 
--  def BOOST_VERSION = thirdPartyVersions["BOOST_VERSION"]
-+  def BOOST_VERSION = thirdPartyVersions["BOOST_VERSION"] ?: "1.83.0"
-   def boost_file = new File(downloadsDir, "boost_${BOOST_VERSION}.tar.gz")
-   def DOUBLE_CONVERSION_VERSION = thirdPartyVersions["DOUBLE_CONVERSION_VERSION"]
-   def double_conversion_file = new File(downloadsDir, "double-conversion-${DOUBLE_CONVERSION_VERSION}.tar.gz")
-@@ -352,7 +352,7 @@ if (ENABLE_FRAME_PROCESSORS) {
- 
-   task downloadBoost(dependsOn: createNativeDepsDirectories, type: Download) {
-     def transformedVersion = BOOST_VERSION.replace("_", ".")
--    def srcUrl = "https://boostorg.jfrog.io/artifactory/main/release/${transformedVersion}/source/boost_${BOOST_VERSION}.tar.gz"
-+    def srcUrl = "https://archives.boost.io/release/${transformedVersion}/source/boost_${BOOST_VERSION}.tar.gz"
-     if (REACT_NATIVE_VERSION < 69) {
-       srcUrl = "https://github.com/react-native-community/boost-for-react-native/releases/download/v${transformedVersion}-0/boost_${BOOST_VERSION}.tar.gz"
-     }
diff --git a/src/pages/iou/request/step/IOURequestStepScan/NavigationAwareCamera/index.native.js b/src/pages/iou/request/step/IOURequestStepScan/NavigationAwareCamera/index.native.js
index 65c17d3cb7a..64fa291b200 100644
--- a/src/pages/iou/request/step/IOURequestStepScan/NavigationAwareCamera/index.native.js
+++ b/src/pages/iou/request/step/IOURequestStepScan/NavigationAwareCamera/index.native.js
@@ -15,6 +15,7 @@ const NavigationAwareCamera = React.forwardRef(({cameraTabIndex, ...props}, ref)
     return (
         <Camera
             ref={ref}
+            photoQualityBalance="speed"
             // eslint-disable-next-line react/jsx-props-no-spreading
             {...props}
             isActive={isCameraActive}
diff --git a/src/pages/iou/request/step/IOURequestStepScan/index.native.js b/src/pages/iou/request/step/IOURequestStepScan/index.native.js
index e2308d305a2..03f9c6c7969 100644
--- a/src/pages/iou/request/step/IOURequestStepScan/index.native.js
+++ b/src/pages/iou/request/step/IOURequestStepScan/index.native.js
@@ -5,7 +5,7 @@ import {ActivityIndicator, Alert, AppState, InteractionManager, View} from 'reac
 import {Gesture, GestureDetector} from 'react-native-gesture-handler';
 import {RESULTS} from 'react-native-permissions';
 import Animated, {runOnJS, useAnimatedStyle, useSharedValue, withDelay, withSequence, withSpring, withTiming} from 'react-native-reanimated';
-import {useCameraDevices} from 'react-native-vision-camera';
+import {useCameraDevice} from 'react-native-vision-camera';
 import Hand from '@assets/images/hand.svg';
 import Shutter from '@assets/images/shutter.svg';
 import AttachmentPicker from '@components/AttachmentPicker';
@@ -60,9 +60,11 @@ function IOURequestStepScan({
 }) {
     const theme = useTheme();
     const styles = useThemeStyles();
-    const devices = useCameraDevices('wide-angle-camera');
-    const device = devices.back;
+    const device = useCameraDevice('back', {
+        physicalDevices: ['wide-angle-camera'],
+    });
 
+    const hasFlash = device != null && device.hasFlash;
     const camera = useRef(null);
     const [flash, setFlash] = useState(false);
     const [cameraPermissionStatus, setCameraPermissionStatus] = useState(undefined);
@@ -236,8 +238,7 @@ function IOURequestStepScan({
 
         return camera.current
             .takePhoto({
-                qualityPrioritization: 'speed',
-                flash: flash ? 'on' : 'off',
+                flash: flash && hasFlash ? 'on' : 'off',
             })
             .then((photo) => {
                 // Store the receipt on the transaction object in Onyx
@@ -257,7 +258,7 @@ function IOURequestStepScan({
                 showCameraAlert();
                 Log.warn('Error taking photo', error);
             });
-    }, [flash, action, translate, transactionID, updateScanAndNavigate, navigateToConfirmationStep, cameraPermissionStatus]);
+    }, [flash, hasFlash, action, translate, transactionID, updateScanAndNavigate, navigateToConfirmationStep, cameraPermissionStatus]);
 
     // Wait for camera permission status to render
     if (cameraPermissionStatus == null) {
@@ -356,20 +357,22 @@ function IOURequestStepScan({
                         height={CONST.RECEIPT.SHUTTER_SIZE}
                     />
                 </PressableWithFeedback>
-                <PressableWithFeedback
-                    role={CONST.ACCESSIBILITY_ROLE.BUTTON}
-                    accessibilityLabel={translate('receipt.flash')}
-                    style={[styles.alignItemsEnd]}
-                    disabled={cameraPermissionStatus !== RESULTS.GRANTED}
-                    onPress={() => setFlash((prevFlash) => !prevFlash)}
-                >
-                    <Icon
-                        height={32}
-                        width={32}
-                        src={Expensicons.Bolt}
-                        fill={flash ? theme.iconHovered : theme.textSupporting}
-                    />
-                </PressableWithFeedback>
+                {hasFlash && (
+                    <PressableWithFeedback
+                        role={CONST.ACCESSIBILITY_ROLE.BUTTON}
+                        accessibilityLabel={translate('receipt.flash')}
+                        style={[styles.alignItemsEnd]}
+                        disabled={cameraPermissionStatus !== RESULTS.GRANTED}
+                        onPress={() => setFlash((prevFlash) => !prevFlash)}
+                    >
+                        <Icon
+                            height={32}
+                            width={32}
+                            src={Expensicons.Bolt}
+                            fill={flash ? theme.iconHovered : theme.textSupporting}
+                        />
+                    </PressableWithFeedback>
+                )}
             </View>
         </StepScreenWrapper>
     );
