mirror of
https://github.com/austinried/subtracks.git
synced 2025-12-27 00:59:28 +01:00
move album to cover art mapping to music
This commit is contained in:
parent
a95372fa55
commit
d72258c68e
@ -1,4 +1,4 @@
|
||||
import { CacheFileTypeKey } from '@app/models/music'
|
||||
import { CacheItemTypeKey } from '@app/models/music'
|
||||
import { selectCache } from '@app/state/cache'
|
||||
import { selectMusic } from '@app/state/music'
|
||||
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(
|
||||
useCallback(
|
||||
(store: Store) => {
|
||||
|
||||
@ -80,13 +80,13 @@ export type ListableItem = Song | AlbumListItem | Artist | PlaylistListItem
|
||||
|
||||
export type HomeLists = { [key: string]: AlbumListItem[] }
|
||||
|
||||
export enum CacheFileType {
|
||||
export enum CacheItemType {
|
||||
coverArt,
|
||||
artistArt,
|
||||
song,
|
||||
}
|
||||
|
||||
export type CacheFileTypeKey = keyof typeof CacheFileType
|
||||
export type CacheItemTypeKey = keyof typeof CacheItemType
|
||||
|
||||
export type CacheFile = {
|
||||
path: string
|
||||
|
||||
@ -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 produce from 'immer'
|
||||
import RNFS from 'react-native-fs'
|
||||
@ -9,9 +9,9 @@ const imageDownloadQueue = new PromiseQueue(10)
|
||||
|
||||
export type CacheDownload = CacheFile & CacheRequest
|
||||
|
||||
export type CacheDirsByServer = Record<string, Record<CacheFileTypeKey, string>>
|
||||
export type CacheFilesByServer = Record<string, Record<CacheFileTypeKey, Record<string, CacheFile>>>
|
||||
export type CacheRequestsByServer = Record<string, Record<CacheFileTypeKey, Record<string, CacheRequest>>>
|
||||
export type CacheDirsByServer = Record<string, Record<CacheItemTypeKey, string>>
|
||||
export type CacheFilesByServer = Record<string, Record<CacheItemTypeKey, Record<string, CacheFile>>>
|
||||
export type CacheRequestsByServer = Record<string, Record<CacheItemTypeKey, Record<string, CacheRequest>>>
|
||||
|
||||
// export type CacheItemsDb = Record<
|
||||
// string,
|
||||
@ -24,7 +24,7 @@ export type CacheRequestsByServer = Record<string, Record<CacheFileTypeKey, Reco
|
||||
// >
|
||||
|
||||
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
|
||||
cacheDirs: CacheDirsByServer
|
||||
@ -32,19 +32,11 @@ export type CacheSlice = {
|
||||
cacheRequests: CacheRequestsByServer
|
||||
|
||||
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 = {
|
||||
getCoverArtPath: (store: CacheSlice) => store.getCoverArtPath,
|
||||
cacheItem: (store: CacheSlice) => store.cacheItem,
|
||||
|
||||
fetchAlbumCoverArt: (store: CacheSlice) => store.fetchAlbumCoverArt,
|
||||
getCoverArtPath: (store: CacheSlice) => store.getCoverArtPath,
|
||||
}
|
||||
|
||||
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 }))
|
||||
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
|
||||
},
|
||||
})
|
||||
|
||||
@ -14,6 +14,7 @@ import {
|
||||
PlaylistListItem,
|
||||
PlaylistWithSongs,
|
||||
SearchResults,
|
||||
Song,
|
||||
} from '@app/models/music'
|
||||
import { Store } from '@app/state/store'
|
||||
import { GetAlbumList2Type, StarParams } from '@app/subsonic/params'
|
||||
@ -65,6 +66,12 @@ export type MusicSlice = {
|
||||
starredAlbums: { [id: string]: boolean }
|
||||
starredArtists: { [id: string]: boolean }
|
||||
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 = {
|
||||
@ -429,4 +436,70 @@ export const createMusicSlice = (set: SetState<Store>, get: GetState<Store>): Mu
|
||||
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
|
||||
},
|
||||
})
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import { CacheFileType } from '@app/models/music'
|
||||
import { CacheItemType } from '@app/models/music'
|
||||
import { AppSettings, Server } from '@app/models/settings'
|
||||
import { Store } from '@app/state/store'
|
||||
import { SubsonicApiClient } from '@app/subsonic/api'
|
||||
@ -50,7 +50,7 @@ export const createSettingsSlice = (set: SetState<Store>, get: GetState<Store>):
|
||||
return
|
||||
}
|
||||
|
||||
for (const type in CacheFileType) {
|
||||
for (const type in CacheItemType) {
|
||||
await mkdir(`${RNFS.DocumentDirectoryPath}/servers/${id}/${type}`)
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user