diff --git a/apps/mobile/app.config.ts b/apps/mobile/app.config.ts index 5607e20..9391227 100644 --- a/apps/mobile/app.config.ts +++ b/apps/mobile/app.config.ts @@ -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" && diff --git a/apps/mobile/app/(app)/(tabs)/home/album/[id].tsx b/apps/mobile/app/(app)/(tabs)/home/album/[id].tsx index 54632b2..9c8dbf2 100644 --- a/apps/mobile/app/(app)/(tabs)/home/album/[id].tsx +++ b/apps/mobile/app/(app)/(tabs)/home/album/[id].tsx @@ -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 diff --git a/apps/mobile/app/(app)/(tabs)/home/playlist/[id].tsx b/apps/mobile/app/(app)/(tabs)/home/playlist/[id].tsx index 5167c07..38ab0de 100644 --- a/apps/mobile/app/(app)/(tabs)/home/playlist/[id].tsx +++ b/apps/mobile/app/(app)/(tabs)/home/playlist/[id].tsx @@ -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( diff --git a/apps/mobile/contexts/playback.tsx b/apps/mobile/contexts/playback.tsx index c9e430f..758be5d 100644 --- a/apps/mobile/contexts/playback.tsx +++ b/apps/mobile/contexts/playback.tsx @@ -87,7 +87,11 @@ type PlaybackContextValue = { playShuffled: (songIds: Id<"songs">[]) => Promise addToQueueNext: (songIds: Id<"songs">[]) => Promise addToQueueLast: (songIds: Id<"songs">[]) => Promise - playFromSong: (songId: Id<"songs">, songIds: Id<"songs">[]) => Promise + playFromSong: ( + songId: Id<"songs">, + songIds: Id<"songs">[], + origin?: string + ) => Promise playFromLibrarySong: ( songId: Id<"songs">, sort: { @@ -264,11 +268,13 @@ export function PlaybackProvider({ children }: { children: ReactNode }) { const pendingSeekRef = useRef(null) const seekInFlightRef = useRef(false) const loadRequestIdRef = useRef(0) + const queueOriginRef = useRef(undefined) const playRequestRef = useRef(0) const ignoreTrackChangeTimerRef = useRef | 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]) diff --git a/apps/web/components/album-view.tsx b/apps/web/components/album-view.tsx index cb426ea..d819197 100644 --- a/apps/web/components/album-view.tsx +++ b/apps/web/components/album-view.tsx @@ -547,7 +547,8 @@ export function AlbumView({ onPlay={() => void playFromSong( track.id as Id<"songs">, - albumSongIds + albumSongIds, + `album:${album.id}` ) } /> diff --git a/apps/web/components/playback-provider.tsx b/apps/web/components/playback-provider.tsx index e871a63..973604d 100644 --- a/apps/web/components/playback-provider.tsx +++ b/apps/web/components/playback-provider.tsx @@ -63,7 +63,11 @@ type PlaybackContextValue = { playShuffled: (songIds: Id<"songs">[]) => Promise addToQueueNext: (songIds: Id<"songs">[]) => Promise addToQueueLast: (songIds: Id<"songs">[]) => Promise - playFromSong: (songId: Id<"songs">, songIds: Id<"songs">[]) => Promise + playFromSong: ( + songId: Id<"songs">, + songIds: Id<"songs">[], + origin?: string + ) => Promise playFromLibrarySong: ( songId: Id<"songs">, sort: { @@ -1050,6 +1054,7 @@ export function PlaybackProvider({ children }: { children: ReactNode }) { } }, [currentSongId, currentSong]) + const queueOriginRef = useRef(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[]>, 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] ) diff --git a/apps/web/components/playlist-view.tsx b/apps/web/components/playlist-view.tsx index 6dbce0c..b0bfbec 100644 --- a/apps/web/components/playlist-view.tsx +++ b/apps/web/components/playlist-view.tsx @@ -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}` + ) } /> )) diff --git a/packages/convex/client/playback-queue.ts b/packages/convex/client/playback-queue.ts index dbb124e..6063b5b 100644 --- a/packages/convex/client/playback-queue.ts +++ b/packages/convex/client/playback-queue.ts @@ -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)], diff --git a/packages/convex/convex/playback.ts b/packages/convex/convex/playback.ts index 3bf1456..6dbfe23 100644 --- a/packages/convex/convex/playback.ts +++ b/packages/convex/convex/playback.ts @@ -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, {