Skip to content

Commit

Permalink
Fix rare crash in GutenbergWebViewController
Browse files Browse the repository at this point in the history
  • Loading branch information
kean committed Jun 20, 2024
1 parent db96084 commit 26bd9b3
Showing 1 changed file with 7 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import UIKit
import WebKit
import Gutenberg
import Combine

class GutenbergWebViewController: GutenbergWebSingleBlockViewController, WebKitAuthenticatable, NoResultsViewHost {
enum GutenbergWebError: Error {
Expand All @@ -12,6 +13,7 @@ class GutenbergWebViewController: GutenbergWebSingleBlockViewController, WebKitA
private let progressView = WebProgressView()
private let userId: String
let gutenbergReadySemaphore = DispatchSemaphore(value: 0)
private var cancellables: [AnyCancellable] = []

init(with post: AbstractPost, block: Block) throws {
authenticator = GutenbergRequestAuthenticator(blog: post.blog)
Expand Down Expand Up @@ -45,28 +47,6 @@ class GutenbergWebViewController: GutenbergWebSingleBlockViewController, WebKitA
}
}

deinit {
webView.removeObserver(self, forKeyPath: #keyPath(WKWebView.estimatedProgress))
}

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey: Any]?, context: UnsafeMutableRawPointer?) {
guard
let object = object as? WKWebView,
object == webView,
let keyPath = keyPath
else {
return
}

switch keyPath {
case #keyPath(WKWebView.estimatedProgress):
progressView.progress = Float(webView.estimatedProgress)
progressView.isHidden = webView.estimatedProgress == 1
default:
assertionFailure("Observed change to web view that we are not handling")
}
}

override func getRequest(for webView: WKWebView, completion: @escaping (URLRequest) -> Void) {
authenticatedRequest(for: url, on: webView) { (request) in
completion(request)
Expand Down Expand Up @@ -155,7 +135,11 @@ class GutenbergWebViewController: GutenbergWebSingleBlockViewController, WebKitA
}

private func startObservingWebView() {
webView.addObserver(self, forKeyPath: #keyPath(WKWebView.estimatedProgress), options: [.new], context: nil)
webView.publisher(for: \.estimatedProgress, options: [.new]).sink { [weak self] progress in
guard let self else { return }
progressView.progress = Float(progress)
progressView.isHidden = progress == 1
}.store(in: &cancellables)
}

private func addProgressView() {
Expand Down

0 comments on commit 26bd9b3

Please sign in to comment.