@@ -51,7 +51,7 @@ final class Downloader: NSObject, Sendable {
5151 private let sessionConfig : URLSessionConfiguration
5252 let session : SessionActor = . init( )
5353 private let task : TaskActor = . init( )
54-
54+
5555 /// Actor to manage background download task completion
5656 private let backgroundDownloadState : BackgroundDownloadState = . init( )
5757
@@ -225,7 +225,7 @@ final class Downloader: NSObject, Sendable {
225225 }
226226 )
227227 }
228-
228+
229229 /// Performs download using URLSession downloadTask (works in background)
230230 ///
231231 /// - Parameters:
@@ -238,23 +238,23 @@ final class Downloader: NSObject, Sendable {
238238 guard let session = await session. get ( ) else {
239239 throw DownloadError . unexpectedError
240240 }
241-
241+
242242 // Reset background download state
243243 await backgroundDownloadState. reset ( )
244-
244+
245245 // Create and start download task
246246 let downloadTask = session. downloadTask ( with: request)
247247 downloadTask. resume ( )
248-
248+
249249 // Wait for download to complete via delegate callbacks
250250 let result = await backgroundDownloadState. waitForCompletion ( )
251-
251+
252252 switch result {
253253 case . success( let tempURL) :
254254 // Move file from temp location to destination
255255 try Task . checkCancellation ( )
256256 try FileManager . default. moveDownloadedFile ( from: tempURL, to: destination)
257-
257+
258258 case . failure( let error) :
259259 // Retry logic
260260 if numRetries > 0 {
@@ -411,9 +411,9 @@ final class Downloader: NSObject, Sendable {
411411
412412extension Downloader: URLSessionDownloadDelegate {
413413 func urlSession( _: URLSession , downloadTask: URLSessionDownloadTask , didWriteData _: Int64 , totalBytesWritten: Int64 , totalBytesExpectedToWrite: Int64 ) {
414- let progress = totalBytesExpectedToWrite > 0
415- ? Double ( totalBytesWritten) / Double( totalBytesExpectedToWrite)
416- : 0
414+ let progress = totalBytesExpectedToWrite > 0
415+ ? Double ( totalBytesWritten) / Double( totalBytesExpectedToWrite)
416+ : 0
417417 Task {
418418 await self . broadcaster. broadcast ( state: . downloading( progress, nil ) )
419419 }
@@ -423,7 +423,7 @@ extension Downloader: URLSessionDownloadDelegate {
423423 // Copy file to a safe location before the system deletes it
424424 let tempDir = FileManager . default. temporaryDirectory
425425 let safeTempURL = tempDir. appendingPathComponent ( UUID ( ) . uuidString + " _ " + location. lastPathComponent)
426-
426+
427427 do {
428428 try FileManager . default. copyItem ( at: location, to: safeTempURL)
429429 Task {
@@ -553,34 +553,35 @@ actor TaskActor {
553553private actor BackgroundDownloadState {
554554 private var continuation: CheckedContinuation < Result < URL , Error > , Never > ?
555555 private var result : Result < URL , Error > ?
556-
556+
557557 /// Resets the state for a new download
558558 func reset( ) {
559559 continuation = nil
560560 result = nil
561561 }
562-
562+
563563 /// Waits for the download to complete and returns the result
564564 func waitForCompletion( ) async -> Result < URL , Error > {
565565 // If result is already available, return it immediately
566566 if let result {
567567 return result
568568 }
569-
569+
570570 // Otherwise, wait for completion signal
571571 return await withCheckedContinuation { cont in
572572 self . continuation = cont
573573 }
574574 }
575-
575+
576576 /// Signals that the download has completed
577577 func complete( with result: Result < URL , Error > ) {
578578 self . result = result
579-
579+
580580 // If someone is waiting, resume them
581581 if let continuation {
582582 continuation. resume ( returning: result)
583583 self . continuation = nil
584584 }
585585 }
586586}
587+
0 commit comments