Just a general question. I want to make a small ge...
# compose
a
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
Did you consider relying on
Flow
?
a
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
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
@Michael Paus Hmm... you are right. I forgot there is a
collectAsState
function.