mirror of
https://github.com/advplyr/audiobookshelf-app.git
synced 2025-08-05 02:25:45 +02:00
Handle download complete lifecycle
This commit is contained in:
parent
5495bcb945
commit
b549528e23
5 changed files with 62 additions and 9 deletions
|
@ -9,7 +9,7 @@ import Foundation
|
|||
import Unrealm
|
||||
|
||||
struct DownloadItem: Realmable, Codable {
|
||||
var id: String = UUID().uuidString
|
||||
var id: String?
|
||||
var libraryItemId: String?
|
||||
var episodeId: String?
|
||||
var userMediaProgress: MediaProgress?
|
||||
|
@ -36,6 +36,7 @@ struct DownloadItem: Realmable, Codable {
|
|||
|
||||
extension DownloadItem {
|
||||
init(libraryItem: LibraryItem, server: ServerConnectionConfig) {
|
||||
self.id = libraryItem.id
|
||||
self.libraryItemId = libraryItem.id
|
||||
//self.episodeId // TODO
|
||||
self.userMediaProgress = libraryItem.userMediaProgress
|
||||
|
@ -52,7 +53,7 @@ extension DownloadItem {
|
|||
}
|
||||
|
||||
func didDownloadSuccessfully() -> Bool {
|
||||
self.downloadItemParts.allSatisfy({ $0.failed = false })
|
||||
self.downloadItemParts.allSatisfy({ $0.failed == false })
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -101,6 +102,10 @@ extension DownloadItemPart {
|
|||
self.destinationUri = destination.path
|
||||
}
|
||||
|
||||
func mimeType() -> String? {
|
||||
audioTrack?.mimeType ?? episode?.audioTrack?.mimeType
|
||||
}
|
||||
|
||||
func downloadURL() -> URL? {
|
||||
if let uri = self.uri {
|
||||
return URL(string: uri)
|
||||
|
|
|
@ -63,7 +63,7 @@ struct LocalFile: Realmable, Codable {
|
|||
}
|
||||
|
||||
struct LocalMediaProgress: Realmable, Codable {
|
||||
var id: String = UUID().uuidString
|
||||
var id: String = ""
|
||||
var localLibraryItemId: String = ""
|
||||
var localEpisodeId: String?
|
||||
var duration: Double = 0
|
||||
|
|
|
@ -75,6 +75,7 @@ extension LocalFile {
|
|||
self.init()
|
||||
self.id = "\(libraryItemId)_\(filename.toBase64())"
|
||||
self.filename = filename
|
||||
self.mimeType = mimeType
|
||||
self.contentUrl = localUrl.absoluteString
|
||||
self.absolutePath = localUrl.path
|
||||
self.size = Int(localUrl.fileSize)
|
||||
|
@ -90,3 +91,27 @@ extension LocalFile {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension LocalMediaProgress {
|
||||
init(localLibraryItem: LocalLibraryItem, episode: LocalPodcastEpisode?, progress: MediaProgress) {
|
||||
self.id = localLibraryItem.id
|
||||
self.localLibraryItemId = localLibraryItem.id
|
||||
self.libraryItemId = localLibraryItem.libraryItemId
|
||||
|
||||
if let episode = episode {
|
||||
self.id += "-\(episode.id)"
|
||||
self.episodeId = episode.id
|
||||
}
|
||||
|
||||
self.serverAddress = localLibraryItem.serverAddress
|
||||
self.serverUserId = localLibraryItem.serverUserId
|
||||
self.serverConnectionConfigId = localLibraryItem.serverConnectionConfigId
|
||||
|
||||
self.duration = progress.duration
|
||||
self.currentTime = progress.currentTime
|
||||
self.isFinished = false
|
||||
self.lastUpdate = progress.lastUpdate
|
||||
self.startedAt = progress.startedAt
|
||||
self.finishedAt = progress.finishedAt
|
||||
}
|
||||
}
|
||||
|
|
|
@ -177,6 +177,12 @@ class Database {
|
|||
}
|
||||
}
|
||||
|
||||
public func removeDownloadItem(_ downloadItem: DownloadItem) {
|
||||
Database.realmQueue.sync {
|
||||
try! instance.write { instance.delete(downloadItem) }
|
||||
}
|
||||
}
|
||||
|
||||
public func getDeviceSettings() -> DeviceSettings {
|
||||
return Database.realmQueue.sync {
|
||||
return instance.objects(DeviceSettings.self).first ?? getDefaultDeviceSettings()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue