Created
March 23, 2019 19:23
-
-
Save NunoAlexandre/a85f3200a78b2e95919c730a507d0e1c to your computer and use it in GitHub Desktop.
The refactored version of https://gist.github.com/NunoAlexandre/85a0b73410fdb44e776bc6a5ea58042b
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@discardableResult | |
func attemptDownload(forItem item: Item) -> Promise<URL> { | |
if !canSubmitDownload(forItem: item) { | |
return Promise.reject(DownloadError.duplicatedDownload) | |
} | |
else if !SettingsManager.shared.canDownloadOrStream() { | |
let error = DownloadError.canNotUseInternet | |
self.registerFailedDownload(item, withError: error) | |
return Promise.reject(error) | |
} | |
else { | |
return | |
self | |
.registerStartOfDownload(item) | |
.then { self.download(item: item) } | |
.then { self.unpackDownloaded(item: item, at: $0) } | |
.then { self.moveToDownloadsDirectory(item, from: $0) } | |
.chain { _ in self.registerSuccessfulDownload(item) } | |
.onErrorDo { self.registerFailedDownload(item, withError: $0) } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment