From 694d730ebda26d98ad13def77570c84ae8736221 Mon Sep 17 00:00:00 2001 From: austinried <4966622+austinried@users.noreply.github.com> Date: Fri, 20 Aug 2021 12:52:57 +0900 Subject: [PATCH] optimize gathering album art a bit --- app/hooks/music.ts | 6 +++--- app/screens/SongListView.tsx | 9 +-------- app/state/musicmap.ts | 15 ++++++++++++--- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/app/hooks/music.ts b/app/hooks/music.ts index c5bee1f..26fc962 100644 --- a/app/hooks/music.ts +++ b/app/hooks/music.ts @@ -10,7 +10,7 @@ export const useArtistInfo = (id: string) => { if (!artistInfo) { fetchArtistInfo(id) } - }) + }, [artistInfo, fetchArtistInfo, id]) return artistInfo } @@ -23,7 +23,7 @@ export const useAlbumWithSongs = (id: string) => { if (!album) { fetchAlbum(id) } - }) + }, [album, fetchAlbum, id]) return album } @@ -36,7 +36,7 @@ export const usePlaylistWithSongs = (id: string) => { if (!playlist) { fetchPlaylist(id) } - }) + }, [fetchPlaylist, id, playlist]) return playlist } diff --git a/app/screens/SongListView.tsx b/app/screens/SongListView.tsx index 422ca5e..8a34b8b 100644 --- a/app/screens/SongListView.tsx +++ b/app/screens/SongListView.tsx @@ -12,8 +12,7 @@ import { useStore } from '@app/state/store' import { selectTrackPlayer } from '@app/state/trackplayer' import colors from '@app/styles/colors' import font from '@app/styles/font' -import { useNavigation } from '@react-navigation/native' -import React, { useEffect, useState } from 'react' +import React, { useState } from 'react' import { ActivityIndicator, StyleSheet, Text, View } from 'react-native' type SongListType = 'album' | 'playlist' @@ -144,12 +143,6 @@ const SongListView = React.memo<{ title: string type: SongListType }>(({ id, title, type }) => { - const navigation = useNavigation() - - useEffect(() => { - navigation.setOptions({ title }) - }) - return type === 'album' ? : }) diff --git a/app/state/musicmap.ts b/app/state/musicmap.ts index f9a3fa7..db6d3a1 100644 --- a/app/state/musicmap.ts +++ b/app/state/musicmap.ts @@ -55,11 +55,20 @@ export const createMusicMapSlice = (set: SetState, get: GetState): }, mapChildrenToSongs: async (children, coverArt) => { - const songMaps: Promise[] = [] + const albumIds = children.reduce((acc, val) => { + if (val.albumId && !(val.albumId in acc)) { + acc[val.albumId] = get().getAlbumCoverArt(val.albumId) + } + return acc + }, {} as Record>) + + await Promise.all(Object.values(albumIds)) + + const songs: Song[] = [] for (const child of children) { - songMaps.push(get().mapChildToSong(child, coverArt)) + songs.push(await get().mapChildToSong(child, coverArt || (await get().getAlbumCoverArt(child.albumId)))) } - return await Promise.all(songMaps) + return songs }, mapArtistID3toArtist: artist => {