sindrenm
10/16/2024, 12:02 PMTextFieldState
changes? I was hoping I could do something like what I have in the thread.sindrenm
10/16/2024, 12:03 PMclass LoginViewModel : ViewModel() {
init {
viewModelScope.launch {
combine(
snapshotFlow { uiState.username.text },
snapshotFlow { uiState.password.text },
) { username, password -> username.isNotBlank() && password.isNotBlank() }
.distinctUntilChanged()
.collect { isFormFilled ->
uiState = uiState.copy(
signInButtonState = if (isFormFilled) {
SignInButtonState.Enabled
} else {
SignInButtonState.Disabled
},
)
}
}
}
}
sindrenm
10/16/2024, 12:03 PM@Test
fun `If the form is filled, the Sign In button is enabled`() = runTest {
val viewModel: LoginViewModel = // ...
viewModel.uiState.username.edit { insert(0, "AzureDiamond") }
viewModel.uiState.password.edit { insert(0, "hunter2") }
advanceUntilIdle()
assertEquals(SignInButtonState.Enabled, viewModel.uiState.signInButtonState)
}
Sean Proctor
10/16/2024, 12:14 PMval signInButtonState get() = if (username.isNotBlank() && password.isNotBlank()) SignInButtonState.Enabled else SignInButtonState.Disabled
sindrenm
10/16/2024, 12:17 PMSignInButtonState.Loading
, for instance, that would need to be set outside.Stylianos Gakis
10/16/2024, 12:17 PMbackgroundScope
and you can pass that into the constructor of your ViewModel as of the later versions.
That way you can at least take the ViewModel quirks out of the way.sindrenm
10/16/2024, 12:20 PMsindrenm
10/16/2024, 12:23 PMStylianos Gakis
10/16/2024, 12:23 PMsindrenm
10/16/2024, 12:24 PMinternal class LoginViewModel @Inject constructor(
// ...
coroutineSScope: CoroutineScope,
) : ViewModel(coroutineSScope) {
// still using this.viewModelScope in the class body
}
sindrenm
10/16/2024, 12:25 PMViewModel.viewModelScope
extension doesn't seem straight-forward, either.sindrenm
10/16/2024, 12:26 PMStylianos Gakis
10/16/2024, 12:34 PMDispatchers.main.immediate + SupervisorScope()
Albert Chang
10/16/2024, 12:43 PMsnapshotFlow
to work in unit tests.sindrenm
10/16/2024, 12:45 PMsindrenm
10/16/2024, 12:45 PMsindrenm
10/16/2024, 12:51 PM@Test
fun `If the form is filled, the Sign In button should be enabled`() = runTest {
val viewModel = LoginViewModel(
repository = FakeAuthRepository(loginResult = Unit.right()),
tracker = fakeTracker,
networkConfigRepository = FakeNetworkConfigRepository(),
)
viewModel.uiState.username.edit { insert(0, "AzureDiamond") }
viewModel.uiState.password.edit { insert(0, "hunter2") }
Snapshot.sendApplyNotifications()
assertEquals(SignInButtonState.Enabled, viewModel.uiState.signInButtonState)
}
Thanks!sindrenm
10/16/2024, 12:51 PMadvanceUntilIdle()
here? 🤔