title: Kotlin Coroutines Flow with Room and state handling
body: <p>I'm trying out the new coroutine's flow, my goal is to make a simple repository that can fetch data from a web api and save it to db, also return a flow from the db.</p>
  
  <p>I'm using room and firebase as the web api, now everything seems pretty straight forward until i try to pass errors coming from the api to the ui.</p>
  
  <p>Since i get a flow from the database which only contains the data and no state, what is the correct approach to give it a state (like loading, content, error) by combining it with the web api result?</p>
  
  <p>Some of the code i wrote:</p>
  
  <p>The DAO:</p>
  
  <pre><code>@Query("SELECT * FROM users")
  fun getUsers(): Flow&lt;List&lt;UserPojo&gt;&gt;
  </code></pre>
  
  <p>The Repository:</p>
  
  <pre><code>val users: Flow&lt;List&lt;UserPojo&gt;&gt; = userDao.getUsers()
  </code></pre>
  
  <p>The Api call: </p>
  
  <pre><code>override fun downloadUsers(filters: UserListFilters, onResult: (result: FailableWrapper&lt;MutableList&lt;UserApiPojo&gt;&gt;) -&gt; Unit) {
      val data = Gson().toJson(filters)
  
      functions.getHttpsCallable("users").call(data).addOnSuccessListener {
          try {
              val type = object : TypeToken&lt;List&lt;UserApiPojo&gt;&gt;() {}.type
              val users = Gson().fromJson&lt;List&lt;UserApiPojo&gt;&gt;(it.data.toString(), type)
              onResult.invoke(FailableWrapper(users.toMutableList(), null))
          } catch (e: java.lang.Exception) {
              onResult.invoke(FailableWrapper(null, "Error parsing data"))
          }
      }.addOnFailureListener {
          onResult(FailableWrapper(null, it.localizedMessage))
      }
  }
  </code></pre>
  
  <p>I hope the question is clear enough
  Thanks for the help</p>
  
  <p><strong>Edit:</strong> Since the question wasn't clear i'll try to clarify. My issue is that with the default flow emitted by room you only have the data, so if i were to subscribe to the flow i would only receive the data (eg. In this case i would only receive a list of users). What i need to achieve is some way to notify the state of the app, like loading or error. At the moment the only way i can think of is a "response" object that contains the state, but i can't seem to find a way to implement it.</p>
  
  <p>Something like:</p>
  
  <pre><code>fun getUsers(): Flow&lt;Lce&lt;List&lt;UserPojo&gt;&gt;&gt;{
      emit(Loading())
      downloadFromApi()
      if(downloadSuccessful)
          return flowFromDatabase
      else
          emit(Error(throwable))
  }
  </code></pre>
  
  <p>But the obvious issue i'm running into is that the flow from the database is of type <code>Flow&lt;List&lt;UserPojo&gt;&gt;</code>, i don't know how to "enrich it" with the state editing the flow, without losing the subscription from the database and without running a new network call every time the db is updated (by doing it in a map transformation).</p>
  
  <p>Hope it's clearer</p>