move album to cover art mapping to music

This commit is contained in:
austinried 2021-08-14 17:46:16 +09:00
parent a95372fa55
commit d72258c68e
5 changed files with 85 additions and 86 deletions

View File

@ -1,4 +1,4 @@
import { CacheFileTypeKey } from '@app/models/music' import { CacheItemTypeKey } from '@app/models/music'
import { selectCache } from '@app/state/cache' import { selectCache } from '@app/state/cache'
import { selectMusic } from '@app/state/music' import { selectMusic } from '@app/state/music'
import { selectSettings } from '@app/state/settings' import { selectSettings } from '@app/state/settings'
@ -64,7 +64,7 @@ export const useStarred = (id: string, type: string) => {
) )
} }
const useFileRequest = (key: CacheFileTypeKey, id: string) => { const useFileRequest = (key: CacheItemTypeKey, id: string) => {
const file = useStore( const file = useStore(
useCallback( useCallback(
(store: Store) => { (store: Store) => {

View File

@ -80,13 +80,13 @@ export type ListableItem = Song | AlbumListItem | Artist | PlaylistListItem
export type HomeLists = { [key: string]: AlbumListItem[] } export type HomeLists = { [key: string]: AlbumListItem[] }
export enum CacheFileType { export enum CacheItemType {
coverArt, coverArt,
artistArt, artistArt,
song, song,
} }
export type CacheFileTypeKey = keyof typeof CacheFileType export type CacheItemTypeKey = keyof typeof CacheItemType
export type CacheFile = { export type CacheFile = {
path: string path: string

View File

@ -1,4 +1,4 @@
import { CacheFile, CacheFileTypeKey, CacheRequest, Song } from '@app/models/music' import { CacheFile, CacheItemTypeKey, CacheRequest } from '@app/models/music'
import PromiseQueue from '@app/util/PromiseQueue' import PromiseQueue from '@app/util/PromiseQueue'
import produce from 'immer' import produce from 'immer'
import RNFS from 'react-native-fs' import RNFS from 'react-native-fs'
@ -9,9 +9,9 @@ const imageDownloadQueue = new PromiseQueue(10)
export type CacheDownload = CacheFile & CacheRequest export type CacheDownload = CacheFile & CacheRequest
export type CacheDirsByServer = Record<string, Record<CacheFileTypeKey, string>> export type CacheDirsByServer = Record<string, Record<CacheItemTypeKey, string>>
export type CacheFilesByServer = Record<string, Record<CacheFileTypeKey, Record<string, CacheFile>>> export type CacheFilesByServer = Record<string, Record<CacheItemTypeKey, Record<string, CacheFile>>>
export type CacheRequestsByServer = Record<string, Record<CacheFileTypeKey, Record<string, CacheRequest>>> export type CacheRequestsByServer = Record<string, Record<CacheItemTypeKey, Record<string, CacheRequest>>>
// export type CacheItemsDb = Record< // export type CacheItemsDb = Record<
// string, // string,
@ -24,7 +24,7 @@ export type CacheRequestsByServer = Record<string, Record<CacheFileTypeKey, Reco
// > // >
export type CacheSlice = { export type CacheSlice = {
cacheItem: (key: CacheFileTypeKey, itemId: string, url: string | (() => string | Promise<string>)) => Promise<void> cacheItem: (key: CacheItemTypeKey, itemId: string, url: string | (() => string | Promise<string>)) => Promise<void>
// cache: CacheItemsDb // cache: CacheItemsDb
cacheDirs: CacheDirsByServer cacheDirs: CacheDirsByServer
@ -32,19 +32,11 @@ export type CacheSlice = {
cacheRequests: CacheRequestsByServer cacheRequests: CacheRequestsByServer
getCoverArtPath: (coverArt: string) => Promise<string | undefined> getCoverArtPath: (coverArt: string) => Promise<string | undefined>
albumCoverArt: { [id: string]: string | undefined }
albumCoverArtRequests: { [id: string]: Promise<void> }
fetchAlbumCoverArt: (id: string) => Promise<void>
getAlbumCoverArt: (id: string | undefined) => Promise<string | undefined>
mapSongCoverArtFromAlbum: (songs: Song[]) => Promise<Song[]>
} }
export const selectCache = { export const selectCache = {
getCoverArtPath: (store: CacheSlice) => store.getCoverArtPath,
cacheItem: (store: CacheSlice) => store.cacheItem, cacheItem: (store: CacheSlice) => store.cacheItem,
getCoverArtPath: (store: CacheSlice) => store.getCoverArtPath,
fetchAlbumCoverArt: (store: CacheSlice) => store.fetchAlbumCoverArt,
} }
export const createCacheSlice = (set: SetState<Store>, get: GetState<Store>): CacheSlice => ({ export const createCacheSlice = (set: SetState<Store>, get: GetState<Store>): CacheSlice => ({
@ -138,70 +130,4 @@ export const createCacheSlice = (set: SetState<Store>, get: GetState<Store>): Ca
await get().cacheItem('coverArt', coverArt, () => client.getCoverArtUri({ id: coverArt })) await get().cacheItem('coverArt', coverArt, () => client.getCoverArtUri({ id: coverArt }))
return get().cacheFiles[activeServerId].coverArt[coverArt].path return get().cacheFiles[activeServerId].coverArt[coverArt].path
}, },
albumCoverArt: {},
albumCoverArtRequests: {},
fetchAlbumCoverArt: async id => {
const client = get().client
if (!client) {
return
}
const inProgress = get().albumCoverArtRequests[id]
if (inProgress !== undefined) {
return await inProgress
}
const promise = new Promise<void>(async resolve => {
try {
const response = await client.getAlbum({ id })
set(
produce<CacheSlice>(state => {
state.albumCoverArt[id] = response.data.album.coverArt
}),
)
} finally {
resolve()
}
}).then(() => {
set(
produce<CacheSlice>(state => {
delete state.albumCoverArtRequests[id]
}),
)
})
set(
produce<CacheSlice>(state => {
state.albumCoverArtRequests[id] = promise
}),
)
return await promise
},
getAlbumCoverArt: async id => {
if (!id) {
return
}
const existing = get().albumCoverArt[id]
if (existing) {
return existing
}
await get().fetchAlbumCoverArt(id)
return get().albumCoverArt[id]
},
mapSongCoverArtFromAlbum: async songs => {
const mapped: Song[] = []
for (const s of songs) {
mapped.push({
...s,
coverArt: await get().getAlbumCoverArt(s.albumId),
})
}
return mapped
},
}) })

View File

@ -14,6 +14,7 @@ import {
PlaylistListItem, PlaylistListItem,
PlaylistWithSongs, PlaylistWithSongs,
SearchResults, SearchResults,
Song,
} from '@app/models/music' } from '@app/models/music'
import { Store } from '@app/state/store' import { Store } from '@app/state/store'
import { GetAlbumList2Type, StarParams } from '@app/subsonic/params' import { GetAlbumList2Type, StarParams } from '@app/subsonic/params'
@ -65,6 +66,12 @@ export type MusicSlice = {
starredAlbums: { [id: string]: boolean } starredAlbums: { [id: string]: boolean }
starredArtists: { [id: string]: boolean } starredArtists: { [id: string]: boolean }
starItem: (id: string, type: string, unstar?: boolean) => Promise<void> starItem: (id: string, type: string, unstar?: boolean) => Promise<void>
albumIdCoverArt: { [id: string]: string | undefined }
albumIdCoverArtRequests: { [id: string]: Promise<void> }
fetchAlbumCoverArt: (id: string) => Promise<void>
getAlbumCoverArt: (id: string | undefined) => Promise<string | undefined>
mapSongCoverArtFromAlbum: (songs: Song[]) => Promise<Song[]>
} }
export const selectMusic = { export const selectMusic = {
@ -429,4 +436,70 @@ export const createMusicSlice = (set: SetState<Store>, get: GetState<Store>): Mu
setStarred(unstar) setStarred(unstar)
} }
}, },
albumIdCoverArt: {},
albumIdCoverArtRequests: {},
fetchAlbumCoverArt: async id => {
const client = get().client
if (!client) {
return
}
const inProgress = get().albumIdCoverArtRequests[id]
if (inProgress !== undefined) {
return await inProgress
}
const promise = new Promise<void>(async resolve => {
try {
const response = await client.getAlbum({ id })
set(
produce<MusicSlice>(state => {
state.albumIdCoverArt[id] = response.data.album.coverArt
}),
)
} finally {
resolve()
}
}).then(() => {
set(
produce<MusicSlice>(state => {
delete state.albumIdCoverArtRequests[id]
}),
)
})
set(
produce<MusicSlice>(state => {
state.albumIdCoverArtRequests[id] = promise
}),
)
return await promise
},
getAlbumCoverArt: async id => {
if (!id) {
return
}
const existing = get().albumIdCoverArt[id]
if (existing) {
return existing
}
await get().fetchAlbumCoverArt(id)
return get().albumIdCoverArt[id]
},
mapSongCoverArtFromAlbum: async songs => {
const mapped: Song[] = []
for (const s of songs) {
mapped.push({
...s,
coverArt: await get().getAlbumCoverArt(s.albumId),
})
}
return mapped
},
}) })

View File

@ -1,4 +1,4 @@
import { CacheFileType } from '@app/models/music' import { CacheItemType } from '@app/models/music'
import { AppSettings, Server } from '@app/models/settings' import { AppSettings, Server } from '@app/models/settings'
import { Store } from '@app/state/store' import { Store } from '@app/state/store'
import { SubsonicApiClient } from '@app/subsonic/api' import { SubsonicApiClient } from '@app/subsonic/api'
@ -50,7 +50,7 @@ export const createSettingsSlice = (set: SetState<Store>, get: GetState<Store>):
return return
} }
for (const type in CacheFileType) { for (const type in CacheItemType) {
await mkdir(`${RNFS.DocumentDirectoryPath}/servers/${id}/${type}`) await mkdir(`${RNFS.DocumentDirectoryPath}/servers/${id}/${type}`)
} }