Does anyone have a Flow<> example with Compo...
# compose
b
Does anyone have a Flow<> example with Compose for observing a list?
z
List
or
MutableList
?
b
just an immutable one
the JetNews sample just has lambda's as return results for it's repository calls and looking at using usecases instead of passing the repository via args
z
so you’ve got a
Flow<List<T>>
and you want to do something like
val currentList = listFlow.collectAsState(null)
?
b
essentially ya
have it update a LazyColumnItems on change
is where my head was at at least
j
There is a
Flow<T>.collectAsState()
extension function (wasn't sure if maybe that's what was being referred to by
collectAsState
reference above?)
z
Copy code
val listFlow: Flow<List<T>> = …
val currentList: List<T>? = listFlow.collectAsState(null)
if (currentList == null) {
  LoadingUI()
} else {
  LazyColumnItems(currentList)
}
b
oh boss
thank you
z
if you have a
StateFlow
, you don’t need to provide the initial value yourself
b
even better
thanks @Zach Klippenstein (he/him) [MOD]
Copy code
@Composable
internal fun BookmarkList(
        padding: Modifier,
        loadBookmarks: State<List<Bookmark>>
) {
    with(loadBookmarks) {
        ScrollableColumn(modifier = padding) {
            LazyColumnItems(value) { bookmark ->
                BookmarkCard(bookmark = bookmark) { editRequest ->
                    
                }
            }
        }
    }
}
there we go
Copy code
@Composable
private fun AppContent(
    navigationViewModel: NavigationViewModel,
    bookmarkViewModel: BookmarkViewModel
) {
    Surface(color = MaterialTheme.colors.background) {
        when (val screen = navigationViewModel.currentScreen) {
            Screen.Home -> {
                Crossfade(screen) {
                    HomeScreen(
                            navigateTo = navigationViewModel::navigateTo,
                            loadBookmarks = bookmarkViewModel.loadAll()
                    )
                }
            }
        }
    }
}
State<List<Bookmark>> from DI/passed args
z
Passing a state in seems like a smell to me. Just pass the actual value in instead, it’s simpler API, simpler for testing, and simpler to reason about.
b
fair reasoning
z
Also making
this
a
State
i think makes this code harder to read. I’ve never seen any compose code do this, and i don’t see any
this
references in that snippet so are you even using it?
(unsolicited code review, sorry 😛)
b
the
value
passed to LazyColumnItems
no keep it coming 🙂
z
Ah, yes. I would definitely miss that in the first 2 or 3 reads of this code, and would definitely recommend just calling referencing the val directly (which is even less boilerplate if you just pass the actual value in)
b
yep, that makes complete sense
looking into that now
AS is being dumb and is showing errors for kotlin pre-release but is still compiling fine so it's quite fun
z
Irrelevant if you change to just pass the value in, but calling a
State
val
loadAnything
is confusing to me. The name “load*” reads like a verb, like it’s a function that performs a “load” action, but a state is more of a noun.
b
yeah i had that named to match the usecase that I was attempting to pass in
agreed on the naming
z
Ah, i see