JetBrains / compose-multiplatform

Compose Multiplatform, a modern UI framework for Kotlin that makes building performant and beautiful user interfaces easy and enjoyable.
https://jetbrains.com/lp/compose-multiplatform
Apache License 2.0
15.35k stars 1.12k forks source link

LazyLayoutItemsProvider generification #2170

Open LAP2 opened 2 years ago

LAP2 commented 2 years ago

Hi team. Want to suggest you following improvement for LazyLayoutItemsProvider interface. Now we have following interface:

@Stable
@ExperimentalFoundationApi
interface LazyLayoutItemsProvider {

    /** The total number of items in the lazy layout (visible or not). */
    val itemsCount: Int

    /** Returns the content lambda for the given index and scope object */
    fun getContent(index: Int): @Composable () -> Unit

    /**
     * Returns the content type for the item on this index. It is used to improve the item
     * compositions reusing efficiency.
     **/
    fun getContentType(index: Int): Any?

    /**
     * Returns the key for the item on this index.
     *
     * @see getDefaultLazyLayoutKey which you can use if the user didn't provide a key.
     */
    fun getKey(index: Int): Any

    /**
     * Contains the mapping between the key and the index. It could contain not all the items of
     * the list as an optimization.
     **/
    val keyToIndexMap: Map<Any, Int>
}

It is good for lazy layouts which implements something list based that index could be described as Int. But what if we want to implement lazy data grid or some other bidirectional lazy layout (graph view, mindmap), than possibly we will need two indexes (column, row) or other representations. So I suggest you following improvment:

@Stable
@ExperimentalFoundationApi
interface LazyLayoutItemsProvider<IndexType> {

    /** The total number of items in the lazy layout (visible or not). */
    val itemsCount: Int

    /** Returns the content lambda for the given index and scope object */
    fun getContent(index: IndexType): @Composable () -> Unit

    /**
     * Returns the content type for the item on this index. It is used to improve the item
     * compositions reusing efficiency.
     **/
    fun getContentType(index: IndexType): Any?

    /**
     * Returns the key for the item on this index.
     *
     * @see getDefaultLazyLayoutKey which you can use if the user didn't provide a key.
     */
    fun getKey(index: IndexType): Any

    /**
     * Contains the mapping between the key and the index. It could contain not all the items of
     * the list as an optimization.
     **/
    val keyToIndexMap: Map<Any, IndexType>
}

BTW. In large data sets amount of items might be bigger than Int ether.

kirill-grouchnikov commented 2 years ago

https://issuetracker.google.com/issues/new?component=612128&template=1253476 is the right place for general Compose issues

LAP2 commented 2 years ago

I know. But it possibly took the few eternities to push such improvement through google issue tracker. I had post it here with hope, that some one already pass through gerrit process "wall", and could provide this proposal for greater good.