Skip to content

Commit

Permalink
create FileUtils.showCameraPermissionsAlert
Browse files Browse the repository at this point in the history
  • Loading branch information
luacmartins committed Oct 11, 2023
1 parent 4b6b39e commit 47149ff
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 47 deletions.
27 changes: 3 additions & 24 deletions src/components/AttachmentPicker/index.native.js
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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
*/
Expand All @@ -155,7 +134,7 @@ function AttachmentPicker({type, children, shouldHideCameraOption}) {
if (response.errorCode) {
switch (response.errorCode) {
case 'permission':
showPermissionsAlert();
FileUtils.showCameraPermissionsAlert();
return resolve();
default:
showGeneralAlert();
Expand All @@ -168,7 +147,7 @@ function AttachmentPicker({type, children, shouldHideCameraOption}) {
return resolve(response.assets);
});
}),
[showGeneralAlert, showPermissionsAlert, type],
[showGeneralAlert, type],
);

/**
Expand Down
22 changes: 22 additions & 0 deletions src/libs/fileDownload/FileUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -213,6 +234,7 @@ export {
showGeneralErrorAlert,
showSuccessAlert,
showPermissionErrorAlert,
showCameraPermissionsAlert,
splitExtensionFromFileName,
getAttachmentName,
getFileType,
Expand Down
25 changes: 2 additions & 23 deletions src/pages/iou/ReceiptSelector/index.native.js
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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
Expand All @@ -131,7 +110,7 @@ function ReceiptSelector({route, report, iou, transactionID, isInTabNavigator})
setCameraPermissionStatus(status);

if (status === RESULTS.BLOCKED) {
showPermissionsAlert();
FileUtils.showCameraPermissionsAlert();
}
})
.catch(() => {
Expand Down

0 comments on commit 47149ff

Please sign in to comment.