From ebddf37f478ffd6d2760da211fcc5ec1e0c47b9d Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Tue, 5 Mar 2024 21:18:46 -0800 Subject: [PATCH] =?UTF-8?q?Create=20ArticleIconSchemeHandlerDelegate,=20so?= =?UTF-8?q?=20ArticleIconSchemeHandler=20doesn=E2=80=99t=20have=20to=20kno?= =?UTF-8?q?w=20about=20SceneCoordinator.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- iOS/Article/ArticleIconSchemeHandler.swift | 22 ++++++++++++---------- iOS/Article/WebViewController.swift | 12 +++++++++++- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/iOS/Article/ArticleIconSchemeHandler.swift b/iOS/Article/ArticleIconSchemeHandler.swift index 62fe0ee62..c1bc6df7c 100644 --- a/iOS/Article/ArticleIconSchemeHandler.swift +++ b/iOS/Article/ArticleIconSchemeHandler.swift @@ -8,19 +8,23 @@ import Foundation import WebKit -import Articles -class ArticleIconSchemeHandler: NSObject, WKURLSchemeHandler { +protocol ArticleIconSchemeHandlerDelegate: AnyObject { - weak var coordinator: SceneCoordinator? - - init(coordinator: SceneCoordinator) { - self.coordinator = coordinator + func iconImage(for articleID: String) -> IconImage? +} + +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)) } - } diff --git a/iOS/Article/WebViewController.swift b/iOS/Article/WebViewController.swift index f03775035..d19bbbe3c 100644 --- a/iOS/Article/WebViewController.swift +++ b/iOS/Article/WebViewController.swift @@ -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 {