From 34207314f548ee4244d1b48b3bb36b7097fc770f Mon Sep 17 00:00:00 2001 From: Ty Fiero Date: Thu, 25 Apr 2024 15:26:42 -0700 Subject: [PATCH] utils --- .../ios/react-native/src/utils/state.ts | 10 +++++ .../react-native/src/utils/useSoundEffect.ts | 38 +++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 software/source/clients/ios/react-native/src/utils/state.ts create mode 100644 software/source/clients/ios/react-native/src/utils/useSoundEffect.ts diff --git a/software/source/clients/ios/react-native/src/utils/state.ts b/software/source/clients/ios/react-native/src/utils/state.ts new file mode 100644 index 0000000..2047977 --- /dev/null +++ b/software/source/clients/ios/react-native/src/utils/state.ts @@ -0,0 +1,10 @@ +// store.js +import { create } from "zustand"; + +const useStore = create((set: any) => ({ + count: 0, + increase: () => set((state: any) => ({ count: state.count + 1 })), + decrease: () => set((state: any) => ({ count: state.count - 1 })), +})); + +export default useStore; diff --git a/software/source/clients/ios/react-native/src/utils/useSoundEffect.ts b/software/source/clients/ios/react-native/src/utils/useSoundEffect.ts new file mode 100644 index 0000000..250353c --- /dev/null +++ b/software/source/clients/ios/react-native/src/utils/useSoundEffect.ts @@ -0,0 +1,38 @@ +import { useEffect, useState } from "react"; +import { Audio, InterruptionModeAndroid, InterruptionModeIOS } from "expo-av"; + +const useSoundEffect = (soundFile) => { + const [sound, setSound] = useState(null); // Explicitly set initial state to null + + useEffect(() => { + const loadSound = async () => { + // await Audio.setAudioModeAsync({ + // staysActiveInBackground: true, + // shouldDuckAndroid: true, + // playThroughEarpieceAndroid: false, + // interruptionModeIOS: InterruptionModeIOS.DoNotMix, + // interruptionModeAndroid: InterruptionModeAndroid.DoNotMix, + // allowsRecordingIOS: false, + // playsInSilentModeIOS: true, + // }); + const { sound: newSound } = await Audio.Sound.createAsync(soundFile); + setSound(newSound); + }; + + loadSound(); + + return () => { + sound?.unloadAsync(); + }; + }, [soundFile, sound]); // Include sound in the dependency array + + const playSound = async () => { + if (sound) { + await sound.playAsync(); + } + }; + + return playSound; +}; + +export default useSoundEffect;