Report download progress to the UI

This commit is contained in:
ronaldheft
2022-08-06 18:21:11 -04:00
parent d5d65e244b
commit af2c609405
3 changed files with 47 additions and 7 deletions
+11 -4
View File
@@ -28,6 +28,10 @@ struct DownloadItem: Realmable, Codable {
static func indexedProperties() -> [String] {
["libraryItemId"]
}
private enum CodingKeys : String, CodingKey {
case id, libraryItemId, episodeId, userMediaProgress, serverConnectionConfigId, serverAddress, serverUserId, mediaType, itemTitle, downloadItemParts
}
}
extension DownloadItem {
@@ -58,17 +62,20 @@ struct DownloadItemPart: Realmable, Codable {
var uri: String?
var destinationUri: String?
var finalDestinationUri: String?
var downloadId: Int?
var progress: Int = 0
var progress: Double = 0
var task: URLSessionDownloadTask!
private enum CodingKeys : String, CodingKey {
case id, progress
static func primaryKey() -> String? {
return "id"
}
static func ignoredProperties() -> [String] {
["task"]
}
private enum CodingKeys : String, CodingKey {
case id, filename, completed, moved, failed, progress
}
}
extension DownloadItemPart {
+14
View File
@@ -169,6 +169,20 @@ class Database {
}
}
public func updateDownloadItemPartPercent(downloadItemPartId: String, percent: Double) {
Database.realmQueue.sync {
try! instance.write {
let part = instance.object(ofType: DownloadItemPart.self, forPrimaryKey: downloadItemPartId)
guard var part = part else {
NSLog("downloadItemPartId not found (\(downloadItemPartId)")
return
}
part.progress = percent
instance.add(part, update: .modified)
}
}
}
public func getDeviceSettings() -> DeviceSettings {
return Database.realmQueue.sync {
return instance.objects(DeviceSettings.self).first ?? getDefaultDeviceSettings()