I am having a really hard time wrapping my head ar...
# compose-desktop
y
I am having a really hard time wrapping my head around the state/flow concepts to build my own compose-desktop app. I am currently looking at the Jetcaster jetpack compose app as an example that uses a database (from https://github.com/android/compose-samples). So this is the part that I really don't understand:
Copy code
// 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?
To add some extra context, the
categoriesSortedByPodcastCount
is implemented this way:
Copy code
@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)
t
sorry I have not enough inside about the project currently but I would guess: Thr dao.insert function updates an internal List of categories which is implemented as some kind of LiveData and in the UI the list Composable is using a state synced with this LiveData object so wenn the list changes the State changes and the function gets recomposed Plz correct me if Im wrong