ABRAR WIRYAWAN
04/07/2024, 11:54 PMABRAR WIRYAWAN
04/07/2024, 11:54 PM@Test
fun testStateFlow2() = runTest {
var emailState by mutableStateOf("")
var passwordState by mutableStateOf("")
val allInputValid: StateFlow<Boolean> = combine(
snapshotFlow { emailState },
snapshotFlow { passwordState }
) { emailValue, passwordValue ->
emailValue.isNotEmpty() && passwordValue.isNotEmpty()
}.stateIn(
CoroutineScope(UnconfinedTestDispatcher(testScheduler)),
SharingStarted.WhileSubscribed(5000),
false
)
val test = mutableListOf<String>()
val job = launch {
allInputValid.collect {
test.add("Value emitted: $it")
println(it)
}
}
advanceUntilIdle()
emailState = "test_email"
passwordState = "test_password"
advanceUntilIdle()
println(test)
job.cancel()
}
Rob
04/08/2024, 1:18 AMRob
04/08/2024, 1:19 AMprintln
inside combine
lambda to see if it is getting invoked with new values and what it is emitting.ABRAR WIRYAWAN
04/08/2024, 1:26 AMemail: - pass: // inside combine
false // inside collect
[Value emitted: false] // last println before cancel
ABRAR WIRYAWAN
04/08/2024, 1:28 AMRob
04/08/2024, 1:41 AMRob
04/08/2024, 1:42 AMABRAR WIRYAWAN
04/08/2024, 1:46 AMRob
04/08/2024, 1:47 AMState<T>
from an observable. I don't think snapshotFlow is going to work unless State<T>
is in a composable. But I'm am not an expert.
https://developer.android.com/develop/ui/compose/side-effects#snapshotFlowABRAR WIRYAWAN
04/08/2024, 1:52 AMABRAR WIRYAWAN
04/08/2024, 2:02 AMZach Klippenstein (he/him) [MOD]
04/08/2024, 4:14 PMABRAR WIRYAWAN
04/11/2024, 10:26 AM