mirror of
https://github.com/advplyr/audiobookshelf-app.git
synced 2025-08-28 05:53:59 +02:00
Handle server success
This commit is contained in:
parent
10f2da9e90
commit
ec4647e2bc
2 changed files with 15 additions and 3 deletions
|
@ -261,7 +261,8 @@ class PlayerHandler {
|
||||||
for session in realm.objects(PlaybackSession.self).where({ $0.serverConnectionConfigId == Store.serverConfig?.id }) {
|
for session in realm.objects(PlaybackSession.self).where({ $0.serverConnectionConfigId == Store.serverConfig?.id }) {
|
||||||
NSLog("Sending sessionId(\(session.id)) to server")
|
NSLog("Sending sessionId(\(session.id)) to server")
|
||||||
let sessionRef = ThreadSafeReference(to: session)
|
let sessionRef = ThreadSafeReference(to: session)
|
||||||
ApiClient.reportLocalPlaybackProgress(session.freeze()) { success in
|
|
||||||
|
func handleSuccess(_ 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) {
|
||||||
|
@ -269,6 +270,17 @@ class PlayerHandler {
|
||||||
session?.delete()
|
session?.delete()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if session.isLocal {
|
||||||
|
ApiClient.reportLocalPlaybackProgress(session.freeze()) { success in
|
||||||
|
handleSuccess(success)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
let playbackReport = PlaybackReport(currentTime: session.currentTime, duration: session.duration, timeListened: session.timeListening)
|
||||||
|
ApiClient.reportPlaybackProgress(report: playbackReport, sessionId: session.id) { success in
|
||||||
|
handleSuccess(success)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -162,8 +162,8 @@ class ApiClient {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static func reportPlaybackProgress(report: PlaybackReport, sessionId: String) {
|
public static func reportPlaybackProgress(report: PlaybackReport, sessionId: String, callback: @escaping (_ success: Bool) -> Void) {
|
||||||
try? postResource(endpoint: "api/session/\(sessionId)/sync", parameters: report.asDictionary().mapValues({ value in "\(value)" }), callback: nil)
|
try? postResource(endpoint: "api/session/\(sessionId)/sync", parameters: report.asDictionary().mapValues({ value in "\(value)" }), callback: callback)
|
||||||
}
|
}
|
||||||
|
|
||||||
public static func reportLocalPlaybackProgress(_ session: PlaybackSession, callback: @escaping (_ success: Bool) -> Void) {
|
public static func reportLocalPlaybackProgress(_ session: PlaybackSession, callback: @escaping (_ success: Bool) -> Void) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue