Alexander Maryanovsky
03/03/2022, 2:17 PMStateFlow
rather than just state values (e.g. by by mutableStateOf
)?Filip Wiesner
03/03/2022, 2:18 PMAlexander Maryanovsky
03/03/2022, 2:22 PMFilip Wiesner
03/03/2022, 2:24 PMYou should use the thing that most suits your project and codestyle😄 Use whatever you feel like using. There is no "problem" with either approach
Alexander Maryanovsky
03/03/2022, 2:31 PM_foo
uglinessFilip Wiesner
03/03/2022, 2:38 PMAlexander Maryanovsky
03/03/2022, 2:50 PMMichael Paus
03/03/2022, 2:52 PMStateFlow
is a pure Kotlin concept and does not depend on the compose framework but mutableStateOf
is not. So if you want to keep your models pure it is better to follow the guidelines. In general it is not a good idea trying to be smarter than the inventors of a technology.Filip Wiesner
03/03/2022, 2:57 PMCan you point me to that? Seems strange and would cause unnecessary recompositions.https://developer.android.com/jetpack/guide/ui-layer#define-ui-state
Alexander Maryanovsky
03/03/2022, 3:00 PMFilip Wiesner
03/03/2022, 3:01 PMOne difference is thatIt is suggested in Adroid guidelines that you can use Compose State when building your app with Compose UI:is a pure Kotlin concept and does not depend on the compose framework butStateFlow
is not. So if you want to keep your models pure it is better to follow the guidelines.mutableStateOf
In Jetpack Compose apps, you can use Compose's observable State APIs such asormutableStateOf
for the exposure of UI state. Any type of observable data holder such assnapshotFlow
orStateFlow
that you see in this guide can be easily consumed in Compose using the appropriate extensions.LiveData
Michael Paus
03/03/2022, 3:07 PMAlexander Maryanovsky
03/03/2022, 3:15 PMStateFlow
in your viewmodel, you need to convert it to a compose state via StateFlow.collectAsState
to be able to use in in compose UI.
If you have compose state in your viewmodel, you need to convert it to a flow via snapshotFlow
to be able to use it in JavaFX UI.
Seems symmetrical. No?• UI states: single stream or multiple streams? The key guiding principle for choosing between exposing UI state in a single stream or in multiple streams is the previous bullet point: the relationship between the items emitted. The biggest advantage to a single-stream exposure is convenience and data consistency: consumers of state always have the latest information available at any instant in time. However, there are instances where separate streams of state from the ViewModel might be appropriate:
◦ Unrelated data types: Some states that are needed to render the UI might be completely independent from each other. In cases like these, the costs of bundling these disparate states together might outweigh the benefits, especially if one of these states is updated more frequently than the other.
◦diffing: The more fields there are in aUiState
object, the more likely it is that the stream will emit as a result of one of its fields being updated. Because views don’t have a diffing mechanism to understand whether consecutive emissions are different or the same, every emission causes an update to the view. This means that mitigation using theUiState
APIs or methods likeFlow
on thedistinctUntilChanged()
might be necessary.LiveData
Michael Paus
03/03/2022, 3:39 PMLandry Norris
03/03/2022, 6:38 PM