Skip to content

Commit d2cd66d

Browse files
committed
Change to use Foundation.Progress
1 parent 3325909 commit d2cd66d

File tree

8 files changed

+23
-22
lines changed

8 files changed

+23
-22
lines changed

Sources/APIKit/Concurrency/Concurrency.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public extension Session {
2323
return try await withTaskCancellationHandler(operation: {
2424
return try await withCheckedThrowingContinuation { continuation in
2525
Task {
26-
let sessionTask = createSessionTask(request, callbackQueue: callbackQueue, progressHandler: { _, _, _ in }) { result in
26+
let sessionTask = createSessionTask(request, callbackQueue: callbackQueue, progressHandler: { _ in }) { result in
2727
continuation.resume(with: result)
2828
}
2929
await cancellationHandler.register(with: sessionTask)

Sources/APIKit/Session.swift

+5-5
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ open class Session {
3636
/// - parameter handler: The closure that receives result of the request.
3737
/// - returns: The new session task.
3838
@discardableResult
39-
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? {
39+
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? {
4040
return shared.send(request, callbackQueue: callbackQueue, progressHandler: progressHandler, completionHandler: completionHandler)
4141
}
4242

@@ -54,7 +54,7 @@ open class Session {
5454
/// - parameter handler: The closure that receives result of the request.
5555
/// - returns: The new session task.
5656
@discardableResult
57-
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? {
57+
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? {
5858
let task = createSessionTask(request, callbackQueue: callbackQueue, progressHandler: progressHandler, completionHandler: completionHandler)
5959
task?.resume()
6060
return task
@@ -77,7 +77,7 @@ open class Session {
7777
}
7878
}
7979

80-
internal func createSessionTask<Request: APIKit.Request>(_ request: Request, callbackQueue: CallbackQueue?, progressHandler: @escaping (Int64, Int64, Int64) -> Void, completionHandler: @escaping (Result<Request.Response, SessionTaskError>) -> Void) -> SessionTask? {
80+
internal func createSessionTask<Request: APIKit.Request>(_ request: Request, callbackQueue: CallbackQueue?, progressHandler: @escaping (Progress) -> Void, completionHandler: @escaping (Result<Request.Response, SessionTaskError>) -> Void) -> SessionTask? {
8181
let callbackQueue = callbackQueue ?? self.callbackQueue
8282
let urlRequest: URLRequest
8383
do {
@@ -90,8 +90,8 @@ open class Session {
9090
}
9191

9292
let task = adapter.createTask(with: urlRequest,
93-
progressHandler: { bytesSent, totalBytesSent, totalBytesExpectedToSend in
94-
progressHandler(bytesSent, totalBytesSent, totalBytesExpectedToSend)
93+
progressHandler: { progress in
94+
progressHandler(progress)
9595
},
9696
completionHandler: { data, urlResponse, error in
9797
let result: Result<Request.Response, SessionTaskError>

Sources/APIKit/SessionAdapter/SessionAdapter.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public protocol SessionTask: AnyObject {
1111
/// with `Session`.
1212
public protocol SessionAdapter {
1313
/// Returns instance that conforms to `SessionTask`. `handler` must be called after success or failure.
14-
func createTask(with URLRequest: URLRequest, progressHandler: @escaping (Int64, Int64, Int64) -> Void, completionHandler: @escaping (Data?, URLResponse?, Error?) -> Void) -> SessionTask
14+
func createTask(with URLRequest: URLRequest, progressHandler: @escaping (Progress) -> Void, completionHandler: @escaping (Data?, URLResponse?, Error?) -> Void) -> SessionTask
1515

1616
/// Collects tasks from backend networking stack. `handler` must be called after collecting.
1717
func getTasks(with handler: @escaping ([SessionTask]) -> Void)

Sources/APIKit/SessionAdapter/URLSessionAdapter.swift

+8-5
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ open class URLSessionAdapter: NSObject, SessionAdapter, URLSessionDelegate, URLS
2626
}
2727

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

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

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

67-
private func progressHandler(for task: URLSessionTask) -> ((Int64, Int64, Int64) -> Void)? {
68-
return objc_getAssociatedObject(task, &taskAssociatedObjectProgressHandlerKey) as? (Int64, Int64, Int64) -> Void
67+
private func progressHandler(for task: URLSessionTask) -> ((Progress) -> Void)? {
68+
return objc_getAssociatedObject(task, &taskAssociatedObjectProgressHandlerKey) as? (Progress) -> Void
6969
}
70+
7071
// MARK: URLSessionTaskDelegate
7172
open func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
7273
handler(for: task)?(buffer(for: task) as Data?, task.response, error)
@@ -79,6 +80,8 @@ open class URLSessionAdapter: NSObject, SessionAdapter, URLSessionDelegate, URLS
7980

8081
// MARK: URLSessionDataDelegate
8182
open func urlSession(_ session: URLSession, task: URLSessionTask, didSendBodyData bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64) {
82-
progressHandler(for: task)?(bytesSent, totalBytesSent, totalBytesExpectedToSend)
83+
let progress = Progress(totalUnitCount: totalBytesExpectedToSend)
84+
progress.completedUnitCount = totalBytesSent
85+
progressHandler(for: task)?(progress)
8386
}
8487
}

Tests/APIKitTests/SessionAdapterType/URLSessionAdapterSubclassTests.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class URLSessionAdapterSubclassTests: XCTestCase {
6464
let adapter = SessionAdapter(configuration: configuration)
6565
let session = Session(adapter: adapter)
6666

67-
session.send(request, progressHandler: { _, _, _ in
67+
session.send(request, progressHandler: { _ in
6868
expectation.fulfill()
6969
})
7070

Tests/APIKitTests/SessionTests.swift

+3-5
Original file line numberDiff line numberDiff line change
@@ -225,10 +225,8 @@ class SessionTests: XCTestCase {
225225
let expectation = self.expectation(description: "wait for response")
226226
let request = TestRequest(method: .post)
227227

228-
session.send(request, progressHandler: { bytesSent, totalBytesSent, totalBytesExpectedToSend in
229-
XCTAssertNotNil(bytesSent)
230-
XCTAssertNotNil(totalBytesSent)
231-
XCTAssertNotNil(totalBytesExpectedToSend)
228+
session.send(request, progressHandler: { progress in
229+
XCTAssertNotNil(progress)
232230
expectation.fulfill()
233231
})
234232

@@ -250,7 +248,7 @@ class SessionTests: XCTestCase {
250248
return testSesssion
251249
}
252250

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

255253
functionCallFlags[(#function)] = true
256254
return super.send(request)

Tests/APIKitTests/TestComponents/TestSessionAdapter.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,15 @@ class TestSessionAdapter: SessionAdapter {
4242
if task.cancelled {
4343
task.completionHandler(nil, nil, Error.cancelled)
4444
} else {
45-
task.progressHandler(1, 1, 1)
45+
task.progressHandler(Progress(totalUnitCount: 1))
4646
task.completionHandler(data, urlResponse, error)
4747
}
4848
}
4949

5050
tasks = []
5151
}
5252

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

Tests/APIKitTests/TestComponents/TestSessionTask.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import APIKit
44
class TestSessionTask: SessionTask {
55

66
var completionHandler: (Data?, URLResponse?, Error?) -> Void
7-
var progressHandler: (Int64, Int64, Int64) -> Void
7+
var progressHandler: (Progress) -> Void
88
var cancelled = false
99

10-
init(progressHandler: @escaping (Int64, Int64, Int64) -> Void, completionHandler: @escaping (Data?, URLResponse?, Error?) -> Void) {
10+
init(progressHandler: @escaping (Progress) -> Void, completionHandler: @escaping (Data?, URLResponse?, Error?) -> Void) {
1111
self.completionHandler = completionHandler
1212
self.progressHandler = progressHandler
1313
}

0 commit comments

Comments
 (0)