Keep queued WKWebViews in a hidden view until they are needed. Issue #1563
This commit is contained in:
parent
fdde879910
commit
f9589faa59
@ -121,8 +121,6 @@ class WebViewController: UIViewController {
|
|||||||
webView.configuration.userContentController.add(WrapperScriptMessageHandler(self), name: MessageName.imageWasClicked)
|
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.imageWasShown)
|
||||||
|
|
||||||
// Even though page.html should be loaded into this webview, we have to do it again
|
|
||||||
// to work around this bug: http://www.openradar.me/22855188
|
|
||||||
self.reloadHTML()
|
self.reloadHTML()
|
||||||
|
|
||||||
self.view.setNeedsLayout()
|
self.view.setNeedsLayout()
|
||||||
|
@ -14,17 +14,20 @@ import WebKit
|
|||||||
class WebViewProvider: NSObject, WKNavigationDelegate {
|
class WebViewProvider: NSObject, WKNavigationDelegate {
|
||||||
|
|
||||||
let articleIconSchemeHandler: ArticleIconSchemeHandler
|
let articleIconSchemeHandler: ArticleIconSchemeHandler
|
||||||
|
let viewController: UIViewController
|
||||||
|
|
||||||
private let minimumQueueDepth = 3
|
private let minimumQueueDepth = 3
|
||||||
private let maximumQueueDepth = 6
|
private let maximumQueueDepth = 6
|
||||||
private var queue: [WKWebView] = []
|
private var queue = UIView()
|
||||||
|
|
||||||
private var waitingForFirstLoad = true
|
private var waitingForFirstLoad = true
|
||||||
private var waitingCompletionHandler: ((WKWebView) -> ())?
|
private var waitingCompletionHandler: ((WKWebView) -> ())?
|
||||||
|
|
||||||
init(coordinator: SceneCoordinator) {
|
init(coordinator: SceneCoordinator, viewController: UIViewController) {
|
||||||
articleIconSchemeHandler = ArticleIconSchemeHandler(coordinator: coordinator)
|
articleIconSchemeHandler = ArticleIconSchemeHandler(coordinator: coordinator)
|
||||||
|
self.viewController = viewController
|
||||||
super.init()
|
super.init()
|
||||||
|
self.viewController.view.insertSubview(queue, at: 0)
|
||||||
replenishQueueIfNeeded()
|
replenishQueueIfNeeded()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -37,12 +40,12 @@ class WebViewProvider: NSObject, WKNavigationDelegate {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func enqueueWebView(_ webView: WKWebView) {
|
func enqueueWebView(_ webView: WKWebView) {
|
||||||
guard queue.count < maximumQueueDepth else {
|
guard queue.subviews.count < maximumQueueDepth else {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
webView.navigationDelegate = self
|
webView.navigationDelegate = self
|
||||||
queue.insert(webView, at: 0)
|
queue.insertSubview(webView, at: 0)
|
||||||
|
|
||||||
webView.loadFileURL(ArticleRenderer.page.url, allowingReadAccessTo: ArticleRenderer.page.baseURL)
|
webView.loadFileURL(ArticleRenderer.page.url, allowingReadAccessTo: ArticleRenderer.page.baseURL)
|
||||||
|
|
||||||
@ -63,7 +66,7 @@ class WebViewProvider: NSObject, WKNavigationDelegate {
|
|||||||
// MARK: Private
|
// MARK: Private
|
||||||
|
|
||||||
private func replenishQueueIfNeeded() {
|
private func replenishQueueIfNeeded() {
|
||||||
while queue.count < minimumQueueDepth {
|
while queue.subviews.count < minimumQueueDepth {
|
||||||
let preferences = WKPreferences()
|
let preferences = WKPreferences()
|
||||||
preferences.javaScriptCanOpenWindowsAutomatically = false
|
preferences.javaScriptCanOpenWindowsAutomatically = false
|
||||||
preferences.javaScriptEnabled = true
|
preferences.javaScriptEnabled = true
|
||||||
@ -81,7 +84,8 @@ class WebViewProvider: NSObject, WKNavigationDelegate {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private func completeRequest(completion: @escaping (WKWebView) -> ()) {
|
private func completeRequest(completion: @escaping (WKWebView) -> ()) {
|
||||||
if let webView = queue.popLast() {
|
if let webView = queue.subviews.last as? WKWebView {
|
||||||
|
webView.removeFromSuperview()
|
||||||
webView.navigationDelegate = nil
|
webView.navigationDelegate = nil
|
||||||
replenishQueueIfNeeded()
|
replenishQueueIfNeeded()
|
||||||
completion(webView)
|
completion(webView)
|
||||||
|
@ -30,7 +30,7 @@ class SceneCoordinator: NSObject, UndoableCommandRunner, UnreadCountProvider {
|
|||||||
return rootSplitViewController.undoManager
|
return rootSplitViewController.undoManager
|
||||||
}
|
}
|
||||||
|
|
||||||
lazy var webViewProvider = WebViewProvider(coordinator: self)
|
lazy var webViewProvider = WebViewProvider(coordinator: self, viewController: rootSplitViewController)
|
||||||
|
|
||||||
private var panelMode: PanelMode = .unset
|
private var panelMode: PanelMode = .unset
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user