boxons/app/timer.tsx

154 lines
4.0 KiB
TypeScript
Raw Normal View History

2024-10-13 11:29:36 +00:00
import { useEffect, useState } from 'react';
import { useRoute, useNavigation } from '@react-navigation/native';
2024-10-13 11:29:36 +00:00
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 { Audio } from 'expo-av';
import { Sound } from 'expo-av/build/Audio';
import { activateKeepAwakeAsync, deactivateKeepAwake } from 'expo-keep-awake';
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);
const [sound, setSound] = useState<Sound>();
2024-10-13 11:29:36 +00:00
useEffect(() => {
configureAudio();
2024-10-13 11:29:36 +00:00
handleStart();
activateKeepAwakeAsync();
2024-10-13 11:29:36 +00:00
}, []);
// when the user exits the screen, desactivate the keepAwake
useEffect(() => {
const unsubscribe = navigation.addListener('beforeRemove', (_) => {
deactivateKeepAwake();
});
return unsubscribe;
}, [navigation]);
2024-10-13 11:29:36 +00:00
useEffect(() => {
let timer: NodeJS.Timeout;
if (isRunning && timeLeft > 0) {
timer = setTimeout(() => {
setTimeLeft(timeLeft - 1);
}, 1000);
} else if (isRunning && timeLeft === 0) {
if (currentRep < reps) {
if (isWorkPhase) {
// If the work phase is over, move on to the rest phase
playSound()
2024-10-13 11:29:36 +00:00
setIsWorkPhase(false);
setTimeLeft(restTime);
} else {
// If the rest phase is complete, move on to the next work phase
playSound()
2024-10-13 11:29:36 +00:00
setIsWorkPhase(true);
setTimeLeft(workTime);
setCurrentRep(currentRep + 1);
}
} else {
// Done
playSound();
2024-10-13 11:29:36 +00:00
setIsRunning(false);
setIsFinish(true);
}
}
return () => clearTimeout(timer);
}, [isRunning, timeLeft, currentRep, isWorkPhase, reps, workTime, restTime]);
const handleStart = () => {
setCurrentRep(1);
setIsWorkPhase(true);
setTimeLeft(workTime);
setIsRunning(true);
setIsFinish(false);
};
async function configureAudio() {
await Audio.setAudioModeAsync({
allowsRecordingIOS: false,
staysActiveInBackground: false,
playsInSilentModeIOS: true,
shouldDuckAndroid: true,
playThroughEarpieceAndroid: false,
});
}
async function playSound() {
const { sound } = await Audio.Sound.createAsync(require('../assets/audios/boxingBell.mp3'));
setSound(sound);
await sound.playAsync();
}
useEffect(() => {
return sound
? () => {
sound.unloadAsync();
}
: undefined;
}, [sound]);
2024-10-13 11:29:36 +00:00
const handleContine = () => {
setIsRunning(true);
};
const handleStop = () => {
setIsRunning(false);
};
const renderBgColor: () => TimerBgColor = () => {
if (isFinish) return 'black';
if (isWorkPhase) return 'red';
return 'green';
}
return (
<Container bgColor={renderBgColor()}>
{isFinish && (
<FinishContent handleStart={handleStart} />
)}
{!isFinish && (
<TimerContent
isWorkPhase={isWorkPhase}
timeLeft={timeLeft}
reps={reps}
bgColor={renderBgColor()}
currentRep={currentRep}
isRunning={isRunning}
handleStop={handleStop}
handleContine={handleContine}
/>
)}
</Container>
);
}
const Container = styled(View)<{ bgColor: TimerBgColor }>(({ theme, bgColor }) => ({
flex: 1,
alignItems: 'center',
justifyContent: 'center',
backgroundColor: theme.colors[bgColor]
}));