Skip to content

Commit

Permalink
Merge pull request #38270 from aswin-s/fix/issue-36551
Browse files Browse the repository at this point in the history
fix: request camera permission only after screen transition is complete
  • Loading branch information
marcaaron authored Mar 18, 2024
2 parents 065e52f + a1cd526 commit 17be26d
Showing 1 changed file with 35 additions and 30 deletions.
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

0 comments on commit 17be26d

Please sign in to comment.