From 8e854a8bdd976c27a352774e36b50759d49823c0 Mon Sep 17 00:00:00 2001 From: Lucas Reis Date: Wed, 19 Feb 2025 07:17:30 -0300 Subject: [PATCH] Implementing the Audio Playback/Download on the chat (#7900) * feat: added new check for isAudio and added the supported mime types * feat: adding the progress and audio on the audio file message * feat: finishing the layout of the audio_file * feat: play and pause audio * feat: update the progress bar when audio is playing * feat: update the timeframe of the audio * feat: update with the new design * feat: adding download and preview * feat: creates a hook for the file download and preview * feat: adding useCallback to make the return stable * fix: iOS issue when playing in loop * feat: adding localization for the error * fix: removing code tha was inserted for debug * feat: add a new line on the en.json * fix: fixing types * feat: adding the onSeek method inside the progress bar * feat: changing progress value to animated value * feat: changing to GestureDetector and making the seek method work * feat: adding a touchable without feedback to prevent the audio to triggering other page * feat: adding the drag on the seek method * feat: making the download button more gray * fix: fix tests * fix: prevent onProgress from clearing the seconds when the audio is paused * feat: clamping the position of the cursor to never be dragged offscreen * feat: enhancing the experience of dragging the cursor on the progress bar * feat: differentiate between the throttles * feat: remvoing the aac audio files support * feat: pausing if the focus has changed * feat: render differently the audio file when in the files list * refactor: optimize audio file component by using useCallback for event handlers * refactor: extract stopPropagation function for better readability in AudioFile component * feat: implement custom useThrottled hook and replace lodash throttle in AudioFile component * refactor: update useThrottled hook to accept a generic callback type * fix: loading of uri of audio files after merge * feat: add audioFile style to enhance layout for audio files * fix: tests --- .../channel_bookmark/bookmark_document.tsx | 7 +- app/components/document/index.tsx | 139 +---------- app/components/files/audio_file.tsx | 226 ++++++++++++++++++ app/components/files/document_file.tsx | 13 +- app/components/files/file.tsx | 37 ++- app/components/progress_bar/index.tsx | 143 +++++++++-- app/hooks/files.test.ts | 1 + app/hooks/files.ts | 156 +++++++++++- app/hooks/throttled.ts | 18 ++ app/utils/file/index.ts | 20 ++ assets/base/i18n/en.json | 1 + 11 files changed, 581 insertions(+), 180 deletions(-) create mode 100644 app/components/files/audio_file.tsx create mode 100644 app/hooks/throttled.ts diff --git a/app/components/channel_bookmarks/channel_bookmark/bookmark_document.tsx b/app/components/channel_bookmarks/channel_bookmark/bookmark_document.tsx index 20bb4a711..12d2b2b5f 100644 --- a/app/components/channel_bookmarks/channel_bookmark/bookmark_document.tsx +++ b/app/components/channel_bookmarks/channel_bookmark/bookmark_document.tsx @@ -2,12 +2,13 @@ // See LICENSE.txt for license information. import {Button} from '@rneui/base'; -import React, {useCallback, useRef, useState} from 'react'; +import React, {useCallback, useRef} from 'react'; import {StyleSheet, View} from 'react-native'; import Document, {type DocumentRef} from '@components/document'; import ProgressBar from '@components/progress_bar'; import {useTheme} from '@context/theme'; +import {useDownloadFileAndPreview} from '@hooks/files'; import BookmarkDetails from './bookmark_details'; @@ -33,9 +34,9 @@ const styles = StyleSheet.create({ }); const BookmarkDocument = ({bookmark, canDownloadFiles, file, onLongPress}: Props) => { - const [progress, setProgress] = useState(0); const document = useRef(null); const theme = useTheme(); + const {progress, toggleDownloadAndPreview} = useDownloadFileAndPreview(); const handlePress = useCallback(async () => { if (document.current) { @@ -47,7 +48,7 @@ const BookmarkDocument = ({bookmark, canDownloadFiles, file, onLongPress}: Props