boxons/app/Timer.tsx

183 lines
4.9 KiB
TypeScript
Raw Normal View History

2024-12-08 17:01:28 +00:00
import { useEffect, useState } from "react";
import { useRoute, useNavigation } from "@react-navigation/native";
import { View } from "@/components/shared/Themed";
import styled from "@emotion/native";
import TimerContent from "@/components/useCases/timer/view/TimerContent";
import FinishContent from "@/components/useCases/timer/view/FinishContent";
import { TimerBgColor } from "@/components/useCases/timer/business/type";
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";
import { useNotification } from "@/components/useCases/timer/business/useNotifications";
2024-10-13 11:29:36 +00:00
interface TimerProps {
reps: number;
restTime: number;
workTime: number;
}
export default function Timer() {
const navigation = useNavigation();
2024-10-13 11:29:36 +00:00
const route = useRoute();
const { reps, restTime, workTime } = route.params as TimerProps;
2024-10-13 11:29:36 +00:00
const [currentRep, setCurrentRep] = useState<number>(0);
const [timeLeft, setTimeLeft] = useState<number>(0);
const [isWorkPhase, setIsWorkPhase] = useState<boolean>(true);
const [isRunning, setIsRunning] = useState<boolean>(false);
const [isFinish, setIsFinish] = useState<boolean>(false);
2024-10-13 21:18:08 +00:00
const [soundEnabled, setSoundEnabled] = useState<boolean>(true);
2024-12-08 17:01:28 +00:00
const { playSound } = useAudio(
require("../assets/audios/boxingBell.mp3"),
soundEnabled,
);
const { updateNotification, cancelNotification } = useNotification(
"123456789",
{
channelId: "timer-channel",
channelName: "Timer Notification",
channelDescription: "Notifications pour le timer",
},
);
2024-10-13 11:29:36 +00:00
useEffect(() => {
2024-10-13 21:18:08 +00:00
const init = async () => {
try {
2024-12-08 17:01:28 +00:00
const soundEnabledLocal = await loadUserSettings("soundEnabled");
2024-10-13 21:18:08 +00:00
setSoundEnabled(Boolean(Number(soundEnabledLocal)));
handleStart();
await activateKeepAwakeAsync();
} catch (error) {
2024-12-08 17:01:28 +00:00
throw new Error("Erreur lors du chargement des paramètres utilisateur");
2024-10-13 21:18:08 +00:00
}
};
init();
2024-10-13 11:29:36 +00:00
}, []);
// when the user exits the screen, desactivate the keepAwake
useEffect(() => {
2024-12-08 17:01:28 +00:00
const unsubscribe = navigation.addListener("beforeRemove", (_) => {
deactivateKeepAwake();
});
return unsubscribe;
}, [navigation]);
2024-10-13 11:29:36 +00:00
useEffect(() => {
2024-10-20 08:01:01 +00:00
let timerId: number | null = null;
2024-10-13 11:29:36 +00:00
if (isRunning && timeLeft > 0) {
2024-10-20 08:01:01 +00:00
timerId = BackgroundTimer.setInterval(() => {
2024-12-08 17:01:28 +00:00
const newTime = timeLeft - 1;
setTimeLeft(newTime);
updateNotification(
"Timer en cours",
`Phase: ${isWorkPhase ? "Travail" : "Repos"}, Temps restant: ${newTime}s`,
);
}, 1000);
2024-10-13 11:29:36 +00:00
} else if (isRunning && timeLeft === 0) {
nextRep();
2024-10-13 11:29:36 +00:00
}
2024-10-20 08:01:01 +00:00
return () => {
if (timerId !== null) {
BackgroundTimer.clearInterval(timerId);
}
};
}, [isRunning, timeLeft]);
2024-10-13 11:29:36 +00:00
const handleStart = () => {
setCurrentRep(1);
setIsWorkPhase(true);
setTimeLeft(workTime);
setIsRunning(true);
setIsFinish(false);
2024-12-08 17:01:28 +00:00
updateNotification(
"Timer en cours",
`Phase: ${isWorkPhase ? "Travail" : "Repos"}, Temps restant: ${timeLeft}s`,
);
2024-10-13 11:29:36 +00:00
};
const nextRep = () => {
if (currentRep < reps) {
if (isWorkPhase) {
2024-10-20 08:01:01 +00:00
playSound();
setIsWorkPhase(false);
setTimeLeft(restTime);
} else {
2024-10-20 08:01:01 +00:00
playSound();
setIsWorkPhase(true);
setTimeLeft(workTime);
setCurrentRep((prevRep) => prevRep + 1);
}
} else {
2024-10-20 08:01:01 +00:00
playSound();
setIsFinish(true);
setIsRunning(false);
2024-12-08 17:01:28 +00:00
cancelNotification();
}
};
const previousRep = () => {
if (isWorkPhase) {
if (currentRep > 1) {
setIsWorkPhase(false);
setTimeLeft(restTime);
setCurrentRep((prevRep) => prevRep - 1);
}
} else {
setIsWorkPhase(true);
setTimeLeft(workTime);
}
};
2024-10-13 11:29:36 +00:00
const handleContine = () => {
setIsRunning(true);
};
const handleStop = () => {
setIsRunning(false);
};
const renderBgColor: () => TimerBgColor = () => {
2024-12-08 17:01:28 +00:00
if (isFinish) return "black";
if (isWorkPhase) return "red";
2024-10-13 11:29:36 +00:00
2024-12-08 17:01:28 +00:00
return "green";
};
2024-10-13 11:29:36 +00:00
return (
<Container bgColor={renderBgColor()}>
2024-12-08 17:01:28 +00:00
{isFinish && <FinishContent handleStart={handleStart} />}
2024-10-13 11:29:36 +00:00
{!isFinish && (
<TimerContent
isWorkPhase={isWorkPhase}
timeLeft={timeLeft}
reps={reps}
bgColor={renderBgColor()}
currentRep={currentRep}
isRunning={isRunning}
nextRep={nextRep}
previousRep={previousRep}
2024-10-13 11:29:36 +00:00
handleStop={handleStop}
handleContine={handleContine}
/>
)}
</Container>
);
}
2024-12-08 17:01:28 +00:00
const Container = styled(View)<{ bgColor: TimerBgColor }>(
({ theme, bgColor }) => ({
flex: 1,
alignItems: "center",
justifyContent: "center",
backgroundColor: theme.colors[bgColor],
}),
);