boxons/app/Timer.tsx

229 lines
6.2 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";
import { useTimerContext } from "@/app/store/TimerContext";
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 { timerState } = useTimerContext();
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);
2025-04-08 20:53:40 +00:00
const [isPreparationPhase, setIsPreparationPhase] = 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) {
throw new Error("Error loading user settings");
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);
2025-04-08 20:53:40 +00:00
let phaseText = "Repos";
if (isPreparationPhase) {
phaseText = "Préparation";
} else if (isWorkPhase) {
phaseText = "Travail";
}
2024-12-08 17:01:28 +00:00
updateNotification(
"Timer en cours",
2025-04-08 20:53:40 +00:00
`Phase: ${phaseText}, Temps restant: ${newTime}s`,
2024-12-08 17:01:28 +00:00
);
}, 1000);
2024-10-13 11:29:36 +00:00
} else if (isRunning && timeLeft === 0) {
2025-04-08 20:53:40 +00:00
if (isPreparationPhase) {
startFirstRep();
} else {
nextRep();
}
2024-10-13 11:29:36 +00:00
}
2024-10-20 08:01:01 +00:00
return () => {
if (timerId !== null) {
BackgroundTimer.clearInterval(timerId);
}
};
2025-04-08 20:53:40 +00:00
}, [isRunning, timeLeft, isPreparationPhase]);
2024-10-13 11:29:36 +00:00
const handleStart = () => {
2025-04-08 20:53:40 +00:00
// Démarrer avec la phase de préparation si elle est configurée
if (timerState.preparationTime > 0) {
setIsPreparationPhase(true);
setCurrentRep(0);
setTimeLeft(timerState.preparationTime);
} else {
setIsPreparationPhase(false);
setCurrentRep(1);
setIsWorkPhase(true);
setTimeLeft(timerState.workTime);
}
2024-10-13 11:29:36 +00:00
setIsRunning(true);
setIsFinish(false);
2025-04-08 20:53:40 +00:00
const phaseText = timerState.preparationTime > 0 ? "Préparation" : "Travail";
2024-12-08 17:01:28 +00:00
updateNotification(
"Timer en cours",
2025-04-08 20:53:40 +00:00
`Phase: ${phaseText}, Temps restant: ${timeLeft}s`,
2024-12-08 17:01:28 +00:00
);
2024-10-13 11:29:36 +00:00
};
2025-04-08 20:53:40 +00:00
const startFirstRep = () => {
playSound();
setIsPreparationPhase(false);
setCurrentRep(1);
setIsWorkPhase(true);
setTimeLeft(timerState.workTime);
};
const handleReset = () => {
setCurrentRep(0);
setIsWorkPhase(true);
2025-04-08 20:53:40 +00:00
setIsPreparationPhase(false);
setTimeLeft(timerState.workTime);
setIsRunning(false);
setIsFinish(false);
cancelNotification();
};
const nextRep = () => {
if (currentRep < timerState.reps) {
if (isWorkPhase) {
2024-10-20 08:01:01 +00:00
playSound();
setIsWorkPhase(false);
setTimeLeft(timerState.restTime);
} else {
2024-10-20 08:01:01 +00:00
playSound();
setIsWorkPhase(true);
setTimeLeft(timerState.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(timerState.restTime);
setCurrentRep((prevRep) => prevRep - 1);
}
} else {
setIsWorkPhase(true);
setTimeLeft(timerState.workTime);
}
};
2025-04-08 21:29:14 +00:00
const handleContinue = () => {
2024-10-13 11:29:36 +00:00
setIsRunning(true);
};
const handleStop = () => {
setIsRunning(false);
};
const renderBgColor: () => TimerBgColor = () => {
2024-12-08 17:01:28 +00:00
if (isFinish) return "black";
2025-04-08 20:53:40 +00:00
if (isPreparationPhase) return "grey";
2025-04-10 08:01:27 +00:00
if (isWorkPhase) return "pink";
2024-10-13 11:29:36 +00:00
2025-04-10 08:01:27 +00:00
return "blue";
2024-12-08 17:01:28 +00:00
};
2024-10-13 11:29:36 +00:00
return (
<Container bgColor={renderBgColor()}>
{isFinish && <FinishContent handleStart={handleStart} handleReset={handleReset} />}
2024-10-13 11:29:36 +00:00
{!isFinish && (
<TimerContent
isWorkPhase={isWorkPhase}
2025-04-08 20:53:40 +00:00
isPreparationPhase={isPreparationPhase}
2024-10-13 11:29:36 +00:00
timeLeft={timeLeft}
reps={timerState.reps}
2024-10-13 11:29:36 +00:00
bgColor={renderBgColor()}
currentRep={currentRep}
isRunning={isRunning}
nextRep={nextRep}
previousRep={previousRep}
handleReset={handleReset}
2024-10-13 11:29:36 +00:00
handleStop={handleStop}
2025-04-08 21:29:14 +00:00
handleContinue={handleContinue}
2024-10-13 11:29:36 +00:00
/>
)}
</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.fixed[bgColor],
2024-12-08 17:01:28 +00:00
}),
);