Skip to content

Commit

Permalink
Change to use Foundation.Progress
Browse files Browse the repository at this point in the history
  • Loading branch information
Econa77 committed Aug 13, 2022
1 parent d410edd commit 2c5dbee
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 20 deletions.
8 changes: 4 additions & 4 deletions Sources/APIKit/Session.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ open class Session {
/// - parameter handler: The closure that receives result of the request.
/// - returns: The new session task.
@discardableResult
open class func send<Request: APIKit.Request>(_ request: Request, callbackQueue: CallbackQueue? = nil, progressHandler: @escaping (Int64, Int64, Int64) -> Void = { _, _, _ in }, completionHandler: @escaping (Result<Request.Response, SessionTaskError>) -> Void = { _ in }) -> SessionTask? {
open class func send<Request: APIKit.Request>(_ request: Request, callbackQueue: CallbackQueue? = nil, progressHandler: @escaping (Progress) -> Void = { _ in }, completionHandler: @escaping (Result<Request.Response, SessionTaskError>) -> Void = { _ in }) -> SessionTask? {
return shared.send(request, callbackQueue: callbackQueue, progressHandler: progressHandler, completionHandler: completionHandler)
}

Expand All @@ -54,7 +54,7 @@ open class Session {
/// - parameter handler: The closure that receives result of the request.
/// - returns: The new session task.
@discardableResult
open func send<Request: APIKit.Request>(_ request: Request, callbackQueue: CallbackQueue? = nil, progressHandler: @escaping (Int64, Int64, Int64) -> Void = { _, _, _ in }, completionHandler: @escaping (Result<Request.Response, SessionTaskError>) -> Void = { _ in }) -> SessionTask? {
open func send<Request: APIKit.Request>(_ request: Request, callbackQueue: CallbackQueue? = nil, progressHandler: @escaping (Progress) -> Void = { _ in }, completionHandler: @escaping (Result<Request.Response, SessionTaskError>) -> Void = { _ in }) -> SessionTask? {
let callbackQueue = callbackQueue ?? self.callbackQueue

let urlRequest: URLRequest
Expand All @@ -68,8 +68,8 @@ open class Session {
}

let task = adapter.createTask(with: urlRequest,
progressHandler: { bytesSent, totalBytesSent, totalBytesExpectedToSend in
progressHandler(bytesSent, totalBytesSent, totalBytesExpectedToSend)
progressHandler: { progress in
progressHandler(progress)
},
completionHandler: { data, urlResponse, error in
let result: Result<Request.Response, SessionTaskError>
Expand Down
2 changes: 1 addition & 1 deletion Sources/APIKit/SessionAdapter/SessionAdapter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public protocol SessionTask: AnyObject {
/// with `Session`.
public protocol SessionAdapter {
/// Returns instance that conforms to `SessionTask`. `handler` must be called after success or failure.
func createTask(with URLRequest: URLRequest, progressHandler: @escaping (Int64, Int64, Int64) -> Void, completionHandler: @escaping (Data?, URLResponse?, Error?) -> Void) -> SessionTask
func createTask(with URLRequest: URLRequest, progressHandler: @escaping (Progress) -> Void, completionHandler: @escaping (Data?, URLResponse?, Error?) -> Void) -> SessionTask

/// Collects tasks from backend networking stack. `handler` must be called after collecting.
func getTasks(with handler: @escaping ([SessionTask]) -> Void)
Expand Down
13 changes: 8 additions & 5 deletions Sources/APIKit/SessionAdapter/URLSessionAdapter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ open class URLSessionAdapter: NSObject, SessionAdapter, URLSessionDelegate, URLS
}

/// Creates `URLSessionDataTask` instance using `dataTaskWithRequest(_:completionHandler:)`.
open func createTask(with URLRequest: URLRequest, progressHandler: @escaping (Int64, Int64, Int64) -> Void, completionHandler: @escaping (Data?, URLResponse?, Error?) -> Void) -> SessionTask {
open func createTask(with URLRequest: URLRequest, progressHandler: @escaping (Progress) -> Void, completionHandler: @escaping (Data?, URLResponse?, Error?) -> Void) -> SessionTask {
let task = urlSession.dataTask(with: URLRequest)

setBuffer(NSMutableData(), forTask: task)
Expand Down Expand Up @@ -60,13 +60,14 @@ open class URLSessionAdapter: NSObject, SessionAdapter, URLSessionDelegate, URLS
return objc_getAssociatedObject(task, &taskAssociatedObjectCompletionHandlerKey) as? (Data?, URLResponse?, Error?) -> Void
}

private func setProgressHandler(_ progressHandler: @escaping (Int64, Int64, Int64) -> Void, forTask task: URLSessionTask) {
private func setProgressHandler(_ progressHandler: @escaping (Progress) -> Void, forTask task: URLSessionTask) {
objc_setAssociatedObject(task, &taskAssociatedObjectProgressHandlerKey, progressHandler as Any, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}

private func progressHandler(for task: URLSessionTask) -> ((Int64, Int64, Int64) -> Void)? {
return objc_getAssociatedObject(task, &taskAssociatedObjectProgressHandlerKey) as? (Int64, Int64, Int64) -> Void
private func progressHandler(for task: URLSessionTask) -> ((Progress) -> Void)? {
return objc_getAssociatedObject(task, &taskAssociatedObjectProgressHandlerKey) as? (Progress) -> Void
}

// MARK: URLSessionTaskDelegate
open func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
handler(for: task)?(buffer(for: task) as Data?, task.response, error)
Expand All @@ -79,6 +80,8 @@ open class URLSessionAdapter: NSObject, SessionAdapter, URLSessionDelegate, URLS

// MARK: URLSessionDataDelegate
open func urlSession(_ session: URLSession, task: URLSessionTask, didSendBodyData bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64) {
progressHandler(for: task)?(bytesSent, totalBytesSent, totalBytesExpectedToSend)
let progress = Progress(totalUnitCount: totalBytesExpectedToSend)
progress.completedUnitCount = totalBytesSent
progressHandler(for: task)?(progress)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class URLSessionAdapterSubclassTests: XCTestCase {
let adapter = SessionAdapter(configuration: configuration)
let session = Session(adapter: adapter)

session.send(request, progressHandler: { _, _, _ in
session.send(request, progressHandler: { _ in
expectation.fulfill()
})

Expand Down
8 changes: 3 additions & 5 deletions Tests/APIKitTests/SessionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -225,10 +225,8 @@ class SessionTests: XCTestCase {
let expectation = self.expectation(description: "wait for response")
let request = TestRequest(method: .post)

session.send(request, progressHandler: { bytesSent, totalBytesSent, totalBytesExpectedToSend in
XCTAssertNotNil(bytesSent)
XCTAssertNotNil(totalBytesSent)
XCTAssertNotNil(totalBytesExpectedToSend)
session.send(request, progressHandler: { progress in
XCTAssertNotNil(progress)
expectation.fulfill()
})

Expand All @@ -250,7 +248,7 @@ class SessionTests: XCTestCase {
return testSesssion
}

override func send<Request: APIKit.Request>(_ request: Request, callbackQueue: CallbackQueue?, progressHandler: @escaping (Int64, Int64, Int64) -> Void, completionHandler: @escaping (Result<Request.Response, SessionTaskError>) -> Void) -> SessionTask? {
override func send<Request: APIKit.Request>(_ request: Request, callbackQueue: CallbackQueue?, progressHandler: @escaping (Progress) -> Void, completionHandler: @escaping (Result<Request.Response, SessionTaskError>) -> Void) -> SessionTask? {

functionCallFlags[(#function)] = true
return super.send(request)
Expand Down
4 changes: 2 additions & 2 deletions Tests/APIKitTests/TestComponents/TestSessionAdapter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ class TestSessionAdapter: SessionAdapter {
if task.cancelled {
task.completionHandler(nil, nil, Error.cancelled)
} else {
task.progressHandler(1, 1, 1)
task.progressHandler(Progress(totalUnitCount: 1))
task.completionHandler(data, urlResponse, error)
}
}

tasks = []
}

func createTask(with URLRequest: URLRequest, progressHandler: @escaping (Int64, Int64, Int64) -> Void, completionHandler: @escaping (Data?, URLResponse?, Swift.Error?) -> Void) -> SessionTask {
func createTask(with URLRequest: URLRequest, progressHandler: @escaping (Progress) -> Void, completionHandler: @escaping (Data?, URLResponse?, Swift.Error?) -> Void) -> SessionTask {
let task = TestSessionTask(progressHandler: progressHandler, completionHandler: completionHandler)
tasks.append(task)

Expand Down
4 changes: 2 additions & 2 deletions Tests/APIKitTests/TestComponents/TestSessionTask.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import APIKit
class TestSessionTask: SessionTask {

var completionHandler: (Data?, URLResponse?, Error?) -> Void
var progressHandler: (Int64, Int64, Int64) -> Void
var progressHandler: (Progress) -> Void
var cancelled = false

init(progressHandler: @escaping (Int64, Int64, Int64) -> Void, completionHandler: @escaping (Data?, URLResponse?, Error?) -> Void) {
init(progressHandler: @escaping (Progress) -> Void, completionHandler: @escaping (Data?, URLResponse?, Error?) -> Void) {
self.completionHandler = completionHandler
self.progressHandler = progressHandler
}
Expand Down

0 comments on commit 2c5dbee

Please sign in to comment.