From 591b08aad268135266627ef8bf522dbe73a54f20 Mon Sep 17 00:00:00 2001 From: Jay Ohms Date: Mon, 18 Mar 2024 17:19:03 -0400 Subject: [PATCH 1/2] Automatically initialize the Bridge for each WebView when bridge components are registered --- .../core/turbo/session/TurboSessionNavHostFragment.kt | 6 ++++++ .../dev/hotwire/demo/main/MainSessionNavHostFragment.kt | 4 ---- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/core/src/main/kotlin/dev/hotwire/core/turbo/session/TurboSessionNavHostFragment.kt b/core/src/main/kotlin/dev/hotwire/core/turbo/session/TurboSessionNavHostFragment.kt index 58064fa..6e6e524 100644 --- a/core/src/main/kotlin/dev/hotwire/core/turbo/session/TurboSessionNavHostFragment.kt +++ b/core/src/main/kotlin/dev/hotwire/core/turbo/session/TurboSessionNavHostFragment.kt @@ -5,6 +5,7 @@ import android.os.Bundle import androidx.appcompat.app.AppCompatActivity import androidx.navigation.fragment.NavHostFragment import androidx.navigation.fragment.findNavController +import dev.hotwire.core.bridge.Bridge import dev.hotwire.core.config.Hotwire import dev.hotwire.core.turbo.nav.TurboNavDestination import dev.hotwire.core.turbo.nav.TurboNavGraphBuilder @@ -55,6 +56,11 @@ abstract class TurboSessionNavHostFragment : NavHostFragment() { */ open fun onSessionCreated() { session.pathConfiguration.load(Hotwire.config.pathConfigurationLocation) + + // Initialize bridge with new WebView instance + if (Hotwire.registeredBridgeComponentFactories.isNotEmpty()) { + Bridge.initialize(session.webView) + } } /** diff --git a/demo/src/main/kotlin/dev/hotwire/demo/main/MainSessionNavHostFragment.kt b/demo/src/main/kotlin/dev/hotwire/demo/main/MainSessionNavHostFragment.kt index 0089c78..cf68b7d 100644 --- a/demo/src/main/kotlin/dev/hotwire/demo/main/MainSessionNavHostFragment.kt +++ b/demo/src/main/kotlin/dev/hotwire/demo/main/MainSessionNavHostFragment.kt @@ -1,6 +1,5 @@ package dev.hotwire.demo.main -import dev.hotwire.core.bridge.Bridge import dev.hotwire.core.turbo.session.TurboSessionNavHostFragment import dev.hotwire.demo.util.HOME_URL import dev.hotwire.demo.util.initDayNightTheme @@ -16,8 +15,5 @@ class MainSessionNavHostFragment : TurboSessionNavHostFragment() { // Configure WebView session.webView.initDayNightTheme() - - // Initialize Strada bridge with new WebView instance - Bridge.initialize(session.webView) } } From 92f8c87cfbe6a06fd918e2ffa55da4b9e020bc61 Mon Sep 17 00:00:00 2001 From: Jay Ohms Date: Mon, 18 Mar 2024 17:23:54 -0400 Subject: [PATCH 2/2] Mark Bridge.initialize() as internal --- core/src/main/kotlin/dev/hotwire/core/bridge/Bridge.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/main/kotlin/dev/hotwire/core/bridge/Bridge.kt b/core/src/main/kotlin/dev/hotwire/core/bridge/Bridge.kt index 3d98cc6..f64ab38 100644 --- a/core/src/main/kotlin/dev/hotwire/core/bridge/Bridge.kt +++ b/core/src/main/kotlin/dev/hotwire/core/bridge/Bridge.kt @@ -120,7 +120,7 @@ class Bridge internal constructor(webView: WebView) { companion object { private val instances = mutableListOf() - fun initialize(webView: WebView) { + internal fun initialize(webView: WebView) { if (getBridgeFor(webView) == null) { initialize(Bridge(webView)) }