add navigation focus to AudioPlayer component and fix the jest

This commit is contained in:
Reinaldo Neto 2023-10-05 02:07:11 -03:00
parent 5625d5311b
commit c157552bfb
2 changed files with 22 additions and 1 deletions

View File

@ -3,6 +3,7 @@ import { View } from 'react-native';
import { AVPlaybackStatus } from 'expo-av';
import { activateKeepAwake, deactivateKeepAwake } from 'expo-keep-awake';
import { useSharedValue } from 'react-native-reanimated';
import { useNavigation } from '@react-navigation/native';
import { useTheme } from '../../theme';
import styles from './styles';
@ -36,6 +37,8 @@ const AudioPlayer = ({
const audioUri = useRef<string>('');
const navigation = useNavigation();
const onPlaybackStatusUpdate = (status: AVPlaybackStatus) => {
if (status) {
onPlaying(status);
@ -124,6 +127,16 @@ const AudioPlayer = ({
}
}, [paused]);
useEffect(() => {
const unsubscribeFocus = navigation.addListener('focus', () => {
audioPlayer.setOnPlaybackStatusUpdate(audioUri.current, onPlaybackStatusUpdate);
});
return () => {
unsubscribeFocus();
};
}, [navigation]);
return (
<>
<View style={[styles.audioContainer, { backgroundColor: colors.surfaceTint, borderColor: colors.strokeExtraLight }]}>

View File

@ -32,7 +32,15 @@ const mockedNavigate = jest.fn();
jest.mock('@react-navigation/native', () => ({
...jest.requireActual('@react-navigation/native'),
useNavigation: () => mockedNavigate
useNavigation: () => ({
navigate: jest.fn(),
addListener: jest.fn().mockImplementation((event, callback) => {
callback();
return {
remove: jest.fn()
};
})
})
}));
jest.mock('react-native-notifications', () => ({