mirror of
https://github.com/advplyr/audiobookshelf-app.git
synced 2025-07-15 16:34:43 +02:00
Clean up progress syncing in PlayerHandler
This commit is contained in:
parent
a82474cf45
commit
2912d442b5
1 changed files with 28 additions and 29 deletions
|
@ -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,34 +228,33 @@ 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)
|
||||||
|
Database.shared.saveLocalMediaProgress(localMediaProgress)
|
||||||
|
|
||||||
|
NSLog("Local progress saved to the database")
|
||||||
|
|
||||||
|
// Send the local progress back to front-end
|
||||||
|
NotificationCenter.default.post(name: NSNotification.Name(PlayerEvents.localProgress.rawValue), object: nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
localMediaProgress.updateFromPlaybackSession(session)
|
|
||||||
Database.shared.saveLocalMediaProgress(localMediaProgress)
|
|
||||||
|
|
||||||
NSLog("Local progress saved to the database")
|
|
||||||
|
|
||||||
// Send the local progress back to front-end
|
|
||||||
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue