Yan Pujante
03/13/2021, 7:52 PM// from DiscoverViewModel
init {
viewModelScope.launch {
// Combines the latest value from each of the flows, allowing us to generate a
// view state instance which only contains the latest values.
combine(
categoryStore.categoriesSortedByPodcastCount() //. simplified cause not important
_selectedCategory
) { categories, selectedCategory ->
DiscoverViewState(
categories = categories,
selectedCategory = selectedCategory
)
}.collect { _state.value = it }
}
}
// then in CategoryStore
/**
* Returns a flow containing a list of categories which is sorted by the number
* of podcasts in each category.
*/
fun categoriesSortedByPodcastCount(
limit: Int = Integer.MAX_VALUE
): Flow<List<Category>> {
return categoriesDao.categoriesSortedByPodcastCount(limit)
}
/**
* Adds the category to the database if it doesn't already exist.
*
* @return the id of the newly inserted/existing category
*/
suspend fun addCategory(category: Category): Long {
return when (val local = categoriesDao.getCategoryWithName(category.name)) {
null -> categoriesDao.insert(category)
else -> local.id
}
}
when addCategory is called, how does the DiscoverViewModel state gets updated exactly? Clearly after adding a category to the database, the list of categories is different. I just don't see from this code how that gets propagated. Is there something missing? Am I missing something?Yan Pujante
03/13/2021, 7:55 PMcategoriesSortedByPodcastCount
is implemented this way:
@Query(
"""
SELECT categories.* FROM categories
INNER JOIN (
SELECT category_id, COUNT(podcast_uri) AS podcast_count FROM podcast_category_entries
GROUP BY category_id
) ON category_id = categories.id
ORDER BY podcast_count DESC
LIMIT :limit
"""
)
abstract fun categoriesSortedByPodcastCount(
limit: Int
): Flow<List<Category>>
And I guess I have no visibility in how Room implements this api (the code must be generated)TheMrCodes
03/13/2021, 10:21 PM