Soo … if anyone is still around … trying to put so...
# compose
b
Soo … if anyone is still around … trying to put some of these concepts together … Considering a use case where I’ve got a high level composable that represents an entire screen that shows a list of “posts”. Posts are stored in a room DB. When the “screen” composable is added to the compose tree, I want to get a flow of post from the db, collect and show them. Also, make a network call to fetch any new posts and put them in the DB. Should the list of posts be empty, I want to show a “loading” composable, but only if the network fetch is still ongoing. if the post list is empty after the network fetch is empty, then I can show a “no posts available” composable…. Given the above, does something like this seem reasonable?
Copy code
@Composable
fun HomeScreen(viewModel: MainViewModel, onPostClicked: (Post) -> Unit = {}) {
    val updatesComplete by produceState(initialValue = false, subject = viewModel) {
        // suspending method that fetches updates from a network api, and updates a room DB.
        viewModel.updatePosts()
        value = true
    }

    val postList by remember(viewModel) {
        // A Flow of posts from a room DB.
        viewModel.recentPosts
    }.collectAsState(initial = listOf())

    val uiState by derivedStateOf {
        when {
            postList.isNotEmpty() -> UiState(data = postList)
            !updatesComplete -> UiState(loading = true)
            else ->  UiState(data = postList)
        }
    }

    AppScreen {
        when {
            uiState.loading ->  Box(modifier = Modifier.fillMaxSize()) {
                CircularProgressIndicator(modifier = Modifier.align(Alignment.Center))
            }
            uiState.data!!.isEmpty() -> Text("No Posts Available")
            else -> ShowPosts(uiState.data)
        }
    }
}