mirror of
https://github.com/advplyr/audiobookshelf-app.git
synced 2026-09-01 08:07:20 +02:00
Convert PlayerHandler to shared instance
This commit is contained in:
@@ -40,7 +40,7 @@ public class AbsAudioPlayer: CAPPlugin {
|
|||||||
// Fetch the most recent active session
|
// Fetch the most recent active session
|
||||||
let activeSession = try await Realm().objects(PlaybackSession.self).where({ $0.isActiveSession == true }).last
|
let activeSession = try await Realm().objects(PlaybackSession.self).where({ $0.isActiveSession == true }).last
|
||||||
if let activeSession = activeSession {
|
if let activeSession = activeSession {
|
||||||
await PlayerProgress.syncFromServer()
|
await PlayerProgress.shared.syncFromServer()
|
||||||
try self.startPlaybackSession(activeSession, playWhenReady: false, playbackRate: PlayerSettings.main().playbackRate)
|
try self.startPlaybackSession(activeSession, playWhenReady: false, playbackRate: PlayerSettings.main().playbackRate)
|
||||||
}
|
}
|
||||||
} catch {
|
} catch {
|
||||||
|
|||||||
@@ -240,11 +240,11 @@ 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 { PlayerProgress.syncFromPlayer() }
|
if session.isLocal { PlayerProgress.shared.syncFromPlayer() }
|
||||||
Task { await PlayerProgress.syncToServer() }
|
Task { await PlayerProgress.shared.syncToServer() }
|
||||||
}
|
}
|
||||||
|
|
||||||
@objc public static func syncServerProgressDuringPause() {
|
@objc public static func syncServerProgressDuringPause() {
|
||||||
Task { await PlayerProgress.syncFromServer() }
|
Task { await PlayerProgress.shared.syncFromServer() }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,25 +11,27 @@ import RealmSwift
|
|||||||
|
|
||||||
class PlayerProgress {
|
class PlayerProgress {
|
||||||
|
|
||||||
|
public static let shared = PlayerProgress()
|
||||||
|
|
||||||
private init() {}
|
private init() {}
|
||||||
|
|
||||||
public static func syncFromPlayer() {
|
public func syncFromPlayer() {
|
||||||
updateLocalMediaProgressFromLocalSession()
|
updateLocalMediaProgressFromLocalSession()
|
||||||
}
|
}
|
||||||
|
|
||||||
public static func syncToServer() async {
|
public func syncToServer() async {
|
||||||
let backgroundToken = await UIApplication.shared.beginBackgroundTask(withName: "ABS:syncToServer")
|
let backgroundToken = await UIApplication.shared.beginBackgroundTask(withName: "ABS:syncToServer")
|
||||||
updateAllServerSessionFromLocalSession()
|
updateAllServerSessionFromLocalSession()
|
||||||
await UIApplication.shared.endBackgroundTask(backgroundToken)
|
await UIApplication.shared.endBackgroundTask(backgroundToken)
|
||||||
}
|
}
|
||||||
|
|
||||||
public static func syncFromServer() async {
|
public func syncFromServer() async {
|
||||||
let backgroundToken = await UIApplication.shared.beginBackgroundTask(withName: "ABS:syncFromServer")
|
let backgroundToken = await UIApplication.shared.beginBackgroundTask(withName: "ABS:syncFromServer")
|
||||||
await updateLocalSessionFromServerMediaProgress()
|
await updateLocalSessionFromServerMediaProgress()
|
||||||
await UIApplication.shared.endBackgroundTask(backgroundToken)
|
await UIApplication.shared.endBackgroundTask(backgroundToken)
|
||||||
}
|
}
|
||||||
|
|
||||||
private static func updateLocalMediaProgressFromLocalSession() {
|
private func updateLocalMediaProgressFromLocalSession() {
|
||||||
guard let session = PlayerHandler.getPlaybackSession() else { return }
|
guard let session = PlayerHandler.getPlaybackSession() else { return }
|
||||||
guard session.isLocal else { return }
|
guard session.isLocal else { return }
|
||||||
|
|
||||||
@@ -49,7 +51,7 @@ class PlayerProgress {
|
|||||||
NotificationCenter.default.post(name: NSNotification.Name(PlayerEvents.localProgress.rawValue), object: nil)
|
NotificationCenter.default.post(name: NSNotification.Name(PlayerEvents.localProgress.rawValue), object: nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
private static func updateAllServerSessionFromLocalSession() {
|
private func updateAllServerSessionFromLocalSession() {
|
||||||
let sessions = try! Realm().objects(PlaybackSession.self).where({ $0.serverConnectionConfigId == Store.serverConfig?.id })
|
let sessions = try! Realm().objects(PlaybackSession.self).where({ $0.serverConnectionConfigId == Store.serverConfig?.id })
|
||||||
for session in sessions {
|
for session in sessions {
|
||||||
let session = session.freeze()
|
let session = session.freeze()
|
||||||
@@ -57,7 +59,7 @@ class PlayerProgress {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static func updateServerSessionFromLocalSession(_ session: PlaybackSession) async {
|
private func updateServerSessionFromLocalSession(_ session: PlaybackSession) async {
|
||||||
NSLog("Sending sessionId(\(session.id)) to server")
|
NSLog("Sending sessionId(\(session.id)) to server")
|
||||||
|
|
||||||
var success = false
|
var success = false
|
||||||
@@ -75,7 +77,7 @@ class PlayerProgress {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static func updateLocalSessionFromServerMediaProgress() async {
|
private func updateLocalSessionFromServerMediaProgress() async {
|
||||||
NSLog("checkCurrentSessionProgress: Checking if local media progress was updated on server")
|
NSLog("checkCurrentSessionProgress: Checking if local media progress was updated on server")
|
||||||
guard let session = PlayerHandler.getPlaybackSession()?.freeze() else { return }
|
guard let session = PlayerHandler.getPlaybackSession()?.freeze() else { return }
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user