From dee8676df54ed5ab4b6708b5266a142a9e755f29 Mon Sep 17 00:00:00 2001 From: Violet Caulfield <42452695+anultravioletaurora@users.noreply.github.com> Date: Sun, 2 Nov 2025 09:42:04 -0600 Subject: [PATCH] hot fix for playback reporting not working --- src/providers/Player/index.tsx | 48 ++++++++++++++-------------------- 1 file changed, 19 insertions(+), 29 deletions(-) diff --git a/src/providers/Player/index.tsx b/src/providers/Player/index.tsx index 33c43a50..26fc7906 100644 --- a/src/providers/Player/index.tsx +++ b/src/providers/Player/index.tsx @@ -1,12 +1,9 @@ import { usePerformanceMonitor } from '../../hooks/use-performance-monitor' import TrackPlayer, { Event, State, useTrackPlayerEvents } from 'react-native-track-player' -import { createContext, useCallback, useEffect } from 'react' +import { createContext, useCallback, useEffect, useState } from 'react' import { handleActiveTrackChanged } from './functions' import JellifyTrack from '../../types/JellifyTrack' -import { useIsRestoring } from '@tanstack/react-query' import { useAutoDownload } from '../../stores/settings/usage' -import { queryClient } from '../../constants/query-client' -import { NOW_PLAYING_QUERY_KEY } from './constants/query-keys' import reportPlaybackStopped from '../../api/mutations/playback/functions/playback-stopped' import reportPlaybackCompleted from '../../api/mutations/playback/functions/playback-completed' import isPlaybackFinished from '../../api/mutations/playback/utils' @@ -16,7 +13,6 @@ import reportPlaybackStarted from '../../api/mutations/playback/functions/playba import calculateTrackVolume from './utils/normalization' import saveAudioItem from '../../api/mutations/download/utils' import { useDownloadingDeviceProfile } from '../../stores/device-profile' -import { NOW_PLAYING_QUERY } from './constants/queries' import Initialize from './functions/initialization' import { useEnableAudioNormalization } from '../../stores/settings/player' @@ -33,6 +29,8 @@ export const PlayerContext = createContext({}) export const PlayerProvider: () => React.JSX.Element = () => { const { api } = useJellifyContext() + const [initialized, setInitialized] = useState(false) + const [autoDownload] = useAutoDownload() const [enableAudioNormalization] = useEnableAudioNormalization() @@ -41,8 +39,6 @@ export const PlayerProvider: () => React.JSX.Element = () => { usePerformanceMonitor('PlayerProvider', 3) - const isRestoring = useIsRestoring() - const eventHandler = useCallback( // eslint-disable-next-line @typescript-eslint/no-explicit-any async (event: any) => { @@ -56,48 +52,39 @@ export const PlayerProvider: () => React.JSX.Element = () => { // if the index from the event differs from what we have stored if (event.track && enableAudioNormalization) { console.debug('Normalizing audio track') - nowPlaying = event.track as JellifyTrack - const volume = calculateTrackVolume(nowPlaying) + const volume = calculateTrackVolume(event.track) await TrackPlayer.setVolume(volume) } else if (event.track) { - reportPlaybackStarted(api, event.track) + await reportPlaybackStarted(api, event.track) } await handleActiveTrackChanged() if (event.lastTrack) { if (isPlaybackFinished(event.lastPosition, event.lastTrack.duration ?? 1)) - reportPlaybackCompleted(api, event.lastTrack as JellifyTrack) - else reportPlaybackStopped(api, event.lastTrack as JellifyTrack) + await reportPlaybackCompleted(api, event.lastTrack as JellifyTrack) + else await reportPlaybackStopped(api, event.lastTrack as JellifyTrack) } break case Event.PlaybackProgressUpdated: console.debug(`Completion percentage: ${event.position / event.duration}`) - nowPlaying = queryClient.getQueryData(NOW_PLAYING_QUERY_KEY) + if (event.track) await reportPlaybackProgress(api, event.track, event.position) - if (nowPlaying) { - reportPlaybackProgress(api, nowPlaying, event.position) - } - - if (event.position / event.duration > 0.3 && autoDownload && nowPlaying) - saveAudioItem(api, nowPlaying.item, downloadingDeviceProfile, true) + if (event.position / event.duration > 0.3 && autoDownload && event.track) + await saveAudioItem(api, event.track.item, downloadingDeviceProfile, true) break case Event.PlaybackState: - nowPlaying = queryClient.getQueryData(NOW_PLAYING_QUERY_KEY) - switch (event.state) { case State.Playing: - if (nowPlaying) reportPlaybackStarted(api, nowPlaying) - queryClient.ensureQueryData(NOW_PLAYING_QUERY) + if (event.track) await reportPlaybackStarted(api, event.track) + break + default: + if (event.track) await reportPlaybackStopped(api, event.track) break - case State.Paused: - case State.Stopped: - case State.Ended: - if (nowPlaying) reportPlaybackStopped(api, nowPlaying) } break } @@ -108,8 +95,11 @@ export const PlayerProvider: () => React.JSX.Element = () => { useTrackPlayerEvents(PLAYER_EVENTS, eventHandler) useEffect(() => { - if (!isRestoring) Initialize() - }, [isRestoring]) + if (!initialized) { + setInitialized(true) + Initialize() + } + }, []) return (