I have two structures. I am confused about which o...
# compose
r
I have two structures. I am confused about which one is preferable or both have problems there are better solutions than this. 1. I have all the state property and onChangeProperty method in ViewModel. I get the response from the network and update them in ViewModel. Then just use those states in UI. 2. All properties and methods in the UI. I just get a response from the network then update a single state, observe it and update all properties in the UI. See code in the thread
1st One. I have removed some unnecessary part from the code.
Copy code
var username by mutableStateOf("")
        private set
var password by mutableStateOf("")
        private set
var usernameError by mutableStateOf("")
    private set
var passwordError by mutableStateOf("")
    private set
fun onUsernameChange(name: String) {
        username = name
        if (name.isNotEmpty()) {
            usernameError = ""
        }
    }

 fun login() {
        if (username.isEmpty()) {
            usernameError = "Username field is empty!"
            return
        }
        if (password.isEmpty()) {
            passwordError = "Password field is empty!"
            return
        }

        viewModelScope.launch {
            val response = repository.login(LoginRequest(username, password))
            response.let {
                when (response) {
                    is Resource.Success -> {
                            response.value.errors?.username?.let {
                                if (it == "no_value") {
                                    usernameError = "Username can't be empty!"
                                } else if (it == "no_user") {
                                    usernameError =
                                        "There is no user with this username!"
                                }
                           }
                    is Resource.Failure -> {
                        isNetworkError = true
                 }
             }
         } 
    }
2nd one is - In response just expose a single Resource state and check all is Resource.success, is Resource.Failure in the UI and update those value which is already there in the UI
Copy code
val (username, onUsernameChange) = remember { mutableStateOf("") }