From 877fdb1a93690263072be3df363218177a9b3272 Mon Sep 17 00:00:00 2001 From: Jackson Harper Date: Mon, 28 Mar 2022 10:52:31 -0700 Subject: [PATCH 1/3] Reload the web reader if its process is terminated --- .../Sources/App/Views/WebReader/WebReaderCoordinator.swift | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReaderCoordinator.swift b/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReaderCoordinator.swift index c4ad4d6d5..57fcc5b42 100644 --- a/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReaderCoordinator.swift +++ b/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReaderCoordinator.swift @@ -69,6 +69,10 @@ extension WebReaderCoordinator: WKNavigationDelegate { webView.backgroundColor = .systemBackground #endif } + + func webViewWebContentProcessDidTerminate(_ webView: WKWebView) { + self.needsReload = true + } } #if os(iOS) From 278578c82ee9c3842c56b5f3c51d812768bbc3e9 Mon Sep 17 00:00:00 2001 From: Satindar Dhillon Date: Mon, 28 Mar 2022 17:00:34 -0700 Subject: [PATCH 2/3] load webreader in updateUIView func when needsRelaod is true --- .../App/Views/WebReader/WebReader.swift | 25 +++++++++++-------- .../WebReader/WebReaderCoordinator.swift | 4 +-- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReader.swift b/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReader.swift index 83e07696a..8c190241e 100644 --- a/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReader.swift +++ b/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReader.swift @@ -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 @@ -68,6 +57,20 @@ struct WebReader: UIViewRepresentable { } func updateUIView(_ webView: WKWebView, context: Context) { + if context.coordinator.needsReload { + webView.loadHTMLString( + WebReaderContent( + articleContent: articleContent, + item: item, + isDark: UITraitCollection.current.userInterfaceStyle == .dark, + fontSize: fontSize() + ) + .styledContent, + baseURL: ViewsPackage.bundleURL + ) + context.coordinator.needsReload = false + } + if annotationSaveTransactionID != context.coordinator.lastSavedAnnotationID { context.coordinator.lastSavedAnnotationID = annotationSaveTransactionID (webView as? WebView)?.saveAnnotation(annotation: annotation) diff --git a/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReaderCoordinator.swift b/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReaderCoordinator.swift index 57fcc5b42..1b8a72048 100644 --- a/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReaderCoordinator.swift +++ b/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReaderCoordinator.swift @@ -70,8 +70,8 @@ extension WebReaderCoordinator: WKNavigationDelegate { #endif } - func webViewWebContentProcessDidTerminate(_ webView: WKWebView) { - self.needsReload = true + func webViewWebContentProcessDidTerminate(_: WKWebView) { + needsReload = true } } From 870af33acf6cc99f25969d390f927523ab2f3004 Mon Sep 17 00:00:00 2001 From: Satindar Dhillon Date: Mon, 28 Mar 2022 18:45:03 -0700 Subject: [PATCH 3/3] check for root element on webview and relaod content of it's missing --- .../App/Views/WebReader/WebReader.swift | 49 +++++++++++++------ .../WebReader/WebReaderCoordinator.swift | 2 +- 2 files changed, 36 insertions(+), 15 deletions(-) diff --git a/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReader.swift b/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReader.swift index 8c190241e..26ee03a8d 100644 --- a/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReader.swift +++ b/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReader.swift @@ -52,25 +52,12 @@ struct WebReader: UIViewRepresentable { context.coordinator.linkHandler = openLinkAction context.coordinator.webViewActionHandler = webViewActionHandler context.coordinator.updateNavBarVisibilityRatio = navBarVisibilityRatioUpdater + loadContent(webView: webView) return webView } func updateUIView(_ webView: WKWebView, context: Context) { - if context.coordinator.needsReload { - webView.loadHTMLString( - WebReaderContent( - articleContent: articleContent, - item: item, - isDark: UITraitCollection.current.userInterfaceStyle == .dark, - fontSize: fontSize() - ) - .styledContent, - baseURL: ViewsPackage.bundleURL - ) - context.coordinator.needsReload = false - } - if annotationSaveTransactionID != context.coordinator.lastSavedAnnotationID { context.coordinator.lastSavedAnnotationID = annotationSaveTransactionID (webView as? WebView)?.saveAnnotation(annotation: annotation) @@ -85,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 + ) } } diff --git a/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReaderCoordinator.swift b/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReaderCoordinator.swift index 1b8a72048..c75bca135 100644 --- a/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReaderCoordinator.swift +++ b/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReaderCoordinator.swift @@ -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?