fix(mobile): track progress syncing

This commit is contained in:
joshuafhiggins authored and Joshua Higgins committed 2026-08-02 04:14:01 +00:00
1 parent 578e72b338
commit fd9d9ed201
1 file changed
+25 -10
+25 -10
View File
@@ -108,6 +108,8 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
const isPlayingRef = useRef(false)
const progressRestoredForSongRef = useRef<Id<"songs"> | null>(null)
const lastSavedProgressRef = useRef(-1)
const prevIsPlayingRef = useRef(false)
const currentTimeRef = useRef(0)
const pendingSeekRef = useRef<number | null>(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">[]) => {