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