square / wire

gRPC and protocol buffers for Android, Kotlin, Swift and Java.
https://square.github.io/wire/
Apache License 2.0
4.24k stars 571 forks source link

Compose immutable annotations #2127

Open matejdro opened 2 years ago

matejdro commented 2 years ago

When displaying UI using Jetpack Compose, data model must have @Immutable annotation to achieve the best performance: https://developer.android.com/reference/kotlin/androidx/compose/runtime/Immutable

Could you add an option to include such annotation on all generated wire classes?

oldergod commented 2 years ago

Wire doesn't generate data classes.

JakeWharton commented 2 years ago

The annotation works on any type

takahirom commented 1 year ago

Not sure if this is really possible. If this feature is enabled, it will be possible to draw the contents of the protobuf in Compose. For example, using Zipline and Molecule, we would be able to use protobufs to create the content we want to display in the UI and then display it in the UI. The reason why we want to use protobuf is to ensure compatibility between versions. It would be best to share a design system like Redwood, but I personally find it a bit daunting.

JakeWharton commented 1 year ago

The right fix here is to contribute our base class to the Compose compiler in the list of implicitly stable/immutable types.

takahirom commented 1 year ago

Thanks for your advice. I think we can add com.squareup.wire.Message here. The method for it is already there. 👀 https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:compose/compiler/compiler-hosted/src/main/java/androidx/compose/compiler/plugins/kotlin/analysis/Stability.kt;l=222?q=stableTypes&ss=androidx%2Fplatform%2Fframeworks%2Fsupport

takahirom commented 1 year ago

Perhaps the person reporting that this type is stable/immutable should not be an outsider like me. The reason is that reviewers cannot decide whether they should believe me or not.

oldergod commented 1 year ago

Interesting, that'd be sweet to get it in.

takahirom commented 1 year ago

I think it is really interesting to be able to do something like a server driven UI while having state within the client.

takahirom commented 1 year ago

@oldergod Could you provide the Message type to AOSP? Let me know if there is anything I can do to help.

oldergod commented 1 year ago

I've created a change. Let's see how it goes.

https://android-review.googlesource.com/c/platform/frameworks/support/+/2456088

Update: The above PR is now closed.

oldergod commented 8 months ago

Related: #2794

But on top of that, since Compose compiler 1.5.5, one can locally configure stable types as so:

    plugins.withId("org.jetbrains.compose") {
        tasks.withType<KotlinCompile> {
            val configPath = rootDir.resolve("my_compose_compiler_config.conf").path
            compilerOptions.freeCompilerArgs.addAll(
                "-P", "plugin:androidx.compose.compiler.plugins.kotlin:stabilityConfigurationPath=$configPath",
            )
        }
    }

And my_compose_compiler_config.conf would be

com.squareup.wire.Message
com.squareup.wire.WireEnum