From 58af2fe4b3e53304a994275e9cc8befd36c6f1ff Mon Sep 17 00:00:00 2001 From: simorgh3196 Date: Fri, 13 Sep 2024 21:35:25 +0900 Subject: [PATCH] Fixed build errors occurring in Xcode16-RC --- Sources/WebUI/EnhancedWKWebView.swift | 59 ++++++++++----------- Sources/WebUI/WebView.swift | 1 + Sources/WebUI/WebViewProxy.swift | 75 +++++++++++++-------------- Tests/WebUITests/Mock.swift | 10 ++++ 4 files changed, 77 insertions(+), 68 deletions(-) diff --git a/Sources/WebUI/EnhancedWKWebView.swift b/Sources/WebUI/EnhancedWKWebView.swift index a4384a6..f94c0b7 100644 --- a/Sources/WebUI/EnhancedWKWebView.swift +++ b/Sources/WebUI/EnhancedWKWebView.swift @@ -54,7 +54,7 @@ class EnhancedWKWebView: WKWebView { fatalError("init(coder:) has not been implemented") } - final class NavigationDelegateProxy: NSObject, WKNavigationDelegate { + final class NavigationDelegateProxy: NSObject { weak var delegate: (any WKNavigationDelegate)? let refreshControl: RefreshControl @@ -74,38 +74,39 @@ class EnhancedWKWebView: WKWebView { return delegate } + } +} - private func endRefreshing() { - Task { @MainActor [refreshControl] in - refreshControl.endRefreshing() - } +extension EnhancedWKWebView.NavigationDelegateProxy: WKNavigationDelegate { + private func endRefreshing() { + Task { @MainActor [refreshControl] in + refreshControl.endRefreshing() } + } - // MARK: WKNavigationDelegate - func webView( - _ webView: WKWebView, - didFail navigation: WKNavigation!, - withError error: any Error - ) { - endRefreshing() - delegate?.webView?(webView, didFail: navigation, withError: error) - } + func webView( + _ webView: WKWebView, + didFail navigation: WKNavigation!, + withError error: any Error + ) { + endRefreshing() + delegate?.webView?(webView, didFail: navigation, withError: error) + } - func webView( - _ webView: WKWebView, - didFailProvisionalNavigation navigation: WKNavigation!, - withError error: any Error - ) { - endRefreshing() - delegate?.webView?(webView, didFailProvisionalNavigation: navigation, withError: error) - } + func webView( + _ webView: WKWebView, + didFailProvisionalNavigation navigation: WKNavigation!, + withError error: any Error + ) { + endRefreshing() + delegate?.webView?(webView, didFailProvisionalNavigation: navigation, withError: error) + } - func webView( - _ webView: WKWebView, - didFinish navigation: WKNavigation! - ) { - endRefreshing() - delegate?.webView?(webView, didFinish: navigation) - } + func webView( + _ webView: WKWebView, + didFinish navigation: WKNavigation! + ) { + endRefreshing() + delegate?.webView?(webView, didFinish: navigation) } } diff --git a/Sources/WebUI/WebView.swift b/Sources/WebUI/WebView.swift index 4907c89..0a12998 100644 --- a/Sources/WebUI/WebView.swift +++ b/Sources/WebUI/WebView.swift @@ -28,6 +28,7 @@ public struct WebView { /// - Parameters: /// - request: The initial request specifying the URL to load. /// - configuration: The configuration for the new web view. + @MainActor public init(request: URLRequest? = nil, configuration: WKWebViewConfiguration = .init()) { self.initialRequest = request self.configuration = configuration diff --git a/Sources/WebUI/WebViewProxy.swift b/Sources/WebUI/WebViewProxy.swift index 71c1e4b..f3d96f9 100644 --- a/Sources/WebUI/WebViewProxy.swift +++ b/Sources/WebUI/WebViewProxy.swift @@ -29,12 +29,12 @@ public final class WebViewProxy: ObservableObject { /// A Boolean value indicating whether there is a forward item in the back-forward list that can be navigated to. @Published public private(set) var canGoForward = false - private var task: Task? + private var tasks: [Task] = [] nonisolated init() {} deinit { - task?.cancel() + tasks.forEach { $0.cancel() } } func setUp(_ webView: Remakeable) { @@ -48,46 +48,41 @@ public final class WebViewProxy: ObservableObject { } private func observe(_ webView: WKWebView) { - task?.cancel() - task = Task { - await withTaskGroup(of: Void.self) { group in - group.addTask { @MainActor in - for await value in webView.publisher(for: \.title).bufferedValues() { - self.title = value - } - } + tasks.forEach { $0.cancel() } + tasks.removeAll() - group.addTask { @MainActor in - for await value in webView.publisher(for: \.url).bufferedValues() { - self.url = value - } + tasks = [ + Task { @MainActor in + for await value in webView.publisher(for: \.title).bufferedValues() { + self.title = value } - - group.addTask { @MainActor in - for await value in webView.publisher(for: \.isLoading).bufferedValues() { - self.isLoading = value - } + }, + Task { @MainActor in + for await value in webView.publisher(for: \.url).bufferedValues() { + self.url = value } - - group.addTask { @MainActor in - for await value in webView.publisher(for: \.estimatedProgress).bufferedValues() { - self.estimatedProgress = value - } + }, + Task { @MainActor in + for await value in webView.publisher(for: \.isLoading).bufferedValues() { + self.isLoading = value } - - group.addTask { @MainActor in - for await value in webView.publisher(for: \.canGoBack).bufferedValues() { - self.canGoBack = value - } + }, + Task { @MainActor in + for await value in webView.publisher(for: \.estimatedProgress).bufferedValues() { + self.estimatedProgress = value } - - group.addTask { @MainActor in - for await value in webView.publisher(for: \.canGoForward).bufferedValues() { - self.canGoForward = value - } + }, + Task { @MainActor in + for await value in webView.publisher(for: \.canGoBack).bufferedValues() { + self.canGoBack = value + } + }, + Task { @MainActor in + for await value in webView.publisher(for: \.canGoForward).bufferedValues() { + self.canGoForward = value } } - } + ] } /// Navigates to a requested URL. @@ -140,10 +135,12 @@ public final class WebViewProxy: ObservableObject { guard let webView else { return nil } return try await withCheckedThrowingContinuation { continuation in webView.wrappedValue.evaluateJavaScript(javaScriptString) { result, error in - if let error { - continuation.resume(throwing: error) - } else { - continuation.resume(returning: result) + Task { @MainActor in + if let error { + continuation.resume(throwing: error) + } else { + continuation.resume(returning: result) + } } } } diff --git a/Tests/WebUITests/Mock.swift b/Tests/WebUITests/Mock.swift index c7ba285..f2b51ba 100644 --- a/Tests/WebUITests/Mock.swift +++ b/Tests/WebUITests/Mock.swift @@ -28,6 +28,15 @@ final class EnhancedWKWebViewMock: EnhancedWKWebView { return nil } + #if compiler(>=6.0) + override func evaluateJavaScript( + _ javaScriptString: String, + completionHandler: (@MainActor (Any?, (any Error)?) -> Void)? = nil + ) { + self.javaScriptString = javaScriptString + completionHandler?(true, nil) + } + #else override func evaluateJavaScript( _ javaScriptString: String, completionHandler: ((Any?, (any Error)?) -> Void)? = nil @@ -35,6 +44,7 @@ final class EnhancedWKWebViewMock: EnhancedWKWebView { self.javaScriptString = javaScriptString completionHandler?(true, nil) } + #endif } final class UIDelegateMock: NSObject, WKUIDelegate {}