Change to always reload the page when rendering an article. Issue #1713

This commit is contained in:
Maurice Parker 2020-01-26 20:29:58 -07:00
parent 4a63b28c73
commit 97d6c69431
3 changed files with 10 additions and 24 deletions

View File

@ -14,15 +14,14 @@ import Account
struct ArticleRenderer { struct ArticleRenderer {
typealias Rendering = (style: String, html: String) typealias Rendering = (style: String, html: String)
typealias Page = (html: String, baseURL: URL) typealias Page = (url: URL, baseURL: URL)
static var imageIconScheme = "nnwImageIcon" static var imageIconScheme = "nnwImageIcon"
static var page: Page = { static var page: Page = {
let pageURL = Bundle.main.url(forResource: "page", withExtension: "html")! let url = Bundle.main.url(forResource: "page", withExtension: "html")!
let html = try! String(contentsOf: pageURL) let baseURL = url.deletingLastPathComponent()
let baseURL = pageURL.deletingLastPathComponent() return Page(url: url, baseURL: baseURL)
return Page(html: html, baseURL: baseURL)
}() }()
private let article: Article? private let article: Article?

View File

@ -30,7 +30,6 @@ class WebViewController: UIViewController {
private var topShowBarsViewConstraint: NSLayoutConstraint! private var topShowBarsViewConstraint: NSLayoutConstraint!
private var bottomShowBarsViewConstraint: NSLayoutConstraint! private var bottomShowBarsViewConstraint: NSLayoutConstraint!
private var renderingTracker = 0
private var webView: WKWebView! private var webView: WKWebView!
private lazy var contextMenuInteraction = UIContextMenuInteraction(delegate: self) private lazy var contextMenuInteraction = UIContextMenuInteraction(delegate: self)
private var isFullScreenAvailable: Bool { private var isFullScreenAvailable: Bool {
@ -44,7 +43,7 @@ class WebViewController: UIViewController {
private var isShowingExtractedArticle = false { private var isShowingExtractedArticle = false {
didSet { didSet {
if isShowingExtractedArticle != oldValue { if isShowingExtractedArticle != oldValue {
renderPage() reloadHTML()
} }
} }
} }
@ -66,7 +65,7 @@ class WebViewController: UIViewController {
} }
if article != oldValue { if article != oldValue {
restoreWindowScrollY = 0 restoreWindowScrollY = 0
renderPage() reloadHTML()
} }
} }
} }
@ -267,7 +266,7 @@ extension WebViewController: ArticleExtractorDelegate {
func articleExtractionDidFail(with: Error) { func articleExtractionDidFail(with: Error) {
stopArticleExtractor() stopArticleExtractor()
articleExtractorButtonState = .error articleExtractorButtonState = .error
renderPage() reloadHTML()
} }
func articleExtractionDidComplete(extractedArticle: ExtractedArticle) { func articleExtractionDidComplete(extractedArticle: ExtractedArticle) {
@ -351,7 +350,7 @@ extension WebViewController: WKNavigationDelegate {
} }
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) { func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
self.renderPage() renderPage()
} }
} }
@ -451,24 +450,12 @@ private struct ImageClickMessage: Codable {
private extension WebViewController { private extension WebViewController {
func reloadHTML() { func reloadHTML() {
let url = Bundle.main.url(forResource: "page", withExtension: "html")! webView?.loadFileURL(ArticleRenderer.page.url, allowingReadAccessTo: ArticleRenderer.page.baseURL)
webView.loadFileURL(url, allowingReadAccessTo: url.deletingLastPathComponent())
renderingTracker = 0
} }
func renderPage() { func renderPage() {
guard let webView = webView else { return } guard let webView = webView else { return }
// It looks like we need to clean up the webview every once in a while by reloading it from scratch
// Otherwise it will stop responding or cause rendering artifacts. This typically only comes into
// play on the iPad where we aren't constantly pushing and popping this controller.
if (renderingTracker > 10) {
reloadHTML()
return
}
renderingTracker += 1
let style = ArticleStylesManager.shared.currentStyle let style = ArticleStylesManager.shared.currentStyle
let rendering: ArticleRenderer.Rendering let rendering: ArticleRenderer.Rendering

View File

@ -40,7 +40,7 @@ class WebViewProvider: NSObject, WKNavigationDelegate {
webView.navigationDelegate = self webView.navigationDelegate = self
queue.insert(webView, at: 0) queue.insert(webView, at: 0)
webView.loadHTMLString(ArticleRenderer.page.html, baseURL: ArticleRenderer.page.baseURL) webView.loadFileURL(ArticleRenderer.page.url, allowingReadAccessTo: ArticleRenderer.page.baseURL)
} }