Hello everyone! I was wondering how I should appro...
# android
d
Hello everyone! I was wondering how I should approach one problem: so in my chat app a request should be made to the API, namely, sending a message. The tricky part is when the request is sent, some result should be returned to the client (let's say a partially filled message to be displayed to the user), and then when a response from the server comes (in a handler-listener that we place on the socket.io connection) we have the fully filled result. I wanted to do this via the Flow, but I'm not sure if it's the correct way to do. Any advice? Thank you in advance.
c
The go-to-solution to this is using a sealed class to represent some kind of loading state, for example:
Copy code
sealed class LoadState {
  object Init: LoadState
  object Loading: LoadState
  data class Success<T>(data: T): LoadState
  data class Error(cause: Throwable): LoadState
}

fun request() = flow {
  emit(LoadState.Init)
  emit(LoadState.Loading)
  // some processing...
  emit(LoadState.Success("Success!"))
}
d
Yeah, I had in mind something like this, so I could first emit my initial "mockup" message with state "Sending" and then, on response, to emit the final message with all the required fields filled on the server side. I just wasn't sure it's ok to use flow for situations like this (where I just need to "return" two very similar values in an async environment). Thank you!