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
|
2020-05-07 18:32:11 +02:00
|
|
|
import RSCore
|
2019-09-21 17:37:21 +02:00
|
|
|
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-02-26 00:10:51 +01:00
|
|
|
class WebViewProvider: NSObject {
|
2019-09-21 17:37:21 +02:00
|
|
|
|
2020-05-07 18:32:11 +02:00
|
|
|
private let articleIconSchemeHandler: ArticleIconSchemeHandler
|
|
|
|
private let operationQueue = MainThreadOperationQueue()
|
2020-01-30 01:39:40 +01:00
|
|
|
private var queue = UIView()
|
2019-09-21 17:37:21 +02:00
|
|
|
|
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)
|
|
|
|
super.init()
|
2020-01-30 20:53:27 +01:00
|
|
|
viewController.view.insertSubview(queue, at: 0)
|
2020-02-05 01:00:26 +01:00
|
|
|
replenishQueueIfNeeded()
|
|
|
|
}
|
|
|
|
|
|
|
|
func flushQueue() {
|
2020-05-07 18:32:11 +02:00
|
|
|
operationQueue.add(WebViewProviderFlushQueueOperation(queue: queue))
|
2020-02-05 01:00:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func replenishQueueIfNeeded() {
|
2020-05-07 18:32:11 +02:00
|
|
|
operationQueue.add(WebViewProviderReplenishQueueOperation(queue: queue, articleIconSchemeHandler: articleIconSchemeHandler))
|
|
|
|
}
|
|
|
|
|
|
|
|
func dequeueWebView(completion: @escaping (PreloadedWebView) -> ()) {
|
|
|
|
operationQueue.add(WebViewProviderDequeueOperation(queue: queue, articleIconSchemeHandler: articleIconSchemeHandler, completion: completion))
|
|
|
|
operationQueue.add(WebViewProviderReplenishQueueOperation(queue: queue, articleIconSchemeHandler: articleIconSchemeHandler))
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
class WebViewProviderFlushQueueOperation: MainThreadOperation {
|
|
|
|
|
|
|
|
// MainThreadOperation
|
|
|
|
public var isCanceled = false
|
|
|
|
public var id: Int?
|
|
|
|
public weak var operationDelegate: MainThreadOperationDelegate?
|
|
|
|
public var name: String? = "WebViewProviderFlushQueueOperation"
|
|
|
|
public var completionBlock: MainThreadOperation.MainThreadOperationCompletionBlock?
|
|
|
|
|
|
|
|
private var queue: UIView
|
|
|
|
|
|
|
|
init(queue: UIView) {
|
|
|
|
self.queue = queue
|
|
|
|
}
|
|
|
|
|
|
|
|
func run() {
|
|
|
|
queue.subviews.forEach { $0.removeFromSuperview() }
|
|
|
|
self.operationDelegate?.operationDidComplete(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
class WebViewProviderReplenishQueueOperation: MainThreadOperation {
|
|
|
|
|
|
|
|
// MainThreadOperation
|
|
|
|
public var isCanceled = false
|
|
|
|
public var id: Int?
|
|
|
|
public weak var operationDelegate: MainThreadOperationDelegate?
|
|
|
|
public var name: String? = "WebViewProviderReplenishQueueOperation"
|
|
|
|
public var completionBlock: MainThreadOperation.MainThreadOperationCompletionBlock?
|
|
|
|
|
|
|
|
private let minimumQueueDepth = 3
|
|
|
|
|
|
|
|
private var queue: UIView
|
|
|
|
private var articleIconSchemeHandler: ArticleIconSchemeHandler
|
|
|
|
|
|
|
|
init(queue: UIView, articleIconSchemeHandler: ArticleIconSchemeHandler) {
|
|
|
|
self.queue = queue
|
|
|
|
self.articleIconSchemeHandler = articleIconSchemeHandler
|
|
|
|
}
|
|
|
|
|
|
|
|
func run() {
|
2020-02-05 01:00:26 +01:00
|
|
|
while queue.subviews.count < minimumQueueDepth {
|
2020-05-07 18:32:11 +02:00
|
|
|
let webView = PreloadedWebView(articleIconSchemeHandler: articleIconSchemeHandler)
|
|
|
|
queue.insertSubview(webView, at: 0)
|
|
|
|
webView.preload()
|
2020-02-05 01:00:26 +01:00
|
|
|
}
|
2020-05-07 18:32:11 +02:00
|
|
|
self.operationDelegate?.operationDidComplete(self)
|
2020-02-05 01:00:26 +01:00
|
|
|
}
|
2020-01-27 20:58:32 +01:00
|
|
|
|
2020-05-07 18:32:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class WebViewProviderDequeueOperation: MainThreadOperation {
|
|
|
|
|
|
|
|
// MainThreadOperation
|
|
|
|
public var isCanceled = false
|
|
|
|
public var id: Int?
|
|
|
|
public weak var operationDelegate: MainThreadOperationDelegate?
|
|
|
|
public var name: String? = "WebViewProviderFlushQueueOperation"
|
|
|
|
public var completionBlock: MainThreadOperation.MainThreadOperationCompletionBlock?
|
|
|
|
|
|
|
|
private var queue: UIView
|
|
|
|
private var articleIconSchemeHandler: ArticleIconSchemeHandler
|
|
|
|
private var completion: (PreloadedWebView) -> ()
|
|
|
|
|
|
|
|
init(queue: UIView, articleIconSchemeHandler: ArticleIconSchemeHandler, completion: @escaping (PreloadedWebView) -> ()) {
|
|
|
|
self.queue = queue
|
|
|
|
self.articleIconSchemeHandler = articleIconSchemeHandler
|
|
|
|
self.completion = completion
|
|
|
|
}
|
|
|
|
|
|
|
|
func run() {
|
2020-02-26 00:10:51 +01:00
|
|
|
if let webView = queue.subviews.last as? PreloadedWebView {
|
|
|
|
webView.ready { preloadedWebView in
|
|
|
|
preloadedWebView.removeFromSuperview()
|
2020-05-07 18:32:11 +02:00
|
|
|
self.completion(preloadedWebView)
|
2020-02-26 00:10:51 +01:00
|
|
|
}
|
2020-07-08 20:22:19 +02:00
|
|
|
self.operationDelegate?.operationDidComplete(self)
|
2019-09-21 17:37:21 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-26 00:10:51 +01:00
|
|
|
assertionFailure("Creating PreloadedWebView in \(#function); queue has run dry.")
|
|
|
|
|
|
|
|
let webView = PreloadedWebView(articleIconSchemeHandler: articleIconSchemeHandler)
|
2020-05-07 18:32:11 +02:00
|
|
|
webView.preload()
|
2020-02-26 00:10:51 +01:00
|
|
|
webView.ready { preloadedWebView in
|
2020-05-07 18:32:11 +02:00
|
|
|
self.completion(preloadedWebView)
|
2019-09-21 17:37:21 +02:00
|
|
|
}
|
2020-07-08 20:22:19 +02:00
|
|
|
self.operationDelegate?.operationDidComplete(self)
|
2019-09-21 17:37:21 +02:00
|
|
|
}
|
|
|
|
|
2020-02-05 01:00:26 +01:00
|
|
|
}
|