Starting logic for handling completed downloads

This commit is contained in:
ronaldheft 2022-08-06 21:32:04 -04:00
parent 7fded5e105
commit 5495bcb945
2 changed files with 29 additions and 1 deletions

View file

@ -39,6 +39,7 @@ public class AbsDownloader: CAPPlugin, URLSessionDownloadDelegate {
public func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
handleDownloadTaskUpdate(downloadTask: task) { downloadItem, downloadItemPart in
if let error = error {
downloadItemPart.completed = true
downloadItemPart.failed = true
throw error
}
@ -78,13 +79,32 @@ public class AbsDownloader: CAPPlugin, URLSessionDownloadDelegate {
Database.shared.updateDownloadItemPart(downloadItemPart)
// Notify the UI
try! notifyListeners("onItemDownloadUpdate", data: downloadItem.asDictionary())
try? notifyListeners("onItemDownloadUpdate", data: downloadItem.asDictionary())
// Handle a completed download
if ( downloadItem.isDoneDownloading() ) {
handleDownloadTaskCompleteFromDownloadItem(downloadItem)
}
} catch {
NSLog("DownloadItemError")
debugPrint(error)
}
}
private func handleDownloadTaskCompleteFromDownloadItem(_ downloadItem: DownloadItem) {
var statusNotification = [String: String]()
if ( downloadItem.didDownloadSuccessfully() ) {
ApiClient.getLibraryItemWithProgress(libraryItemId: downloadItem.libraryItemId!, episodeId: downloadItem.episodeId) { libraryItem in
//let localDirectory = documentsDirectory.appendingPathComponent("\(libraryItem.id)")
//let localLibraryItem = LocalLibraryItem(libraryItem, localUrl: localDirectory, server: Store.serverConfig!, files: <#T##[LocalFile]#>)
}
}
notifyListeners("onItemDownloadComplete", data: statusNotification)
}
@objc func downloadLibraryItem(_ call: CAPPluginCall) {
let libraryItemId = call.getString("libraryItemId")
let episodeId = call.getString("episodeId")

View file

@ -46,6 +46,14 @@ extension DownloadItem {
self.itemTitle = libraryItem.media.metadata.title
self.media = libraryItem.media
}
func isDoneDownloading() -> Bool {
self.downloadItemParts.allSatisfy({ $0.completed })
}
func didDownloadSuccessfully() -> Bool {
self.downloadItemParts.allSatisfy({ $0.failed = false })
}
}
struct DownloadItemPart: Realmable, Codable {