From 67b3200a02a9a36cd84bb888b4ca4243f335bd2d Mon Sep 17 00:00:00 2001 From: Reinaldo Neto Date: Wed, 23 Aug 2023 11:57:11 -0300 Subject: [PATCH] fix unloadAll, add hit slop to slider, show the duration on the first render --- .../message/Components/Audio/Slider.tsx | 51 ++++++++++++------- app/lib/methods/handleAudioMedia.ts | 17 ++++--- 2 files changed, 45 insertions(+), 23 deletions(-) diff --git a/app/containers/message/Components/Audio/Slider.tsx b/app/containers/message/Components/Audio/Slider.tsx index 2ee1203bb..7b744cb64 100644 --- a/app/containers/message/Components/Audio/Slider.tsx +++ b/app/containers/message/Components/Audio/Slider.tsx @@ -23,6 +23,13 @@ interface ISlider { onChangeTime: (time: number) => Promise; } +const BUTTON_HIT_SLOP = { + top: 8, + right: 8, + bottom: 8, + left: 8 +}; + const Slider = ({ currentTime, duration, loaded = false, onChangeTime }: ISlider) => { const { colors } = useTheme(); @@ -32,6 +39,7 @@ const Slider = ({ currentTime, duration, loaded = false, onChangeTime }: ISlider const scale = useSharedValue(1); const isHandlePan = useSharedValue(false); const onEndGestureHandler = useSharedValue(false); + const isTimeChanged = useSharedValue(false); const styleLine = useAnimatedStyle(() => ({ width: x.value, @@ -61,7 +69,7 @@ const Slider = ({ currentTime, duration, loaded = false, onChangeTime }: ISlider } else { x.value = moveInX; } - + isTimeChanged.value = true; scale.value = 1.3; }, onEnd: () => { @@ -82,32 +90,41 @@ const Slider = ({ currentTime, duration, loaded = false, onChangeTime }: ISlider } }); + const formatTime = (ms: number) => { + 'worklet'; + + const minutes = Math.floor(ms / 60); + const remainingSeconds = Math.floor(ms % 60); + const formattedMinutes = String(minutes).padStart(2, '0'); + const formattedSeconds = String(remainingSeconds).padStart(2, '0'); + return `${formattedMinutes}:${formattedSeconds}`; + }; + useDerivedValue(() => { - let minutes; - let remainingSeconds; if (isHandlePan.value) { const cTime = (x.value * duration.value) / maxWidth.value || 0; currentTime.value = cTime; - minutes = Math.floor(cTime / 60); - remainingSeconds = Math.floor(cTime % 60); + current.value = formatTime(cTime); } else { const xTime = (currentTime.value * maxWidth.value) / duration.value || 0; x.value = xTime; - minutes = Math.floor(currentTime.value / 60); - remainingSeconds = Math.floor(currentTime.value % 60); + current.value = formatTime(currentTime.value); + if (currentTime.value !== 0) { + isTimeChanged.value = true; + } } - const formattedMinutes = String(minutes).padStart(2, '0'); - const formattedSeconds = String(remainingSeconds).padStart(2, '0'); - current.value = `${formattedMinutes}:${formattedSeconds}`; }, [x, maxWidth, duration, isHandlePan, currentTime]); - const getCurrentTime = useAnimatedProps( - () => - ({ + const getCurrentTime = useAnimatedProps(() => { + if (isTimeChanged.value) { + return { text: current.value - } as any), - [current] - ); + } as any; + } + return { + text: formatTime(duration.value) + } as any; + }, [current, isTimeChanged, duration]); const thumbColor = loaded ? colors.audioPlayerPrimary : colors.tintDisabled; @@ -123,7 +140,7 @@ const Slider = ({ currentTime, duration, loaded = false, onChangeTime }: ISlider - + diff --git a/app/lib/methods/handleAudioMedia.ts b/app/lib/methods/handleAudioMedia.ts index 1aec74b6c..f1180f771 100644 --- a/app/lib/methods/handleAudioMedia.ts +++ b/app/lib/methods/handleAudioMedia.ts @@ -82,17 +82,22 @@ class HandleAudioMedia { async unloadAudio(uri: string) { await this.audioQueue[uri]?.stopAsync(); await this.audioQueue[uri]?.unloadAsync(); + delete this.audioQueue[uri]; this.audioPlaying = ''; } async unloadAllAudios() { const audiosLoaded = Object.values(this.audioQueue); - await Promise.allSettled( - audiosLoaded.map(async audio => { - await audio?.stopAsync(); - await audio?.unloadAsync(); - }) - ); + try { + await Promise.all( + audiosLoaded.map(async audio => { + await audio?.stopAsync(); + await audio?.unloadAsync(); + }) + ); + } catch { + // Do nothing + } this.audioPlaying = ''; this.audioQueue = {}; }