https://kotlinlang.org logo
#compose
Title
# compose
k

Koneko Toujou

02/06/2023, 1:20 PM
also can Compose be used in a scriptable way? similar to View's programmable api
Copy code
fun onAdd(v: ViewGroup, child: View) {
    v.addView(child);
}
but for Composables instead
j

Jakub Syty

02/06/2023, 1:22 PM
The whole purpose of compose is to be declarative instead of imperative. So you would be fighting yourself here
In compose you just do for example
Copy code
Column {
    Composable1()
    Composable2()
}
t

Trym Nilsen

02/06/2023, 1:23 PM
Create a state that you can imperatively modify and then build your ui declaratively based on the latest current state
k

Koneko Toujou

02/06/2023, 1:23 PM
so for example, language bindings CANNOT be created for compose to construct composables in said language?
j

Jakub Syty

02/06/2023, 1:24 PM
what do you mean android bindings?
k

Koneko Toujou

02/06/2023, 1:24 PM
eg, python, lua, ruby, ect
j

Jakub Syty

02/06/2023, 1:25 PM
nope, compose is stricly kotlin technology
since it relies on multiple kotlin constructs and the most important - kotlin compiler plugin
k

Koneko Toujou

02/06/2023, 1:25 PM
hmm
j

Jakub Syty

02/06/2023, 1:26 PM
you could generate kotlin code in other languages and then compile it, but.. why 😄
for now (and i doubt it will ever change) you can only write compose ui in kotlin
k

Koneko Toujou

02/06/2023, 1:27 PM
could a function return a composable?
eg
Copy code
fun retText(s: String): Text {
    return Text(text: s)
}
j

Jakub Syty

02/06/2023, 1:28 PM
i'm not sure if it's possible but this seems like hard antipattern
k

Koneko Toujou

02/06/2023, 1:28 PM
damn
f

Filip Wiesner

02/06/2023, 1:33 PM
could a function return a composable?
eg
```fun retText(s: String): Text {
return Text(text: s)
}```
"Composable" is not an object - it's a function. So you could do
Copy code
fun retText(s: String): @Composable () -> Unit = @Composable { Text(s) }
k

Koneko Toujou

02/06/2023, 1:35 PM
oh ok
Copy code
fun retText(s: String): @Composable () -> Unit = @Composable { Text(s) }

fun retBox(): @Composable () -> Unit = @Composable { Box() }

fun addTextToBox(box: @Composable () -> Unit, box: @Composable () -> Unit): @Composable () -> Unit = @Composable { box(); text(); }
could we do something like this?
j

Jakub Syty

02/06/2023, 1:39 PM
while that should work i don't know how the kotlin compiler can build recomposition avoidance around that
the function will return new composable at each recomposition
k

Koneko Toujou

02/06/2023, 1:40 PM
tho i have no idea if it is possible to actually add said text to said box as if
Copy code
Box {
    Text(s)
}
f

Filip Wiesner

02/06/2023, 1:41 PM
It would be better to give as a hint of what exactly you are trying to do. What is the end-goal
k

Koneko Toujou

02/06/2023, 1:42 PM
basically i want to create a scriptable UI
f

Filip Wiesner

02/06/2023, 1:43 PM
What does that mean? 😄
j

Jakub Syty

02/06/2023, 1:43 PM
ok, i think you mean something like generating ui from json?
k

Koneko Toujou

02/06/2023, 1:44 PM
like...
Copy code
val box = box()
val text = text("hello")
box.add(text)
display(box) // displays the constructed [box] object in compose kotlin
f

Filip Wiesner

02/06/2023, 1:44 PM
Yeah, I would answer the same Jakub did 😄
The whole purpose of compose is to be declarative instead of imperative. So you would be fighting yourself here
j

Jakub Syty

02/06/2023, 1:45 PM
this seems like just fighting the declarative ui
why 😄
i understand json defined views for something like lists (https://medium.com/mobile-app-development-publication/jetpack-compose-enables-json-defined-view-layout-b1f8a0d5df8c), but why would you do imperative scripting over declarative framework?
k

Koneko Toujou

02/06/2023, 1:46 PM
because Android google play store disallows loading of pre-compiled android resources (eg, JAR, AAR, DEX) from internet i assume this also includes (.kotlin/.java files to compile)/.class files to load
j

Jakub Syty

02/06/2023, 1:47 PM
look at the link i send - this is the best you can get
but doing whole ui, with nesting and so on - i don't think you can do that in performant way
and this all seems so unnecessary, why go native if you want to have fully online declared ui? Just embed WebView and you don't have to reinvent the wheel
the performance will be probably even better this way
k

Koneko Toujou

02/06/2023, 1:49 PM
because such will be downloaded by the user and then stored locally to load up at any time
j

Jakub Syty

02/06/2023, 1:49 PM
You can (and should) cache html
that's not my point
you want to reinvent online scripting language
and it exists
k

Koneko Toujou

02/06/2023, 1:51 PM
would web compose be as fast as jvm compose
assuming all files are stored locally
and would such require an active internet connection
j

Jakub Syty

02/06/2023, 1:52 PM
look, creating all composables dynamically like you wanted to basically destroys all optimizations that compose offers. Not to mention - what about state? How do you want to handle that?
compose web is just a front-end framework, like any other
it would be probably faster than this "dynamic native compose" you think about
i just don't see your idea as feasible, both in state management and performance ways
k

Koneko Toujou

02/06/2023, 1:55 PM
alright
google play only allows downloading and execution of javascript for web browser applications, right?
eg https://www.theregister.com/2021/07/29/google_play_python_javascript/
Starting October 15, 2021, Google said, "We're clarifying the Device and Network Abuse policy to prohibit apps or SDKs with interpreted languages (e.g., JavaScript) loaded at run time from violating any Google Play policies."
action against apps that "interfere with, disrupt, damage, or access in an unauthorized manner the user's device, other devices or computers, servers, networks, application programming interfaces (APIs), or services."
Fetching executable code from sources other than Google Play is also disallowed, except for code running in a virtual machine that has limited access to Android APIs, like JavaScript running in a WebView or browser.
specifically https://support.google.com/googleplay/android-developer/answer/9888379
An app distributed via Google Play may not modify, replace or update itself using any method other than Google Play's update mechanism. Likewise, an app may not download executable code (e.g. dex, JAR, .so files) from a source other than Google Play. This restriction does not apply to code that runs in a virtual machine or an interpreter where either provides indirect access to Android APIs (such as JavaScript in a WebView or browser).
Apps or third-party code (e.g. SDKs) with interpreted languages (JavaScript, Python, Lua, etc.) loaded at run time (e.g. not packaged with the app) must not allow potential violations of Google Play policies.
e

eygraber

02/06/2023, 2:17 PM
It's not exactly what you're looking for, but I think it'll probably suit your underlying use case
k

Koneko Toujou

02/06/2023, 3:28 PM
alright
j

Jakub Syty

02/06/2023, 3:29 PM
redwood is about using "crossplatform" compose, not using other language
it's not a magic layer that can be passed through wire to mobile devices
it's just a way to share compose code in KMM
e

eygraber

02/06/2023, 3:30 PM
It is with Zipline
They have a sample that shows this
j

Jakub Syty

02/06/2023, 3:32 PM
well, yes, so the zipline is
i was talking about redwood on its own
h

hfhbd

02/06/2023, 3:33 PM
Yes. But you could also simple iterate any list etc of data to create views dynamically. I mean, this is the whole purpose of UI...
j

Jakub Syty

02/06/2023, 3:33 PM
but still, how would you handle state in zipline/redwood combo?
yes, that's my whole argument
things like zipline and redwood are for really big players
huge investment, that probably does not make sense for smaller apps
e

eygraber

02/06/2023, 3:36 PM
I've done proof of concepts with both and I found a lot of upside to the redwood approach. Only downside is that the iOS team didn't want to use it 😅 I wouldn't categorize my company as a really big player.
j

Jakub Syty

02/06/2023, 3:36 PM
it's definitely relative 😄
while i like redwood idea on it's own
i don't really see much value in ui declared on server side
maybe in few cases, like some marketing screen, but only for simple things
j

jw

02/06/2023, 3:41 PM
redwood is not for server-side
even redwood+zipline isn't server-side, it's client-side code shipped faster than app store releases
if you want server-side or imperative in another language then build a model that represents UI, iterate over that model in Compose, put a big
when
statement, and then have branches for each node type. it's the same thing you'd do with views, except instead of calling
addView
you call a composable. The model objects drive the logic, whether you end up calling views, composables, println, or anything else is mostly irrelevant.
e

eygraber

02/06/2023, 3:50 PM
Couldn't you point Zipline at a manifest that is generated from a dynamic endpoint and turn off caching 😝
j

jw

02/06/2023, 6:04 PM
yes but that would involve running the Kotlin compiler in the request path so... better bump those timeouts really high
16 Views