mirror of
https://github.com/advplyr/audiobookshelf-app.git
synced 2025-08-19 17:18:46 +02:00
Starting logic for handling completed downloads
This commit is contained in:
parent
7fded5e105
commit
5495bcb945
2 changed files with 29 additions and 1 deletions
|
@ -39,6 +39,7 @@ public class AbsDownloader: CAPPlugin, URLSessionDownloadDelegate {
|
||||||
public func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
|
public func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
|
||||||
handleDownloadTaskUpdate(downloadTask: task) { downloadItem, downloadItemPart in
|
handleDownloadTaskUpdate(downloadTask: task) { downloadItem, downloadItemPart in
|
||||||
if let error = error {
|
if let error = error {
|
||||||
|
downloadItemPart.completed = true
|
||||||
downloadItemPart.failed = true
|
downloadItemPart.failed = true
|
||||||
throw error
|
throw error
|
||||||
}
|
}
|
||||||
|
@ -78,13 +79,32 @@ public class AbsDownloader: CAPPlugin, URLSessionDownloadDelegate {
|
||||||
Database.shared.updateDownloadItemPart(downloadItemPart)
|
Database.shared.updateDownloadItemPart(downloadItemPart)
|
||||||
|
|
||||||
// Notify the UI
|
// 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 {
|
} catch {
|
||||||
NSLog("DownloadItemError")
|
NSLog("DownloadItemError")
|
||||||
debugPrint(error)
|
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) {
|
@objc func downloadLibraryItem(_ call: CAPPluginCall) {
|
||||||
let libraryItemId = call.getString("libraryItemId")
|
let libraryItemId = call.getString("libraryItemId")
|
||||||
let episodeId = call.getString("episodeId")
|
let episodeId = call.getString("episodeId")
|
||||||
|
|
|
@ -46,6 +46,14 @@ extension DownloadItem {
|
||||||
self.itemTitle = libraryItem.media.metadata.title
|
self.itemTitle = libraryItem.media.metadata.title
|
||||||
self.media = libraryItem.media
|
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 {
|
struct DownloadItemPart: Realmable, Codable {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue