mirror of
https://github.com/anultravioletaurora/Jellify.git
synced 2025-12-16 18:55:44 -06:00
hot fix for playback reporting not working
This commit is contained in:
@@ -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<PlayerContext>({})
|
||||
export const PlayerProvider: () => React.JSX.Element = () => {
|
||||
const { api } = useJellifyContext()
|
||||
|
||||
const [initialized, setInitialized] = useState<boolean>(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<JellifyTrack>(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<JellifyTrack>(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 (
|
||||
<PlayerContext.Provider value={{}}>
|
||||
|
||||
Reference in New Issue
Block a user