https://kotlinlang.org logo
Title
m

Matti MK

10/07/2021, 1:45 PM
private val networkResponse = onContinueClicked.filter { it }.zip(
        combine(
            email.filter { it.count() >= MIN_INPUT_LENGTH },
            password.filter { it.count() >= MIN_INPUT_LENGTH })
        { e, p -> Pair(e, p) }
    ) { _, cred ->
        val (user, pass) = cred
        repository.login(username = user, password = pass)
    }
I’m playing around with flows: aim is to do a simple login network request.
email
and
password
are
MutableStateFlow
, this works fine except I’d like to emit a
Loading
value before the second to last line. The
login
call does not return a flow. I think I’m missing the use of the correct operator here in place of
zip
or I should have my repository return a flow (and
startWith
a given state in the repo).
Went about it like this, not really happy with it but seems to work. Any ideas for cleaning this up?
private val networkRequestPrecondition: Flow<Pair<String, String>> =
    onContinueClicked.filter { it }
        .zip(
            combine(
                email.filter { it.count() >= MIN_INPUT_LENGTH },
                password.filter { it.count() >= MIN_INPUT_LENGTH })
            { e, p -> Pair(e, p) }
        ) { _, p -> p }

private val createRequest: Flow<Resource<Response>> = networkRequestPrecondition.transform {
    emit(Resource.Loading())
    emit(repository.log(it.first, it.second))
}