mirror of
https://github.com/advplyr/audiobookshelf-app.git
synced 2026-08-26 05:14:09 +02:00
Functions to update local progress
This commit is contained in:
@@ -151,6 +151,8 @@ struct LocalMediaProgress: Realmable, Codable {
|
|||||||
var libraryItemId: String?
|
var libraryItemId: String?
|
||||||
var episodeId: String?
|
var episodeId: String?
|
||||||
|
|
||||||
|
var progressPercent: Int { Int(self.progress * 100) }
|
||||||
|
|
||||||
static func primaryKey() -> String? {
|
static func primaryKey() -> String? {
|
||||||
return "id"
|
return "id"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -152,4 +152,32 @@ extension LocalMediaProgress {
|
|||||||
self.startedAt = progress.startedAt
|
self.startedAt = progress.startedAt
|
||||||
self.finishedAt = progress.finishedAt
|
self.finishedAt = progress.finishedAt
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mutating func updateIsFinished(_ finished: Bool) {
|
||||||
|
if self.isFinished != finished {
|
||||||
|
self.progress = finished ? 1.0 : 0.0
|
||||||
|
}
|
||||||
|
|
||||||
|
self.isFinished = finished
|
||||||
|
self.lastUpdate = Int(Date().timeIntervalSince1970)
|
||||||
|
self.finishedAt = finished ? lastUpdate : nil
|
||||||
|
}
|
||||||
|
|
||||||
|
mutating func updateFromPlaybackSession(_ playbackSession: PlaybackSession) {
|
||||||
|
self.currentTime = playbackSession.currentTime
|
||||||
|
self.progress = playbackSession.progress
|
||||||
|
self.lastUpdate = Int(Date().timeIntervalSince1970)
|
||||||
|
self.isFinished = playbackSession.progress >= 100.0
|
||||||
|
self.finishedAt = self.isFinished ? self.lastUpdate : nil
|
||||||
|
}
|
||||||
|
|
||||||
|
mutating func updateFromServerMediaProgress(_ serverMediaProgress: MediaProgress) {
|
||||||
|
self.isFinished = serverMediaProgress.isFinished
|
||||||
|
self.progress = serverMediaProgress.progress
|
||||||
|
self.currentTime = serverMediaProgress.currentTime
|
||||||
|
self.duration = serverMediaProgress.duration
|
||||||
|
self.lastUpdate = serverMediaProgress.lastUpdate
|
||||||
|
self.finishedAt = serverMediaProgress.finishedAt
|
||||||
|
self.startedAt = serverMediaProgress.startedAt
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,4 +29,11 @@ struct PlaybackSession: Decodable, Encodable {
|
|||||||
var localLibraryItem: LocalLibraryItem?
|
var localLibraryItem: LocalLibraryItem?
|
||||||
var serverConnectionConfigId: String?
|
var serverConnectionConfigId: String?
|
||||||
var serverAddress: String?
|
var serverAddress: String?
|
||||||
|
|
||||||
|
var totalDuration: Double {
|
||||||
|
var total = 0.0
|
||||||
|
self.audioTracks.forEach { total += $0.duration }
|
||||||
|
return total
|
||||||
|
}
|
||||||
|
var progress: Double { self.currentTime / self.totalDuration }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -65,6 +65,7 @@ class NowPlayingInfo {
|
|||||||
|
|
||||||
MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo
|
MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo
|
||||||
}
|
}
|
||||||
|
|
||||||
public func reset() {
|
public func reset() {
|
||||||
nowPlayingInfo = [:]
|
nowPlayingInfo = [:]
|
||||||
MPNowPlayingInfoCenter.default().nowPlayingInfo = nil
|
MPNowPlayingInfoCenter.default().nowPlayingInfo = nil
|
||||||
@@ -89,6 +90,7 @@ class NowPlayingInfo {
|
|||||||
nowPlayingInfo[MPMediaItemPropertyArtist] = metadata!.author ?? "unknown"
|
nowPlayingInfo[MPMediaItemPropertyArtist] = metadata!.author ?? "unknown"
|
||||||
nowPlayingInfo[MPMediaItemPropertyAlbumTitle] = metadata!.series
|
nowPlayingInfo[MPMediaItemPropertyAlbumTitle] = metadata!.series
|
||||||
}
|
}
|
||||||
|
|
||||||
private func shouldFetchCover(id: String) -> Bool {
|
private func shouldFetchCover(id: String) -> Bool {
|
||||||
nowPlayingInfo[MPNowPlayingInfoPropertyExternalContentIdentifier] as? String != id || nowPlayingInfo[MPMediaItemPropertyArtwork] == nil
|
nowPlayingInfo[MPNowPlayingInfoPropertyExternalContentIdentifier] as? String != id || nowPlayingInfo[MPMediaItemPropertyArtwork] == nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user