From 1bffbb7312f37198cc8854c828c544ba16bfe8de Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Sat, 7 Oct 2017 12:36:09 -0700 Subject: [PATCH] =?UTF-8?q?Use=20new=20UserAgent.headers()=20function.=20R?= =?UTF-8?q?eorganize=20=E2=80=94=C2=A0API=20up=20top,=20with=20extensions?= =?UTF-8?q?=20for=20protocol=20conformances.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Frameworks/RSWeb/RSWeb/DownloadSession.swift | 91 +++++++++++--------- 1 file changed, 50 insertions(+), 41 deletions(-) diff --git a/Frameworks/RSWeb/RSWeb/DownloadSession.swift b/Frameworks/RSWeb/RSWeb/DownloadSession.swift index 912c56d85..0a9c052c4 100755 --- a/Frameworks/RSWeb/RSWeb/DownloadSession.swift +++ b/Frameworks/RSWeb/RSWeb/DownloadSession.swift @@ -25,17 +25,17 @@ public protocol DownloadSessionDelegate { } -@objc public final class DownloadSession: NSObject, URLSessionDataDelegate { +@objc public final class DownloadSession: NSObject { public var progress = DownloadProgress(numberOfTasks: 0) - fileprivate var urlSession: URLSession! - fileprivate var tasksInProgress = Set() - fileprivate var tasksPending = Set() - fileprivate var taskIdentifierToInfoDictionary = [Int: DownloadInfo]() - fileprivate let representedObjects = NSMutableSet() - fileprivate let delegate: DownloadSessionDelegate - fileprivate var redirectCache = [String: String]() + private var urlSession: URLSession! + private var tasksInProgress = Set() + private var tasksPending = Set() + private var taskIdentifierToInfoDictionary = [Int: DownloadInfo]() + private let representedObjects = NSMutableSet() + private let delegate: DownloadSessionDelegate + private var redirectCache = [String: String]() public init(delegate: DownloadSessionDelegate) { @@ -52,10 +52,8 @@ public protocol DownloadSessionDelegate { sessionConfiguration.httpCookieStorage = nil sessionConfiguration.urlCache = nil - if let userAgent = UserAgent.fromInfoPlist() { - var headers = [AnyHashable : Any]() - headers[HTTPRequestHeader.userAgent] = userAgent - sessionConfiguration.httpAdditionalHeaders = headers + if let userAgentHeaders = UserAgent.headers() { + sessionConfiguration.httpAdditionalHeaders = userAgentHeaders } urlSession = URLSession(configuration: sessionConfiguration, delegate: self, delegateQueue: OperationQueue.main) @@ -64,9 +62,36 @@ public protocol DownloadSessionDelegate { deinit { urlSession.invalidateAndCancel() } - - // MARK: URLSessionTaskDelegate - + + // MARK: - API + + public func cancel() { + + // TODO + } + + public func downloadObjects(_ objects: NSSet) { + + var numberOfTasksAdded = 0 + + for oneObject in objects { + + if !representedObjects.contains(oneObject) { + representedObjects.add(oneObject) + addDataTask(oneObject as AnyObject) + numberOfTasksAdded += 1 + } + } + + progress.addToNumberOfTasks(numberOfTasksAdded) + updateProgress() + } +} + +// MARK: - URLSessionTaskDelegate + +extension DownloadSession: URLSessionTaskDelegate { + public func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) { tasksInProgress.remove(task) @@ -92,9 +117,12 @@ public protocol DownloadSessionDelegate { completionHandler(request) } - - // MARK: URLSessionDataDelegate - +} + +// MARK: - URLSessionDataDelegate + +extension DownloadSession: URLSessionDataDelegate { + public func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive response: URLResponse, completionHandler: @escaping (URLSession.ResponseDisposition) -> Void) { tasksInProgress.insert(dataTask) @@ -146,31 +174,10 @@ public protocol DownloadSessionDelegate { } } - // MARK: API - - public func cancel() { - - // TODO - } - - public func downloadObjects(_ objects: NSSet) { - - var numberOfTasksAdded = 0 - - for oneObject in objects { - - if !representedObjects.contains(oneObject) { - representedObjects.add(oneObject) - addDataTask(oneObject as AnyObject) - numberOfTasksAdded += 1 - } - } - - progress.addToNumberOfTasks(numberOfTasksAdded) - updateProgress() - } } +// MARK: - Private + private extension DownloadSession { func updateProgress() { @@ -275,6 +282,8 @@ private extension DownloadSession { } } +// MARK: - DownloadInfo + private final class DownloadInfo { let representedObject: AnyObject