Fix more edge cases on player initialization

This commit is contained in:
ronaldheft
2022-08-19 22:15:06 -04:00
parent a59f3bb957
commit 062a217946
5 changed files with 89 additions and 36 deletions
+2 -28
View File
@@ -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)
}
}
}
+10 -4
View File
@@ -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) {