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

Arjan van Wieringen

05/28/2022, 6:06 AM
Just a general question. I want to make a small generic helper library for something. This library will require some observable behavior. Without using Compose I'd use a simple listener in order to have no reliance on any other lib. However, I was thinking: Does it make sense to expose the observable variables as
State<T>
variables? So consumers of the library can use this in their composable scopes? So the actual question is: Does it make sense to put
State<T>
inside generic libraries? This creates a dependency on compose runtime, but this is my opinion nothing different than relying on RxJava. And to turn it into something like Flow we only need to use
snapshotFlow
I believe.
s

ste

05/28/2022, 8:23 AM
Did you consider relying on
Flow
?
a

Arjan van Wieringen

05/28/2022, 9:03 AM
No, not really. IMHO State behaves like a nice reactive primitive inside coroutines, so I'd use that. You can easily derive a Flow from State using snapshotFlow.
m

Michael Paus

05/28/2022, 11:08 AM
I prefer exactly the opposite so that my models stay independent of any Compose dependencies. IMHO, using StateFlows in your model and observe them in your compose code is equally easy but much cleaner.
a

Arjan van Wieringen

05/28/2022, 11:37 AM
@Michael Paus Hmm... you are right. I forgot there is a
collectAsState
function.