Код: Выделить всё
data class LoginUiState(
val loginModel: LoginModel = LoginModel(),
) : UiState
Код: Выделить всё
@HiltViewModel
class LoginViewModel @Inject constructor() : BaseViewModel() {
override fun handleUiEvent(uiEvent: UiEvent) {
when (uiEvent) {
is LoginUiEvent.LoadScreenData -> {
loadScreenData()
}
is LoginUiEvent.OnClickLogin -> {
onClickLogin()
}
}
}
private fun loadScreenData() {
updateState { currentState ->
currentState.value = LoginUiState(LoginModel("login", "password"))
}
}
private fun onClickLogin() {
updateState { currentState ->
currentState.value = currentState.value?.copy(loginModel = LoginModel(login = "d"))
}
}
}
Код: Выделить всё
abstract class BaseViewModel : ViewModel() {
private var _uiState = mutableStateOf(null)
val uiState: State = _uiState
abstract fun handleUiEvent(uiEvent: UiEvent)
fun updateState(block: (state: MutableState) -> Unit) {
block.invoke(_uiState)
}
}
Подробнее здесь: https://stackoverflow.com/questions/781 ... y-function