https://kotlinlang.org logo
Title
f

fengdai

07/01/2022, 1:32 AM
Which should I prefer for creating item of Paging’s LoadState?
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:
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

Ian Lake

07/01/2022, 1:40 AM
Generally, you'll want to avoid an item that doesn't output any composables, so the first one seems more correct.
f

fengdai

07/01/2022, 1:48 AM
Thanks, Ian. What if I change the second one to this:
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

Ian Lake

07/01/2022, 1:50 AM
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

fengdai

07/01/2022, 1:56 AM
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

Ian Lake

07/01/2022, 1:57 AM
Conditionally creating the item, always
1
f

fengdai

07/01/2022, 1:58 AM
Great. Thanks again.
Would you mind to describe the reason?
i

Ian Lake

07/01/2022, 2:10 AM
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
👍 2
👀 2
👍🏻 1