fix current idx not being set on toggle shuffle

switch to passing params instead of function
This commit is contained in:
austinried 2021-08-11 14:46:30 +09:00
parent 9fda955df6
commit f6ecc0bf40
7 changed files with 47 additions and 38 deletions

View File

@ -1,4 +1,5 @@
import { useStarred } from '@app/hooks/music' import { useStarred } from '@app/hooks/music'
import { useIsPlaying } from '@app/hooks/trackplayer'
import { AlbumListItem, Artist, ListableItem, Song } from '@app/models/music' import { AlbumListItem, Artist, ListableItem, Song } from '@app/models/music'
import { selectMusic } from '@app/state/music' import { selectMusic } from '@app/state/music'
import { useStore } from '@app/state/store' import { useStore } from '@app/state/store'
@ -16,15 +17,16 @@ import PressableOpacity from './PressableOpacity'
import Star from './Star' import Star from './Star'
const TitleTextSong = React.memo<{ const TitleTextSong = React.memo<{
isPlaying?: () => boolean contextId?: string
queueId: number
title?: string title?: string
}>(({ isPlaying, title }) => { }>(({ contextId, queueId, title }) => {
const playing = () => isPlaying && isPlaying() const playing = useIsPlaying(contextId, queueId)
return ( return (
<View style={styles.textLine}> <View style={styles.textLine}>
<Text style={[styles.title, { color: playing() ? colors.accent : colors.text.primary }]}> <Text style={[styles.title, { color: playing ? colors.accent : colors.text.primary }]}>
{playing() ? ( {playing ? (
<View style={styles.playingIcon}> <View style={styles.playingIcon}>
<IconFA5 name="play" size={9} color={colors.accent} /> <IconFA5 name="play" size={9} color={colors.accent} />
</View> </View>
@ -49,13 +51,14 @@ const TitleText = React.memo<{
const ListItem: React.FC<{ const ListItem: React.FC<{
item: ListableItem item: ListableItem
isPlaying?: () => boolean contextId?: string
queueId?: number
onPress?: () => void onPress?: () => void
showArt?: boolean showArt?: boolean
showStar?: boolean showStar?: boolean
listStyle?: 'big' | 'small' listStyle?: 'big' | 'small'
subtitle?: string subtitle?: string
}> = ({ item, isPlaying, onPress, showArt, showStar, subtitle, listStyle }) => { }> = ({ item, contextId, queueId, onPress, showArt, showStar, subtitle, listStyle }) => {
const navigation = useNavigation() const navigation = useNavigation()
const starred = useStarred(item.id, item.itemType) const starred = useStarred(item.id, item.itemType)
@ -138,6 +141,13 @@ const ListItem: React.FC<{
starItem(item.id, item.itemType, starred) starItem(item.id, item.itemType, starred)
}, [item.id, item.itemType, starItem, starred]) }, [item.id, item.itemType, starItem, starred])
let title = <></>
if (item.itemType === 'song' && queueId !== undefined) {
title = <TitleTextSong contextId={contextId} queueId={queueId} title={item.title} />
} else if (item.itemType !== 'song') {
title = <TitleText title={item.name} />
}
return ( return (
<View style={[styles.container, sizeStyle.container]}> <View style={[styles.container, sizeStyle.container]}>
<PressableComponent> <PressableComponent>
@ -151,11 +161,7 @@ const ListItem: React.FC<{
<></> <></>
)} )}
<View style={styles.text}> <View style={styles.text}>
{item.itemType === 'song' ? ( {title}
<TitleTextSong isPlaying={isPlaying} title={item.title} />
) : (
<TitleText title={item.name} />
)}
{subtitle ? ( {subtitle ? (
<View style={styles.textLine}> <View style={styles.textLine}>
{starred ? ( {starred ? (

View File

@ -135,6 +135,7 @@ function unshuffleTracks(tracks: TrackExt[], shuffleOrder: number[]): TrackExt[]
} }
export const useToggleShuffle = () => { export const useToggleShuffle = () => {
const setCurrentTrackIdx = useStore(selectTrackPlayer.setCurrentTrackIdx)
const setQueue = useStore(selectTrackPlayer.setQueue) const setQueue = useStore(selectTrackPlayer.setQueue)
const setShuffleOrder = useStore(selectTrackPlayer.setShuffleOrder) const setShuffleOrder = useStore(selectTrackPlayer.setShuffleOrder)
const getShuffleOrder = useCallback(() => useStore.getState().shuffleOrder, []) const getShuffleOrder = useCallback(() => useStore.getState().shuffleOrder, [])
@ -176,6 +177,7 @@ export const useToggleShuffle = () => {
} }
setQueue(await getQueue()) setQueue(await getQueue())
setCurrentTrackIdx(await getCurrentTrack())
setProgress(progress) setProgress(progress)
}) })
} }
@ -243,21 +245,23 @@ export const useSetQueue = () => {
}) })
} }
export const useIsPlaying = () => { export const useIsPlaying = (contextId: string | undefined, track: number) => {
const queueContextId = useStore(selectTrackPlayer.queueContextId) const queueContextId = useStore(selectTrackPlayer.queueContextId)
const currentTrackIdx = useStore(selectTrackPlayer.currentTrackIdx) const currentTrackIdx = useStore(selectTrackPlayer.currentTrackIdx)
const shuffleOrder = useStore(selectTrackPlayer.shuffleOrder) const shuffleOrder = useStore(selectTrackPlayer.shuffleOrder)
return (contextId: string | undefined, track: number) => { if (contextId === undefined) {
if (contextId === undefined) { console.log(currentTrackIdx)
return track === currentTrackIdx return track === currentTrackIdx
}
if (shuffleOrder) {
const shuffledTrack = shuffleOrder.findIndex(i => i === track)
track = shuffledTrack !== undefined ? shuffledTrack : -1
}
return contextId === queueContextId && track === currentTrackIdx
} }
if (shuffleOrder) {
console.log('asdf')
const shuffledTrack = shuffleOrder.findIndex(i => i === track)
track = shuffledTrack !== undefined ? shuffledTrack : -1
}
return contextId === queueContextId && track === currentTrackIdx
} }
function mapSongToTrack(song: Song, coverArtUri: (coverArt?: string) => string | undefined): TrackExt { function mapSongToTrack(song: Song, coverArtUri: (coverArt?: string) => string | undefined): TrackExt {

View File

@ -5,7 +5,7 @@ import GradientScrollView from '@app/components/GradientScrollView'
import Header from '@app/components/Header' import Header from '@app/components/Header'
import ListItem from '@app/components/ListItem' import ListItem from '@app/components/ListItem'
import { useArtistInfo } from '@app/hooks/music' import { useArtistInfo } from '@app/hooks/music'
import { useIsPlaying, useSetQueue } from '@app/hooks/trackplayer' import { useSetQueue } from '@app/hooks/trackplayer'
import { Album, Song } from '@app/models/music' import { Album, Song } from '@app/models/music'
import colors from '@app/styles/colors' import colors from '@app/styles/colors'
import font from '@app/styles/font' import font from '@app/styles/font'
@ -45,7 +45,6 @@ const TopSongs = React.memo<{
artistId: string artistId: string
}>(({ songs, name, artistId }) => { }>(({ songs, name, artistId }) => {
const setQueue = useSetQueue() const setQueue = useSetQueue()
const isPlaying = useIsPlaying()
return ( return (
<> <>
@ -54,7 +53,8 @@ const TopSongs = React.memo<{
<ListItem <ListItem
key={i} key={i}
item={s} item={s}
isPlaying={() => isPlaying(artistId, i)} contextId={artistId}
queueId={i}
showArt={true} showArt={true}
subtitle={s.album} subtitle={s.album}
onPress={() => setQueue(songs, name, 'artist', artistId, i)} onPress={() => setQueue(songs, name, 'artist', artistId, i)}

View File

@ -1,7 +1,7 @@
import GradientScrollView from '@app/components/GradientScrollView' import GradientScrollView from '@app/components/GradientScrollView'
import ListItem from '@app/components/ListItem' import ListItem from '@app/components/ListItem'
import NowPlayingBar from '@app/components/NowPlayingBar' import NowPlayingBar from '@app/components/NowPlayingBar'
import { mapTrackExtToSong, useIsPlaying, useSkipTo } from '@app/hooks/trackplayer' import { mapTrackExtToSong, useSkipTo } from '@app/hooks/trackplayer'
import { useStore } from '@app/state/store' import { useStore } from '@app/state/store'
import { selectTrackPlayer } from '@app/state/trackplayer' import { selectTrackPlayer } from '@app/state/trackplayer'
import React from 'react' import React from 'react'
@ -10,7 +10,6 @@ import { StyleSheet, View } from 'react-native'
const NowPlayingQueue = React.memo<{}>(() => { const NowPlayingQueue = React.memo<{}>(() => {
const queue = useStore(selectTrackPlayer.queue) const queue = useStore(selectTrackPlayer.queue)
const skipTo = useSkipTo() const skipTo = useSkipTo()
const isPlaying = useIsPlaying()
return ( return (
<View style={styles.outerContainer}> <View style={styles.outerContainer}>
@ -20,7 +19,7 @@ const NowPlayingQueue = React.memo<{}>(() => {
<ListItem <ListItem
key={i} key={i}
item={song} item={song}
isPlaying={() => isPlaying(undefined, i)} queueId={i}
onPress={() => skipTo(i)} onPress={() => skipTo(i)}
showArt={true} showArt={true}
subtitle={`${song.artist}${song.album}`} subtitle={`${song.artist}${song.album}`}

View File

@ -327,7 +327,7 @@ const PlayerControls = () => {
<View style={controlsStyles.container}> <View style={controlsStyles.container}>
<View style={controlsStyles.top}> <View style={controlsStyles.top}>
<View style={controlsStyles.center}> <View style={controlsStyles.center}>
<PressableOpacity onPress={() => toggleRepeat()} disabled={disabled}> <PressableOpacity onPress={() => toggleRepeat()} disabled={disabled} hitSlop={16}>
<Icon name="repeat" size={26} color={repeatMode === RepeatMode.Off ? 'white' : colors.accent} /> <Icon name="repeat" size={26} color={repeatMode === RepeatMode.Off ? 'white' : colors.accent} />
<Text style={[controlsStyles.repeatExt, repeatMode === RepeatMode.Track ? { opacity: 1 } : {}]}>1</Text> <Text style={[controlsStyles.repeatExt, repeatMode === RepeatMode.Track ? { opacity: 1 } : {}]}>1</Text>
</PressableOpacity> </PressableOpacity>
@ -346,16 +346,16 @@ const PlayerControls = () => {
</View> </View>
<View style={controlsStyles.center}> <View style={controlsStyles.center}>
<PressableOpacity onPress={() => toggleShuffle()} disabled={disabled}> <PressableOpacity onPress={() => toggleShuffle()} disabled={disabled} hitSlop={16}>
<Icon name="shuffle" size={26} color={shuffled ? colors.accent : 'white'} /> <Icon name="shuffle" size={26} color={shuffled ? colors.accent : 'white'} />
</PressableOpacity> </PressableOpacity>
</View> </View>
</View> </View>
<View style={controlsStyles.bottom}> <View style={controlsStyles.bottom}>
<PressableOpacity onPress={undefined} disabled={true}> <PressableOpacity onPress={undefined} disabled={true} hitSlop={16}>
<IconMatCom name="cast-audio" size={20} color="white" /> <IconMatCom name="cast-audio" size={20} color="white" />
</PressableOpacity> </PressableOpacity>
<PressableOpacity onPress={() => navigation.navigate('queue')} disabled={disabled}> <PressableOpacity onPress={() => navigation.navigate('queue')} disabled={disabled} hitSlop={16}>
<IconMatCom name="playlist-play" size={24} color="white" /> <IconMatCom name="playlist-play" size={24} color="white" />
</PressableOpacity> </PressableOpacity>
</View> </View>

View File

@ -3,7 +3,7 @@ import Header from '@app/components/Header'
import ListItem from '@app/components/ListItem' import ListItem from '@app/components/ListItem'
import NothingHere from '@app/components/NothingHere' import NothingHere from '@app/components/NothingHere'
import { useActiveListRefresh2 } from '@app/hooks/server' import { useActiveListRefresh2 } from '@app/hooks/server'
import { useIsPlaying, useSetQueue } from '@app/hooks/trackplayer' import { useSetQueue } from '@app/hooks/trackplayer'
import { ListableItem, SearchResults, Song } from '@app/models/music' import { ListableItem, SearchResults, Song } from '@app/models/music'
import { selectMusic } from '@app/state/music' import { selectMusic } from '@app/state/music'
import { useStore } from '@app/state/store' import { useStore } from '@app/state/store'
@ -15,12 +15,12 @@ import { ActivityIndicator, StatusBar, StyleSheet, TextInput, View } from 'react
const SongItem = React.memo<{ item: Song }>(({ item }) => { const SongItem = React.memo<{ item: Song }>(({ item }) => {
const setQueue = useSetQueue() const setQueue = useSetQueue()
const isPlaying = useIsPlaying()
return ( return (
<ListItem <ListItem
item={item} item={item}
isPlaying={() => isPlaying(item.id, 0)} contextId={item.id}
queueId={0}
showArt={true} showArt={true}
showStar={false} showStar={false}
onPress={() => setQueue([item], item.title, 'song', item.id, 0)} onPress={() => setQueue([item], item.title, 'song', item.id, 0)}

View File

@ -5,7 +5,7 @@ import ListItem from '@app/components/ListItem'
import ListPlayerControls from '@app/components/ListPlayerControls' import ListPlayerControls from '@app/components/ListPlayerControls'
import NothingHere from '@app/components/NothingHere' import NothingHere from '@app/components/NothingHere'
import { useAlbumWithSongs, useCoverArtUri, usePlaylistWithSongs } from '@app/hooks/music' import { useAlbumWithSongs, useCoverArtUri, usePlaylistWithSongs } from '@app/hooks/music'
import { useIsPlaying, useSetQueue } from '@app/hooks/trackplayer' import { useSetQueue } from '@app/hooks/trackplayer'
import { AlbumWithSongs, PlaylistWithSongs, Song } from '@app/models/music' import { AlbumWithSongs, PlaylistWithSongs, Song } from '@app/models/music'
import colors from '@app/styles/colors' import colors from '@app/styles/colors'
import font from '@app/styles/font' import font from '@app/styles/font'
@ -28,7 +28,6 @@ const Songs = React.memo<{
itemId: string itemId: string
}>(({ songs, name, type, itemId }) => { }>(({ songs, name, type, itemId }) => {
const setQueue = useSetQueue() const setQueue = useSetQueue()
const isPlaying = useIsPlaying()
const _songs = [...songs] const _songs = [...songs]
let typeName = '' let typeName = ''
@ -57,7 +56,8 @@ const Songs = React.memo<{
<ListItem <ListItem
key={i} key={i}
item={s} item={s}
isPlaying={() => isPlaying(itemId, i)} contextId={itemId}
queueId={i}
subtitle={s.artist} subtitle={s.artist}
onPress={() => setQueue(songs, name, type, itemId, i)} onPress={() => setQueue(songs, name, type, itemId, i)}
showArt={type === 'playlist'} showArt={type === 'playlist'}