fix(mobile): track progress reset when skipping

This commit is contained in:
joshuafhiggins authored and Joshua Higgins committed 2026-08-02 04:14:01 +00:00
1 parent fd9d9ed201
commit cba10aa02b
1 file changed
+47 -1
+47 -1
View File
@@ -111,6 +111,10 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
const prevIsPlayingRef = useRef(false)
const currentTimeRef = useRef(0)
const pendingSeekRef = useRef<number | null>(null)
const prevSongIdRef = useRef<Id<"songs"> | null>(null)
const pendingSongChangeRef = useRef(false)
const currentSongIdRef = useRef<Id<"songs"> | null>(null)
const loadedSongIdRef = useRef<Id<"songs"> | 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