From bb4a59e4d54c4b37d1cba4d249e1f3d9890a187e Mon Sep 17 00:00:00 2001 From: Danish Ahmed Mirza Date: Tue, 21 Jun 2022 23:10:45 +0530 Subject: [PATCH] Add tracks to TrackPlayer on initial render --- .../message/Components/Audio/index.tsx | 28 ++++++++-------- .../message/Components/Audio/services.ts | 4 +-- .../message/Components/Audio/tracksStorage.ts | 32 +++++++++++++++++-- jest.setup.js | 4 +-- 4 files changed, 47 insertions(+), 21 deletions(-) diff --git a/app/containers/message/Components/Audio/index.tsx b/app/containers/message/Components/Audio/index.tsx index 36a20fca8..33b080046 100644 --- a/app/containers/message/Components/Audio/index.tsx +++ b/app/containers/message/Components/Audio/index.tsx @@ -22,7 +22,7 @@ import { IAttachment } from '../../../../definitions'; import { TSupportedThemes } from '../../../../theme'; import { setupService } from './services'; import Slider from './Slider'; -import { useTracks } from './tracksStorage'; +import { addTrack, clearTracks, getTrackIndex, useTracks } from './tracksStorage'; interface IButton { loading: boolean; @@ -140,6 +140,11 @@ IMessageAudioProps) => { const sound = new Audio.Sound(); sound.setOnPlaybackStatusUpdate(updateTrackDuration); await sound.loadAsync({ uri: `${url}?rc_uid=${user.id}&rc_token=${user.token}` }); + if (!isReply) { + const index = await TrackPlayer.add(track); + // @ts-ignore + index >= 0 && addTrack({ trackIndex: index, trackId: track.id }); + } } catch { // Do nothing } @@ -148,12 +153,13 @@ IMessageAudioProps) => { setup(); return () => { TrackPlayer.destroy(); + clearTracks(); setCurrentTrackId(null); }; }, []); useEffect(() => { - if (currentTrackId && currentTrackId !== track.id) { + if (!currentTrackId || currentTrackId !== track.id) { setCurrentPosition(0); setPaused(true); } @@ -209,8 +215,9 @@ IMessageAudioProps) => { } else if (currentTrackId === track.id) { TrackPlayer.play(); } else { - TrackPlayer.reset(); - await TrackPlayer.add(track); + const index = getTrackIndex(track.id); + index && TrackPlayer.skip(index); + if (currentPosition > 0) await TrackPlayer.seekTo(currentPosition); TrackPlayer.play(); setCurrentTrackId(track.id); } @@ -236,15 +243,10 @@ IMessageAudioProps) => { try { if (currentTrackId === track.id) { await TrackPlayer.seekTo(value); - } else { - TrackPlayer.reset(); - await TrackPlayer.add(track); - await TrackPlayer.seekTo(value); - setCurrentTrackId(track.id); - } - if (paused) { - TrackPlayer.play(); - setPaused(false); + if (paused) { + TrackPlayer.play(); + setPaused(false); + } } } catch { // Do nothing diff --git a/app/containers/message/Components/Audio/services.ts b/app/containers/message/Components/Audio/services.ts index 7edd621f1..4dbe63514 100644 --- a/app/containers/message/Components/Audio/services.ts +++ b/app/containers/message/Components/Audio/services.ts @@ -1,6 +1,6 @@ import TrackPlayer, { Event, State, Capability } from 'react-native-track-player'; -import { clearCurrentTrack } from './tracksStorage'; +import { clearTracks } from './tracksStorage'; let wasPausedByDuck = false; @@ -16,7 +16,7 @@ export const playbackService = async () => { }); TrackPlayer.addEventListener(Event.RemoteStop, () => { - clearCurrentTrack(); + clearTracks(); TrackPlayer.destroy(); }); diff --git a/app/containers/message/Components/Audio/tracksStorage.ts b/app/containers/message/Components/Audio/tracksStorage.ts index b2fe5ab49..e87767995 100644 --- a/app/containers/message/Components/Audio/tracksStorage.ts +++ b/app/containers/message/Components/Audio/tracksStorage.ts @@ -1,9 +1,35 @@ import MMKVStorage, { create } from 'react-native-mmkv-storage'; -const TracksStorage = new MMKVStorage.Loader().withInstanceID('tracks').initialize(); +interface Track { + trackId: string; + trackIndex: number; +} +const TracksStorage = new MMKVStorage.Loader().withInstanceID('tracks').initialize(); export const useTracks = create(TracksStorage); -export const clearCurrentTrack = () => { - TracksStorage.removeItem('currentTrack'); +export const initializeTracks = () => { + const tracks = TracksStorage.getArray('tracks'); + if (!tracks) TracksStorage.setArray('tracks', []); +}; + +export const addTrack = (track: Track) => { + initializeTracks(); + const tracks: Track[] = TracksStorage.getArray('tracks') || []; + if (tracks.find((t: Track) => t.trackId === track.trackId)) { + return; + } + TracksStorage.setArray('tracks', [...tracks, track]); +}; + +export const getTrackIndex = (trackId: string) => { + const tracks: Track[] = TracksStorage.getArray('tracks') || []; + const index = tracks.findIndex((t: Track) => t.trackId === trackId); + if (index !== -1) { + return tracks[index].trackIndex; + } +}; + +export const clearTracks = () => { + TracksStorage.setArray('tracks', []); }; diff --git a/jest.setup.js b/jest.setup.js index fb6418bcc..2ae131d89 100644 --- a/jest.setup.js +++ b/jest.setup.js @@ -122,7 +122,5 @@ jest.mock('react-native-track-player', () => ({ })); jest.mock('./app/containers/message/Components/Audio/tracksStorage.ts', () => ({ - useTracks: () => ({ - currentTrack: '' - }) + useTracks: () => ['', jest.fn()] }));