Hey everyone! .. I am trying to a build a server-d...
# compose
y
Hey everyone! .. I am trying to a build a server-driven UI which supports declaring the layout at run-time, from the server, instead of at compile-time. Usually at compile-time I would use
Row
,
Column
or
Box
depending on what I want. You get additional modifiers based on the scope you are in..i.e
BoxScope.Modifier.matchsParentSize()
,
RowScope.Modifier.weight
..etc. I am looking into a way to get these values from the server. Since I don’t know which layout will be rendering at run-time.. I can’t have a scope. I have looked into something like
Copy code
fun MyCustomLayout(someLayoutEnum: LayoutTypeEnum, content: @Composable () -> Unit) {
// do some work here
}
Which works to define the layout at run-time but you would still be stuck not knowing the scope and not being able to use
RowScope.Modifier.weight
for example. What I am going with right now is copying the Jetpack compose team logic of using a MeasurePolicy + Layout, which is either internal or private, and modifying it to accept a list of modifiers for each child.
Copy code
val measurePolicy = rowMeasurePolicy(horizontalArrangement,verticalAlignment, modifiers) // This could be columnMeasurePolicy or boxMeasurePolicy
    Layout(
        content = { content() },
        measurePolicy = measurePolicy,
        modifier = modifier
    )
My question is, does anyone know a better way of doing this?
c
If I'm understanding correctly... I would have three root layouts then. One for row, col, and box. and then depending on the type that you get from your server, you'll know what type it is when you deserilize it and then just map that type to one of the three composables you just created.
y
Yeah that’s one part. The second part is the content of the composable.. for example if the server is sending Row ' -> Text(weight = 1) ’-> Text() We won’t be able to use
Modifier.weight
since we don’t know which scope to use of
BoxScope
,
RowScope
or
ColumnScope
1
c
Gotcha. I didn't know the SDUI was able to get that granular.