How to show an emptyView with no items in compose ...
# compose
m
How to show an emptyView with no items in compose paging. It seems impossible. We have to skip the condition for initial load (which the itemCount == 0). Also we cannot rely on endOfPaginationReached because the page can load from any position. Why is this api so hard ?
f
Just write your own paging, it's one list, one lambda function and one page variable, paging library is a nightmare to manage, even paging3 has some quirks you need to work around them, like multiple if clauses if pagination is impossible anymore because you reached the end, checking if initial load didn't return content etc... Manually you'd have only one boolean value isLoading and less frustrations
m
Indeed. All that extra boilerplate no1 asked. The combinedLoadState mess with 500k diferent states. So anoyibg
i
Is your problem just with the initial load? There's an open bug on that where it defaults to NotLoading vs what it should be starting at (Loading)
The details of combinedLoadState are way more useful when you are looking at adding a loading spinner as the first or last item of your endless list: loading each new set of data isn't instantaneous
m
Ya i get that. But the basic thing, showing an empty view if no items are present is kinda hard to achieve.
a
Hey. Have you tried writing something like this?
Copy code
if (lazyPagingItems.itemCount == 0) {
    EmptyUI()
} else {
    LazyColumn(…)
}
i
yep, or if you want to take loading into consideration:
Copy code
val loadState = lazyPagingItems.loadState
val finishedLoading =
  loadState.refresh !is LoadState.Loading &&
  loadState.prepend !is LoadState.Loading &&
  loadState.append  !is LoadState.Loading
if (lazyPagingItems.itemCount == 0 && finishedLoading) {
  EmptyUI()
} else {
  LazyColumn(...)
}
m
first one doesnt work. Paging always start with itemCount == 0, i dont want to show the emptyView for a split second
second also doesn’t work - for room only paging (without removeMediator), there is no Loading. always notLoading
i
like I said, there's a bug where Paging starts at
NotLoading
, when it really should be
Loading
until the first set of data comes back: https://issuetracker.google.com/224855902
but you'll get
Loading
every time you scroll down enough to load more data - specifically
loadState.append
will change. Of course, if your data loads faster than it takes to do a single composition...
a
I have the same problem too, Did you find any solution for this?
1108 Views