Merge pull request #332 from omnivore-app/fix/webview-reload

Reload the web reader if its process is terminated
This commit is contained in:
Satindar Dhillon
2022-03-29 09:48:33 -07:00
committed by GitHub
2 changed files with 40 additions and 12 deletions

View File

@ -29,17 +29,6 @@ struct WebReader: UIViewRepresentable {
let webView = WebViewManager.shared()
let contentController = WKUserContentController()
webView.loadHTMLString(
WebReaderContent(
articleContent: articleContent,
item: item,
isDark: UITraitCollection.current.userInterfaceStyle == .dark,
fontSize: fontSize()
)
.styledContent,
baseURL: ViewsPackage.bundleURL
)
webView.navigationDelegate = context.coordinator
webView.isOpaque = false
webView.backgroundColor = .clear
@ -63,6 +52,7 @@ struct WebReader: UIViewRepresentable {
context.coordinator.linkHandler = openLinkAction
context.coordinator.webViewActionHandler = webViewActionHandler
context.coordinator.updateNavBarVisibilityRatio = navBarVisibilityRatioUpdater
loadContent(webView: webView)
return webView
}
@ -82,5 +72,39 @@ struct WebReader: UIViewRepresentable {
context.coordinator.previousDecreaseFontActionID = decreaseFontActionID
(webView as? WebView)?.decreaseFontSize()
}
// If the webview had been terminated `needsReload` will have been set to true
if context.coordinator.needsReload {
loadContent(webView: webView)
context.coordinator.needsReload = false
return
}
if webView.isLoading { return }
// If the root element is not detected then `WKWebView` may have unloaded the content
// so we need to load it again.
webView.evaluateJavaScript("document.getElementById('root') ? true : false") { hasRootElement, _ in
guard let hasRootElement = hasRootElement as? Bool else { return }
if !hasRootElement {
DispatchQueue.main.async {
loadContent(webView: webView)
}
}
}
}
func loadContent(webView: WKWebView) {
webView.loadHTMLString(
WebReaderContent(
articleContent: articleContent,
item: item,
isDark: UITraitCollection.current.userInterfaceStyle == .dark,
fontSize: fontSize()
)
.styledContent,
baseURL: ViewsPackage.bundleURL
)
}
}

View File

@ -12,7 +12,7 @@ typealias WKScriptMessageReplyHandler = (Any?, String?) -> Void
final class WebReaderCoordinator: NSObject {
var webViewActionHandler: (WKScriptMessage, WKScriptMessageReplyHandler?) -> Void = { _, _ in }
var linkHandler: (URL) -> Void = { _ in }
var needsReload = true
var needsReload = false
var lastSavedAnnotationID: UUID?
var previousIncreaseFontActionID: UUID?
var previousDecreaseFontActionID: UUID?
@ -69,6 +69,10 @@ extension WebReaderCoordinator: WKNavigationDelegate {
webView.backgroundColor = .systemBackground
#endif
}
func webViewWebContentProcessDidTerminate(_: WKWebView) {
needsReload = true
}
}
#if os(iOS)