LazyStaggeredGrid icon indicating copy to clipboard operation
LazyStaggeredGrid copied to clipboard

Does it support compose paging library?

Open ahmed-shehataa opened this issue 3 years ago • 3 comments

Can I use this library with paging flow source instead of fixed list?

ahmed-shehataa avatar Feb 04 '22 23:02 ahmed-shehataa

I don't think so. StaggeredGridScope has no method that accepts the PagingData<_, _> instance. Would it be possible to add support? @nesyou01

mainrs avatar Jul 05 '22 13:07 mainrs

i created one. similarly how it's done in LazyColumn

import androidx.compose.runtime.Composable
import androidx.paging.compose.LazyPagingItems
import com.nesyou.staggeredgrid.StaggeredGridScope


fun <T : Any> StaggeredGridScope.pagedItems(
    items: LazyPagingItems<T>,
    itemContent: @Composable StaggeredGridScope.(value: T?) -> Unit
) {
    items(
        count = items.itemCount,
    ) { index ->
        itemContent(items[index])
    }
}

not sure if it's optimal though. i'm pretty new to compose.

mykola-dev avatar Jul 31 '22 12:07 mykola-dev

I think to make this properly work in all use-cases, the extension function definitely needs a key parameter. Else animations and add/remove operations might not behave correctly. If I understand it correctly, dynamically mutating the view would de-sync the PagingData, since without any specific key parameter (as linked below), the index is used as the key and for fetching new data.

https://github.com/androidx/androidx/blob/0d7d221b4aedee1f8d8f27fc1d4f43508de6067a/paging/paging-compose/src/main/java/androidx/paging/compose/LazyPagingItems.kt#L279-L336

This needs an underlying change in the library, since it does not support keys right now.

mainrs avatar Jul 31 '22 13:07 mainrs