fix: shuffle the full collection when selecting a song
This commit is contained in:
9 files changed
+109
-31
No files matched your search
@@ -1,7 +1,7 @@
|
||||
import type { ConfigContext, ExpoConfig } from "expo/config"
|
||||
|
||||
export default ({ config }: ConfigContext): ExpoConfig => {
|
||||
const variant =
|
||||
const variant: string =
|
||||
process.env.APP_VARIANT ?? process.env.EAS_BUILD_PROFILE ?? "production"
|
||||
if (
|
||||
variant !== "development" &&
|
||||
|
||||
@@ -111,7 +111,9 @@ export default function AlbumScreen() {
|
||||
songIds={songIds}
|
||||
palette={palette}
|
||||
favorited={isSongFavorite(item.track.id)}
|
||||
onPlay={playFromSong}
|
||||
onPlay={(songId, ids) =>
|
||||
playFromSong(songId, ids, `album:${album.id}`)
|
||||
}
|
||||
onAction={handleAlbumTrackAction}
|
||||
/>
|
||||
) : null
|
||||
|
||||
@@ -161,8 +161,9 @@ function PlaylistContent({
|
||||
const swipeGroup = useSwipeRowGroup()
|
||||
|
||||
const handlePlaySong = useCallback(
|
||||
(song: BrowseSong) => void playFromSong(song.id, songIds),
|
||||
[playFromSong, songIds]
|
||||
(song: BrowseSong) =>
|
||||
void playFromSong(song.id, songIds, `playlist:${playlistId}`),
|
||||
[playFromSong, songIds, playlistId]
|
||||
)
|
||||
|
||||
const handleSongMenuAction = useCallback(
|
||||
|
||||
@@ -87,7 +87,11 @@ type PlaybackContextValue = {
|
||||
playShuffled: (songIds: Id<"songs">[]) => Promise<void>
|
||||
addToQueueNext: (songIds: Id<"songs">[]) => Promise<void>
|
||||
addToQueueLast: (songIds: Id<"songs">[]) => Promise<void>
|
||||
playFromSong: (songId: Id<"songs">, songIds: Id<"songs">[]) => Promise<void>
|
||||
playFromSong: (
|
||||
songId: Id<"songs">,
|
||||
songIds: Id<"songs">[],
|
||||
origin?: string
|
||||
) => Promise<void>
|
||||
playFromLibrarySong: (
|
||||
songId: Id<"songs">,
|
||||
sort: {
|
||||
@@ -264,11 +268,13 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
|
||||
const pendingSeekRef = useRef<number | null>(null)
|
||||
const seekInFlightRef = useRef(false)
|
||||
const loadRequestIdRef = useRef(0)
|
||||
const queueOriginRef = useRef<string | undefined>(undefined)
|
||||
const playRequestRef = useRef(0)
|
||||
const ignoreTrackChangeTimerRef = useRef<ReturnType<
|
||||
typeof setTimeout
|
||||
> | null>(null)
|
||||
|
||||
const [queueStartVersion, setQueueStartVersion] = useState(0)
|
||||
const [isPlaying, setIsPlayingState] = useState(false)
|
||||
const [currentTime, setCurrentTime] = useState(0)
|
||||
const [measuredDuration, setDuration] = useState(0)
|
||||
@@ -348,6 +354,21 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
|
||||
loadedLoadKeyRef.current = null
|
||||
}, [])
|
||||
|
||||
// A new queue must not inherit the native player's position or a pending seek.
|
||||
const resetQueuePlayback = useCallback(() => {
|
||||
loadRequestIdRef.current += 1
|
||||
destroyPlaylist()
|
||||
pendingSeekRef.current = null
|
||||
seekInFlightRef.current = false
|
||||
progressRestoredForSongRef.current = null
|
||||
lastSavedProgressRef.current = -1
|
||||
currentTimeRef.current = 0
|
||||
setCurrentTime(0)
|
||||
setDuration(0)
|
||||
setProgressToRestore(null)
|
||||
setQueueStartVersion((version) => version + 1)
|
||||
}, [destroyPlaylist])
|
||||
|
||||
const handlePlaylistStatusUpdate = useCallback(
|
||||
(status: AudioPlaylistStatus) => {
|
||||
if (!status.isLoaded) return
|
||||
@@ -565,11 +586,12 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
|
||||
if (progressRestoredForSongRef.current === currentSongId) return
|
||||
progressRestoredForSongRef.current = currentSongId
|
||||
lastSavedProgressRef.current = -1
|
||||
pendingSeekRef.current = null
|
||||
currentTimeRef.current = 0
|
||||
setCurrentTime(0)
|
||||
setDuration(0)
|
||||
setProgressToRestore(store.getState().progressSeconds)
|
||||
}, [currentSongId, destroyPlaylist, store])
|
||||
}, [currentSongId, destroyPlaylist, queueStartVersion, store])
|
||||
|
||||
useEffect(() => {
|
||||
if (progressToRestore === null || progressToRestore <= 0) return
|
||||
@@ -807,6 +829,7 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
|
||||
handlePlaybackLoadFailure,
|
||||
historySyncKey,
|
||||
queueSyncKey,
|
||||
queueStartVersion,
|
||||
repeatEnabled,
|
||||
scheduleTrackChangeHandling,
|
||||
userPlaybackKey,
|
||||
@@ -827,18 +850,19 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
|
||||
) => playbackQueue.PlaybackQueueState,
|
||||
recorded: {
|
||||
playlistId?: Id<"playlists">
|
||||
origin?: string
|
||||
entity?: { kind: MusicEntityKind; id: string }
|
||||
} = {}
|
||||
) => {
|
||||
playRequestRef.current += 1
|
||||
const previousSongId = store.getState().queue[0]
|
||||
queueOriginRef.current =
|
||||
recorded.origin ??
|
||||
(recorded.playlistId ? `playlist:${recorded.playlistId}` : undefined)
|
||||
resetQueuePlayback()
|
||||
store.update(recipe)
|
||||
const firstSongId = store.getState().queue[0]
|
||||
if (!firstSongId) return
|
||||
|
||||
if (firstSongId === previousSongId) {
|
||||
void seekTo(0)
|
||||
}
|
||||
setIsPlaying(true)
|
||||
void (async () => {
|
||||
const deadline = Date.now() + 20_000
|
||||
@@ -869,7 +893,7 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
|
||||
: { songId: firstSongId }
|
||||
)
|
||||
},
|
||||
[store, seekTo, setIsPlaying, recordPlay]
|
||||
[store, resetQueuePlayback, setIsPlaying, recordPlay]
|
||||
)
|
||||
|
||||
const playCollection = useCallback(
|
||||
@@ -878,6 +902,7 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
|
||||
shuffled: boolean,
|
||||
recorded: {
|
||||
playlistId?: Id<"playlists">
|
||||
origin?: string
|
||||
entity?: { kind: MusicEntityKind; id: string }
|
||||
} = {}
|
||||
) => {
|
||||
@@ -942,12 +967,19 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
|
||||
)
|
||||
|
||||
const playFromSong = useCallback(
|
||||
async (songId: Id<"songs">, songIds: Id<"songs">[]) => {
|
||||
startPlayback((state) =>
|
||||
playbackQueue.startQueueFromSong(state, songId, songIds)
|
||||
async (songId: Id<"songs">, songIds: Id<"songs">[], origin?: string) => {
|
||||
if (
|
||||
origin &&
|
||||
queueOriginRef.current === origin &&
|
||||
store.getState().queue[0] === songId
|
||||
)
|
||||
return
|
||||
startPlayback(
|
||||
(state) => playbackQueue.startQueueFromSong(state, songId, songIds),
|
||||
{ origin }
|
||||
)
|
||||
},
|
||||
[startPlayback]
|
||||
[startPlayback, store]
|
||||
)
|
||||
|
||||
const playFromLibrarySong = useCallback(
|
||||
@@ -1017,6 +1049,7 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
|
||||
(albumId: Id<"albums">, songIds?: Id<"songs">[]) =>
|
||||
playCollection(loadAlbumSongIds(albumId, songIds), false, {
|
||||
entity: { kind: "album", id: albumId },
|
||||
origin: `album:${albumId}`,
|
||||
}),
|
||||
[playCollection, loadAlbumSongIds]
|
||||
)
|
||||
@@ -1025,6 +1058,7 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
|
||||
(albumId: Id<"albums">, songIds?: Id<"songs">[]) =>
|
||||
playCollection(loadAlbumSongIds(albumId, songIds), true, {
|
||||
entity: { kind: "album", id: albumId },
|
||||
origin: `album:${albumId}`,
|
||||
}),
|
||||
[playCollection, loadAlbumSongIds]
|
||||
)
|
||||
@@ -1087,12 +1121,14 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
|
||||
async (songIds: Id<"songs">[], shuffled: boolean) => {
|
||||
if (songIds.length === 0) return
|
||||
playRequestRef.current += 1
|
||||
queueOriginRef.current = undefined
|
||||
resetQueuePlayback()
|
||||
store.update((state) =>
|
||||
playbackQueue.startQueue(state, songIds, shuffled)
|
||||
)
|
||||
setIsPlaying(false)
|
||||
},
|
||||
[store, setIsPlaying]
|
||||
[store, resetQueuePlayback, setIsPlaying]
|
||||
)
|
||||
|
||||
const startFromSong = useCallback(
|
||||
@@ -1103,17 +1139,20 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
|
||||
play: boolean
|
||||
) => {
|
||||
playRequestRef.current += 1
|
||||
queueOriginRef.current = undefined
|
||||
resetQueuePlayback()
|
||||
store.update((state) => playbackQueue.setShuffleEnabled(state, shuffled))
|
||||
store.update((state) =>
|
||||
playbackQueue.startQueueFromSong(state, songId, songIds)
|
||||
)
|
||||
setIsPlaying(play)
|
||||
},
|
||||
[store, setIsPlaying]
|
||||
[store, resetQueuePlayback, setIsPlaying]
|
||||
)
|
||||
|
||||
const clearQueue = useCallback(async () => {
|
||||
playRequestRef.current += 1
|
||||
queueOriginRef.current = undefined
|
||||
store.update(playbackQueue.clearQueue)
|
||||
setIsPlaying(false)
|
||||
}, [store, setIsPlaying])
|
||||
|
||||
@@ -547,7 +547,8 @@ export function AlbumView({
|
||||
onPlay={() =>
|
||||
void playFromSong(
|
||||
track.id as Id<"songs">,
|
||||
albumSongIds
|
||||
albumSongIds,
|
||||
`album:${album.id}`
|
||||
)
|
||||
}
|
||||
/>
|
||||
|
||||
@@ -63,7 +63,11 @@ type PlaybackContextValue = {
|
||||
playShuffled: (songIds: Id<"songs">[]) => Promise<void>
|
||||
addToQueueNext: (songIds: Id<"songs">[]) => Promise<void>
|
||||
addToQueueLast: (songIds: Id<"songs">[]) => Promise<void>
|
||||
playFromSong: (songId: Id<"songs">, songIds: Id<"songs">[]) => Promise<void>
|
||||
playFromSong: (
|
||||
songId: Id<"songs">,
|
||||
songIds: Id<"songs">[],
|
||||
origin?: string
|
||||
) => Promise<void>
|
||||
playFromLibrarySong: (
|
||||
songId: Id<"songs">,
|
||||
sort: {
|
||||
@@ -1050,6 +1054,7 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
|
||||
}
|
||||
}, [currentSongId, currentSong])
|
||||
|
||||
const queueOriginRef = useRef<string | undefined>(undefined)
|
||||
const playRequestRef = useRef(0)
|
||||
|
||||
const startPlayback = useCallback(
|
||||
@@ -1057,9 +1062,12 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
|
||||
recipe: (
|
||||
state: playbackQueue.PlaybackQueueState
|
||||
) => playbackQueue.PlaybackQueueState,
|
||||
recorded: { playlistId?: Id<"playlists"> } = {}
|
||||
recorded: { playlistId?: Id<"playlists">; origin?: string } = {}
|
||||
) => {
|
||||
playRequestRef.current += 1
|
||||
queueOriginRef.current =
|
||||
recorded.origin ??
|
||||
(recorded.playlistId ? `playlist:${recorded.playlistId}` : undefined)
|
||||
const previousSongId = store.getState().queue[0]
|
||||
store.update(recipe)
|
||||
const firstSongId = store.getState().queue[0]
|
||||
@@ -1084,7 +1092,7 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
|
||||
async (
|
||||
songIds: Id<"songs">[] | Promise<Id<"songs">[]>,
|
||||
shuffled: boolean,
|
||||
recorded: { playlistId?: Id<"playlists"> } = {}
|
||||
recorded: { playlistId?: Id<"playlists">; origin?: string } = {}
|
||||
) => {
|
||||
const request = ++playRequestRef.current
|
||||
let resolvedSongIds: Id<"songs">[]
|
||||
@@ -1149,12 +1157,19 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
|
||||
)
|
||||
|
||||
const playFromSong = useCallback(
|
||||
async (songId: Id<"songs">, songIds: Id<"songs">[]) => {
|
||||
startPlayback((state) =>
|
||||
playbackQueue.startQueueFromSong(state, songId, songIds)
|
||||
async (songId: Id<"songs">, songIds: Id<"songs">[], origin?: string) => {
|
||||
if (
|
||||
origin &&
|
||||
queueOriginRef.current === origin &&
|
||||
store.getState().queue[0] === songId
|
||||
)
|
||||
return
|
||||
startPlayback(
|
||||
(state) => playbackQueue.startQueueFromSong(state, songId, songIds),
|
||||
{ origin }
|
||||
)
|
||||
},
|
||||
[startPlayback]
|
||||
[startPlayback, store]
|
||||
)
|
||||
|
||||
const playFromLibrarySong = useCallback(
|
||||
@@ -1222,13 +1237,17 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
|
||||
|
||||
const playAlbum = useCallback(
|
||||
(albumId: Id<"albums">, songIds?: Id<"songs">[]) =>
|
||||
playCollection(loadAlbumSongIds(albumId, songIds), false),
|
||||
playCollection(loadAlbumSongIds(albumId, songIds), false, {
|
||||
origin: `album:${albumId}`,
|
||||
}),
|
||||
[playCollection, loadAlbumSongIds]
|
||||
)
|
||||
|
||||
const playAlbumShuffled = useCallback(
|
||||
(albumId: Id<"albums">, songIds?: Id<"songs">[]) =>
|
||||
playCollection(loadAlbumSongIds(albumId, songIds), true),
|
||||
playCollection(loadAlbumSongIds(albumId, songIds), true, {
|
||||
origin: `album:${albumId}`,
|
||||
}),
|
||||
[playCollection, loadAlbumSongIds]
|
||||
)
|
||||
|
||||
|
||||
@@ -697,7 +697,11 @@ export function PlaylistView({ playlist }: { playlist: BrowsePlaylistDetail }) {
|
||||
}}
|
||||
isNowPlaying={currentSong?.id === song.id}
|
||||
onPlay={() =>
|
||||
void playFromSong(song.id as Id<"songs">, playlistSongIds)
|
||||
void playFromSong(
|
||||
song.id as Id<"songs">,
|
||||
playlistSongIds,
|
||||
`playlist:${playlist.id}`
|
||||
)
|
||||
}
|
||||
/>
|
||||
))
|
||||
|
||||
@@ -64,7 +64,11 @@ export function startQueueFromSong(
|
||||
songIds: Id<"songs">[]
|
||||
): PlaybackQueueState {
|
||||
const index = songIds.indexOf(songId)
|
||||
const rest = index === -1 ? [] : songIds.slice(index + 1)
|
||||
const rest = state.shuffleEnabled
|
||||
? uniqueSongIds(songIds).filter((id) => id !== songId)
|
||||
: index === -1
|
||||
? []
|
||||
: songIds.slice(index + 1)
|
||||
return {
|
||||
...state,
|
||||
queue: [songId, ...(state.shuffleEnabled ? shuffleArray(rest) : rest)],
|
||||
|
||||
@@ -433,7 +433,11 @@ export const playFromSong = mutation({
|
||||
}
|
||||
|
||||
const index = songIds.indexOf(songId)
|
||||
const rest = index === -1 ? [] : songIds.slice(index + 1)
|
||||
const rest = user.shuffleEnabled
|
||||
? uniqueSongIds(songIds).filter((id) => id !== songId)
|
||||
: index === -1
|
||||
? []
|
||||
: songIds.slice(index + 1)
|
||||
const queueRest = user.shuffleEnabled ? shuffleArray(rest) : rest
|
||||
|
||||
await setQueueState(ctx, userId, {
|
||||
@@ -588,7 +592,11 @@ export const playFromLibrarySong = mutation({
|
||||
|
||||
const songIds = await getSortedLibrarySongIds(ctx, sortKey, sortDirection)
|
||||
const index = songIds.indexOf(songId)
|
||||
const rest = index === -1 ? [] : songIds.slice(index + 1)
|
||||
const rest = user.shuffleEnabled
|
||||
? uniqueSongIds(songIds).filter((id) => id !== songId)
|
||||
: index === -1
|
||||
? []
|
||||
: songIds.slice(index + 1)
|
||||
const queueRest = user.shuffleEnabled ? shuffleArray(rest) : rest
|
||||
|
||||
await setQueueState(ctx, userId, {
|
||||
|
||||
Reference in new issue
Block a user