I am facing one strange issue. I am trying to make login screen screen and once login is successful i am navigating user to home screen.
So issue is that after successful login navigateToDashboard
is continuously emitting data.
The user is navigated to home screen but in logs it continuously prints called. Also i am not able go back from home screen.
ViewModel
private var _navigateToDashboard: MutableStateFlow<Boolean> = MutableStateFlow(false)
var navigateToDashboard: StateFlow<Boolean> = _navigateToDashboard
fun loginUser() {
viewModelScope.launch(Dispatchers.IO) {
isLoading.value = true
try {
val response = repo.loginUsers(email = email.value, password = password.value)
pref.storePrefValue(UserPreferences.token, response.data?.authToken ?: "")
pref.storePrefValue(
UserPreferences.loginData,
AppUtility.modelToJson(response) ?: ""
)
isLoading.value = false
_navigateToDashboard.value = true
Timber.e("ho")
} catch (exception: Exception) {
isLoading.value = false
Timber.e(exception)
}
}
}
Login Screen
Column(
Modifier.fillMaxWidth().fillMaxHeight().padding(40.dp),
) {
val authViewModel: AuthViewModel = hiltViewModel()
val navigateToDashboard by authViewModel.navigateToDashboard.collectAsState()
if (navigateToDashboard) {
Timber.e("called")
navController.popBackStack()
navController.navigate("home")
} else {
AppTextInput(
"Email Id",
value = authViewModel.email.value,
modifier = Modifier.fillMaxWidth().padding(top = 20.dp)
.focusRequester(focusRequester),
keyboardOptions = KeyboardOptions(
keyboardType = KeyboardType.Email,
imeAction = ImeAction.Next
),
keyboardActions = KeyboardActions(onNext = {
focusManager.moveFocus(FocusDirection.Down)
}),
) {
authViewModel.setEmail(it)
}
AppTextInput(
"Password",
value = authViewModel.password.value,
modifier = Modifier.fillMaxWidth().padding(top = 20.dp),
keyboardActions = KeyboardActions(onDone = {
keyboardController?.hide()
}), visualTransformation = PasswordVisualTransformation()
) {
authViewModel.setPassword(it)
}
LoadingButton(
isLoading = authViewModel.isLoading.value,
title = "Login",
boxModifier = Modifier.padding(top = 10.dp).fillMaxWidth(),
buttonModifier = Modifier.fillMaxWidth()
) {
keyboardController?.hide()
authViewModel.loginUser()
}
}
}