feat: add keep screen awake when timer is running

merge-requests/10/head
Torpenn 2024-10-13 21:32:28 +02:00
parent f321ff01ea
commit 7910dc4cf8
3 changed files with 19 additions and 17 deletions

View File

@ -1,5 +1,5 @@
import { useEffect, useState } from 'react'; import { useEffect, useState } from 'react';
import { useRoute } from '@react-navigation/native'; import { useRoute, useNavigation } from '@react-navigation/native';
import { View } from '@/components/shared/Themed'; import { View } from '@/components/shared/Themed';
import styled from '@emotion/native'; import styled from '@emotion/native';
@ -8,6 +8,7 @@ import FinishContent from '@/components/useCases/timer/view/FinishContent';
import { TimerBgColor } from '@/components/useCases/timer/business/type'; import { TimerBgColor } from '@/components/useCases/timer/business/type';
import { Audio } from 'expo-av'; import { Audio } from 'expo-av';
import { Sound } from 'expo-av/build/Audio'; import { Sound } from 'expo-av/build/Audio';
import { activateKeepAwakeAsync, deactivateKeepAwake } from 'expo-keep-awake';
interface TimerProps { interface TimerProps {
reps: number; reps: number;
@ -16,9 +17,10 @@ interface TimerProps {
} }
export default function Timer() { export default function Timer() {
const navigation = useNavigation();
const route = useRoute(); const route = useRoute();
const { reps, restTime, workTime } = route.params as TimerProps;
const { reps, restTime, workTime } = route.params as TimerProps;
const [currentRep, setCurrentRep] = useState<number>(0); const [currentRep, setCurrentRep] = useState<number>(0);
const [timeLeft, setTimeLeft] = useState<number>(0); const [timeLeft, setTimeLeft] = useState<number>(0);
const [isWorkPhase, setIsWorkPhase] = useState<boolean>(true); const [isWorkPhase, setIsWorkPhase] = useState<boolean>(true);
@ -27,9 +29,20 @@ export default function Timer() {
const [sound, setSound] = useState<Sound>(); const [sound, setSound] = useState<Sound>();
useEffect(() => { useEffect(() => {
configureAudio();
handleStart(); handleStart();
activateKeepAwakeAsync();
}, []); }, []);
// when the user exits the screen, desactivate the keepAwake
useEffect(() => {
const unsubscribe = navigation.addListener('beforeRemove', (_) => {
deactivateKeepAwake();
});
return unsubscribe;
}, [navigation]);
useEffect(() => { useEffect(() => {
let timer: NodeJS.Timeout; let timer: NodeJS.Timeout;
@ -53,6 +66,7 @@ export default function Timer() {
} }
} else { } else {
// Done // Done
playSound();
setIsRunning(false); setIsRunning(false);
setIsFinish(true); setIsFinish(true);
} }
@ -79,10 +93,6 @@ export default function Timer() {
}); });
} }
useEffect(() => {
configureAudio();
}, []);
async function playSound() { async function playSound() {
const { sound } = await Audio.Sound.createAsync(require('../assets/audios/boxingBell.mp3')); const { sound } = await Audio.Sound.createAsync(require('../assets/audios/boxingBell.mp3'));
setSound(sound); setSound(sound);

11
package-lock.json generated
View File

@ -17,6 +17,7 @@
"expo": "~51.0.28", "expo": "~51.0.28",
"expo-av": "~14.0.7", "expo-av": "~14.0.7",
"expo-font": "~12.0.9", "expo-font": "~12.0.9",
"expo-keep-awake": "~13.0.2",
"expo-linking": "~6.3.1", "expo-linking": "~6.3.1",
"expo-router": "~3.5.23", "expo-router": "~3.5.23",
"expo-splash-screen": "~0.27.5", "expo-splash-screen": "~0.27.5",
@ -30,7 +31,6 @@
"react-native-reanimated": "~3.10.1", "react-native-reanimated": "~3.10.1",
"react-native-safe-area-context": "4.10.5", "react-native-safe-area-context": "4.10.5",
"react-native-screens": "3.31.1", "react-native-screens": "3.31.1",
"react-native-sound": "^0.11.2",
"react-native-timer-picker": "^1.10.3", "react-native-timer-picker": "^1.10.3",
"react-native-web": "~0.19.10" "react-native-web": "~0.19.10"
}, },
@ -19637,15 +19637,6 @@
"react-native": "*" "react-native": "*"
} }
}, },
"node_modules/react-native-sound": {
"version": "0.11.2",
"resolved": "https://registry.npmjs.org/react-native-sound/-/react-native-sound-0.11.2.tgz",
"integrity": "sha512-LmGc8lgOK3qecYMVQpyHvww/C+wgT6sWeMpVbOe4NCRGC2yKd4fo4U0KBUo9PO7AqKESO3I/2GZg1/C0+bwiiA==",
"license": "MIT",
"peerDependencies": {
"react-native": ">=0.8.0"
}
},
"node_modules/react-native-timer-picker": { "node_modules/react-native-timer-picker": {
"version": "1.10.3", "version": "1.10.3",
"resolved": "https://registry.npmjs.org/react-native-timer-picker/-/react-native-timer-picker-1.10.3.tgz", "resolved": "https://registry.npmjs.org/react-native-timer-picker/-/react-native-timer-picker-1.10.3.tgz",

View File

@ -47,7 +47,8 @@
"react-native-screens": "3.31.1", "react-native-screens": "3.31.1",
"react-native-timer-picker": "^1.10.3", "react-native-timer-picker": "^1.10.3",
"react-native-web": "~0.19.10", "react-native-web": "~0.19.10",
"expo-av": "~14.0.7" "expo-av": "~14.0.7",
"expo-keep-awake": "~13.0.2"
}, },
"devDependencies": { "devDependencies": {
"@babel/core": "^7.20.0", "@babel/core": "^7.20.0",