1 /* <lambda>null2 * Copyright 2023 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package androidx.compose.foundation.lazy.layout 18 19 /** 20 * Common parts backing the interval-based content of lazy layout defined through `item` DSL. 21 * 22 * Note: this class is a part of [LazyLayout] harness that allows for building custom lazy layouts. 23 * LazyLayout and all corresponding APIs are still under development and are subject to change. 24 */ 25 abstract class LazyLayoutIntervalContent<Interval : LazyLayoutIntervalContent.Interval> { 26 abstract val intervals: IntervalList<Interval> 27 28 /** The total amount of items in all the intervals. */ 29 val itemCount: Int 30 get() = intervals.size 31 32 /** Returns item key based on a global index. */ 33 fun getKey(index: Int): Any = 34 withInterval(index) { localIndex, content -> 35 content.key?.invoke(localIndex) ?: getDefaultLazyLayoutKey(index) 36 } 37 38 /** Returns content type based on a global index. */ 39 fun getContentType(index: Int): Any? = 40 withInterval(index) { localIndex, content -> content.type.invoke(localIndex) } 41 42 /** 43 * Runs a [block] on the content of the interval associated with the provided [globalIndex] with 44 * providing a local index in the given interval. 45 */ 46 inline fun <T> withInterval( 47 globalIndex: Int, 48 block: (localIntervalIndex: Int, content: Interval) -> T 49 ): T { 50 val interval = intervals[globalIndex] 51 val localIntervalIndex = globalIndex - interval.startIndex 52 return block(localIntervalIndex, interval.value) 53 } 54 55 /** 56 * Common content of individual intervals in `item` DSL of lazy layouts. 57 * 58 * Note: this class is a part of [LazyLayout] harness that allows for building custom lazy 59 * layouts. LazyLayout and all corresponding APIs are still under development and are subject to 60 * change. 61 */ 62 interface Interval { 63 /** Returns item key based on a local index for the current interval. */ 64 val key: ((index: Int) -> Any)? 65 get() = null 66 67 /** Returns item type based on a local index for the current interval. */ 68 val type: ((index: Int) -> Any?) 69 get() = { null } 70 } 71 } 72