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

aoriani

10/26/2023, 11:04 PM
How much of a crime would be to do something like
val _composables_  = _MutableStateFlow_<List<@Composable () -> Unit>>(_emptyList_())
?
😁 1
p

Pablichjenkov

10/26/2023, 11:07 PM
I don't see any wrong doing, at first sight
j

jw

10/26/2023, 11:15 PM
Seems fine. You probably don't need a state flow, though. Return
@Composable { for (lambda in lambdas) lambda()
and encapsulate the state list. Compose will recompose when it changes, and your consumers only get a single composable lambda to put wherever. No collection or iteration at the consumption site, just a single invocation
👍 1
a

aoriani

10/26/2023, 11:25 PM
Thanks. I am currently working on a screen whose content is defined by the backend, so I trying to figure how to convert the BE output to composables.
j

jw

10/26/2023, 11:27 PM
Mutating a MutableStateList of Compose lambdas would be a good solution. No need for a state flow. Mutate the list and Compose will react to its changes
a

aoriani

10/26/2023, 11:29 PM
Thanks @jw
p

Pablichjenkov

10/27/2023, 1:26 AM
Oh interesting, I am working on a similar concept. The only way I have found is either using polymorphism , sort of:
Copy code
interface Component {
  @Composable
  fun View()
}
Or without polymorphism, having a huge Composable mapper function that basically does this:
Copy code
@Composable
fun genericRenderer(
serverComponentMetadata ComponentMetadata
) {
when(serverComponentMetadata) {
  Component1 -> Component1Composable(
serverComponentMetadata
)
  Component2 -> Component2Composable(
serverComponentMetadata
)

...
}
}
But this map tends to grow huge and also calling this function pretty much everywhere sounds repetitive. Haven't found any other alternatives.
2 Views