diff --git a/ios/App/App/plugins/AbsDownloader.swift b/ios/App/App/plugins/AbsDownloader.swift index dbbd3902..198e913d 100644 --- a/ios/App/App/plugins/AbsDownloader.swift +++ b/ios/App/App/plugins/AbsDownloader.swift @@ -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") diff --git a/ios/App/Shared/models/DownloadItem.swift b/ios/App/Shared/models/DownloadItem.swift index 241671a7..e521c647 100644 --- a/ios/App/Shared/models/DownloadItem.swift +++ b/ios/App/Shared/models/DownloadItem.swift @@ -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 {