Skip to content

Commit

Permalink
feat: support for Binding
Browse files Browse the repository at this point in the history
  • Loading branch information
Vehmloewff committed Oct 2, 2024
1 parent dbd8ba6 commit 9a3609d
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 36 deletions.
26 changes: 4 additions & 22 deletions app/src/main/java/com/example/objectionapp/Bridge.kt
Original file line number Diff line number Diff line change
Expand Up @@ -71,27 +71,10 @@ class Bridge(private var logger: Logger, private var session: Session) : Corouti
)
}

fun emitNullEvent(objectId: String, key: String, onComplete: () -> Unit) {
fun emitBindingUpdate(key: String, data: JsonElement, onComplete: () -> Unit) {
sendMessage(
OutgoingMessage.EmitEvent(
OutgoingMessage.EmitBindingUpdate(
requestId = listenForAcknowledgement(onComplete),
objectId = objectId,
key = key,
data = JsonNull
)
)
}

fun emitEvent(
objectId: String,
key: String,
data: JsonElement,
onComplete: () -> Unit
) {
sendMessage(
OutgoingMessage.EmitEvent(
requestId = listenForAcknowledgement(onComplete),
objectId = objectId,
key = key,
data = data
)
Expand Down Expand Up @@ -209,10 +192,9 @@ sealed class OutgoingMessage {
) : OutgoingMessage()

@Serializable
@SerialName("emit_event")
data class EmitEvent(
@SerialName("emit_binding_update")
data class EmitBindingUpdate(
@SerialName("request_id") val requestId: String,
@SerialName("object_id") val objectId: String,
val key: String,
val data: JsonElement
) : OutgoingMessage()
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/java/com/example/objectionapp/Providers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ fun useObject(id: String?): Object? {
return obj
}

@Composable
fun useLogger(scope: String): Logger {
return useController().logger.scope(scope)
}

@Composable
fun useDefaultTheme(): Theme {
val obj = useObject(defaultThemeId) ?: throw Exception("No object exists for '$defaultThemeId'")
Expand Down
42 changes: 41 additions & 1 deletion app/src/main/java/com/example/objectionapp/Schema.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.example.objectionapp

import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.Color
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.SerialInfo
import kotlinx.serialization.SerialName
Expand Down Expand Up @@ -33,13 +35,34 @@ annotation class AnyObjectReference
@OptIn(ExperimentalSerializationApi::class)
@SerialInfo
@Target(AnnotationTarget.CLASS)
annotation class IsColor
private annotation class IsColor

@OptIn(ExperimentalSerializationApi::class)
@SerialInfo
@Target(AnnotationTarget.CLASS)
private annotation class IsBinding

@OptIn(ExperimentalSerializationApi::class)
@SerialInfo
@Target(AnnotationTarget.CLASS)
annotation class ContentKey(val key: String)

@Serializable
@IsBinding
data class Binding<T>(
val key: String, val child: T
)

@Serializable
@IsColor
data class ColorData(
val red: Int, val green: Int, val blue: Int, val alpha: Int
) {
fun intoColor(): Color {
return Color(red = red, green = green, blue = blue, alpha = alpha)
}
}

fun getSchema(klass: KClass<*>): Schema {
return Schema(getItemSchema(serialDescriptor(klass.createType())))
}
Expand Down Expand Up @@ -126,6 +149,19 @@ private fun getClassSchema(descriptor: SerialDescriptor): ItemSchema {

for (annotation in descriptor.annotations) {
if (annotation is IsColor) return ItemSchema.ColorSchema
if (annotation is IsBinding) {
var childDescriptor: SerialDescriptor? = null

for (childIndex in 0..<descriptor.elementsCount) {
if (descriptor.getElementName(childIndex) === "child") {
childDescriptor = descriptor.getElementDescriptor(childIndex)
}
}

if (childDescriptor == null) throw Exception("Binding class did not have a 'child' property")

return ItemSchema.BindingSchema(child = getItemSchema(childDescriptor))
}
}

for (childIndex in 0..<descriptor.elementsCount) {
Expand Down Expand Up @@ -241,6 +277,10 @@ sealed class ItemSchema {
data class ReferenceSchema(
@SerialName("expected_top_level_variant") val expectedTopLevelVariant: String?
) : ItemSchema()

@Serializable
@SerialName("binding")
data class BindingSchema(val child: ItemSchema) : ItemSchema()
}

@Serializable
Expand Down
12 changes: 0 additions & 12 deletions app/src/main/java/com/example/objectionapp/Shared.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,3 @@ package com.example.objectionapp
import androidx.compose.ui.graphics.Color
import kotlinx.serialization.Serializable

@Serializable
@IsColor
data class ColorData(
val red: Int,
val green: Int,
val blue: Int,
val alpha: Int
) {
fun intoColor(): Color {
return Color(red = red, green = green, blue = blue, alpha = alpha)
}
}
2 changes: 1 addition & 1 deletion app/src/main/java/com/example/objectionapp/StandardIcon.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fun StandardIcon(
modifier: Modifier = Modifier,
tint: Color = LocalContentColor.current,
) {
val logger = useController().logger.scope("StandardIcon")
val logger = useLogger("StandardIcon")
val theme = useDefaultTheme()
val icon: ImageVector? = remember(name) {
try {
Expand Down

0 comments on commit 9a3609d

Please sign in to comment.