From 926045e3e858fb55026539fb30d27f57c119fe6e Mon Sep 17 00:00:00 2001 From: Ben Xu Date: Mon, 6 May 2024 15:01:47 -0400 Subject: [PATCH] add js docs --- .../mobile/react-native/src/screens/Main.tsx | 51 +++++++++++++------ 1 file changed, 36 insertions(+), 15 deletions(-) diff --git a/software/source/clients/mobile/react-native/src/screens/Main.tsx b/software/source/clients/mobile/react-native/src/screens/Main.tsx index 660bc91..1ddc888 100644 --- a/software/source/clients/mobile/react-native/src/screens/Main.tsx +++ b/software/source/clients/mobile/react-native/src/screens/Main.tsx @@ -56,6 +56,27 @@ const Main: React.FC = ({ route }) => { const [accumulatedMessage, setAccumulatedMessage] = useState(""); const scrollViewRef = useRef(null); + /** + * Checks if audioDir exists in device storage, if not creates it. + */ + async function dirExists() { + try { + const dirInfo = await FileSystem.getInfoAsync(audioDir); + if (!dirInfo.exists) { + console.error("audio directory doesn't exist, creating..."); + await FileSystem.makeDirectoryAsync(audioDir, { intermediates: true }); + } + } catch (error) { + console.error("Error checking or creating directory:", error); + } + } + + /** + * Writes the buffer to a temp file in audioDir in base64 encoding. + * + * @param {string} buffer + * @returns tempFilePath or null + */ const constructTempFilePath = async (buffer: string) => { try { await dirExists(); @@ -76,21 +97,10 @@ const Main: React.FC = ({ route }) => { } }; - async function dirExists() { - /** - * Checks if audio directory exists in device storage, if not creates it. - */ - try { - const dirInfo = await FileSystem.getInfoAsync(audioDir); - if (!dirInfo.exists) { - console.error("audio directory doesn't exist, creating..."); - await FileSystem.makeDirectoryAsync(audioDir, { intermediates: true }); - } - } catch (error) { - console.error("Error checking or creating directory:", error); - } - } - + /** + * Plays the next audio in audioQueue if the queue is not empty + * and there is no currently playing audio. + */ const playNextAudio = useCallback(async () => { if (audioQueueRef.current.length > 0 && soundRef.current == null) { const uri = audioQueueRef.current.at(0) as string; @@ -110,6 +120,11 @@ const Main: React.FC = ({ route }) => { } },[]); + /** + * Queries the currently playing Expo Audio.Sound object soundRef + * for playback status. When the status denotes soundRef has finished + * playback, we unload the sound and call playNextAudio(). + */ const _onPlayBackStatusUpdate = useCallback( async (status: any) => { if (status.didJustFinish) { @@ -124,6 +139,9 @@ const Main: React.FC = ({ route }) => { } },[]); + /** + * Single swipe to return to the Home screen from the Main page. + */ useEffect(() => { const backAction = () => { navigation.navigate("Home"); // Always navigate back to Home @@ -139,6 +157,9 @@ const Main: React.FC = ({ route }) => { return () => backHandler.remove(); }, [navigation]); + /** + * Handles all WebSocket events + */ useEffect(() => { let websocket: WebSocket; try {