Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Android - It is impossible to scan the receipt with the camera, an error is displayed. #35640

Merged
merged 5 commits into from
Feb 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading