Skip to content

Commit

Permalink
fix: android-scan camera preview in Scan becomes blank
Browse files Browse the repository at this point in the history
  • Loading branch information
dukenv0307 committed Oct 18, 2023
1 parent 9d79fd1 commit 18d8ccc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,21 @@ import {Camera} from 'react-native-vision-camera';
import {useTabAnimation} from '@react-navigation/material-top-tabs';
import {useNavigation} from '@react-navigation/native';
import PropTypes from 'prop-types';
import CONST from '../../../CONST';

const propTypes = {
/* The index of the tab that contains this camera */
cameraTabIndex: PropTypes.number.isRequired,

/* Whether we're in a tab navigator */
isInTabNavigator: PropTypes.bool.isRequired,

/** Name of the selected receipt tab */
selectedTab: PropTypes.string.isRequired,
};

// Wraps a camera that will only be active when the tab is focused or as soon as it starts to become focused.
const NavigationAwareCamera = React.forwardRef(({cameraTabIndex, isInTabNavigator, ...props}, ref) => {
const NavigationAwareCamera = React.forwardRef(({cameraTabIndex, isInTabNavigator, selectedTab, ...props}, ref) => {
// Get navigation to get initial isFocused value (only needed once during init!)
const navigation = useNavigation();
const [isCameraActive, setIsCameraActive] = useState(navigation.isFocused());
Expand All @@ -31,6 +35,9 @@ const NavigationAwareCamera = React.forwardRef(({cameraTabIndex, isInTabNavigato
}

const listenerId = tabPositionAnimation.addListener(({value}) => {
if (selectedTab !== CONST.TAB.SCAN) {
return;
}
// Activate camera as soon the index is animating towards the `cameraTabIndex`
setIsCameraActive(value > cameraTabIndex - 1 && value < cameraTabIndex + 1);
});
Expand Down
7 changes: 6 additions & 1 deletion src/pages/iou/ReceiptSelector/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,20 @@ const propTypes = {

/** Whether or not the receipt selector is in a tab navigator for tab animations */
isInTabNavigator: PropTypes.bool,

/** Name of the selected receipt tab */
selectedTab: PropTypes.string,
};

const defaultProps = {
report: {},
iou: iouDefaultProps,
transactionID: '',
isInTabNavigator: true,
selectedTab: '',
};

function ReceiptSelector({route, report, iou, transactionID, isInTabNavigator}) {
function ReceiptSelector({route, report, iou, transactionID, isInTabNavigator, selectedTab}) {
const devices = useCameraDevices('wide-angle-camera');
const device = devices.back;

Expand Down Expand Up @@ -195,6 +199,7 @@ function ReceiptSelector({route, report, iou, transactionID, isInTabNavigator})
photo
cameraTabIndex={pageIndex}
isInTabNavigator={isInTabNavigator}
selectedTab={selectedTab}
/>
)}
<View style={[styles.flexRow, styles.justifyContentAround, styles.alignItemsCenter, styles.pv3]}>
Expand Down

0 comments on commit 18d8ccc

Please sign in to comment.