Which should I prefer for creating item of Paging’...
# compose
f
Which should I prefer for creating item of Paging’s LoadState?
Copy code
LazyColumn {
    ...
    if (lazyItems.loadState.append is LoadState.Loading) {
        item(contentType = Loading) {
            PagingLoadingItem()
        }
    }
    if (lazyItems.loadState.append is LoadState.Error) {
        item(contentType = Error) {
            PagingErrorItem {
                lazyItems.retry()
            }
        }
    }
    if (lazyItems.loadState.append.endOfPaginationReached) {
        item(contentType = EndReached) {
            PagingEndItem()
        }
    }
Or:
Copy code
LazyColumn {
    ...
    item(contentType = LoadStateIndicator) {
        if (lazyItems.loadState.append is LoadState.Loading) {
            PagingLoadingItem()
        }
        if (lazyItems.loadState.append is LoadState.Error) {
            PagingErrorItem {
                lazyItems.retry()
            }
        }
        if (lazyItems.loadState.append.endOfPaginationReached) {
            PagingEndItem()
        }
    }
}
i
Generally, you'll want to avoid an item that doesn't output any composables, so the first one seems more correct.
f
Thanks, Ian. What if I change the second one to this:
Copy code
LazyColumn {
    ...
    item(contentType = LoadStateIndicator) {
        Box {
            if (lazyItems.loadState.append is LoadState.Loading) {
                PagingLoadingItem()
            }
            if (lazyItems.loadState.append is LoadState.Error) {
                PagingErrorItem {
                    lazyItems.retry()
                }
            }
            if (lazyItems.loadState.append.endOfPaginationReached) {
                PagingEndItem()
            }
        }
    }
}
Is this still worse?
i
It is still an item with zero width and height in some cases - there's no actual content in your Box, which is no better than no Box at all
Breaking each of those into their own
LazyItemScope
extension method might make it easier to read
f
What if I specify the width and height? Sorry, I didn’t describe my question clearly. Actually, I what to know: “*conditionally creating `item`*s” VS “*conditionally creating the content of an `item`*”, which if better?
i
Conditionally creating the item, always
1
f
Great. Thanks again.
Would you mind to describe the reason?
i
I like to think about it in terms of cost - evaluating the Lazy DSL is fast and if checks are cheap, but keeping track of items and composing them (even if empty) has a cost
👍 3
👍🏻 1
👍🏾 1
👀 3