mirror of
https://github.com/advplyr/audiobookshelf-app.git
synced 2026-08-25 21:04:02 +02:00
Fix more edge cases on player initialization
This commit is contained in:
@@ -297,33 +297,7 @@ class PlayerHandler {
|
||||
}
|
||||
}
|
||||
|
||||
@objc public static func syncServerProgressDuringPause() {
|
||||
guard Connectivity.isConnectedToInternet else { return }
|
||||
DispatchQueue.global(qos: .utility).async {
|
||||
NSLog("checkCurrentSessionProgress: Checking if local media progress was updated on server")
|
||||
guard let session = getPlaybackSession() else { return }
|
||||
let sessionRef = ThreadSafeReference(to: session)
|
||||
|
||||
ApiClient.getMediaProgress(libraryItemId: session.libraryItemId!, episodeId: session.episodeId) { progress in
|
||||
guard let session = try! Realm().resolve(sessionRef) else { return }
|
||||
guard let progress = progress else { return }
|
||||
|
||||
let serverLastUpdate = progress.lastUpdate
|
||||
guard let localLastUpdate = session.updatedAt else { return }
|
||||
let serverCurrentTime = progress.currentTime
|
||||
let localCurrentTime = session.currentTime
|
||||
|
||||
let serverIsNewerThanLocal = serverLastUpdate > localLastUpdate
|
||||
let currentTimeIsDifferent = serverCurrentTime != localCurrentTime
|
||||
|
||||
if serverIsNewerThanLocal && currentTimeIsDifferent {
|
||||
session.update {
|
||||
session.currentTime = serverCurrentTime
|
||||
session.updatedAt = serverLastUpdate
|
||||
}
|
||||
self.seek(amount: session.currentTime)
|
||||
}
|
||||
}
|
||||
}
|
||||
@objc private static func syncServerProgressDuringPause() {
|
||||
Task { await PlayerProgress.syncLocalFromServer() }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
//
|
||||
// PlayerProgressSync.swift
|
||||
// App
|
||||
//
|
||||
// Created by Ron Heft on 8/19/22.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import UIKit
|
||||
import RealmSwift
|
||||
|
||||
class PlayerProgress {
|
||||
|
||||
private init() {}
|
||||
|
||||
public static func syncLocalFromPlayer() async {
|
||||
|
||||
}
|
||||
|
||||
public static func syncServerFromLocal() async {
|
||||
|
||||
}
|
||||
|
||||
public static func syncLocalFromServer() async {
|
||||
let backgroundToken = await UIApplication.shared.beginBackgroundTask(withName: "ABS:updateLocalSessionFromServerMediaProgress")
|
||||
await updateLocalSessionFromServerMediaProgress()
|
||||
await UIApplication.shared.endBackgroundTask(backgroundToken)
|
||||
}
|
||||
|
||||
private static func updateLocalSessionFromActivePlayer() {
|
||||
|
||||
}
|
||||
|
||||
private static func updateLocalMediaProgressFromLocalSession() {
|
||||
|
||||
}
|
||||
|
||||
private static func updateServerSessionFromLocalSession() {
|
||||
|
||||
}
|
||||
|
||||
private static func updateLocalSessionFromServerMediaProgress() async {
|
||||
NSLog("checkCurrentSessionProgress: Checking if local media progress was updated on server")
|
||||
guard let session = PlayerHandler.getPlaybackSession() else { return }
|
||||
|
||||
// Fetch the current progress
|
||||
let progress = await ApiClient.getMediaProgress(libraryItemId: session.libraryItemId!, episodeId: session.episodeId)
|
||||
guard let progress = progress else { return }
|
||||
|
||||
// Determine which session is newer
|
||||
let serverLastUpdate = progress.lastUpdate
|
||||
guard let localLastUpdate = session.updatedAt else { return }
|
||||
let serverCurrentTime = progress.currentTime
|
||||
let localCurrentTime = session.currentTime
|
||||
|
||||
let serverIsNewerThanLocal = serverLastUpdate > localLastUpdate
|
||||
let currentTimeIsDifferent = serverCurrentTime != localCurrentTime
|
||||
|
||||
// Update the session, if needed
|
||||
if serverIsNewerThanLocal && currentTimeIsDifferent {
|
||||
session.update {
|
||||
session.currentTime = serverCurrentTime
|
||||
session.updatedAt = serverLastUpdate
|
||||
}
|
||||
PlayerHandler.seek(amount: session.currentTime)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -106,6 +106,14 @@ class ApiClient {
|
||||
}
|
||||
}
|
||||
|
||||
public static func getResource<T: Decodable>(endpoint: String, decodable: T.Type = T.self) async -> T? {
|
||||
return await withCheckedContinuation { continuation in
|
||||
getResource(endpoint: endpoint, decodable: decodable) { result in
|
||||
continuation.resume(returning: result)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static func getResource<T: Decodable>(endpoint: String, decodable: T.Type = T.self, callback: ((_ param: T?) -> Void)?) {
|
||||
if (Store.serverConfig == nil) {
|
||||
NSLog("Server config not set")
|
||||
@@ -205,12 +213,10 @@ class ApiClient {
|
||||
}
|
||||
}
|
||||
|
||||
public static func getMediaProgress(libraryItemId: String, episodeId: String?, callback: @escaping (_ progress: MediaProgress?) -> Void) {
|
||||
public static func getMediaProgress(libraryItemId: String, episodeId: String?) async -> MediaProgress? {
|
||||
NSLog("getMediaProgress \(libraryItemId) \(episodeId ?? "NIL")")
|
||||
let endpoint = episodeId?.isEmpty ?? true ? "api/me/progress/\(libraryItemId)" : "api/me/progress/\(libraryItemId)/\(episodeId ?? "")"
|
||||
getResource(endpoint: endpoint, decodable: MediaProgress.self) { obj in
|
||||
callback(obj)
|
||||
}
|
||||
return await getResource(endpoint: endpoint, decodable: MediaProgress.self)
|
||||
}
|
||||
|
||||
public static func getLibraryItemWithProgress(libraryItemId:String, episodeId:String?, callback: @escaping (_ param: LibraryItem?) -> Void) {
|
||||
|
||||
Reference in New Issue
Block a user