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-07-16 20:56:07 +02:00
|
|
|
private var queue = NSMutableArray()
|
2019-09-21 17:37:21 +02:00
|
|
|
|
2020-07-16 20:56:07 +02:00
|
|
|
init(coordinator: SceneCoordinator) {
|
2020-01-27 20:58:32 +01:00
|
|
|
articleIconSchemeHandler = ArticleIconSchemeHandler(coordinator: coordinator)
|
|
|
|
super.init()
|
2020-02-05 01:00:26 +01:00
|
|
|
replenishQueueIfNeeded()
|
|
|
|
}
|
|
|
|
|
|
|
|
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 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
|
|
|
|
|
2020-07-16 20:56:07 +02:00
|
|
|
private var queue: NSMutableArray
|
2020-05-07 18:32:11 +02:00
|
|
|
private var articleIconSchemeHandler: ArticleIconSchemeHandler
|
|
|
|
|
2020-07-16 20:56:07 +02:00
|
|
|
init(queue: NSMutableArray, articleIconSchemeHandler: ArticleIconSchemeHandler) {
|
2020-05-07 18:32:11 +02:00
|
|
|
self.queue = queue
|
|
|
|
self.articleIconSchemeHandler = articleIconSchemeHandler
|
|
|
|
}
|
|
|
|
|
|
|
|
func run() {
|
2020-07-16 20:56:07 +02:00
|
|
|
while queue.count < minimumQueueDepth {
|
2020-05-07 18:32:11 +02:00
|
|
|
let webView = PreloadedWebView(articleIconSchemeHandler: articleIconSchemeHandler)
|
2020-07-16 20:56:07 +02:00
|
|
|
queue.insert(webView, at: 0)
|
2020-05-07 18:32:11 +02:00
|
|
|
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?
|
|
|
|
|
2020-07-16 20:56:07 +02:00
|
|
|
private var queue: NSMutableArray
|
2020-05-07 18:32:11 +02:00
|
|
|
private var articleIconSchemeHandler: ArticleIconSchemeHandler
|
|
|
|
private var completion: (PreloadedWebView) -> ()
|
|
|
|
|
2020-07-16 20:56:07 +02:00
|
|
|
init(queue: NSMutableArray, articleIconSchemeHandler: ArticleIconSchemeHandler, completion: @escaping (PreloadedWebView) -> ()) {
|
2020-05-07 18:32:11 +02:00
|
|
|
self.queue = queue
|
|
|
|
self.articleIconSchemeHandler = articleIconSchemeHandler
|
|
|
|
self.completion = completion
|
|
|
|
}
|
|
|
|
|
|
|
|
func run() {
|
2020-07-16 20:56:07 +02:00
|
|
|
if let webView = queue.lastObject as? PreloadedWebView {
|
2020-02-26 00:10:51 +01:00
|
|
|
webView.ready { preloadedWebView in
|
2020-05-07 18:32:11 +02:00
|
|
|
self.completion(preloadedWebView)
|
2020-07-16 20:56:07 +02:00
|
|
|
self.queue.remove(webView)
|
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
|
|
|
}
|