Merge branch 'master' into bug-ios-background-currenttime

This commit is contained in:
ronaldheft
2022-08-22 18:04:02 -04:00
17 changed files with 285 additions and 64 deletions
@@ -155,8 +155,13 @@ extension LocalMediaProgress {
static func fetchOrCreateLocalMediaProgress(localMediaProgressId: String?, localLibraryItemId: String?, localEpisodeId: String?) -> LocalMediaProgress? {
if let localMediaProgressId = localMediaProgressId {
return Database.shared.getLocalMediaProgress(localMediaProgressId: localMediaProgressId)
} else if let localLibraryItemId = localLibraryItemId {
// Check if it existing in the database, if not, we need to create it
if let progress = Database.shared.getLocalMediaProgress(localMediaProgressId: localMediaProgressId) {
return progress
}
}
if let localLibraryItemId = localLibraryItemId {
guard let localLibraryItem = Database.shared.getLocalLibraryItem(localLibraryItemId: localLibraryItemId) else { return nil }
let episode = localLibraryItem.getPodcastEpisode(episodeId: localEpisodeId)
return LocalMediaProgress(localLibraryItem: localLibraryItem, episode: episode)
+4
View File
@@ -220,6 +220,8 @@ class AudioPlayer: NSObject {
// MARK: - Methods
public func play(allowSeekBack: Bool = false) {
guard self.isInitialized() else { return }
if allowSeekBack {
let diffrence = Date.timeIntervalSinceReferenceDate - lastPlayTime
var time: Int?
@@ -262,6 +264,8 @@ class AudioPlayer: NSObject {
}
public func pause() {
guard self.isInitialized() else { return }
self.audioPlayer.pause()
self.status = 0
self.rate = 0.0
+18 -5
View File
@@ -123,17 +123,26 @@ class PlayerProgress {
}
}
private func updateLocalSessionFromServerMediaProgress() async {
NSLog("checkCurrentSessionProgress: Checking if local media progress was updated on server")
guard let session = PlayerHandler.getPlaybackSession()?.freeze() else { return }
private static func updateLocalSessionFromServerMediaProgress() async {
NSLog("updateLocalSessionFromServerMediaProgress: Checking if local media progress was updated on server")
guard let session = try! await Realm().objects(PlaybackSession.self).last(where: { $0.isActiveSession == true })?.freeze() else {
NSLog("updateLocalSessionFromServerMediaProgress: Failed to get session")
return
}
// Fetch the current progress
let progress = await ApiClient.getMediaProgress(libraryItemId: session.libraryItemId!, episodeId: session.episodeId)
guard let progress = progress else { return }
guard let progress = progress else {
NSLog("updateLocalSessionFromServerMediaProgress: No progress object")
return
}
// Determine which session is newer
let serverLastUpdate = progress.lastUpdate
guard let localLastUpdate = session.updatedAt else { return }
guard let localLastUpdate = session.updatedAt else {
NSLog("updateLocalSessionFromServerMediaProgress: No local session updatedAt")
return
}
let serverCurrentTime = progress.currentTime
let localCurrentTime = session.currentTime
@@ -142,12 +151,16 @@ class PlayerProgress {
// Update the session, if needed
if serverIsNewerThanLocal && currentTimeIsDifferent {
NSLog("updateLocalSessionFromServerMediaProgress: Server has newer time than local serverLastUpdate=\(serverLastUpdate) localLastUpdate=\(localLastUpdate)")
guard let session = session.thaw() else { return }
session.update {
session.currentTime = serverCurrentTime
session.updatedAt = serverLastUpdate
}
NSLog("updateLocalSessionFromServerMediaProgress: Updated session currentTime newCurrentTime=\(serverCurrentTime) previousCurrentTime=\(localCurrentTime)")
PlayerHandler.seek(amount: session.currentTime)
} else {
NSLog("updateLocalSessionFromServerMediaProgress: Local session does not need updating; local has latest progress")
}
}