mirror of
https://github.com/advplyr/audiobookshelf-app.git
synced 2026-08-06 20:08:48 +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
|
||||
let activeSession = try await Realm().objects(PlaybackSession.self).where({ $0.isActiveSession == true }).last
|
||||
if let activeSession = activeSession {
|
||||
await PlayerProgress.syncFromServer()
|
||||
await PlayerProgress.shared.syncFromServer()
|
||||
try self.startPlaybackSession(activeSession, playWhenReady: false, playbackRate: PlayerSettings.main().playbackRate)
|
||||
}
|
||||
} catch {
|
||||
|
||||
@@ -240,11 +240,11 @@ class PlayerHandler {
|
||||
listeningTimePassedSinceLastSync = 0
|
||||
|
||||
// Persist items in the database and sync to the server
|
||||
if session.isLocal { PlayerProgress.syncFromPlayer() }
|
||||
Task { await PlayerProgress.syncToServer() }
|
||||
if session.isLocal { PlayerProgress.shared.syncFromPlayer() }
|
||||
Task { await PlayerProgress.shared.syncToServer() }
|
||||
}
|
||||
|
||||
@objc public static func syncServerProgressDuringPause() {
|
||||
Task { await PlayerProgress.syncFromServer() }
|
||||
Task { await PlayerProgress.shared.syncFromServer() }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,25 +11,27 @@ import RealmSwift
|
||||
|
||||
class PlayerProgress {
|
||||
|
||||
public static let shared = PlayerProgress()
|
||||
|
||||
private init() {}
|
||||
|
||||
public static func syncFromPlayer() {
|
||||
public func syncFromPlayer() {
|
||||
updateLocalMediaProgressFromLocalSession()
|
||||
}
|
||||
|
||||
public static func syncToServer() async {
|
||||
public func syncToServer() async {
|
||||
let backgroundToken = await UIApplication.shared.beginBackgroundTask(withName: "ABS:syncToServer")
|
||||
updateAllServerSessionFromLocalSession()
|
||||
await UIApplication.shared.endBackgroundTask(backgroundToken)
|
||||
}
|
||||
|
||||
public static func syncFromServer() async {
|
||||
public func syncFromServer() async {
|
||||
let backgroundToken = await UIApplication.shared.beginBackgroundTask(withName: "ABS:syncFromServer")
|
||||
await updateLocalSessionFromServerMediaProgress()
|
||||
await UIApplication.shared.endBackgroundTask(backgroundToken)
|
||||
}
|
||||
|
||||
private static func updateLocalMediaProgressFromLocalSession() {
|
||||
private func updateLocalMediaProgressFromLocalSession() {
|
||||
guard let session = PlayerHandler.getPlaybackSession() else { return }
|
||||
guard session.isLocal else { return }
|
||||
|
||||
@@ -49,7 +51,7 @@ class PlayerProgress {
|
||||
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 })
|
||||
for session in sessions {
|
||||
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")
|
||||
|
||||
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")
|
||||
guard let session = PlayerHandler.getPlaybackSession()?.freeze() else { return }
|
||||
|
||||
|
||||
Reference in New Issue
Block a user