Skip to content

Commit

Permalink
Merge pull request Expensify#35640 from Krishna2323/krishna2323/issue…
Browse files Browse the repository at this point in the history
…/34750

fix: Android - It is impossible to scan the receipt with the camera, an error is displayed.
  • Loading branch information
aldo-expensify authored Feb 12, 2024
2 parents 805c891 + 6cf8848 commit 7aba910
Showing 1 changed file with 35 additions and 21 deletions.
56 changes: 35 additions & 21 deletions src/pages/iou/request/step/IOURequestStepScan/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,26 @@ function IOURequestStepScan({
const camera = useRef(null);
const [flash, setFlash] = useState(false);
const [cameraPermissionStatus, setCameraPermissionStatus] = useState(undefined);
const askedForPermission = useRef(false);

const {translate} = useLocalize();

const askForPermissions = (showPermissionsAlert = true) => {
// 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
CameraPermission.requestCameraPermission()
.then((status) => {
setCameraPermissionStatus(status);

if (status === RESULTS.BLOCKED && showPermissionsAlert) {
FileUtils.showCameraPermissionsAlert();
}
})
.catch(() => {
setCameraPermissionStatus(RESULTS.UNAVAILABLE);
});
};

const focusIndicatorOpacity = useSharedValue(0);
const focusIndicatorScale = useSharedValue(2);
const focusIndicatorPosition = useSharedValue({x: 0, y: 0});
Expand Down Expand Up @@ -104,14 +121,22 @@ function IOURequestStepScan({
});

useEffect(() => {
const refreshCameraPermissionStatus = () => {
const refreshCameraPermissionStatus = (shouldAskForPermission = false) => {
CameraPermission.getCameraPermissionStatus()
.then(setCameraPermissionStatus)
.then((res) => {
// In android device app data, the status is not set to blocked until denied twice,
// due to that the app will ask for permission twice whenever users opens uses the scan tab
setCameraPermissionStatus(res);
if (shouldAskForPermission && !askedForPermission.current) {
askedForPermission.current = true;
askForPermissions(false);
}
})
.catch(() => setCameraPermissionStatus(RESULTS.UNAVAILABLE));
};

// Check initial camera permission status
refreshCameraPermissionStatus();
refreshCameraPermissionStatus(true);

// Refresh permission status when app gain focus
const subscription = AppState.addEventListener('change', (appState) => {
Expand Down Expand Up @@ -146,22 +171,6 @@ function IOURequestStepScan({
return true;
};

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
CameraPermission.requestCameraPermission()
.then((status) => {
setCameraPermissionStatus(status);

if (status === RESULTS.BLOCKED) {
FileUtils.showCameraPermissionsAlert();
}
})
.catch(() => {
setCameraPermissionStatus(RESULTS.UNAVAILABLE);
});
};

const navigateBack = () => {
Navigation.goBack();
};
Expand Down Expand Up @@ -213,6 +222,11 @@ function IOURequestStepScan({
};

const capturePhoto = useCallback(() => {
if (!camera.current && (cameraPermissionStatus === RESULTS.DENIED || cameraPermissionStatus === RESULTS.BLOCKED)) {
askForPermissions(cameraPermissionStatus !== RESULTS.DENIED);
return;
}

const showCameraAlert = () => {
Alert.alert(translate('receipt.cameraErrorTitle'), translate('receipt.cameraErrorMessage'));
};
Expand Down Expand Up @@ -245,7 +259,7 @@ function IOURequestStepScan({
showCameraAlert();
Log.warn('Error taking photo', error);
});
}, [flash, action, translate, transactionID, updateScanAndNavigate, navigateToConfirmationStep]);
}, [flash, action, translate, transactionID, updateScanAndNavigate, navigateToConfirmationStep, cameraPermissionStatus]);

// Wait for camera permission status to render
if (cameraPermissionStatus == null) {
Expand Down Expand Up @@ -278,7 +292,7 @@ function IOURequestStepScan({
text={translate('common.continue')}
accessibilityLabel={translate('common.continue')}
style={[styles.p9, styles.pt5]}
onPress={askForPermissions}
onPress={capturePhoto}
/>
</View>
)}
Expand Down

0 comments on commit 7aba910

Please sign in to comment.