I hope to set _isRecording inside SoundViewModel class, and I hope to expose isRecording to UI.
But the Code A is wrong, how can I fix it?
Code A
class SoundViewModel @Inject constructor(): ViewModel() {
private var _isRecording by mutableStateOf(false)
val isRecording: State<Boolean> by _isRecording //It's wrong
//val isRecording: State<Boolean> = _isRecording //It's wrong
..
}
Add content:
To nglauber: Thanks!
I think Code B will work well.
Which is better between your code and Code B?
Code B
class SoundViewModel @Inject constructor(): ViewModel() {
var isRecording by mutableStateOf(false)
private set
}
@Composable
fun YourComposable(soundViewModel: SoundViewModel) {
//I can use soundViewModel.isRecording directly
}