https://kotlinlang.org logo
Title
d

Davide Giuseppe Farella

11/03/2020, 5:29 PM
Hello everyone, I got this scenario
fun login(): Flow<LoginState>

sealed class LoginState {
    object Loading : LoginState()
    class ApproveRequestToken(val url: String) : LoginState()
    object Completed : LoginState()
}
When the
LoginState.ApproveRequestToken
is emitted, I have to wait for user interaction ( approve token via web ), then I want the
Flow
to continue, what would be your best approach?
b

bdawg.io

11/03/2020, 10:03 PM
Your
collect
can suspend for other calls too.
login().collect {
    if (it is ApproveRequestToken) {
        val approval = handleApproval(it.url) // suspend
        doSomethingWithApproval(approval)
    }
}
d

Davide Giuseppe Farella

11/03/2020, 10:28 PM
Thank you, but I was thinking about how to write the Flow for "suspend" it waiting for the approval. I opted for this