Remove LoginState from savedStateHandle (#1382)

This commit is contained in:
David Perez 2024-05-23 09:33:51 -05:00 committed by Álison Fernandes
parent 10dbf03959
commit 1061e27f50
2 changed files with 10 additions and 12 deletions

View file

@ -24,6 +24,7 @@ import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import kotlinx.parcelize.IgnoredOnParcel
import kotlinx.parcelize.Parcelize
import javax.inject.Inject
@ -39,6 +40,7 @@ class LoginViewModel @Inject constructor(
private val vaultRepository: VaultRepository,
savedStateHandle: SavedStateHandle,
) : BaseViewModel<LoginState, LoginEvent, LoginAction>(
// We load the state from the savedStateHandle for testing purposes.
initialState = savedStateHandle[KEY_STATE]
?: LoginState(
emailAddress = LoginArgs(savedStateHandle).emailAddress,
@ -53,10 +55,6 @@ class LoginViewModel @Inject constructor(
) {
init {
// As state updates, write to saved state handle:
stateFlow
.onEach { savedStateHandle[KEY_STATE] = it }
.launchIn(viewModelScope)
authRepository.captchaTokenResultFlow
.onEach {
sendAction(
@ -273,7 +271,8 @@ class LoginViewModel @Inject constructor(
*/
@Parcelize
data class LoginState(
val passwordInput: String,
// We never want this saved since the input is sensitive data.
@IgnoredOnParcel val passwordInput: String = "",
val emailAddress: String,
val captchaToken: String?,
val environmentLabel: String,

View file

@ -37,9 +37,6 @@ import org.junit.jupiter.api.Test
class LoginViewModelTest : BaseViewModelTest() {
private val savedStateHandle = SavedStateHandle().also {
it["email_address"] = EMAIL
}
private val mutableCaptchaTokenResultFlow =
bufferedMutableSharedFlow<CaptchaCallbackTokenResult>()
private val mutableUserStateFlow = MutableStateFlow<UserState?>(null)
@ -149,8 +146,7 @@ class LoginViewModelTest : BaseViewModelTest() {
passwordInput = "input",
isLoginButtonEnabled = true,
)
savedStateHandle["state"] = expectedState
val viewModel = createViewModel()
val viewModel = createViewModel(expectedState)
viewModel.stateFlow.test {
assertEquals(expectedState, awaitItem())
}
@ -447,12 +443,15 @@ class LoginViewModelTest : BaseViewModelTest() {
}
}
private fun createViewModel(): LoginViewModel =
private fun createViewModel(state: LoginState? = null): LoginViewModel =
LoginViewModel(
authRepository = authRepository,
environmentRepository = fakeEnvironmentRepository,
vaultRepository = vaultRepository,
savedStateHandle = savedStateHandle,
savedStateHandle = SavedStateHandle().also {
it["email_address"] = EMAIL
it["state"] = state
}
)
companion object {