Keeping your composables responsive
When implementing composable functions, you should always keep in mind that their main purpose is to declare the UI and to handle user interactions. Ideally, anything needed to achieve this is passed to the composable, including state and logic (such as click handlers), making it stateless. If state is needed only inside a composable, the function may keep state temporarily using remember {}
. Such composables are called stateful. If data is kept in a ViewModel
, composables must interact with it. So, the ViewModel
code must be fast, too.
Communicating with ViewModel instances
Data inside a ViewModel
should be observable. ComposeUnitConverter
uses LiveData
and MutableLiveData
from the Android Architecture Components to achieve this. You can choose other implementations of the Observer pattern, provided there is a way to obtain State
or MutableState
instances that are updated upon changes in the ViewModel
. This is beyond the scope of this book...