Sync local progress with server progress

This commit is contained in:
ronaldheft 2022-08-12 23:11:09 -04:00
parent 8d38f3358e
commit ef661bba37
6 changed files with 89 additions and 26 deletions

View file

@ -196,4 +196,16 @@ extension LocalMediaProgress {
self.finishedAt = serverMediaProgress.finishedAt
self.startedAt = serverMediaProgress.startedAt
}
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 {
guard let localLibraryItem = Database.shared.getLocalLibraryItem(localLibraryItemId: localLibraryItemId) else { return nil }
let episode = localLibraryItem.getPodcastEpisode(episodeId: localEpisodeId)
return LocalMediaProgress(localLibraryItem: localLibraryItem, episode: episode)
} else {
return nil
}
}
}

View file

@ -7,7 +7,7 @@
import Foundation
struct PlaybackSession: Decodable, Encodable {
struct PlaybackSession: Codable {
var id: String
var userId: String?
var libraryItemId: String?
@ -30,10 +30,21 @@ struct PlaybackSession: Decodable, Encodable {
var serverConnectionConfigId: String?
var serverAddress: String?
var isLocal: Bool { self.localLibraryItem != nil }
var localMediaProgressId: String {
if let episodeId = episodeId {
return "\(localLibraryItem!.id)-\(episodeId)"
} else {
return localLibraryItem!.id
}
}
var totalDuration: Double {
var total = 0.0
self.audioTracks.forEach { total += $0.duration }
return total
}
var progress: Double { self.currentTime / self.totalDuration }
}