Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get WebView's URL at runtime #35

Merged
merged 2 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions strada/src/main/kotlin/dev/hotwire/strada/BridgeDelegate.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class BridgeDelegate<D : BridgeDestination>(
internal var bridge: Bridge? = null
private var destinationIsActive: Boolean = false
private val initializedComponents = hashMapOf<String, BridgeComponent<D>>()
private val resolvedLocation: String
get() = bridge?.webView?.url ?: location

val activeComponents: List<BridgeComponent<D>>
get() = initializedComponents.map { it.value }.takeIf { destinationIsActive }.orEmpty()
Expand All @@ -35,7 +37,7 @@ class BridgeDelegate<D : BridgeDestination>(
bridge?.load()
}
} else {
logWarning("bridgeNotInitializedForWebView", location)
logWarning("bridgeNotInitializedForWebView", resolvedLocation)
}
}

Expand All @@ -58,7 +60,7 @@ class BridgeDelegate<D : BridgeDestination>(
}

internal fun bridgeDidReceiveMessage(message: Message): Boolean {
return if (destinationIsActive && location == message.metadata?.url) {
return if (destinationIsActive && resolvedLocation == message.metadata?.url) {
logEvent("bridgeDidReceiveMessage", message.toString())
getOrCreateComponent(message.component)?.didReceive(message)
true
Expand All @@ -75,20 +77,20 @@ class BridgeDelegate<D : BridgeDestination>(
// Lifecycle events

override fun onStart(owner: LifecycleOwner) {
logEvent("bridgeDestinationDidStart", location)
logEvent("bridgeDestinationDidStart", resolvedLocation)
destinationIsActive = true
activeComponents.forEach { it.didStart() }
}

override fun onStop(owner: LifecycleOwner) {
activeComponents.forEach { it.didStop() }
destinationIsActive = false
logEvent("bridgeDestinationDidStop", location)
logEvent("bridgeDestinationDidStop", resolvedLocation)
}

override fun onDestroy(owner: LifecycleOwner) {
destinationIsActive = false
logEvent("bridgeDestinationDidDestroy", location)
logEvent("bridgeDestinationDidDestroy", resolvedLocation)
}

// Retrieve component(s) by type
Expand Down
34 changes: 34 additions & 0 deletions strada/src/test/kotlin/dev/hotwire/strada/BridgeDelegateTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,40 @@ class BridgeDelegateTest {
assertNotNull(delegate.component<TestData.OneBridgeComponent>())
}

@Test
fun bridgeDidReceiveMessageForLocationWithTrailingSlash() {
whenever(webView.url).thenReturn("https://37signals.com/")

val message = Message(
id = "1",
component = "one",
event = "connect",
metadata = Metadata("https://37signals.com/"),
jsonData = """{"title":"Page-title","subtitle":"Page-subtitle"}"""
)

assertNull(delegate.component<TestData.OneBridgeComponent>())
assertEquals(true, delegate.bridgeDidReceiveMessage(message))
assertNotNull(delegate.component<TestData.OneBridgeComponent>())
}

@Test
fun bridgeDidReceiveMessageForResolvedLocation() {
whenever(webView.url).thenReturn("https://37signals.com/new_url")

val message = Message(
id = "1",
component = "one",
event = "connect",
metadata = Metadata("https://37signals.com/new_url"),
jsonData = """{"title":"Page-title","subtitle":"Page-subtitle"}"""
)

assertNull(delegate.component<TestData.OneBridgeComponent>())
assertEquals(true, delegate.bridgeDidReceiveMessage(message))
assertNotNull(delegate.component<TestData.OneBridgeComponent>())
}

@Test
fun bridgeDidReceiveMessageIgnored() {
val message = Message(
Expand Down
Loading