diff --git a/iOS/Article/PreloadedWebView.swift b/iOS/Article/ArticleWebView.swift similarity index 53% rename from iOS/Article/PreloadedWebView.swift rename to iOS/Article/ArticleWebView.swift index b269cd3d6..eaf70b6ce 100644 --- a/iOS/Article/PreloadedWebView.swift +++ b/iOS/Article/ArticleWebView.swift @@ -1,5 +1,5 @@ // -// PreloadedWebView.swift +// ArticleWebView.swift // NetNewsWire-iOS // // Created by Maurice Parker on 2/25/20. @@ -9,10 +9,8 @@ import Foundation import WebKit -final class PreloadedWebView: WKWebView { +final class ArticleWebView: WKWebView { - private var isReady: Bool = false - private var readyCompletion: (() -> Void)? private var fullscreenStateObservation: NSKeyValueObservation? init(articleIconSchemeHandler: ArticleIconSchemeHandler) { @@ -37,43 +35,5 @@ final class PreloadedWebView: WKWebView { required init?(coder: NSCoder) { super.init(coder: coder) - - } - - func preload() { - navigationDelegate = self - loadFileURL(ArticleRenderer.blank.url, allowingReadAccessTo: ArticleRenderer.blank.baseURL) - } - - func ready(completion: @escaping () -> Void) { - if isReady { - completeRequest(completion: completion) - } else { - readyCompletion = completion - } - } -} - -// MARK: WKScriptMessageHandler - -extension PreloadedWebView: WKNavigationDelegate { - - func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) { - isReady = true - if let completion = readyCompletion { - completeRequest(completion: completion) - readyCompletion = nil - } - } -} - -// MARK: Private - -private extension PreloadedWebView { - - func completeRequest(completion: @escaping () -> Void) { - isReady = false - navigationDelegate = nil - completion() } } diff --git a/iOS/Article/WebViewController.swift b/iOS/Article/WebViewController.swift index 4446c35c0..27761c25b 100644 --- a/iOS/Article/WebViewController.swift +++ b/iOS/Article/WebViewController.swift @@ -36,8 +36,8 @@ final class WebViewController: UIViewController { private var topShowBarsViewConstraint: NSLayoutConstraint! private var bottomShowBarsViewConstraint: NSLayoutConstraint! - private var webView: PreloadedWebView? { - return view.subviews[0] as? PreloadedWebView + private var webView: ArticleWebView? { + return view.subviews[0] as? ArticleWebView } private var webViewProcessDidTerminate = false @@ -498,7 +498,7 @@ extension WebViewController: WKNavigationDelegate { func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) { for (index, view) in view.subviews.enumerated() { - if index != 0, let oldWebView = view as? PreloadedWebView { + if index != 0, let oldWebView = view as? ArticleWebView { oldWebView.removeFromSuperview() } } @@ -678,55 +678,46 @@ private extension WebViewController { } webViewProcessDidTerminate = false - - coordinator.webViewProvider.dequeueWebView { webView in - - webView.ready { - - // Add the webview - webView.translatesAutoresizingMaskIntoConstraints = false - self.view.insertSubview(webView, at: 0) - NSLayoutConstraint.activate([ - self.view.leadingAnchor.constraint(equalTo: webView.leadingAnchor), - self.view.trailingAnchor.constraint(equalTo: webView.trailingAnchor), - self.view.topAnchor.constraint(equalTo: webView.topAnchor), - self.view.bottomAnchor.constraint(equalTo: webView.bottomAnchor) - ]) - - // UISplitViewController reports the wrong size to WKWebView which can cause horizontal - // rubberbanding on the iPad. This interferes with our UIPageViewController preventing - // us from easily swiping between WKWebViews. This hack fixes that. - webView.scrollView.contentInset = UIEdgeInsets(top: 0, left: -1, bottom: 0, right: 0) - - webView.scrollView.setZoomScale(1.0, animated: false) - - self.view.setNeedsLayout() - self.view.layoutIfNeeded() - - // Configure the webview - webView.navigationDelegate = self - webView.uiDelegate = self - webView.scrollView.delegate = self - self.configureContextMenuInteraction() - - // Remove possible existing message handlers - webView.configuration.userContentController.removeScriptMessageHandler(forName: MessageName.imageWasClicked) - webView.configuration.userContentController.removeScriptMessageHandler(forName: MessageName.imageWasShown) - webView.configuration.userContentController.removeScriptMessageHandler(forName: MessageName.showFeedInspector) - webView.configuration.userContentController.removeScriptMessageHandler(forName: MessageName.mediaSourceURLs) - - // Add handlers - webView.configuration.userContentController.add(WrapperScriptMessageHandler(self), name: MessageName.imageWasClicked) - webView.configuration.userContentController.add(WrapperScriptMessageHandler(self), name: MessageName.imageWasShown) - webView.configuration.userContentController.add(WrapperScriptMessageHandler(self), name: MessageName.showFeedInspector) - webView.configuration.userContentController.add(WrapperScriptMessageHandler(self), name: MessageName.mediaSourceURLs) - - self.renderPage(webView) - } - } + installWebView(ArticleWebView(articleIconSchemeHandler: articleIconSchemeHandler)) } - func renderPage(_ webView: PreloadedWebView?) { + func installWebView(_ webView: ArticleWebView) { + // Add the webview + webView.translatesAutoresizingMaskIntoConstraints = false + view.insertSubview(webView, at: 0) + NSLayoutConstraint.activate([ + view.leadingAnchor.constraint(equalTo: webView.leadingAnchor), + view.trailingAnchor.constraint(equalTo: webView.trailingAnchor), + view.topAnchor.constraint(equalTo: webView.topAnchor), + view.bottomAnchor.constraint(equalTo: webView.bottomAnchor) + ]) + + // UISplitViewController reports the wrong size to WKWebView which can cause horizontal + // rubberbanding on the iPad. This interferes with our UIPageViewController preventing + // us from easily swiping between WKWebViews. This hack fixes that. + webView.scrollView.contentInset = UIEdgeInsets(top: 0, left: -1, bottom: 0, right: 0) + + webView.scrollView.setZoomScale(1.0, animated: false) + + view.setNeedsLayout() + view.layoutIfNeeded() + + // Configure the webview + webView.navigationDelegate = self + webView.uiDelegate = self + webView.scrollView.delegate = self + configureContextMenuInteraction() + + // Add handlers + webView.configuration.userContentController.add(WrapperScriptMessageHandler(self), name: MessageName.imageWasClicked) + webView.configuration.userContentController.add(WrapperScriptMessageHandler(self), name: MessageName.imageWasShown) + webView.configuration.userContentController.add(WrapperScriptMessageHandler(self), name: MessageName.showFeedInspector) + webView.configuration.userContentController.add(WrapperScriptMessageHandler(self), name: MessageName.mediaSourceURLs) + + renderPage(webView) + } + + func renderPage(_ webView: ArticleWebView?) { guard let webView = webView else { return } // Rendering during an element-fullscreen transition triggers a WebKit diff --git a/iOS/Article/WebViewProvider.swift b/iOS/Article/WebViewProvider.swift deleted file mode 100644 index 396f63afa..000000000 --- a/iOS/Article/WebViewProvider.swift +++ /dev/null @@ -1,105 +0,0 @@ -// -// WebViewProvider.swift -// NetNewsWire-iOS -// -// Created by Maurice Parker on 9/21/19. -// Copyright © 2019 Ranchero Software. All rights reserved. -// - -import Foundation -import UIKit -import RSCore -import WebKit - -/// WKWebView has an awful behavior of a flash to white on first load when in dark mode. -/// Keep a queue of WebViews where we've already done a trivial load so that by the time we need them in the UI, they're past the flash-to-white part of their lifecycle. -@MainActor final class WebViewProvider: NSObject { - private let articleIconSchemeHandler: ArticleIconSchemeHandler - private let operationQueue = MainThreadOperationQueue() - private var queue = NSMutableArray() - - init(coordinator: SceneCoordinator) { - articleIconSchemeHandler = ArticleIconSchemeHandler(coordinator: coordinator) - super.init() - replenishQueueIfNeeded() - NotificationCenter.default.addObserver(self, selector: #selector(handleWillEnterForeground(_:)), name: UIApplication.willEnterForegroundNotification, object: nil) - } - - func replenishQueueIfNeeded() { - operationQueue.add(WebViewProviderReplenishQueueOperation(queue: queue, articleIconSchemeHandler: articleIconSchemeHandler)) - } - - // Pooled web views can lose their content process while the app is in the - // background. A load into a dead web view can fail silently, showing blank — - // so start over with fresh web views. - @objc func handleWillEnterForeground(_ note: Notification) { - flushQueue() - } - - // Refill synchronously: a dequeue operation may already be pending, - // and it must not find the queue empty. - private func flushQueue() { - queue.removeAllObjects() - WebViewProviderReplenishQueueOperation.fill(queue, articleIconSchemeHandler: articleIconSchemeHandler) - } - - func dequeueWebView(completion: @escaping (PreloadedWebView) -> Void) { - operationQueue.add(WebViewProviderDequeueOperation(queue: queue, articleIconSchemeHandler: articleIconSchemeHandler, completion: completion)) - operationQueue.add(WebViewProviderReplenishQueueOperation(queue: queue, articleIconSchemeHandler: articleIconSchemeHandler)) - } -} - -final class WebViewProviderReplenishQueueOperation: MainThreadOperation, @unchecked Sendable { - private static let minimumQueueDepth = 3 - - private var queue: NSMutableArray - private var articleIconSchemeHandler: ArticleIconSchemeHandler - - init(queue: NSMutableArray, articleIconSchemeHandler: ArticleIconSchemeHandler) { - self.queue = queue - self.articleIconSchemeHandler = articleIconSchemeHandler - super.init(name: "WebViewProviderReplenishQueueOperation") - } - - override func run() { - Self.fill(queue, articleIconSchemeHandler: articleIconSchemeHandler) - didComplete() - } - - @MainActor static func fill(_ queue: NSMutableArray, articleIconSchemeHandler: ArticleIconSchemeHandler) { - while queue.count < minimumQueueDepth { - let webView = PreloadedWebView(articleIconSchemeHandler: articleIconSchemeHandler) - webView.preload() - queue.insert(webView, at: 0) - } - } -} - -final class WebViewProviderDequeueOperation: MainThreadOperation, @unchecked Sendable { - private var queue: NSMutableArray - private var articleIconSchemeHandler: ArticleIconSchemeHandler - private var completion: (PreloadedWebView) -> Void - - init(queue: NSMutableArray, articleIconSchemeHandler: ArticleIconSchemeHandler, completion: @escaping (PreloadedWebView) -> Void) { - self.queue = queue - self.articleIconSchemeHandler = articleIconSchemeHandler - self.completion = completion - super.init(name: "WebViewProviderFlushQueueOperation") - } - - override func run() { - if let webView = queue.lastObject as? PreloadedWebView { - self.completion(webView) - self.queue.remove(webView) - didComplete() - return - } - - assertionFailure("Creating PreloadedWebView in \(#function); queue has run dry.") - - let webView = PreloadedWebView(articleIconSchemeHandler: articleIconSchemeHandler) - webView.preload() - self.completion(webView) - didComplete() - } -} diff --git a/iOS/MainFeed/MainFeedCollectionViewController.swift b/iOS/MainFeed/MainFeedCollectionViewController.swift index 2b95f1df4..faf5230d8 100644 --- a/iOS/MainFeed/MainFeedCollectionViewController.swift +++ b/iOS/MainFeed/MainFeedCollectionViewController.swift @@ -38,10 +38,10 @@ final class MainFeedCollectionViewController: UICollectionViewController, Undoab private let keyboardManager = KeyboardManager(type: .sidebar) override var keyCommands: [UIKeyCommand]? { - // If the first responder is the WKWebView (PreloadedWebView) we don’t want to supply any keyboard + // If the first responder is the WKWebView (ArticleWebView) we don’t want to supply any keyboard // commands that the system is looking for by going up the responder chain. They will interfere with // the WKWebViews built in hardware keyboard shortcuts, specifically the up and down arrow keys. - guard let current = UIResponder.currentFirstResponder, !(current is PreloadedWebView) else { + guard let current = UIResponder.currentFirstResponder, !(current is ArticleWebView) else { return nil } diff --git a/iOS/MainTimeline/MainTimelineViewController.swift b/iOS/MainTimeline/MainTimelineViewController.swift index e4684e881..93482dc77 100644 --- a/iOS/MainTimeline/MainTimelineViewController.swift +++ b/iOS/MainTimeline/MainTimelineViewController.swift @@ -139,10 +139,10 @@ final class MainTimelineViewController: UIViewController, UndoableCommandRunner weak var coordinator: SceneCoordinator? var undoableCommands = [UndoableCommand]() override var keyCommands: [UIKeyCommand]? { - // If the first responder is the WKWebView (PreloadedWebView) we don’t want to supply any keyboard + // If the first responder is the WKWebView (ArticleWebView) we don’t want to supply any keyboard // commands that the system is looking for by going up the responder chain. They will interfere with // the WKWebViews built in hardware keyboard shortcuts, specifically the up and down arrow keys. - guard let current = UIResponder.currentFirstResponder, !(current is PreloadedWebView) else { return nil } + guard let current = UIResponder.currentFirstResponder, !(current is ArticleWebView) else { return nil } return keyboardManager.keyCommands } diff --git a/iOS/Resources/blank.html b/iOS/Resources/blank.html deleted file mode 100644 index 6e02cf3a6..000000000 --- a/iOS/Resources/blank.html +++ /dev/null @@ -1,11 +0,0 @@ - - - - - - - diff --git a/iOS/SceneCoordinator.swift b/iOS/SceneCoordinator.swift index 38e8991fd..026359a0c 100644 --- a/iOS/SceneCoordinator.swift +++ b/iOS/SceneCoordinator.swift @@ -59,8 +59,6 @@ struct SidebarItemNode: Hashable, Sendable { return rootSplitViewController.undoManager } - lazy var webViewProvider = WebViewProvider(coordinator: self) - private var activityManager = ActivityManager() private var rootSplitViewController: RootSplitViewController!