mirror of
https://github.com/advplyr/audiobookshelf-app.git
synced 2026-08-29 22:57:16 +02:00
Report download progress to the UI
This commit is contained in:
@@ -34,7 +34,25 @@ public class AbsDownloader: CAPPlugin, URLSessionDownloadDelegate {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
|
public func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
|
||||||
NSLog("Received download status \(downloadTask.taskDescription ?? "Unknown Task"): \(totalBytesWritten)")
|
// Find the download item
|
||||||
|
guard let downloadItemPartId = downloadTask.taskDescription else { return }
|
||||||
|
let downloadItem = Database.shared.getDownloadItem(downloadItemPartId: downloadItemPartId)
|
||||||
|
guard let downloadItem = downloadItem else {
|
||||||
|
NSLog("Download item part (%@) not found!", downloadItemPartId)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Calculate the download percentage
|
||||||
|
let percentDownloaded = (Double(totalBytesWritten) / Double(totalBytesExpectedToWrite)) * 100
|
||||||
|
NSLog("Received download status \(downloadItemPartId): \(percentDownloaded)")
|
||||||
|
Database.shared.updateDownloadItemPartPercent(downloadItemPartId: downloadItemPartId, percent: percentDownloaded)
|
||||||
|
let downloadItemPart = downloadItem.downloadItemParts.filter { part in
|
||||||
|
part.id == downloadItemPartId
|
||||||
|
}.first
|
||||||
|
|
||||||
|
// Notify the UI
|
||||||
|
NSLog("Download progress: \(downloadItemPart?.progress ?? 0)")
|
||||||
|
try! notifyListeners("onItemDownloadUpdate", data: downloadItem.asDictionary())
|
||||||
}
|
}
|
||||||
|
|
||||||
@objc func downloadLibraryItem(_ call: CAPPluginCall) {
|
@objc func downloadLibraryItem(_ call: CAPPluginCall) {
|
||||||
@@ -47,16 +65,17 @@ public class AbsDownloader: CAPPlugin, URLSessionDownloadDelegate {
|
|||||||
ApiClient.getLibraryItemWithProgress(libraryItemId: libraryItemId, episodeId: episodeId) { libraryItem in
|
ApiClient.getLibraryItemWithProgress(libraryItemId: libraryItemId, episodeId: episodeId) { libraryItem in
|
||||||
if (libraryItem == nil) {
|
if (libraryItem == nil) {
|
||||||
NSLog("Library item not found")
|
NSLog("Library item not found")
|
||||||
|
call.resolve(["error": "Library item not found"])
|
||||||
} else {
|
} else {
|
||||||
NSLog("Got library item from server \(libraryItem!.id)")
|
NSLog("Got library item from server \(libraryItem!.id)")
|
||||||
do {
|
do {
|
||||||
try self.startLibraryItemDownload(libraryItem!)
|
try self.startLibraryItemDownload(libraryItem!)
|
||||||
//Database.shared.saveLocalLibraryItem(localLibraryItem: localLibraryItem)
|
call.resolve()
|
||||||
} catch {
|
} catch {
|
||||||
NSLog("Failed to download \(error)")
|
NSLog("Failed to download \(error)")
|
||||||
|
call.resolve(["error": "Failed to download"])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
call.resolve()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -28,6 +28,10 @@ struct DownloadItem: Realmable, Codable {
|
|||||||
static func indexedProperties() -> [String] {
|
static func indexedProperties() -> [String] {
|
||||||
["libraryItemId"]
|
["libraryItemId"]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private enum CodingKeys : String, CodingKey {
|
||||||
|
case id, libraryItemId, episodeId, userMediaProgress, serverConnectionConfigId, serverAddress, serverUserId, mediaType, itemTitle, downloadItemParts
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extension DownloadItem {
|
extension DownloadItem {
|
||||||
@@ -58,17 +62,20 @@ struct DownloadItemPart: Realmable, Codable {
|
|||||||
var uri: String?
|
var uri: String?
|
||||||
var destinationUri: String?
|
var destinationUri: String?
|
||||||
var finalDestinationUri: String?
|
var finalDestinationUri: String?
|
||||||
var downloadId: Int?
|
var progress: Double = 0
|
||||||
var progress: Int = 0
|
|
||||||
var task: URLSessionDownloadTask!
|
var task: URLSessionDownloadTask!
|
||||||
|
|
||||||
private enum CodingKeys : String, CodingKey {
|
static func primaryKey() -> String? {
|
||||||
case id, progress
|
return "id"
|
||||||
}
|
}
|
||||||
|
|
||||||
static func ignoredProperties() -> [String] {
|
static func ignoredProperties() -> [String] {
|
||||||
["task"]
|
["task"]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private enum CodingKeys : String, CodingKey {
|
||||||
|
case id, filename, completed, moved, failed, progress
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extension DownloadItemPart {
|
extension DownloadItemPart {
|
||||||
|
|||||||
@@ -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 {
|
public func getDeviceSettings() -> DeviceSettings {
|
||||||
return Database.realmQueue.sync {
|
return Database.realmQueue.sync {
|
||||||
return instance.objects(DeviceSettings.self).first ?? getDefaultDeviceSettings()
|
return instance.objects(DeviceSettings.self).first ?? getDefaultDeviceSettings()
|
||||||
|
|||||||
Reference in New Issue
Block a user