All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.androidx.compose.foundation.lazy.layout.LazyLayoutIntervalContent.kt Maven / Gradle / Ivy

Go to download

Higher level abstractions of the Compose UI primitives. This library is design system agnostic, providing the high-level building blocks for both application and design-system developers

There is a newer version: 1.8.0-alpha01
Show newest version
/*
 * Copyright 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package androidx.compose.foundation.lazy.layout

import androidx.compose.foundation.ExperimentalFoundationApi

/**
 * Common parts backing the interval-based content of lazy layout defined through `item` DSL.
 */
@ExperimentalFoundationApi
abstract class LazyLayoutIntervalContent {
    abstract val intervals: IntervalList

    /**
     * The total amount of items in all the intervals.
     */
    val itemCount: Int get() = intervals.size

    /**
     * Returns item key based on a global index.
     */
    fun getKey(index: Int): Any =
        withInterval(index) { localIndex, content ->
            content.key?.invoke(localIndex) ?: getDefaultLazyLayoutKey(index)
        }

    /**
     * Returns content type based on a global index.
     */
    fun getContentType(index: Int): Any? =
        withInterval(index) { localIndex, content ->
            content.type.invoke(localIndex)
        }

    /**
     * Runs a [block] on the content of the interval associated with the provided [globalIndex]
     * with providing a local index in the given interval.
     */
    inline fun  withInterval(
        globalIndex: Int,
        block: (localIntervalIndex: Int, content: Interval) -> T
    ): T {
        val interval = intervals[globalIndex]
        val localIntervalIndex = globalIndex - interval.startIndex
        return block(localIntervalIndex, interval.value)
    }

    /**
     * Common content of individual intervals in `item` DSL of lazy layouts.
     */
    @ExperimentalFoundationApi
    interface Interval {
        /**
         * Returns item key based on a local index for the current interval.
         */
        val key: ((index: Int) -> Any)? get() = null

        /**
         * Returns item type based on a local index for the current interval.
         */
        val type: ((index: Int) -> Any?) get() = { null }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy