refacto: isolate sound logic to external hook

merge-requests/10/head
Torpenn 2024-10-20 10:32:29 +02:00
parent 2ad44f32ad
commit 872a52959f
2 changed files with 52 additions and 33 deletions

View File

@ -11,6 +11,7 @@ import { Sound } from 'expo-av/build/Audio';
import { activateKeepAwakeAsync, deactivateKeepAwake } from 'expo-keep-awake';
import { loadUserSettings } from '@/components/shared/business/AsyncStorage';
import BackgroundTimer from 'react-native-background-timer';
import { useAudio } from '@/components/useCases/timer/business/useAudio';
interface TimerProps {
reps: number;
@ -28,8 +29,8 @@ export default function Timer() {
const [isWorkPhase, setIsWorkPhase] = useState<boolean>(true);
const [isRunning, setIsRunning] = useState<boolean>(false);
const [isFinish, setIsFinish] = useState<boolean>(false);
const [sound, setSound] = useState<Sound>();
const [soundEnabled, setSoundEnabled] = useState<boolean>(true);
const { playSound } = useAudio(require('../assets/audios/boxingBell.mp3'), soundEnabled);
useEffect(() => {
const init = async () => {
@ -37,7 +38,6 @@ export default function Timer() {
const soundEnabledLocal = await loadUserSettings('soundEnabled');
setSoundEnabled(Boolean(Number(soundEnabledLocal)));
if (soundEnabled) await configureAudio();
handleStart();
await activateKeepAwakeAsync();
} catch (error) {
@ -61,16 +61,14 @@ export default function Timer() {
let timerId: number | null = null;
if (isRunning && timeLeft > 0) {
// Utilisation de BackgroundTimer pour gérer le timer même en arrière-plan
timerId = BackgroundTimer.setInterval(() => {
setTimeLeft((prevTime) => prevTime - 1);
}, 1000); // Timer d'une seconde
}, 1000);
} else if (isRunning && timeLeft === 0) {
nextRep(); // Passe à la répétition suivante
nextRep();
}
return () => {
// Nettoyage du timer lorsqu'il est arrêté ou que le composant est démonté
if (timerId !== null) {
BackgroundTimer.clearInterval(timerId);
}
@ -85,33 +83,6 @@ export default function Timer() {
setIsFinish(false);
};
async function configureAudio() {
await Audio.setAudioModeAsync({
allowsRecordingIOS: false,
staysActiveInBackground: false,
playsInSilentModeIOS: true,
shouldDuckAndroid: true,
playThroughEarpieceAndroid: false,
});
}
async function playSound() {
if (!soundEnabled) return;
const { sound } = await Audio.Sound.createAsync(require('../assets/audios/boxingBell.mp3'));
setSound(sound);
await sound.playAsync();
}
useEffect(() => {
return sound
? () => {
sound.unloadAsync();
}
: undefined;
}, [sound]);
const nextRep = () => {
if (currentRep < reps) {
if (isWorkPhase) {

View File

@ -0,0 +1,48 @@
import { useEffect, useState } from "react";
import { Audio } from 'expo-av';
import { Sound } from 'expo-av/build/Audio';
export function useAudio(soundFile: any, soundEnabled: boolean) {
const [sound, setSound] = useState<Sound | null>(null);
useEffect(() => {
const configureAudio = async () => {
await Audio.setAudioModeAsync({
allowsRecordingIOS: false,
staysActiveInBackground: false,
playsInSilentModeIOS: true,
shouldDuckAndroid: true,
playThroughEarpieceAndroid: false,
});
};
if (soundEnabled) {
configureAudio();
}
return () => {
if (sound) {
sound.unloadAsync();
}
};
}, [soundEnabled]);
async function playSound() {
if (!soundEnabled) return;
const { sound } = await Audio.Sound.createAsync(soundFile);
setSound(sound);
await sound.playAsync();
}
useEffect(() => {
return sound
? () => {
sound.unloadAsync();
}
: undefined;
}, [sound]);
return { playSound };
}