From cba10aa02bfe74e53545789da084c9a09e16ac70 Mon Sep 17 00:00:00 2001 From: Joshua Higgins Date: Tue, 23 Jun 2026 23:29:03 -0400 Subject: [PATCH] fix(mobile): track progress reset when skipping --- apps/mobile/contexts/playback.tsx | 48 ++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/apps/mobile/contexts/playback.tsx b/apps/mobile/contexts/playback.tsx index e640701..de29b83 100644 --- a/apps/mobile/contexts/playback.tsx +++ b/apps/mobile/contexts/playback.tsx @@ -111,6 +111,10 @@ export function PlaybackProvider({ children }: { children: ReactNode }) { const prevIsPlayingRef = useRef(false) const currentTimeRef = useRef(0) const pendingSeekRef = useRef(null) + const prevSongIdRef = useRef | null>(null) + const pendingSongChangeRef = useRef(false) + const currentSongIdRef = useRef | null>(null) + const loadedSongIdRef = useRef | null>(null) const [isPlaying, setIsPlayingState] = useState(false) const [currentTime, setCurrentTime] = useState(0) @@ -139,6 +143,10 @@ export function PlaybackProvider({ children }: { children: ReactNode }) { const repeatEnabled = playback?.repeatEnabled ?? "none" const queueLength = playback?.queueSongIds.length ?? 0 + useEffect(() => { + currentSongIdRef.current = currentSong?.id ?? null + }, [currentSong?.id]) + const repeatEnabledRef = useRef(repeatEnabled) const queueLengthRef = useRef(queueLength) @@ -162,10 +170,12 @@ export function PlaybackProvider({ children }: { children: ReactNode }) { playerRef.current?.remove() playerRef.current = null loadedSongKeyRef.current = null + loadedSongIdRef.current = null }, []) const handlePlaybackStatusUpdate = useCallback((status: AudioStatus) => { if (!status.isLoaded) return + if (loadedSongIdRef.current !== currentSongIdRef.current) return currentTimeRef.current = status.currentTime setCurrentTime(status.currentTime) @@ -307,11 +317,42 @@ export function PlaybackProvider({ children }: { children: ReactNode }) { [updateSongProgressMutation] ) + useEffect(() => { + if (!currentSong) { + prevSongIdRef.current = null + loadedSongKeyRef.current = null + loadedSongIdRef.current = null + return + } + + if (prevSongIdRef.current === currentSong.id) return + + const isInitialLoad = prevSongIdRef.current === null + prevSongIdRef.current = currentSong.id + loadedSongKeyRef.current = null + loadedSongIdRef.current = null + pendingSeekRef.current = null + progressRestoredForSongRef.current = null + currentTimeRef.current = 0 + setCurrentTime(0) + setDuration(0) + setProgressToRestore(null) + lastSavedProgressRef.current = -1 + if (!isInitialLoad) { + pendingSongChangeRef.current = true + } + }, [currentSong?.id]) + useEffect(() => { if (!currentSong || !playback) return if (progressRestoredForSongRef.current === currentSong.id) return progressRestoredForSongRef.current = currentSong.id lastSavedProgressRef.current = -1 + if (pendingSongChangeRef.current) { + pendingSongChangeRef.current = false + setProgressToRestore(0) + return + } setProgressToRestore(playback.currentSongProgress ?? 0) }, [currentSong?.id, playback]) @@ -345,7 +386,7 @@ export function PlaybackProvider({ children }: { children: ReactNode }) { const song = await convex.query(api.library.getSong, { id: currentSong.id, }) - if (!song) { + if (!song || currentSongIdRef.current !== currentSong.id) { return } @@ -366,6 +407,10 @@ export function PlaybackProvider({ children }: { children: ReactNode }) { return } + if (currentSongIdRef.current !== currentSong.id) { + return + } + const source = songAudioSource( config.serverUrl, currentSong.id, @@ -377,6 +422,7 @@ export function PlaybackProvider({ children }: { children: ReactNode }) { const player = ensurePlayer() loadedSongKeyRef.current = loadKey + loadedSongIdRef.current = currentSong.id player.replace(source) player.volume = volume