From fd9d9ed20110beef88e157790c94d282598384c5 Mon Sep 17 00:00:00 2001 From: Joshua Higgins Date: Tue, 23 Jun 2026 23:20:43 -0400 Subject: [PATCH] fix(mobile): track progress syncing --- apps/mobile/contexts/playback.tsx | 35 ++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/apps/mobile/contexts/playback.tsx b/apps/mobile/contexts/playback.tsx index 69ebd7d..e640701 100644 --- a/apps/mobile/contexts/playback.tsx +++ b/apps/mobile/contexts/playback.tsx @@ -108,6 +108,8 @@ export function PlaybackProvider({ children }: { children: ReactNode }) { const isPlayingRef = useRef(false) const progressRestoredForSongRef = useRef | null>(null) const lastSavedProgressRef = useRef(-1) + const prevIsPlayingRef = useRef(false) + const currentTimeRef = useRef(0) const pendingSeekRef = useRef(null) const [isPlaying, setIsPlayingState] = useState(false) @@ -165,6 +167,7 @@ export function PlaybackProvider({ children }: { children: ReactNode }) { const handlePlaybackStatusUpdate = useCallback((status: AudioStatus) => { if (!status.isLoaded) return + currentTimeRef.current = status.currentTime setCurrentTime(status.currentTime) if (status.duration > 0) { setDuration(status.duration) @@ -174,6 +177,7 @@ export function PlaybackProvider({ children }: { children: ReactNode }) { const seekSeconds = pendingSeekRef.current pendingSeekRef.current = null void playerRef.current?.seekTo(seekSeconds) + currentTimeRef.current = seekSeconds setCurrentTime(seekSeconds) } @@ -287,6 +291,7 @@ export function PlaybackProvider({ children }: { children: ReactNode }) { if (!player) return const clamped = Math.max(0, Math.min(seconds, duration || 0)) await player.seekTo(clamped) + currentTimeRef.current = clamped setCurrentTime(clamped) }, [duration] @@ -310,6 +315,24 @@ export function PlaybackProvider({ children }: { children: ReactNode }) { setProgressToRestore(playback.currentSongProgress ?? 0) }, [currentSong?.id, playback]) + useEffect(() => { + if (progressToRestore === null || progressToRestore <= 0) return + if (!currentSong || !playerRef.current) return + + const maxDuration = + duration > 0 ? duration : progressToRestore + const clamped = Math.min(progressToRestore, maxDuration) + pendingSeekRef.current = clamped + setProgressToRestore(null) + }, [progressToRestore, currentSong?.id, duration]) + + useEffect(() => { + if (!currentSong) return + if (prevIsPlayingRef.current === isPlaying) return + prevIsPlayingRef.current = isPlaying + saveProgress(currentTimeRef.current) + }, [isPlaying, currentSong?.id, saveProgress]) + useEffect(() => { async function loadSong() { if (!config || !currentSong || user === undefined || !user) { @@ -355,13 +378,6 @@ export function PlaybackProvider({ children }: { children: ReactNode }) { const player = ensurePlayer() loadedSongKeyRef.current = loadKey - if (progressToRestore && progressToRestore > 0) { - pendingSeekRef.current = progressToRestore - setProgressToRestore(null) - } else { - pendingSeekRef.current = null - } - player.replace(source) player.volume = volume player.setActiveForLockScreen( @@ -392,7 +408,6 @@ export function PlaybackProvider({ children }: { children: ReactNode }) { destroyPlayer, ensurePlayer, clearQueueMutation, - progressToRestore, currentSong, user, convex, @@ -401,10 +416,10 @@ export function PlaybackProvider({ children }: { children: ReactNode }) { useEffect(() => { if (!isPlaying || !currentSong) return const interval = setInterval(() => { - saveProgress(currentTime) + saveProgress(currentTimeRef.current) }, 5000) return () => clearInterval(interval) - }, [isPlaying, currentSong?.id, currentTime, saveProgress]) + }, [isPlaying, currentSong?.id, saveProgress]) const playInOrder = useCallback( async (songIds: Id<"songs">[]) => {