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: request camera permission only after screen transition is complete #38270

Merged
merged 1 commit into from
Mar 18, 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
65 changes: 35 additions & 30 deletions src/pages/iou/request/step/IOURequestStepScan/index.native.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {useFocusEffect} from '@react-navigation/core';
import lodashGet from 'lodash/get';
import React, {useCallback, useEffect, useRef, useState} from 'react';
import {ActivityIndicator, Alert, AppState, View} from 'react-native';
import React, {useCallback, useRef, useState} from 'react';
import {ActivityIndicator, Alert, AppState, InteractionManager, View} from 'react-native';
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';
Expand Down Expand Up @@ -120,37 +121,41 @@ function IOURequestStepScan({
runOnJS(focusCamera)(point);
});

useEffect(() => {
const refreshCameraPermissionStatus = (shouldAskForPermission = false) => {
CameraPermission.getCameraPermissionStatus()
.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(true);
useFocusEffect(
useCallback(() => {
const refreshCameraPermissionStatus = (shouldAskForPermission = false) => {
CameraPermission.getCameraPermissionStatus()
.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));
};

InteractionManager.runAfterInteractions(() => {
// Check initial camera permission status
refreshCameraPermissionStatus(true);
});

// Refresh permission status when app gain focus
const subscription = AppState.addEventListener('change', (appState) => {
if (appState !== 'active') {
return;
}
// Refresh permission status when app gain focus
const subscription = AppState.addEventListener('change', (appState) => {
if (appState !== 'active') {
return;
}

refreshCameraPermissionStatus();
});
refreshCameraPermissionStatus();
});

return () => {
subscription.remove();
};
}, []);
return () => {
subscription.remove();
};
}, []),
);

const validateReceipt = (file) => {
const {fileExtension} = FileUtils.splitExtensionFromFileName(lodashGet(file, 'name', ''));
Expand Down
Loading