mm So one stackoverflow answer suggested using Med...
# android
u
mm So one stackoverflow answer suggested using MediatorLiveData. So updated my livedata as follows:
Copy code
val loginFieldsValid = MediatorLiveData<Boolean>()
val username = MutableLiveData<String>()
val password = MutableLiveData<String>()
val loginButtonEnabled = MutableLiveData<Boolean>()
And inside
init
, I add the sources to the MediatorLiveData.
Copy code
loginFieldsValid.addSource(username) { validateLoginFields() }
loginFieldsValid.addSource(password) { validateLoginFields() }
The
validateLoginFields()
simply checks, if at the time both username and password fields are not null or empty string.
Copy code
private fun validateLoginFields() {
    loginButtonEnabled.value = !username.value.isNullOrEmpty() && !password.value.isNullOrEmpty()
}
The xml is still the same
Copy code
<Button
            android:id="@+id/login"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="65dp"
            android:enabled="@{loginViewModel.loginButtonEnabled}"
            android:onClick="@{() -> loginViewModel.onLogin()}"
            android:text="@string/login"
            app:layout_constraintEnd_toEndOf="@+id/password_text_input_layout"
            app:layout_constraintTop_toBottomOf="@+id/password_text_input_layout" />
My issue is that the login button is always disabled even if I input some data into both fields.