Thread
#compose
    Colton Idle

    Colton Idle

    1 year ago
    😅 I'm about to commit my first compose code to my teams codebase. It's really really really simple, but although it's only a few lines long I'm hoping a few people could take a look to "code review" if I'm breaking any fundamental rules. It's just the skeleton of getting a username and password field showing in a fragment. While the state actually lives in the AAC ViewModel which exposes a single state via LiveData. If you know anything about that please take a look! There's still a lot of work to do to make this work, but I'm really just trying to see if I understand how state hoisting works in compose, and how to properly pass observable data down and events up through the AAC VM. Code in thread
    Please let me know if anything seems like supppper terrible. I'm planning to moving to StateFlow in the future, but my team is still waiting to adopt it. LiveData it is. Fragment:
    setContent {
        val state = viewModel.state.observeAsState().value!!
        Column() {
            TextField(
                value = state.email,
                onValueChange = viewModel::emailUpdated,
                label = { Text("Email") },
            )
            TextField(
                value = state.password,
                onValueChange = { viewModel.passwordUpdated(it) },
                label = { Text("Pass") },
            )
        }
    }
    ViewModel:
    class LoginViewModel : ViewModel() {
        private val _state = MutableLiveData(LoginViewState())
        val state: LiveData<LoginViewState> = _state
    
        fun emailUpdated(update: String) {
            _state.value = _state.value!!.copy(email = update)
        }
    
        fun passwordUpdated(update: String) {
            _state.value = _state.value!!.copy(password = update)
        }
    }
    
    data class LoginViewState(val email: String = "", val password: String = "")
    Carson Holzheimer

    Carson Holzheimer

    1 year ago
    Nothing to do with compose, but "Pass" instead of "Password" seems strange. Also do you never imagine needing internationalisation?
    Colton Idle

    Colton Idle

    1 year ago
    Just placeholders for now. More so worried about view model exposing a single state
    Carson Holzheimer

    Carson Holzheimer

    1 year ago
    I think that's just personal preference. There's some minor pros and cons in either direction