title: Merging Flow with one shot data
body: <p>I have a <code>Flow</code> that fetches items from database (this is easy using Room), and then I have another data source which is an API. I can call this API (they don't implement Flows because it is an old Java API) which will give me other set of items (same type as the database ones).</p>
  <p>My Room database returns a <code>Flow&lt;List&lt;MyItem&gt;&gt;</code> already and I can show it, and it gets updated on adding/deleting items, but now I need to fetch the other set of items from the API and I don't know how to proceed. Furthermore, I only can fetch them if the user has logged in (i.e., if the user has a valid token).</p>
  <p>So, in order to use <code>combine</code>, I have used the function <code>callbackFlow</code> to get the items as a <code>Flow</code> and then I combine the flow from database and the flow from the API. However, I don't know how to update this flow from the API when the user logs in (fetch items) or logs out (clear items).</p>
  <p>In my ideal world, I would have a <code>LiveData</code> which has the session token, and if it is null I would clear the flow and otherwise I would make the petition to the API, but I don't know how to implement this, I am new to the <code>Flow</code> concept and components.</p>
  <p>Edit: I tried the following, but I don't know if I'm on the right path.</p>
  <pre><code>override fun getItemsAsFlow(): Flow&lt;List&lt;Item&gt;&gt;{
          return flow {
              val isLoggedIn = _loggedIn.value ?: false
              var id = 1000000L
              if (isLoggedIn) {
                  val list = ItemAPIManager.getItems().map {
                      Item(id = id++, name = it.name)
                  }
                  emit(list)
              }
          }
      }
  </code></pre>
  <p>Can anyone help me? Thank you.</p>
  <p>Update: Based on @Tenfour04 answer, I updated my code. My API provider does this:</p>
  <pre><code>override fun getItemsAsFlow(): Flow&lt;List&lt;Item&gt;&gt; {
          val items =
          _loggedIn.flatMapLatest { loggedIn -&gt;
              if(loggedIn) {
                  callbackFlow {
                      var id = 1000000L
                      val list = ItemAPIManager.getItems().map {
                          Item(id = id++, name = it.name)
                      }
                      trySend(list)
                      awaitClose()
                  }
              }
              else emptyFlow()
          }
          return items
      }
  </code></pre>
  <p>My ViewModel where all these merge to create a <code>RecyclerView</code> has this function:</p>
  <pre><code>private val _databaseItems: Flow&lt;List&lt;Item&gt;&gt; = databaseProvider.getItemsAsFlow()
  private val _otherDatabaseItems: Flow&lt;List&lt;Item&gt;&gt; = databaseProvider.getSpecialItemsAsFlow()
  private val _apiItems: Flow&lt;List&lt;Item&gt;&gt; = apiProvider.getItemsAsFlow()
  
  private fun fetchItems(): LiveData&lt;List&lt;ItemViewModel&gt;&gt; {
          val itemViewModelFlow = _databaseItems.combine&lt;List&lt;Item&gt;, List&lt;Item&gt;, List&lt;ItemViewModel&gt;&gt;(_otherDatabaseItems) { dbItems1, dbItems2 -&gt;
              val retList = arrayListOf&lt;ItemViewModel&gt;()
              if (settingsProvider.areTheOtherItemsEnabled) {
                  retList.addAll(createViewData(dbItems2))
              }
              retList.addAll(createViewData(dbItems1))
              return@combine retList
          }.combine&lt;List&lt;ItemViewModel&gt;, List&lt;Item&gt;, List&lt;ItemViewModel&gt;&gt;(_apiItems) { itemViewModels, apiItems -&gt;
              val retList = arrayListOf&lt;ItemViewModel&gt;()
              retList.addAll(itemViewModels)
              retList.addAll(createViewData(apiItems))
              return@combine retList
          }
  
          return itemViewModelFlow.asLiveData()
      }
  </code></pre>
  <p>The fact that I have two database lists is not relevant here, we can treat them as one. Basically, if a setting is enabled, these will get shown to the user, otherwise they won't.</p>
  <p>The <code>createViewData</code> function just transforms a list of items into a list of item view models that are used in the <code>RecyclerView</code>.</p>
  <p>Now if I'm logged in when I start the app, I can see the two database item lists, and I can see the API items, but if I log out I still can see the API items. Furthermore, if I'm logged out I cannot even see the database items (which I should).</p>
  <p>Thank you for your patience, as I'm still learning <code>Flows</code>.</p>