mirror of
https://github.com/austinried/subtracks.git
synced 2025-12-28 17:19:27 +01:00
bugfixes
- fixed image path for RNTP notification - fixed overwhelming number of promises/requests generated when scrolling through artists (could still delay loading those further...) - fixed spinner not spinning while artistInfo is fetched for image url
This commit is contained in:
parent
a5c2aa9cf8
commit
976cd172f4
@ -27,11 +27,6 @@ const ImageSource = React.memo<{ cache?: { file?: CacheFile; request?: CacheRequ
|
|||||||
({ cache, style, imageStyle, resizeMode }) => {
|
({ cache, style, imageStyle, resizeMode }) => {
|
||||||
const [error, setError] = useState(false)
|
const [error, setError] = useState(false)
|
||||||
|
|
||||||
if (error) {
|
|
||||||
console.log('error!')
|
|
||||||
console.log(cache?.file?.path)
|
|
||||||
}
|
|
||||||
|
|
||||||
let source: ImageSourcePropType
|
let source: ImageSourcePropType
|
||||||
if (!error && cache?.file && !cache?.request?.promise) {
|
if (!error && cache?.file && !cache?.request?.promise) {
|
||||||
source = { uri: `file://${cache.file.path}`, cache: 'reload' }
|
source = { uri: `file://${cache.file.path}`, cache: 'reload' }
|
||||||
@ -62,7 +57,7 @@ const ImageSource = React.memo<{ cache?: { file?: CacheFile; request?: CacheRequ
|
|||||||
const ArtistImage = React.memo<ArtistCoverArtProps>(props => {
|
const ArtistImage = React.memo<ArtistCoverArtProps>(props => {
|
||||||
const cache = useArtistArtFile(props.artistId, props.size)
|
const cache = useArtistArtFile(props.artistId, props.size)
|
||||||
|
|
||||||
return <ImageSource cache={cache} {...props} />
|
return <ImageSource cache={cache} {...props} imageStyle={{ ...styles.artistImage, ...props.imageStyle }} />
|
||||||
})
|
})
|
||||||
|
|
||||||
const CoverArtImage = React.memo<CoverArtProps>(props => {
|
const CoverArtImage = React.memo<CoverArtProps>(props => {
|
||||||
@ -100,6 +95,9 @@ const styles = StyleSheet.create({
|
|||||||
width: '100%',
|
width: '100%',
|
||||||
position: 'absolute',
|
position: 'absolute',
|
||||||
},
|
},
|
||||||
|
artistImage: {
|
||||||
|
backgroundColor: 'rgba(81, 28, 99, 0.4)',
|
||||||
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
export default CoverArt
|
export default CoverArt
|
||||||
|
|||||||
@ -1,9 +1,10 @@
|
|||||||
import { CacheImageSize, CacheItemTypeKey } from '@app/models/cache'
|
import { CacheImageSize, CacheItemTypeKey } from '@app/models/cache'
|
||||||
|
import { ArtistInfo } from '@app/models/music'
|
||||||
import { selectCache } from '@app/state/cache'
|
import { selectCache } from '@app/state/cache'
|
||||||
|
import { selectMusic } from '@app/state/music'
|
||||||
import { selectSettings } from '@app/state/settings'
|
import { selectSettings } from '@app/state/settings'
|
||||||
import { useStore, Store } from '@app/state/store'
|
import { useStore, Store } from '@app/state/store'
|
||||||
import { useCallback, useEffect } from 'react'
|
import { useCallback, useEffect } from 'react'
|
||||||
import { useArtistInfo } from './music'
|
|
||||||
|
|
||||||
const useFileRequest = (key: CacheItemTypeKey, id: string) => {
|
const useFileRequest = (key: CacheItemTypeKey, id: string) => {
|
||||||
const file = useStore(
|
const file = useStore(
|
||||||
@ -51,23 +52,37 @@ export const useCoverArtFile = (coverArt = '-1', size: CacheImageSize = 'thumbna
|
|||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}, [cacheItem, client, coverArt, file, type])
|
// intentionally leaving file out so it doesn't re-render if the request fails
|
||||||
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||||
|
}, [cacheItem, client, coverArt, type])
|
||||||
|
|
||||||
return { file, request }
|
return { file, request }
|
||||||
}
|
}
|
||||||
|
|
||||||
export const useArtistArtFile = (artistId: string, size: CacheImageSize = 'thumbnail') => {
|
export const useArtistArtFile = (artistId: string, size: CacheImageSize = 'thumbnail') => {
|
||||||
const type: CacheItemTypeKey = size === 'original' ? 'artistArt' : 'artistArtThumb'
|
const type: CacheItemTypeKey = size === 'original' ? 'artistArt' : 'artistArtThumb'
|
||||||
const artistInfo = useArtistInfo(artistId)
|
const fetchArtistInfo = useStore(selectMusic.fetchArtistInfo)
|
||||||
const { file, request } = useFileRequest(type, artistId)
|
const { file, request } = useFileRequest(type, artistId)
|
||||||
const cacheItem = useStore(selectCache.cacheItem)
|
const cacheItem = useStore(selectCache.cacheItem)
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const url = type === 'artistArtThumb' ? artistInfo?.smallImageUrl : artistInfo?.largeImageUrl
|
if (!file) {
|
||||||
if (!file && artistInfo && url) {
|
cacheItem(type, artistId, async () => {
|
||||||
cacheItem(type, artistId, url)
|
let artistInfo: ArtistInfo | undefined
|
||||||
|
const cachedArtistInfo = useStore.getState().artistInfo[artistId]
|
||||||
|
|
||||||
|
if (cachedArtistInfo) {
|
||||||
|
artistInfo = cachedArtistInfo
|
||||||
|
} else {
|
||||||
|
artistInfo = await fetchArtistInfo(artistId)
|
||||||
|
}
|
||||||
|
|
||||||
|
return type === 'artistArtThumb' ? artistInfo?.smallImageUrl : artistInfo?.largeImageUrl
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}, [artistId, artistInfo, cacheItem, file, type])
|
// intentionally leaving file out so it doesn't re-render if the request fails
|
||||||
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||||
|
}, [artistId, cacheItem, fetchArtistInfo, type])
|
||||||
|
|
||||||
return { file, request }
|
return { file, request }
|
||||||
}
|
}
|
||||||
|
|||||||
@ -70,11 +70,6 @@ const AlbumsList = () => {
|
|||||||
overScrollMode="never"
|
overScrollMode="never"
|
||||||
onEndReached={fetchNextPage}
|
onEndReached={fetchNextPage}
|
||||||
onEndReachedThreshold={6}
|
onEndReachedThreshold={6}
|
||||||
// getItemLayout={(_data, index) => ({
|
|
||||||
// length: height,
|
|
||||||
// offset: height * Math.floor(index / 3),
|
|
||||||
// index,
|
|
||||||
// })}
|
|
||||||
/>
|
/>
|
||||||
</View>
|
</View>
|
||||||
)
|
)
|
||||||
|
|||||||
@ -57,7 +57,7 @@ const SearchResultsView: React.FC<{
|
|||||||
}),
|
}),
|
||||||
[fetchSearchResults, query, type],
|
[fetchSearchResults, query, type],
|
||||||
),
|
),
|
||||||
50,
|
100,
|
||||||
)
|
)
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@ -77,7 +77,8 @@ const SearchResultsView: React.FC<{
|
|||||||
refreshing={refreshing}
|
refreshing={refreshing}
|
||||||
overScrollMode="never"
|
overScrollMode="never"
|
||||||
onEndReached={fetchNextPage}
|
onEndReached={fetchNextPage}
|
||||||
onEndReachedThreshold={1}
|
removeClippedSubviews={true}
|
||||||
|
onEndReachedThreshold={2}
|
||||||
/>
|
/>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
import { CacheFile, CacheItemType, CacheItemTypeKey, CacheRequest } from '@app/models/cache'
|
import { CacheFile, CacheImageSize, CacheItemType, CacheItemTypeKey, CacheRequest } from '@app/models/cache'
|
||||||
import { mkdir, rmdir } from '@app/util/fs'
|
import { mkdir, rmdir } from '@app/util/fs'
|
||||||
import PromiseQueue from '@app/util/PromiseQueue'
|
import PromiseQueue from '@app/util/PromiseQueue'
|
||||||
import produce from 'immer'
|
import produce from 'immer'
|
||||||
@ -6,8 +6,13 @@ import RNFS from 'react-native-fs'
|
|||||||
import { GetState, SetState } from 'zustand'
|
import { GetState, SetState } from 'zustand'
|
||||||
import { Store } from './store'
|
import { Store } from './store'
|
||||||
|
|
||||||
const imageDownloadQueue = new PromiseQueue(50)
|
const queues: Record<CacheItemTypeKey, PromiseQueue> = {
|
||||||
const songDownloadQueue = new PromiseQueue(1)
|
coverArt: new PromiseQueue(5),
|
||||||
|
coverArtThumb: new PromiseQueue(50),
|
||||||
|
artistArt: new PromiseQueue(5),
|
||||||
|
artistArtThumb: new PromiseQueue(50),
|
||||||
|
song: new PromiseQueue(1),
|
||||||
|
}
|
||||||
|
|
||||||
export type CacheDownload = CacheFile & CacheRequest
|
export type CacheDownload = CacheFile & CacheRequest
|
||||||
|
|
||||||
@ -26,14 +31,18 @@ export type CacheRequestsByServer = Record<string, Record<CacheItemTypeKey, Reco
|
|||||||
// >
|
// >
|
||||||
|
|
||||||
export type CacheSlice = {
|
export type CacheSlice = {
|
||||||
cacheItem: (key: CacheItemTypeKey, itemId: string, url: string | (() => string | Promise<string>)) => Promise<void>
|
cacheItem: (
|
||||||
|
key: CacheItemTypeKey,
|
||||||
|
itemId: string,
|
||||||
|
url: string | (() => string | Promise<string | undefined>),
|
||||||
|
) => Promise<void>
|
||||||
|
|
||||||
// cache: DownloadedItemsByServer
|
// cache: DownloadedItemsByServer
|
||||||
cacheDirs: CacheDirsByServer
|
cacheDirs: CacheDirsByServer
|
||||||
cacheFiles: CacheFilesByServer
|
cacheFiles: CacheFilesByServer
|
||||||
cacheRequests: CacheRequestsByServer
|
cacheRequests: CacheRequestsByServer
|
||||||
|
|
||||||
fetchCoverArtFilePath: (coverArt: string) => Promise<string | undefined>
|
fetchCoverArtFilePath: (coverArt: string, size?: CacheImageSize) => Promise<string | undefined>
|
||||||
|
|
||||||
createCache: (serverId: string) => Promise<void>
|
createCache: (serverId: string) => Promise<void>
|
||||||
prepareCache: (serverId: string) => void
|
prepareCache: (serverId: string) => void
|
||||||
@ -80,44 +89,47 @@ export const createCacheSlice = (set: SetState<Store>, get: GetState<Store>): Ca
|
|||||||
}
|
}
|
||||||
|
|
||||||
const path = `${get().cacheDirs[activeServerId][key]}/${itemId}`
|
const path = `${get().cacheDirs[activeServerId][key]}/${itemId}`
|
||||||
const urlResult = typeof url === 'string' ? url : url()
|
|
||||||
const fromUrl = typeof urlResult === 'string' ? urlResult : await urlResult
|
|
||||||
|
|
||||||
const queue = key === 'song' ? songDownloadQueue : imageDownloadQueue
|
const promise = queues[key].enqueue(async () => {
|
||||||
|
const urlResult = typeof url === 'string' ? url : url()
|
||||||
|
const fromUrl = typeof urlResult === 'string' ? urlResult : await urlResult
|
||||||
|
|
||||||
const promise = queue.enqueue(() =>
|
try {
|
||||||
RNFS.downloadFile({
|
if (!fromUrl) {
|
||||||
fromUrl,
|
throw new Error('cannot resolve url for cache request')
|
||||||
toFile: path,
|
}
|
||||||
progressInterval: 100,
|
|
||||||
progress: res => {
|
await RNFS.downloadFile({
|
||||||
set(
|
fromUrl,
|
||||||
produce<CacheSlice>(state => {
|
toFile: path,
|
||||||
state.cacheRequests[activeServerId][key][itemId].progress = Math.max(
|
// progressInterval: 100,
|
||||||
1,
|
// progress: res => {
|
||||||
res.bytesWritten / (res.contentLength || 1),
|
// set(
|
||||||
)
|
// produce<CacheSlice>(state => {
|
||||||
}),
|
// state.cacheRequests[activeServerId][key][itemId].progress = Math.max(
|
||||||
)
|
// 1,
|
||||||
},
|
// res.bytesWritten / (res.contentLength || 1),
|
||||||
})
|
// )
|
||||||
.promise.then(() => {
|
// }),
|
||||||
set(
|
// )
|
||||||
produce<CacheSlice>(state => {
|
// },
|
||||||
state.cacheRequests[activeServerId][key][itemId].progress = 1
|
}).promise
|
||||||
delete state.cacheRequests[activeServerId][key][itemId].promise
|
|
||||||
}),
|
set(
|
||||||
)
|
produce<CacheSlice>(state => {
|
||||||
})
|
state.cacheRequests[activeServerId][key][itemId].progress = 1
|
||||||
.catch(() => {
|
delete state.cacheRequests[activeServerId][key][itemId].promise
|
||||||
set(
|
}),
|
||||||
produce<CacheSlice>(state => {
|
)
|
||||||
delete state.cacheFiles[activeServerId][key][itemId]
|
} catch {
|
||||||
delete state.cacheRequests[activeServerId][key][itemId]
|
set(
|
||||||
}),
|
produce<CacheSlice>(state => {
|
||||||
)
|
delete state.cacheFiles[activeServerId][key][itemId]
|
||||||
}),
|
delete state.cacheRequests[activeServerId][key][itemId]
|
||||||
)
|
}),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
})
|
||||||
set(
|
set(
|
||||||
produce<Store>(state => {
|
produce<Store>(state => {
|
||||||
state.cacheFiles[activeServerId][key][itemId] = {
|
state.cacheFiles[activeServerId][key][itemId] = {
|
||||||
@ -134,7 +146,7 @@ export const createCacheSlice = (set: SetState<Store>, get: GetState<Store>): Ca
|
|||||||
return await promise
|
return await promise
|
||||||
},
|
},
|
||||||
|
|
||||||
fetchCoverArtFilePath: async coverArt => {
|
fetchCoverArtFilePath: async (coverArt, size = 'thumbnail') => {
|
||||||
const client = get().client
|
const client = get().client
|
||||||
if (!client) {
|
if (!client) {
|
||||||
return
|
return
|
||||||
@ -151,10 +163,16 @@ export const createCacheSlice = (set: SetState<Store>, get: GetState<Store>): Ca
|
|||||||
if (inProgress.promise) {
|
if (inProgress.promise) {
|
||||||
await inProgress.promise
|
await inProgress.promise
|
||||||
}
|
}
|
||||||
return existing.path
|
return `file://${existing.path}`
|
||||||
}
|
}
|
||||||
|
|
||||||
await get().cacheItem('coverArt', coverArt, () => client.getCoverArtUri({ id: coverArt }))
|
await get().cacheItem('coverArt', coverArt, () =>
|
||||||
|
client.getCoverArtUri({
|
||||||
|
id: coverArt,
|
||||||
|
size: size === 'thumbnail' ? '256' : undefined,
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
|
||||||
return `file://${get().cacheFiles[activeServerId].coverArt[coverArt].path}`
|
return `file://${get().cacheFiles[activeServerId].coverArt[coverArt].path}`
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|||||||
@ -23,6 +23,8 @@ export const createTrackPlayerMapSlice = (set: SetState<Store>, get: GetState<St
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log(artwork)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
id: song.id,
|
id: song.id,
|
||||||
title: song.title,
|
title: song.title,
|
||||||
|
|||||||
BIN
res/fallback.png
BIN
res/fallback.png
Binary file not shown.
|
Before Width: | Height: | Size: 4.9 KiB After Width: | Height: | Size: 6.9 KiB |
Loading…
x
Reference in New Issue
Block a user