From 872a52959f34ce8c8f00131bd27e167107a90a38 Mon Sep 17 00:00:00 2001 From: Torpenn Date: Sun, 20 Oct 2024 10:32:29 +0200 Subject: [PATCH] refacto: isolate sound logic to external hook --- app/timer.tsx | 37 ++------------ .../useCases/timer/business/useAudio.ts | 48 +++++++++++++++++++ 2 files changed, 52 insertions(+), 33 deletions(-) create mode 100644 components/useCases/timer/business/useAudio.ts diff --git a/app/timer.tsx b/app/timer.tsx index a69ba20..6f7d3ea 100644 --- a/app/timer.tsx +++ b/app/timer.tsx @@ -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(true); const [isRunning, setIsRunning] = useState(false); const [isFinish, setIsFinish] = useState(false); - const [sound, setSound] = useState(); const [soundEnabled, setSoundEnabled] = useState(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) { diff --git a/components/useCases/timer/business/useAudio.ts b/components/useCases/timer/business/useAudio.ts new file mode 100644 index 0000000..d2a0033 --- /dev/null +++ b/components/useCases/timer/business/useAudio.ts @@ -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(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 }; +}