From 4b6b39e90635c5d077a4ad1835a037aa0f41fc1c Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Wed, 11 Oct 2023 12:43:30 -0600 Subject: [PATCH 1/2] fix lint on main --- src/pages/iou/ReceiptSelector/index.native.js | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/pages/iou/ReceiptSelector/index.native.js b/src/pages/iou/ReceiptSelector/index.native.js index 04e748f161a1..c26b529ae173 100644 --- a/src/pages/iou/ReceiptSelector/index.native.js +++ b/src/pages/iou/ReceiptSelector/index.native.js @@ -1,4 +1,4 @@ -import {ActivityIndicator, Alert, AppState, Linking, Text, View} from 'react-native'; +import {ActivityIndicator, Alert, AppState, Text, View, Linking} from 'react-native'; import React, {useCallback, useEffect, useRef, useState} from 'react'; import {useCameraDevices} from 'react-native-vision-camera'; import lodashGet from 'lodash/get'; @@ -102,6 +102,27 @@ function ReceiptSelector({route, report, iou, transactionID, isInTabNavigator}) }; }, []); + /** + * Inform the users when they need to grant camera access and guide them to settings + */ + const showPermissionsAlert = useCallback(() => { + Alert.alert( + translate('attachmentPicker.cameraPermissionRequired'), + translate('attachmentPicker.expensifyDoesntHaveAccessToCamera'), + [ + { + text: translate('common.cancel'), + style: 'cancel', + }, + { + text: translate('common.settings'), + onPress: () => Linking.openSettings(), + }, + ], + {cancelable: false}, + ); + }, [translate]); + const askForPermissions = () => { // There's no way we can check for the BLOCKED status without requesting the permission first // https://github.com/zoontek/react-native-permissions/blob/a836e114ce3a180b2b23916292c79841a267d828/README.md?plain=1#L670 From 47149ff8ac7b4e8f2e48df8f707372dea8c9d964 Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Wed, 11 Oct 2023 12:51:20 -0600 Subject: [PATCH 2/2] create FileUtils.showCameraPermissionsAlert --- .../AttachmentPicker/index.native.js | 27 +++---------------- src/libs/fileDownload/FileUtils.js | 22 +++++++++++++++ src/pages/iou/ReceiptSelector/index.native.js | 25 ++--------------- 3 files changed, 27 insertions(+), 47 deletions(-) diff --git a/src/components/AttachmentPicker/index.native.js b/src/components/AttachmentPicker/index.native.js index 0167bcbb7a4e..063314a4268c 100644 --- a/src/components/AttachmentPicker/index.native.js +++ b/src/components/AttachmentPicker/index.native.js @@ -1,7 +1,7 @@ import _ from 'underscore'; import React, {useState, useRef, useCallback, useMemo} from 'react'; import PropTypes from 'prop-types'; -import {View, Alert, Linking} from 'react-native'; +import {View, Alert} from 'react-native'; import RNDocumentPicker from 'react-native-document-picker'; import RNFetchBlob from 'react-native-blob-util'; import lodashCompact from 'lodash/compact'; @@ -110,27 +110,6 @@ function AttachmentPicker({type, children, shouldHideCameraOption}) { const {translate} = useLocalize(); const {isSmallScreenWidth} = useWindowDimensions(); - /** - * Inform the users when they need to grant camera access and guide them to settings - */ - const showPermissionsAlert = useCallback(() => { - Alert.alert( - translate('attachmentPicker.cameraPermissionRequired'), - translate('attachmentPicker.expensifyDoesntHaveAccessToCamera'), - [ - { - text: translate('common.cancel'), - style: 'cancel', - }, - { - text: translate('common.settings'), - onPress: () => Linking.openSettings(), - }, - ], - {cancelable: false}, - ); - }, [translate]); - /** * A generic handling when we don't know the exact reason for an error */ @@ -155,7 +134,7 @@ function AttachmentPicker({type, children, shouldHideCameraOption}) { if (response.errorCode) { switch (response.errorCode) { case 'permission': - showPermissionsAlert(); + FileUtils.showCameraPermissionsAlert(); return resolve(); default: showGeneralAlert(); @@ -168,7 +147,7 @@ function AttachmentPicker({type, children, shouldHideCameraOption}) { return resolve(response.assets); }); }), - [showGeneralAlert, showPermissionsAlert, type], + [showGeneralAlert, type], ); /** diff --git a/src/libs/fileDownload/FileUtils.js b/src/libs/fileDownload/FileUtils.js index 144965b63336..ba06b80f7c43 100644 --- a/src/libs/fileDownload/FileUtils.js +++ b/src/libs/fileDownload/FileUtils.js @@ -48,6 +48,27 @@ function showPermissionErrorAlert() { ]); } +/** + * Inform the users when they need to grant camera access and guide them to settings + */ +function showCameraPermissionsAlert() { + Alert.alert( + Localize.translateLocal('attachmentPicker.cameraPermissionRequired'), + Localize.translateLocal('attachmentPicker.expensifyDoesntHaveAccessToCamera'), + [ + { + text: Localize.translateLocal('common.cancel'), + style: 'cancel', + }, + { + text: Localize.translateLocal('common.settings'), + onPress: () => Linking.openSettings(), + }, + ], + {cancelable: false}, + ); +} + /** * Generate a random file name with timestamp and file extension * @param {String} url @@ -213,6 +234,7 @@ export { showGeneralErrorAlert, showSuccessAlert, showPermissionErrorAlert, + showCameraPermissionsAlert, splitExtensionFromFileName, getAttachmentName, getFileType, diff --git a/src/pages/iou/ReceiptSelector/index.native.js b/src/pages/iou/ReceiptSelector/index.native.js index c26b529ae173..f2654a9faefb 100644 --- a/src/pages/iou/ReceiptSelector/index.native.js +++ b/src/pages/iou/ReceiptSelector/index.native.js @@ -1,4 +1,4 @@ -import {ActivityIndicator, Alert, AppState, Text, View, Linking} from 'react-native'; +import {ActivityIndicator, Alert, AppState, Text, View} from 'react-native'; import React, {useCallback, useEffect, useRef, useState} from 'react'; import {useCameraDevices} from 'react-native-vision-camera'; import lodashGet from 'lodash/get'; @@ -102,27 +102,6 @@ function ReceiptSelector({route, report, iou, transactionID, isInTabNavigator}) }; }, []); - /** - * Inform the users when they need to grant camera access and guide them to settings - */ - const showPermissionsAlert = useCallback(() => { - Alert.alert( - translate('attachmentPicker.cameraPermissionRequired'), - translate('attachmentPicker.expensifyDoesntHaveAccessToCamera'), - [ - { - text: translate('common.cancel'), - style: 'cancel', - }, - { - text: translate('common.settings'), - onPress: () => Linking.openSettings(), - }, - ], - {cancelable: false}, - ); - }, [translate]); - const askForPermissions = () => { // There's no way we can check for the BLOCKED status without requesting the permission first // https://github.com/zoontek/react-native-permissions/blob/a836e114ce3a180b2b23916292c79841a267d828/README.md?plain=1#L670 @@ -131,7 +110,7 @@ function ReceiptSelector({route, report, iou, transactionID, isInTabNavigator}) setCameraPermissionStatus(status); if (status === RESULTS.BLOCKED) { - showPermissionsAlert(); + FileUtils.showCameraPermissionsAlert(); } }) .catch(() => {