I have the following functions:
fun validateEmail(email: String) = flowOf {
when {
email.isEmpty() -> throw EmailError.Empty
email.contains("@") -> email
else -> throw EmailError.NotValid
}
}
fun validatePassword(password: String) = flowOf {
when {
password.isEmpty() -> throw PasswordError.Empty
else -> password
}
}
fun useCaseLogin(email: String, password: String) = flow {
//response: MyResponse = make and http request
emit(response)
}
Then i would like to know if is posible to run this flows in the following way:
fun login(email: String, password: String) =
validateEmail(email).then(validatePassword(password))
.then(useCaseLogin(email, password)) { response: MyResponse ->
if (response==200) {
emit("Login success!!")
} else {
emit("Login failed")
}
}.catch { e: Throwable ->
when (e) {
is EmailError.NotValid -> {
print(e.localizedMessage)
}
is EmailError.Empty -> {
print(e.localizedMessage)
}
is PasswordError.Empty -> {
print(e.localizedMessage)
}
}
}.asLiveData(viewModelScope.coroutineContext)
Is there a way to write something like this?