Skip to content

Commit

Permalink
Add progress delegate (#30)
Browse files Browse the repository at this point in the history
Co-authored-by: Pouya Yarandi <[email protected]>
  • Loading branch information
pouyayarandi and Pouya Yarandi authored Aug 5, 2023
1 parent 1a1d46d commit 1e94006
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Sources/NetShears/NetShears.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,16 @@ public extension BodyExporterDelegate {
func netShears(exportRequestBodyFor request: NetShearsRequestModel) -> BodyExportType { .default }
}

public protocol TaskProgressDelegate: AnyObject {
func task(_ url: URL, didRecieveProgress progress: Progress)
}

public final class NetShears: NSObject {

public static let shared = NetShears()
public weak var bodyExportDelegate: BodyExporterDelegate?
public weak var taskProgressDelegate: TaskProgressDelegate?

internal var loggerEnable = false
internal var interceptorEnable = false
internal var listenerEnable = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,5 +121,11 @@ extension NetworkInterceptorUrlProtocol: URLSessionDataDelegate {
func urlSessionDidFinishEvents(forBackgroundURLSession session: URLSession) {
client?.urlProtocolDidFinishLoading(self)
}

func urlSession(_ session: URLSession, task: URLSessionTask, didSendBodyData bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64) {
if let url = task.currentRequest?.url {
NetShears.shared.taskProgressDelegate?.task(url, didRecieveProgress: task.progress)
}
}
}

6 changes: 6 additions & 0 deletions Sources/NetShears/URLProtocol/NetworkLoggerUrlProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,12 @@ extension NetworkLoggerUrlProtocol: URLSessionDataDelegate {
func urlSessionDidFinishEvents(forBackgroundURLSession session: URLSession) {
client?.urlProtocolDidFinishLoading(self)
}

func urlSession(_ session: URLSession, task: URLSessionTask, didSendBodyData bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64) {
if let url = task.currentRequest?.url {
NetShears.shared.taskProgressDelegate?.task(url, didRecieveProgress: task.progress)
}
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,12 @@ extension NetwrokListenerUrlProtocol: URLSessionDataDelegate {
func urlSessionDidFinishEvents(forBackgroundURLSession session: URLSession) {
client?.urlProtocolDidFinishLoading(self)
}

func urlSession(_ session: URLSession, task: URLSessionTask, didSendBodyData bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64) {
if let url = task.currentRequest?.url {
NetShears.shared.taskProgressDelegate?.task(url, didRecieveProgress: task.progress)
}
}
}


0 comments on commit 1e94006

Please sign in to comment.