2019-09-21 17:37:21 +02:00
|
|
|
//
|
2020-01-01 00:55:39 +01:00
|
|
|
// WebViewProvider.swift
|
2019-09-21 17:37:21 +02:00
|
|
|
// NetNewsWire-iOS
|
|
|
|
//
|
|
|
|
// Created by Maurice Parker on 9/21/19.
|
|
|
|
// Copyright © 2019 Ranchero Software. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
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-shite part of their lifecycle.
|
2020-01-01 00:55:39 +01:00
|
|
|
class WebViewProvider: NSObject, WKNavigationDelegate {
|
2019-09-21 17:37:21 +02:00
|
|
|
|
2020-01-27 20:58:32 +01:00
|
|
|
let articleIconSchemeHandler: ArticleIconSchemeHandler
|
2020-01-30 01:39:40 +01:00
|
|
|
let viewController: UIViewController
|
2019-11-07 21:29:16 +01:00
|
|
|
|
2019-09-21 17:37:21 +02:00
|
|
|
private let minimumQueueDepth = 3
|
|
|
|
private let maximumQueueDepth = 6
|
2020-01-30 01:39:40 +01:00
|
|
|
private var queue = UIView()
|
2019-09-21 17:37:21 +02:00
|
|
|
|
|
|
|
private var waitingForFirstLoad = true
|
|
|
|
private var waitingCompletionHandler: ((WKWebView) -> ())?
|
|
|
|
|
2020-01-30 01:39:40 +01:00
|
|
|
init(coordinator: SceneCoordinator, viewController: UIViewController) {
|
2020-01-27 20:58:32 +01:00
|
|
|
articleIconSchemeHandler = ArticleIconSchemeHandler(coordinator: coordinator)
|
2020-01-30 01:39:40 +01:00
|
|
|
self.viewController = viewController
|
2020-01-27 20:58:32 +01:00
|
|
|
super.init()
|
2020-01-30 01:39:40 +01:00
|
|
|
self.viewController.view.insertSubview(queue, at: 0)
|
2020-01-27 20:58:32 +01:00
|
|
|
replenishQueueIfNeeded()
|
|
|
|
}
|
|
|
|
|
2019-09-21 17:37:21 +02:00
|
|
|
func dequeueWebView(completion: @escaping (WKWebView) -> ()) {
|
|
|
|
if waitingForFirstLoad {
|
|
|
|
waitingCompletionHandler = completion
|
|
|
|
} else {
|
|
|
|
completeRequest(completion: completion)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func enqueueWebView(_ webView: WKWebView) {
|
2020-01-30 01:39:40 +01:00
|
|
|
guard queue.subviews.count < maximumQueueDepth else {
|
2019-09-21 17:37:21 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
webView.navigationDelegate = self
|
2020-01-30 01:39:40 +01:00
|
|
|
queue.insertSubview(webView, at: 0)
|
2019-09-21 17:37:21 +02:00
|
|
|
|
2020-01-27 04:29:58 +01:00
|
|
|
webView.loadFileURL(ArticleRenderer.page.url, allowingReadAccessTo: ArticleRenderer.page.baseURL)
|
2019-09-21 17:37:21 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: WKNavigationDelegate
|
|
|
|
|
|
|
|
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
|
|
|
|
if waitingForFirstLoad {
|
|
|
|
waitingForFirstLoad = false
|
|
|
|
if let completion = waitingCompletionHandler {
|
|
|
|
completeRequest(completion: completion)
|
|
|
|
waitingCompletionHandler = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Private
|
|
|
|
|
|
|
|
private func replenishQueueIfNeeded() {
|
2020-01-30 01:39:40 +01:00
|
|
|
while queue.subviews.count < minimumQueueDepth {
|
2019-09-21 22:59:51 +02:00
|
|
|
let preferences = WKPreferences()
|
|
|
|
preferences.javaScriptCanOpenWindowsAutomatically = false
|
|
|
|
preferences.javaScriptEnabled = true
|
|
|
|
|
|
|
|
let configuration = WKWebViewConfiguration()
|
|
|
|
configuration.preferences = preferences
|
2019-10-14 02:41:34 +02:00
|
|
|
configuration.setValue(true, forKey: "allowUniversalAccessFromFileURLs")
|
2019-09-21 22:59:51 +02:00
|
|
|
configuration.allowsInlineMediaPlayback = true
|
|
|
|
configuration.mediaTypesRequiringUserActionForPlayback = .video
|
2019-11-07 21:29:16 +01:00
|
|
|
configuration.setURLSchemeHandler(articleIconSchemeHandler, forURLScheme: ArticleRenderer.imageIconScheme)
|
2019-09-21 22:59:51 +02:00
|
|
|
|
|
|
|
let webView = WKWebView(frame: .zero, configuration: configuration)
|
2019-09-21 17:37:21 +02:00
|
|
|
enqueueWebView(webView)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func completeRequest(completion: @escaping (WKWebView) -> ()) {
|
2020-01-30 01:39:40 +01:00
|
|
|
if let webView = queue.subviews.last as? WKWebView {
|
|
|
|
webView.removeFromSuperview()
|
2019-09-21 17:37:21 +02:00
|
|
|
webView.navigationDelegate = nil
|
|
|
|
replenishQueueIfNeeded()
|
|
|
|
completion(webView)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
assertionFailure("Creating WKWebView in \(#function); queue has run dry.")
|
|
|
|
let webView = WKWebView(frame: .zero)
|
|
|
|
completion(webView)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|