-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feat] #484 - downloadHandlers를 관리하는 manager 구현
- Loading branch information
Showing
1 changed file
with
51 additions
and
0 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
.../Projects/Features/BaseFeatureDependency/Sources/WebView/Download/WKDownloadManager.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// | ||
// WKDownloadManager.swift | ||
// BaseFeatureDependency | ||
// | ||
// Created by 장석우 on 1/24/25. | ||
// Copyright © 2025 SOPT-iOS. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import UniformTypeIdentifiers | ||
import WebKit | ||
|
||
public final class WKDownloadManager { | ||
private var downloadHandlers: [UTType: WKDownloadExecutable] | ||
var webVC: SOPTWebViewControllable? | ||
|
||
init( | ||
downloadHandlers: [UTType : WKDownloadExecutable] = [:], | ||
webVC: SOPTWebViewControllable? = nil | ||
) { | ||
self.downloadHandlers = downloadHandlers | ||
self.webVC = webVC | ||
} | ||
|
||
public func register<T: WKDownloadExecutable>(_ object: T) { | ||
self.downloadHandlers[T.key] = object | ||
} | ||
|
||
func download( | ||
_ download: WKDownload, | ||
decideDestinationUsing response: URLResponse, | ||
suggestedFilename: String | ||
) async -> URL? { | ||
guard let mimeType = response.mimeType, | ||
let utType = UTType(mimeType: mimeType) | ||
else { return nil } | ||
|
||
guard let handler = downloadHandlers.first(where: { utType.conforms(to: $0.key) })?.value | ||
else { return nil } | ||
|
||
return await handler.execute(download, response, suggestedFilename, webVC) | ||
} | ||
|
||
public static let `default`: WKDownloadManager = { | ||
let manager = WKDownloadManager() | ||
manager.register(WKImageDownloadHandler()) | ||
return manager | ||
}() | ||
} | ||
|
||
|