Skip to content

Commit

Permalink
Add screen lock
Browse files Browse the repository at this point in the history
  • Loading branch information
patrikniebur committed Sep 22, 2023
1 parent 6ebb26a commit 9871665
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 3 deletions.
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"react-dnd": "^16.0.1",
"react-dnd-html5-backend": "^16.0.1",
"react-dom": "^18.2.0",
"react-screen-wake-lock": "^3.0.2",
"react-speech-recognition": "^3.10.0"
},
"@parcel/transformer-css": {
Expand Down
3 changes: 2 additions & 1 deletion src/ts/Views/ExerciseRunner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import type { DirectoryConfiguration } from "../types";
import { useGlobalContext } from "../GlobalContext";
import { ExerciseItem } from "../components/ExerciseItem";
import { getDirectoryConfig } from "../helpers/directoryFunctions";
import { useVoiceCommands, useKeyboardControls } from "../helpers/hooks";
import { useVoiceCommands, useKeyboardControls, useRefreshingWakeLock } from "../helpers/hooks";
import { VoiceControlledTimer } from "../components/Timer/VoiceControlledTimer";
import { ControlsModal } from "../components/ControlsModal";

Expand All @@ -23,6 +23,7 @@ const BASE_PATH = process.env.BASE_PATH;
export function ExerciseRunner() {
useKeyboardControls({ ArrowLeft: prev, ArrowRight: next });
const [config] = useDirectoryConfiguration();
useRefreshingWakeLock()
const [currentExerciseIndex, setCurrentExerciseIndex] = React.useState(0);
const [lastCommand, resetLastCommand] = useVoiceCommands([
"next",
Expand Down
52 changes: 50 additions & 2 deletions src/ts/helpers/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from "react";
import SpeechRecognition, {
useSpeechRecognition,
} from "react-speech-recognition";
import { useWakeLock } from "react-screen-wake-lock";

export function useVoiceCommands(command: string[]) {
const [lastCommand, setLastCommand] = React.useState("");
Expand Down Expand Up @@ -51,12 +52,59 @@ export function useKeyboardControls(
const listener = (e: KeyboardEvent) => {
const callback = listeners[e.code];
if (callback) {
e.preventDefault()
callback()
e.preventDefault();
callback();
}
};
window.addEventListener("keydown", listener);

return () => window.removeEventListener("keydown", listener);
}, [listeners]);
}

export function useRefreshingWakeLock() {
const locked = React.useRef(false);
const { request, release } = useWakeLock({
onRelease: () => {
console.log("release");
locked.current = false;
},
});

const lock = () => {
console.log("lock");
if (locked.current !== true) {
request().then(() => {
locked.current = true;
});
}
};

/* Lock/unlock on component mount/unmount */
React.useEffect(() => {
lock();

return () => {
if (locked.current) {
release().then(() => {
console.log("release (hook)");
locked.current = false;
});
}
};
}, []);

/* Re-lock after visibility change */
React.useEffect(() => {
const listener = () => {
if (document.visibilityState === "visible" && locked.current === false) {
lock();
}
};
window.addEventListener("visibilitychange", listener);

return () => {
window.removeEventListener("visibilitychange", listener);
};
}, []);
}

0 comments on commit 9871665

Please sign in to comment.