Clean up progress syncing in PlayerHandler

This commit is contained in:
ronaldheft 2022-08-18 15:56:25 -04:00
parent a82474cf45
commit 2912d442b5

View file

@ -162,7 +162,7 @@ class PlayerHandler {
guard player.isInitialized() else { return nil } guard player.isInitialized() else { return nil }
DispatchQueue.main.async { DispatchQueue.main.async {
syncProgress() syncPlayerProgress()
} }
return [ return [
@ -196,11 +196,11 @@ class PlayerHandler {
} }
if listeningTimePassedSinceLastSync >= 5 { if listeningTimePassedSinceLastSync >= 5 {
syncProgress() syncPlayerProgress()
} }
} }
public static func syncProgress() { public static func syncPlayerProgress() {
guard let player = player else { return } guard let player = player else { return }
guard player.isInitialized() else { return } guard player.isInitialized() else { return }
guard let session = getPlaybackSession() else { return } guard let session = getPlaybackSession() else { return }
@ -228,20 +228,20 @@ class PlayerHandler {
listeningTimePassedSinceLastSync = 0 listeningTimePassedSinceLastSync = 0
// Persist items in the database and sync to the server // Persist items in the database and sync to the server
if session.isLocal { if session.isLocal { syncLocalProgress() }
_ = syncLocalProgress() syncServerProgress()
}
syncPlaybackSessionsToServer()
} }
private static func syncLocalProgress() -> LocalMediaProgress? { private static func syncLocalProgress() {
guard let session = getPlaybackSession() else { return nil } DispatchQueue.global(qos: .utility).async {
guard let session = getPlaybackSession() else { return }
guard session.isLocal else { return }
let localMediaProgress = LocalMediaProgress.fetchOrCreateLocalMediaProgress(localMediaProgressId: session.localMediaProgressId, localLibraryItemId: session.localLibraryItem?.id, localEpisodeId: session.episodeId) let localMediaProgress = LocalMediaProgress.fetchOrCreateLocalMediaProgress(localMediaProgressId: session.localMediaProgressId, localLibraryItemId: session.localLibraryItem?.id, localEpisodeId: session.episodeId)
guard let localMediaProgress = localMediaProgress else { guard let localMediaProgress = localMediaProgress else {
// Local media progress should have been created // Local media progress should have been created
// If we're here, it means a library id is invalid // If we're here, it means a library id is invalid
return nil return
} }
localMediaProgress.updateFromPlaybackSession(session) localMediaProgress.updateFromPlaybackSession(session)
@ -251,11 +251,10 @@ class PlayerHandler {
// Send the local progress back to front-end // Send the local progress back to front-end
NotificationCenter.default.post(name: NSNotification.Name(PlayerEvents.localProgress.rawValue), object: nil) NotificationCenter.default.post(name: NSNotification.Name(PlayerEvents.localProgress.rawValue), object: nil)
}
return localMediaProgress
} }
private static func syncPlaybackSessionsToServer() { public static func syncServerProgress() {
guard Connectivity.isConnectedToInternet else { return } guard Connectivity.isConnectedToInternet else { return }
DispatchQueue.global(qos: .utility).async { DispatchQueue.global(qos: .utility).async {
let realm = try! Realm() let realm = try! Realm()
@ -263,7 +262,7 @@ class PlayerHandler {
NSLog("Sending sessionId(\(session.id)) to server") NSLog("Sending sessionId(\(session.id)) to server")
let sessionRef = ThreadSafeReference(to: session) let sessionRef = ThreadSafeReference(to: session)
func handleSuccess(_ success: Bool) { func handleSyncSuccess(_ success: Bool) {
// Remove old sessions after they synced with the server // Remove old sessions after they synced with the server
let session = try! Realm().resolve(sessionRef) let session = try! Realm().resolve(sessionRef)
if success && !(session?.isActiveSession ?? false) { if success && !(session?.isActiveSession ?? false) {
@ -274,12 +273,12 @@ class PlayerHandler {
if session.isLocal { if session.isLocal {
ApiClient.reportLocalPlaybackProgress(session.freeze()) { success in ApiClient.reportLocalPlaybackProgress(session.freeze()) { success in
handleSuccess(success) handleSyncSuccess(success)
} }
} else { } else {
let playbackReport = PlaybackReport(currentTime: session.currentTime, duration: session.duration, timeListened: session.timeListening) let playbackReport = PlaybackReport(currentTime: session.currentTime, duration: session.duration, timeListened: session.timeListening)
ApiClient.reportPlaybackProgress(report: playbackReport, sessionId: session.id) { success in ApiClient.reportPlaybackProgress(report: playbackReport, sessionId: session.id) { success in
handleSuccess(success) handleSyncSuccess(success)
} }
} }
} }