added queue context jump

This commit is contained in:
austinried 2021-08-10 12:17:36 +09:00
parent 22e3446c09
commit 9705a95aaa
7 changed files with 130 additions and 28 deletions

View File

@ -1,6 +1,7 @@
import Button from '@app/components/Button' import Button from '@app/components/Button'
import { useSetQueue } from '@app/hooks/trackplayer' import { useSetQueue } from '@app/hooks/trackplayer'
import { Song } from '@app/models/music' import { Song } from '@app/models/music'
import { QueueContextType } from '@app/state/trackplayer'
import colors from '@app/styles/colors' import colors from '@app/styles/colors'
import React, { useState } from 'react' import React, { useState } from 'react'
import { StyleProp, StyleSheet, View, ViewStyle } from 'react-native' import { StyleProp, StyleSheet, View, ViewStyle } from 'react-native'
@ -11,8 +12,10 @@ const ListPlayerControls = React.memo<{
songs: Song[] songs: Song[]
typeName: string typeName: string
queueName: string queueName: string
queueContextType: QueueContextType
queueContextId: string
style?: StyleProp<ViewStyle> style?: StyleProp<ViewStyle>
}>(({ songs, typeName, queueName, style }) => { }>(({ songs, typeName, queueName, queueContextType, queueContextId, style }) => {
const [downloaded, setDownloaded] = useState(false) const [downloaded, setDownloaded] = useState(false)
const setQueue = useSetQueue() const setQueue = useSetQueue()
@ -28,10 +31,13 @@ const ListPlayerControls = React.memo<{
</Button> </Button>
</View> </View>
<View style={styles.controlsCenter}> <View style={styles.controlsCenter}>
<Button title={`Play ${typeName}`} onPress={() => setQueue(songs, queueName, undefined, false)} /> <Button
title={`Play ${typeName}`}
onPress={() => setQueue(songs, queueName, queueContextType, queueContextId, undefined, false)}
/>
</View> </View>
<View style={styles.controlsSide}> <View style={styles.controlsSide}>
<Button onPress={() => setQueue(songs, queueName, undefined, true)}> <Button onPress={() => setQueue(songs, queueName, queueContextType, queueContextId, undefined, true)}>
<Icon name="shuffle" size={26} color="white" /> <Icon name="shuffle" size={26} color="white" />
</Button> </Button>
</View> </View>

View File

@ -5,6 +5,7 @@ import {
getCurrentTrack, getCurrentTrack,
getQueue, getQueue,
getRepeatMode, getRepeatMode,
QueueContextType,
selectTrackPlayer, selectTrackPlayer,
TrackExt, TrackExt,
trackPlayerCommands, trackPlayerCommands,
@ -179,9 +180,18 @@ export const useSetQueue = () => {
const setShuffleOrder = useStore(selectTrackPlayer.setShuffleOrder) const setShuffleOrder = useStore(selectTrackPlayer.setShuffleOrder)
const setQueueName = useStore(selectTrackPlayer.setName) const setQueueName = useStore(selectTrackPlayer.setName)
const getQueueShuffled = useCallback(() => !!useStore.getState().shuffleOrder, []) const getQueueShuffled = useCallback(() => !!useStore.getState().shuffleOrder, [])
const setQueueContextType = useStore(selectTrackPlayer.setQueueContextType)
const setQueueContextId = useStore(selectTrackPlayer.setQueueContextId)
const coverArtUri = useCoverArtUri() const coverArtUri = useCoverArtUri()
return async (songs: Song[], name: string, playTrack?: number, shuffle?: boolean) => return async (
songs: Song[],
name: string,
contextType: QueueContextType,
contextId: string,
playTrack?: number,
shuffle?: boolean,
) =>
trackPlayerCommands.enqueue(async () => { trackPlayerCommands.enqueue(async () => {
const shuffled = shuffle !== undefined ? shuffle : getQueueShuffled() const shuffled = shuffle !== undefined ? shuffle : getQueueShuffled()
@ -208,6 +218,8 @@ export const useSetQueue = () => {
setQueue(queue) setQueue(queue)
setCurrentTrackIdx(playTrack) setCurrentTrackIdx(playTrack)
setQueueName(name) setQueueName(name)
setQueueContextType(contextType)
setQueueContextId(contextId)
if (playTrack === 0) { if (playTrack === 0) {
await TrackPlayer.add(queue) await TrackPlayer.add(queue)

View File

@ -42,7 +42,8 @@ const AlbumItem = React.memo<{
const TopSongs = React.memo<{ const TopSongs = React.memo<{
songs: Song[] songs: Song[]
name: string name: string
}>(({ songs, name }) => { artistId: string
}>(({ songs, name, artistId }) => {
const setQueue = useSetQueue() const setQueue = useSetQueue()
return ( return (
@ -54,7 +55,7 @@ const TopSongs = React.memo<{
item={s} item={s}
showArt={true} showArt={true}
subtitle={s.album} subtitle={s.album}
onPress={() => setQueue(songs, `Top Songs: ${name}`, i)} onPress={() => setQueue(songs, name, 'artist', artistId, i)}
/> />
))} ))}
</> </>
@ -99,7 +100,11 @@ const ArtistDetails: React.FC<{ id: string }> = ({ id }) => {
<Text style={styles.title}>{artist.name}</Text> <Text style={styles.title}>{artist.name}</Text>
</View> </View>
<View style={styles.container}> <View style={styles.container}>
{artist.topSongs.length > 0 ? <TopSongs songs={artist.topSongs} name={artist.name} /> : <></>} {artist.topSongs.length > 0 ? (
<TopSongs songs={artist.topSongs} name={artist.name} artistId={artist.id} />
) : (
<></>
)}
<Header>Albums</Header> <Header>Albums</Header>
<View style={styles.albums} onLayout={albumsLayout.onLayout}> <View style={styles.albums} onLayout={albumsLayout.onLayout}>
{_albums.map(a => ( {_albums.map(a => (

View File

@ -6,7 +6,7 @@ import { useStarred } from '@app/hooks/music'
import { useNext, usePause, usePlay, usePrevious, useToggleRepeat, useToggleShuffle } from '@app/hooks/trackplayer' import { useNext, usePause, usePlay, usePrevious, useToggleRepeat, useToggleShuffle } from '@app/hooks/trackplayer'
import { selectMusic } from '@app/state/music' import { selectMusic } from '@app/state/music'
import { useStore } from '@app/state/store' import { useStore } from '@app/state/store'
import { selectTrackPlayer } from '@app/state/trackplayer' import { QueueContextType, selectTrackPlayer } from '@app/state/trackplayer'
import colors from '@app/styles/colors' import colors from '@app/styles/colors'
import dimensions from '@app/styles/dimensions' import dimensions from '@app/styles/dimensions'
import font from '@app/styles/font' import font from '@app/styles/font'
@ -22,20 +22,63 @@ import Icon from 'react-native-vector-icons/Ionicons'
import IconMatCom from 'react-native-vector-icons/MaterialCommunityIcons' import IconMatCom from 'react-native-vector-icons/MaterialCommunityIcons'
import IconMat from 'react-native-vector-icons/MaterialIcons' import IconMat from 'react-native-vector-icons/MaterialIcons'
const NowPlayingHeader = React.memo<{ function getContextName(type?: QueueContextType) {
backHandler: () => void switch (type) {
}>(({ backHandler }) => { case 'album':
return 'Album'
case 'artist':
return 'Top Songs'
case 'playlist':
return 'Playlist'
case 'song':
return 'Search Results'
default:
return undefined
}
}
const NowPlayingHeader = React.memo(() => {
const navigation = useNavigation()
const queueName = useStore(selectTrackPlayer.name) const queueName = useStore(selectTrackPlayer.name)
const queueContextType = useStore(selectTrackPlayer.queueContextType)
const queueContextId = useStore(selectTrackPlayer.queueContextId)
let contextName = getContextName(queueContextType)
const back = useCallback(() => {
navigation.navigate('top')
}, [navigation])
const goToContext = useCallback(() => {
if (!queueContextType || !queueContextId || queueContextType === 'song') {
return
}
navigation.navigate('library')
navigation.navigate(queueContextType, { id: queueContextId, title: queueName })
}, [navigation, queueContextId, queueContextType, queueName])
return ( return (
<View style={headerStyles.container}> <View style={headerStyles.container}>
<PressableOpacity onPress={backHandler} style={headerStyles.icons} ripple={true}> <PressableOpacity onPress={back} style={headerStyles.icons} ripple={true}>
<IconMat name="arrow-back" color="white" size={25} /> <IconMat name="arrow-back" color="white" size={25} />
</PressableOpacity> </PressableOpacity>
<View style={headerStyles.center}>
{contextName ? (
<Text numberOfLines={1} style={headerStyles.queueType}>
{contextName}
</Text>
) : (
<></>
)}
<Text numberOfLines={1} style={headerStyles.queueName}> <Text numberOfLines={1} style={headerStyles.queueName}>
{queueName || 'Nothing playing...'} {queueName || 'Nothing playing...'}
</Text> </Text>
<PressableOpacity onPress={undefined} style={headerStyles.icons} ripple={true}> </View>
<PressableOpacity
onPress={goToContext}
style={headerStyles.icons}
disabled={queueContextType === 'song'}
ripple={true}>
<IconMat name="more-vert" color="white" size={25} /> <IconMat name="more-vert" color="white" size={25} />
</PressableOpacity> </PressableOpacity>
</View> </View>
@ -55,11 +98,21 @@ const headerStyles = StyleSheet.create({
width: 42, width: 42,
marginHorizontal: 8, marginHorizontal: 8,
}, },
center: {
flex: 1,
},
queueType: {
fontFamily: font.regular,
fontSize: 14,
color: colors.text.primary,
// flex: 1,
textAlign: 'center',
},
queueName: { queueName: {
fontFamily: font.bold, fontFamily: font.bold,
fontSize: 16, fontSize: 16,
color: colors.text.primary, color: colors.text.primary,
flex: 1, // flex: 1,
textAlign: 'center', textAlign: 'center',
}, },
}) })
@ -315,20 +368,16 @@ type NowPlayingProps = NativeStackScreenProps<RootStackParamList, 'main'>
const NowPlayingView: React.FC<NowPlayingProps> = ({ navigation }) => { const NowPlayingView: React.FC<NowPlayingProps> = ({ navigation }) => {
const track = useStore(selectTrackPlayer.currentTrack) const track = useStore(selectTrackPlayer.currentTrack)
const back = useCallback(() => {
navigation.navigate('top')
}, [navigation])
useEffect(() => { useEffect(() => {
if (!track) { if (!track) {
back() navigation.navigate('top')
} }
}) })
return ( return (
<View style={styles.container}> <View style={styles.container}>
<ImageGradientBackground imageUri={track?.artwork as string} imageKey={`${track?.album}${track?.artist}`} /> <ImageGradientBackground imageUri={track?.artwork as string} imageKey={`${track?.album}${track?.artist}`} />
<NowPlayingHeader backHandler={back} /> <NowPlayingHeader />
<View style={styles.content}> <View style={styles.content}>
<SongCoverArt /> <SongCoverArt />
<SongInfo /> <SongInfo />

View File

@ -21,7 +21,7 @@ const SongItem = React.memo<{ item: Song }>(({ item }) => {
item={item} item={item}
showArt={true} showArt={true}
showStar={false} showStar={false}
onPress={() => setQueue([item], `Search: ${item.title}`, 0)} onPress={() => setQueue([item], item.title, 'song', item.id, 0)}
/> />
) )
}) })

View File

@ -25,7 +25,8 @@ const Songs = React.memo<{
songs: Song[] songs: Song[]
name: string name: string
type: SongListType type: SongListType
}>(({ songs, name, type }) => { itemId: string
}>(({ songs, name, type, itemId }) => {
const setQueue = useSetQueue() const setQueue = useSetQueue()
const _songs = [...songs] const _songs = [...songs]
@ -42,14 +43,21 @@ const Songs = React.memo<{
return ( return (
<> <>
<ListPlayerControls style={styles.controls} songs={_songs} typeName={typeName} queueName={name} /> <ListPlayerControls
style={styles.controls}
songs={_songs}
typeName={typeName}
queueName={name}
queueContextId={itemId}
queueContextType={type}
/>
<View style={styles.songs}> <View style={styles.songs}>
{_songs.map((s, i) => ( {_songs.map((s, i) => (
<ListItem <ListItem
key={i} key={i}
item={s} item={s}
subtitle={s.artist} subtitle={s.artist}
onPress={() => setQueue(songs, name, i)} onPress={() => setQueue(songs, name, type, itemId, i)}
showArt={type === 'playlist'} showArt={type === 'playlist'}
/> />
))} ))}
@ -76,7 +84,7 @@ const SongListDetails = React.memo<{
<Text style={styles.title}>{songList.name}</Text> <Text style={styles.title}>{songList.name}</Text>
{subtitle ? <Text style={styles.subtitle}>{subtitle}</Text> : <></>} {subtitle ? <Text style={styles.subtitle}>{subtitle}</Text> : <></>}
{songList.songs.length > 0 ? ( {songList.songs.length > 0 ? (
<Songs songs={songList.songs} name={songList.name} type={type} /> <Songs songs={songList.songs} name={songList.name} type={type} itemId={songList.id} />
) : ( ) : (
<NothingHere height={300} width={250} /> <NothingHere height={300} width={250} />
)} )}

View File

@ -15,10 +15,18 @@ export type Progress = {
buffered: number buffered: number
} }
export type QueueContextType = 'album' | 'playlist' | 'song' | 'artist'
export type TrackPlayerSlice = { export type TrackPlayerSlice = {
name?: string name?: string
setName: (name?: string) => void setName: (name?: string) => void
queueContextType?: QueueContextType
setQueueContextType: (queueContextType?: QueueContextType) => void
queueContextId?: string
setQueueContextId: (queueContextId?: string) => void
shuffleOrder?: number[] shuffleOrder?: number[]
setShuffleOrder: (shuffleOrder?: number[]) => void setShuffleOrder: (shuffleOrder?: number[]) => void
@ -47,6 +55,12 @@ export const selectTrackPlayer = {
name: (store: TrackPlayerSlice) => store.name, name: (store: TrackPlayerSlice) => store.name,
setName: (store: TrackPlayerSlice) => store.setName, setName: (store: TrackPlayerSlice) => store.setName,
queueContextType: (store: TrackPlayerSlice) => store.queueContextType,
setQueueContextType: (store: TrackPlayerSlice) => store.setQueueContextType,
queueContextId: (store: TrackPlayerSlice) => store.queueContextId,
setQueueContextId: (store: TrackPlayerSlice) => store.setQueueContextId,
shuffleOrder: (store: TrackPlayerSlice) => store.shuffleOrder, shuffleOrder: (store: TrackPlayerSlice) => store.shuffleOrder,
setShuffleOrder: (store: TrackPlayerSlice) => store.setShuffleOrder, setShuffleOrder: (store: TrackPlayerSlice) => store.setShuffleOrder,
shuffled: (store: TrackPlayerSlice) => !!store.shuffleOrder, shuffled: (store: TrackPlayerSlice) => !!store.shuffleOrder,
@ -78,6 +92,12 @@ export const createTrackPlayerSlice = (set: SetState<Store>, get: GetState<Store
name: undefined, name: undefined,
setName: name => set({ name }), setName: name => set({ name }),
queueContextType: undefined,
setQueueContextType: queueContextType => set({ queueContextType }),
queueContextId: undefined,
setQueueContextId: queueContextId => set({ queueContextId }),
shuffleOrder: undefined, shuffleOrder: undefined,
setShuffleOrder: shuffleOrder => set({ shuffleOrder }), setShuffleOrder: shuffleOrder => set({ shuffleOrder }),
@ -122,6 +142,8 @@ export const createTrackPlayerSlice = (set: SetState<Store>, get: GetState<Store
reset: () => { reset: () => {
set({ set({
name: undefined, name: undefined,
queueContextType: undefined,
queueContextId: undefined,
shuffleOrder: undefined, shuffleOrder: undefined,
repeatMode: RepeatMode.Off, repeatMode: RepeatMode.Off,
playerState: State.None, playerState: State.None,