Stephen Vinouze
02/02/2022, 3:55 PMLandingViewModel
exposing a LandingViewState
data class LandingViewState(
val firstPlayerName: String = "",
val secondPlayerName: String = "",
val canPlay: Boolean = false,
)
I would like the last attribute to be derived from name arguments while only exposing the viewState
– you can see that as a reducer. I haven't been able to find a better pattern than this
@HiltViewModel
class LandingViewModel @Inject constructor() : ViewModel() {
val viewState: LandingViewState by derivedStateOf {
LandingViewState(
firstPlayerName, secondPlayerName, firstPlayerName.isNotEmpty() && secondPlayerName.isNotEmpty()
)
}
var firstPlayerName: String by mutableStateOf("")
var secondPlayerName: String by mutableStateOf("")
}
So I've made the viewState
as a derived state and exposed the other attributes. Ideally, I'd only expose the viewState
as a state and transform the canPlay
on the fly as I'd have done with a reducer. But I can't figure out how to do it with compose mutableStateOf
myanmarking
02/02/2022, 8:47 PMdata class LandingViewState(
val firstPlayerName: String = "",
val secondPlayerName: String = ""
){
val canPlay: Boolean = xxx
}
Stephen Vinouze
02/03/2022, 7:49 AM