Skip to content

Commit

Permalink
Merge pull request #56 from hotwired/remove-strada-references
Browse files Browse the repository at this point in the history
Remove Strada references
  • Loading branch information
jayohms authored Sep 25, 2024
2 parents fb5e193 + 5db9cf8 commit 4a95c6f
Show file tree
Hide file tree
Showing 12 changed files with 28 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
registerAdapter() {
this.adapterIsRegistered = true

if (this.isStradaAvailable) {
if (this.isBridgeAvailable) {
this.webBridge.setAdapter(this)
} else {
document.addEventListener("web-bridge:ready", () => this.webBridge.setAdapter(this))
Expand All @@ -42,7 +42,7 @@
notifyBridgeOfSupportedComponentsUpdate() {
this.supportedComponentsUpdated()

if (this.isStradaAvailable) {
if (this.isBridgeAvailable) {
this.webBridge.adapterDidUpdateSupportedComponents()
}
}
Expand All @@ -53,7 +53,7 @@

// Reply to web with message
replyWith(message) {
if (this.isStradaAvailable) {
if (this.isBridgeAvailable) {
this.webBridge.receive(JSON.parse(message))
}
}
Expand All @@ -70,20 +70,20 @@
// Native handler

ready() {
StradaNative.bridgeDidInitialize()
BridgeComponentsNative.bridgeDidInitialize()
}

supportedComponentsUpdated() {
StradaNative.bridgeDidUpdateSupportedComponents()
BridgeComponentsNative.bridgeDidUpdateSupportedComponents()
}

postMessage(message) {
StradaNative.bridgeDidReceiveMessage(message)
BridgeComponentsNative.bridgeDidReceiveMessage(message)
}

// Web global

get isStradaAvailable() {
get isBridgeAvailable() {
return window.Strada
}

Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions core/src/main/kotlin/dev/hotwire/core/bridge/Bridge.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import dev.hotwire.core.logging.logEvent
import kotlinx.serialization.json.JsonElement
import java.lang.ref.WeakReference

// These need to match whatever is set in strada.js
// These need to match whatever is set in bridge_components.js
private const val bridgeGlobal = "window.nativeBridge"
private const val bridgeJavascriptInterface = "StradaNative"
private const val bridgeJavascriptInterface = "BridgeComponentsNative"

@Suppress("unused")
class Bridge internal constructor(webView: WebView) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ abstract class BridgeComponent<in D : BridgeDestination>(
* reply will be ignored.
*/
inline fun <reified T> replyTo(event: String, data: T): Boolean {
return replyTo(event, jsonData = StradaJsonConverter.toJson(data))
return replyTo(event, jsonData = BridgeComponentJsonConverter.toJson(data))
}

private fun reply(message: Message): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@ import dev.hotwire.core.logging.logError
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json

abstract class StradaJsonConverter {
abstract class BridgeComponentJsonConverter {
companion object {
const val NO_CONVERTER =
"A Strada.config.jsonConverter must be set to encode or decode json"
"A Hotwire.config.jsonConverter must be set to encode or decode json"

const val INVALID_CONVERTER =
"The configured json converter must implement a StradaJsonTypeConverter " +
"The configured json converter must implement a BridgeComponentJsonTypeConverter " +
"or use the provided KotlinXJsonConverter."

inline fun <reified T> toObject(jsonData: String): T? {
val converter = requireNotNull(Hotwire.config.jsonConverter) { NO_CONVERTER }

return when (converter) {
is KotlinXJsonConverter -> converter.toObject<T>(jsonData)
is StradaJsonTypeConverter -> converter.toObject(jsonData, T::class.java)
is BridgeComponentJsonTypeConverter -> converter.toObject(jsonData, T::class.java)
else -> throw IllegalStateException(INVALID_CONVERTER)
}
}
Expand All @@ -29,19 +29,19 @@ abstract class StradaJsonConverter {

return when (converter) {
is KotlinXJsonConverter -> converter.toJson(data)
is StradaJsonTypeConverter -> converter.toJson(data, T::class.java)
is BridgeComponentJsonTypeConverter -> converter.toJson(data, T::class.java)
else -> throw IllegalStateException(INVALID_CONVERTER)
}
}
}
}

abstract class StradaJsonTypeConverter : StradaJsonConverter() {
abstract class BridgeComponentJsonTypeConverter : BridgeComponentJsonConverter() {
abstract fun <T> toObject(jsonData: String, type: Class<T>): T?
abstract fun <T> toJson(data: T, type: Class<T>): String
}

class KotlinXJsonConverter : StradaJsonConverter() {
class KotlinXJsonConverter : BridgeComponentJsonConverter() {
val json = Json { ignoreUnknownKeys = true }

inline fun <reified T> toObject(jsonData: String): T? {
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/kotlin/dev/hotwire/core/bridge/Message.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ data class Message constructor(
event: String = this.event,
data: T
): Message {
return replacing(event, StradaJsonConverter.toJson(data))
return replacing(event, BridgeComponentJsonConverter.toJson(data))
}

inline fun <reified T> data(): T? {
return StradaJsonConverter.toObject(jsonData)
return BridgeComponentJsonConverter.toObject(jsonData)
}
}

Expand Down
2 changes: 1 addition & 1 deletion core/src/main/kotlin/dev/hotwire/core/bridge/Repository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import android.content.Context

internal class Repository {
fun getUserScript(context: Context): String {
return context.assets.open("js/strada.js").use {
return context.assets.open("js/bridge_components.js").use {
String(it.readBytes())
}
}
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/kotlin/dev/hotwire/core/config/HotwireConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import android.content.Context
import android.webkit.WebView
import dev.hotwire.core.bridge.BridgeComponent
import dev.hotwire.core.bridge.BridgeComponentFactory
import dev.hotwire.core.bridge.StradaJsonConverter
import dev.hotwire.core.bridge.BridgeComponentJsonConverter
import dev.hotwire.core.turbo.config.PathConfiguration
import dev.hotwire.core.turbo.http.HotwireHttpClient
import dev.hotwire.core.turbo.offline.OfflineRequestHandler
Expand All @@ -24,7 +24,7 @@ class HotwireConfig internal constructor() {
* object in received messages and to encode a data object back to json to
* reply with a custom message back to the web.
*/
var jsonConverter: StradaJsonConverter? = null
var jsonConverter: BridgeComponentJsonConverter? = null

/**
* Experimental: API may be removed, not ready for production use.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ open class HotwireWebView @JvmOverloads constructor(

internal fun installBridge(onBridgeInstalled: () -> Unit) {
val script = "window.turboNative == null"
val bridge = context.contentFromAsset("js/turbo_bridge.js")
val bridge = context.contentFromAsset("js/turbo.js")

runJavascript(script) { s ->
if (s?.toBoolean() == true) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class BridgeComponentTest {

assertThatThrownBy { component.replyTo("connect", MessageData(title = "Page-title")) }
.isInstanceOf(IllegalArgumentException::class.java)
.hasMessage(StradaJsonConverter.NO_CONVERTER)
.hasMessage(BridgeComponentJsonConverter.NO_CONVERTER)
}

@Test
Expand Down
6 changes: 3 additions & 3 deletions core/src/test/kotlin/dev/hotwire/core/bridge/MessageTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class MessageTest {

assertThatThrownBy { message.replacing(data = data) }
.isInstanceOf(IllegalArgumentException::class.java)
.hasMessage(StradaJsonConverter.NO_CONVERTER)
.hasMessage(BridgeComponentJsonConverter.NO_CONVERTER)
}

@Test
Expand All @@ -131,13 +131,13 @@ class MessageTest {

assertThatThrownBy { message.replacing(data = data) }
.isInstanceOf(IllegalStateException::class.java)
.hasMessage(StradaJsonConverter.INVALID_CONVERTER)
.hasMessage(BridgeComponentJsonConverter.INVALID_CONVERTER)
}

@Serializable
private class MessageData(val title: String, val subtitle: String)

private class InvalidMessageData()

private class InvalidJsonConverter : StradaJsonConverter()
private class InvalidJsonConverter : BridgeComponentJsonConverter()
}
2 changes: 1 addition & 1 deletion demo/src/main/assets/json/configuration.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
{
"patterns": [
"/signin$",
"/strada-form$"
"/bridge-form$"
],
"properties": {
"context": "modal",
Expand Down

0 comments on commit 4a95c6f

Please sign in to comment.