From e0dda4da43c007d5897a3a159d382728289b919a Mon Sep 17 00:00:00 2001 From: Fernando Olivares Date: Sun, 20 Oct 2024 23:08:07 -0600 Subject: [PATCH] Add notification when `registerBridgeComponents` is called --- Source/Bridge/Bridge.swift | 1 + Source/Hotwire.swift | 2 ++ Source/HotwireNotifications.swift | 39 ++++++++++++++++++++++++++ Source/Turbo/Navigator/Navigator.swift | 10 +++++++ 4 files changed, 52 insertions(+) create mode 100644 Source/HotwireNotifications.swift diff --git a/Source/Bridge/Bridge.swift b/Source/Bridge/Bridge.swift index b454c43..2f0798f 100644 --- a/Source/Bridge/Bridge.swift +++ b/Source/Bridge/Bridge.swift @@ -25,6 +25,7 @@ public final class Bridge: Bridgable { public static func initialize(_ webView: WKWebView) { if getBridgeFor(webView) == nil { initialize(Bridge(webView: webView)) + webView.customUserAgent = Hotwire.config.userAgent } } diff --git a/Source/Hotwire.swift b/Source/Hotwire.swift index 432109e..fca233e 100644 --- a/Source/Hotwire.swift +++ b/Source/Hotwire.swift @@ -17,6 +17,8 @@ public enum Hotwire { Bridge.initialize(webView) return webView } + + NotificationCenter.default.postDidRegisterBridgeComponents() } static var bridgeComponentTypes = [BridgeComponent.Type]() diff --git a/Source/HotwireNotifications.swift b/Source/HotwireNotifications.swift new file mode 100644 index 0000000..13867fc --- /dev/null +++ b/Source/HotwireNotifications.swift @@ -0,0 +1,39 @@ +// +// HotwireNotifications.swift +// HotwireNative +// +// Created by Fernando Olivares on 20/10/24. +// + +import Foundation +import UIKit + +extension NSNotification.Name { + static let didRegisterBridgeComponents = NSNotification.Name("HotwireNativeDidRegisterBridgeComponents") +} + +extension NotificationCenter { + func removeObservation(_ observationToken: NSObjectProtocol?) { + guard let observationToken else { return } + removeObserver(observationToken) + } + + func removeObservations(_ observationTokens: [NSObjectProtocol?]) { + for observationToken in observationTokens.compactMap({ $0 }) { + removeObserver(observationToken) + } + } +} + +extension NotificationCenter { + + func postDidRegisterBridgeComponents() { + post(name: .didRegisterBridgeComponents, object: nil) + } + + func observeDidRegisterBridgeComponents(reaction: @escaping () -> Void) -> NSObjectProtocol { + addObserver(forName: .didRegisterBridgeComponents, object: nil, queue: .main) { _ in + reaction() + } + } +} diff --git a/Source/Turbo/Navigator/Navigator.swift b/Source/Turbo/Navigator/Navigator.swift index 3a5e635..db68448 100644 --- a/Source/Turbo/Navigator/Navigator.swift +++ b/Source/Turbo/Navigator/Navigator.swift @@ -138,6 +138,15 @@ public class Navigator { self.webkitUIDelegate = WKUIController(delegate: self) session.webView.uiDelegate = webkitUIDelegate modalSession.webView.uiDelegate = webkitUIDelegate + + didRegisterBridgeComponentsObservationToken = NotificationCenter.default.observeDidRegisterBridgeComponents { + Bridge.initialize(session.webView) + Bridge.initialize(modalSession.webView) + } + } + + deinit { + NotificationCenter.default.removeObservation(didRegisterBridgeComponentsObservationToken) } // MARK: Private @@ -146,6 +155,7 @@ public class Navigator { private let navigatorDelegate = DefaultNavigatorDelegate() private var backgroundTerminatedWebViewSessions = [Session]() private var appInBackground = false + private var didRegisterBridgeComponentsObservationToken: NSObjectProtocol? private func controller(for proposal: VisitProposal) -> UIViewController? { switch delegate.handle(proposal: proposal) {