subtracks/app/components/NowPlayingBar.tsx
austinried 8196704ccd
React Query refactor (#91)
* initial react-query experiments

* use queries for item screens

send the data we do have over routing to prepopulate (album/playlist)
use number for starred because sending Date freaks out react-navigation

* add in equiv. song cover art fix

* reorg, switch artistview over

start mapping song cover art when any are available

* refactor useStar to queries

fix caching for starred items and album cover art

* add hook to reset queries on server change

* refactor search to use query

* fix song cover art setting

* use query for artistInfo

* remove last bits of library state

* cleanup

* use query key factory

already fixed one wrong key...

* require coverart size

* let's try no promise queues on these for now

* image cache uses query

* perf fix for playlist parsing

also use placeholder data so we don't have to deal with staleness

* drill that disabled

also list controls doesn't need its own songs hook/copy

* switch to react-native-blob-util for downloads

slightly slower but allows us to use DownloadManager, which backgrounds downloads so they are no longer corrupted when the app suspends

* add a fake "top songs" based on artist search

then sorted by play count/ratings
artistview should load now even if topSongs fails

* try not to swap between topSongs/search on refetch

set queueContext by song list so the index isn't off if the list changes

* add content type validation for file fetching

also try to speed up existing file return by limiting fs ops

* if the HEAD fails, don't queue the download

* clean up params

* reimpl clear image cache

* precompute contextId

prevents wrong "is playing" when any mismatch between queue and list

* clear images from all servers

use external files dir instead of cache

* fix pressable disabled flicker

don't retry topsongs on failure
try to optimize setqueue and fixcoverart a bit

* wait for queries during clear

* break out fetchExistingFile from fetchFile

allows to tell if file is coming from disk or not
only show placeholder/loading spinner if actually fetching image

* forgot these wouldn't do anything with objects

* remove query cache when switching servers

* add content-disposition extension gathering

add support for progress hook (needs native support still)

* added custom RNBU pkg with progress changes

* fully unmount tabs when server changes

prevents unwanted requests, gives fresh start on switch
fix fixCoverArt not re-rendering in certain cases on search

* use serverId from fetch deps

* fix lint

* update licenses

* just use the whole lodash package

* make using cache buster optional
2022-04-11 09:40:51 +09:00

151 lines
3.9 KiB
TypeScript

import CoverArt from '@app/components/CoverArt'
import PressableOpacity from '@app/components/PressableOpacity'
import { usePause, usePlay } from '@app/hooks/trackplayer'
import { useStore } from '@app/state/store'
import colors from '@app/styles/colors'
import font from '@app/styles/font'
import { useNavigation } from '@react-navigation/native'
import React, { useCallback } from 'react'
import { ActivityIndicator, Pressable, StyleSheet, Text, View, ViewStyle } from 'react-native'
import { State } from 'react-native-track-player'
import IconFA5 from 'react-native-vector-icons/FontAwesome5'
const ProgressBar = React.memo(() => {
const position = useStore(store => store.progress.position)
const duration = useStore(store => store.progress.duration)
let progress = 0
if (duration > 0) {
progress = position / duration
}
return (
<View style={progressStyles.container}>
<View style={[progressStyles.left, { flex: progress }]} />
<View style={[progressStyles.right, { flex: 1 - progress }]} />
</View>
)
})
const progressStyles = StyleSheet.create({
container: {
height: 2,
flexDirection: 'row',
},
left: {
backgroundColor: colors.text.primary,
},
right: {
backgroundColor: '#595959',
},
})
const Controls = React.memo(() => {
const state = useStore(store => store.playerState)
const play = usePlay()
const pause = usePause()
let playPauseIcon: string
switch (state) {
case State.Playing:
playPauseIcon = 'pause'
break
default:
playPauseIcon = 'play'
break
}
const action = useCallback(() => {
if (state === State.Playing) {
pause()
} else {
play()
}
}, [state, play, pause])
return (
<View style={styles.controls}>
{state === State.Buffering ? (
<ActivityIndicator color="white" size="large" animating={true} />
) : (
<PressableOpacity onPress={action} hitSlop={14}>
<IconFA5 name={playPauseIcon} size={28} color="white" />
</PressableOpacity>
)}
</View>
)
})
const NowPlayingBar = React.memo(() => {
const navigation = useNavigation()
const currentTrackExists = useStore(store => !!store.currentTrack)
const coverArt = useStore(store => store.currentTrack?.coverArt)
const title = useStore(store => store.currentTrack?.title)
const artist = useStore(store => store.currentTrack?.artist)
const displayStyle: ViewStyle = { display: currentTrackExists ? 'flex' : 'none' }
return (
<Pressable onPress={() => navigation.navigate('now-playing')} style={[styles.container, displayStyle]}>
<ProgressBar />
<View style={styles.subContainer}>
<CoverArt
type="cover"
style={{ height: styles.subContainer.height, width: styles.subContainer.height }}
coverArt={coverArt}
size="thumbnail"
fadeDuration={0}
/>
<View style={styles.detailsContainer}>
<Text numberOfLines={1} style={styles.detailsTitle}>
{title}
</Text>
<Text numberOfLines={1} style={styles.detailsAlbum}>
{artist}
</Text>
</View>
<Controls />
</View>
</Pressable>
)
})
const styles = StyleSheet.create({
container: {
width: '100%',
backgroundColor: colors.gradient.high,
borderBottomColor: colors.gradient.low,
borderBottomWidth: 1,
},
subContainer: {
height: 60,
flexDirection: 'row',
},
detailsContainer: {
flex: 1,
height: '100%',
justifyContent: 'center',
marginLeft: 10,
},
detailsTitle: {
fontFamily: font.semiBold,
fontSize: 13,
color: colors.text.primary,
},
detailsAlbum: {
fontFamily: font.regular,
fontSize: 13,
color: colors.text.secondary,
},
controls: {
flexDirection: 'row',
height: '100%',
justifyContent: 'center',
alignItems: 'center',
marginRight: 18,
marginLeft: 12,
},
})
export default NowPlayingBar