Create ArticleIconSchemeHandlerDelegate, so ArticleIconSchemeHandler doesn’t have to know about SceneCoordinator.

This commit is contained in:
Brent Simmons 2024-03-05 21:18:46 -08:00
parent 28ff23ee7b
commit ebddf37f47
2 changed files with 23 additions and 11 deletions

View File

@ -8,19 +8,23 @@
import Foundation
import WebKit
import Articles
class ArticleIconSchemeHandler: NSObject, WKURLSchemeHandler {
protocol ArticleIconSchemeHandlerDelegate: AnyObject {
weak var coordinator: SceneCoordinator?
func iconImage(for articleID: String) -> IconImage?
}
init(coordinator: SceneCoordinator) {
self.coordinator = coordinator
final class ArticleIconSchemeHandler: NSObject, WKURLSchemeHandler {
weak var delegate: ArticleIconSchemeHandlerDelegate?
init(delegate: ArticleIconSchemeHandlerDelegate) {
self.delegate = delegate
}
func webView(_ webView: WKWebView, start urlSchemeTask: WKURLSchemeTask) {
guard let url = urlSchemeTask.request.url, let coordinator = coordinator else {
guard let url = urlSchemeTask.request.url, let delegate else {
urlSchemeTask.didFailWithError(URLError(.fileDoesNotExist))
return
}
@ -29,7 +33,7 @@ class ArticleIconSchemeHandler: NSObject, WKURLSchemeHandler {
return
}
let articleID = components.path
guard let iconImage = coordinator.articleFor(articleID)?.iconImage() else {
guard let iconImage = delegate.iconImage(for: articleID) else {
urlSchemeTask.didFailWithError(URLError(.fileDoesNotExist))
return
}
@ -49,12 +53,10 @@ class ArticleIconSchemeHandler: NSObject, WKURLSchemeHandler {
urlSchemeTask.didReceive(data)
urlSchemeTask.didFinish()
}
}
func webView(_ webView: WKWebView, stop urlSchemeTask: WKURLSchemeTask) {
urlSchemeTask.didFailWithError(URLError(.unknown))
}
}

View File

@ -39,7 +39,7 @@ class WebViewController: UIViewController {
private var isFullScreenAvailable: Bool {
return AppDefaults.shared.articleFullscreenAvailable && traitCollection.userInterfaceIdiom == .phone && coordinator.isRootSplitCollapsed
}
private lazy var articleIconSchemeHandler = ArticleIconSchemeHandler(coordinator: coordinator);
private lazy var articleIconSchemeHandler = ArticleIconSchemeHandler(delegate: self);
private lazy var transition = ImageTransition(controller: self)
private var clickedImageCompletion: (() -> Void)?
@ -326,6 +326,16 @@ extension WebViewController: ArticleExtractorDelegate {
}
// MARK: ArticleIconSchemeHandlerDelegate
extension WebViewController: ArticleIconSchemeHandlerDelegate {
func iconImage(for articleID: String) -> IconImage? {
coordinator.articleFor(articleID)?.iconImage()
}
}
// MARK: UIContextMenuInteractionDelegate
extension WebViewController: UIContextMenuInteractionDelegate {