Yusuf Ibragimov
12/28/2023, 11:36 AMYusuf Ibragimov
12/28/2023, 11:37 AM@Composable
fun CreatePasscodeScreen(viewModel: CreatePasscodeVM = hiltViewModel()) {
val uiState = viewModel.uiState.collectAsStateWithLifecycle()
LaunchedEffect(uiState.value.enteredPasscode) {
if (uiState.value.enteredPasscode.length == 4) {
viewModel.check()
}
}
Scaffold(
modifier = Modifier.fillMaxSize(),
snackbarHost = {
if (uiState.value.errorMessage.isNotEmpty()) {
Box(
modifier = Modifier
.padding(horizontal = 16.dp)
.clip(RoundedCornerShape(8.dp))
.fillMaxWidth()
) {
Text(
uiState.value.errorMessage,
style = DavrTheme.typography.h1Medium,
fontSize = 16.sp
)
}
}
}
) {
PasscodeContent(
modifier = Modifier.padding(it),
titleResId = uiState.value.titleResId,
numberClick = viewModel::numberClick,
fingerClick = viewModel::fingerClick,
delete = viewModel::delete,
forgotClick = viewModel::forgotClick,
error = uiState.value.error,
enteredPasscode = uiState.value.enteredPasscode
)
}
}
Yusuf Ibragimov
12/28/2023, 11:39 AMdorche
12/28/2023, 3:21 PMShahzad Ansari
12/28/2023, 4:07 PMonEach {}
operator if you are using StateFlow APIs.
// ViewModel
val state: StateFlow = uiState.onEach {
if (state.length == 4) {
// TODO: Your logic
}
}
This block would run on every flow emission.Yusuf Ibragimov
12/29/2023, 4:04 AMprivate val _uiState = MutableStateFlow(PasscodeUiState())
val uiState get() = _uiState.asStateFlow()
init {
viewModelScope.launch {
_uiState.onEach {
if (it.enteredPasscode.length == 4) {
check()
}
}
}
}
I don't know why my code is not working both uiState and _uiStateShahzad Ansari
12/29/2023, 11:05 AMonEach { }
is not a terminal operator. You need to call collect()
with it as well.
_uiState.onEach {
if (it.enteredPasscode.length == 4) {
check()
}
}.collect {}
Atul Gupta
12/30/2023, 8:31 PMcollect
instead of using onEach
as intermediate operator
_uiState.collect {
if (it.enteredPasscode.length == 4) {
check()
}
}