update expressive lazy column
This commit is contained in:
@@ -1,17 +1,19 @@
|
||||
package ru.shadowsparky.ui.components
|
||||
|
||||
import androidx.compose.animation.core.spring
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.PaddingValues
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.height
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.lazy.LazyColumn
|
||||
import androidx.compose.foundation.lazy.LazyItemScope
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.material3.Card
|
||||
import androidx.compose.material3.CardDefaults
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.tooling.preview.Preview
|
||||
import androidx.compose.ui.unit.Dp
|
||||
import androidx.compose.ui.unit.dp
|
||||
|
||||
@@ -24,14 +26,18 @@ private annotation class ExpressiveListDsl
|
||||
internal data class ExpressiveNode(
|
||||
val key: Any?,
|
||||
val contentType: Any?,
|
||||
val content: @Composable () -> Unit
|
||||
val content: @Composable LazyItemScope.() -> Unit
|
||||
)
|
||||
|
||||
internal data class ExpressiveSection(
|
||||
val nodes: List<ExpressiveNode>
|
||||
)
|
||||
|
||||
@ExpressiveListDsl
|
||||
class ExpressiveSectionScope {
|
||||
internal val nodes = mutableListOf<ExpressiveNode>()
|
||||
|
||||
fun item(key: Any? = null, contentType: Any? = null, content: @Composable () -> Unit) {
|
||||
fun item(key: Any? = null, contentType: Any? = null, content: @Composable LazyItemScope.() -> Unit) {
|
||||
nodes += ExpressiveNode(key, contentType, content)
|
||||
}
|
||||
|
||||
@@ -39,7 +45,7 @@ class ExpressiveSectionScope {
|
||||
items: List<T>,
|
||||
key: ((T) -> Any)? = null,
|
||||
contentType: ((T) -> Any)? = null,
|
||||
itemContent: @Composable (T) -> Unit
|
||||
itemContent: @Composable LazyItemScope.(T) -> Unit
|
||||
) {
|
||||
itemsIndexed(items, key, contentType) { _, t -> itemContent(t) }
|
||||
}
|
||||
@@ -48,7 +54,7 @@ class ExpressiveSectionScope {
|
||||
items: List<T>,
|
||||
key: ((T) -> Any)? = null,
|
||||
contentType: ((T) -> Any)? = null,
|
||||
itemContent: @Composable (Int, T) -> Unit
|
||||
itemContent: @Composable LazyItemScope.(Int, T) -> Unit
|
||||
) {
|
||||
items.forEachIndexed { index, t ->
|
||||
nodes += ExpressiveNode(
|
||||
@@ -62,11 +68,13 @@ class ExpressiveSectionScope {
|
||||
|
||||
@ExpressiveListDsl
|
||||
class ExpressiveLazyListScope {
|
||||
internal val sections = mutableListOf<ExpressiveSectionScope>()
|
||||
internal val sections = mutableListOf<ExpressiveSection>()
|
||||
|
||||
fun section(content: ExpressiveSectionScope.() -> Unit) {
|
||||
val s = ExpressiveSectionScope().apply(content)
|
||||
if (s.nodes.isNotEmpty()) sections += s
|
||||
if (s.nodes.isNotEmpty()) {
|
||||
sections += ExpressiveSection(s.nodes)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -96,7 +104,11 @@ fun ExpressiveLazyColumn(
|
||||
|
||||
if (sectionIndex != 0 && sectionSpacing > 0.dp) {
|
||||
item(key = "sectionSpacer_$sectionIndex") {
|
||||
Spacer(Modifier.height(sectionSpacing))
|
||||
Spacer(
|
||||
Modifier
|
||||
.animateItem()
|
||||
.height(sectionSpacing)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -106,12 +118,18 @@ fun ExpressiveLazyColumn(
|
||||
|
||||
item(key = node.key, contentType = node.contentType) {
|
||||
ExpressiveCard(
|
||||
topRadius,
|
||||
bottomRadius,
|
||||
horizontalPadding,
|
||||
verticalSpacing,
|
||||
content = node.content
|
||||
)
|
||||
topRadius = topRadius,
|
||||
bottomRadius = bottomRadius,
|
||||
horizontalPadding = horizontalPadding,
|
||||
verticalSpacing = verticalSpacing,
|
||||
modifier = Modifier.animateItem(
|
||||
fadeInSpec = spring(),
|
||||
fadeOutSpec = spring(),
|
||||
placementSpec = spring()
|
||||
)
|
||||
) {
|
||||
node.content(this@item)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -140,23 +158,3 @@ fun ExpressiveCard(
|
||||
modifier = modifier.padding(horizontal = horizontalPadding, vertical = verticalSpacing)
|
||||
) { content() }
|
||||
}
|
||||
|
||||
@Composable
|
||||
@Preview
|
||||
private fun ExpressiveLazyColumnPreview() {
|
||||
ExpressiveLazyColumn {
|
||||
section {
|
||||
item {
|
||||
TextPreference(text = "item one")
|
||||
}
|
||||
item {
|
||||
TextPreference(text = "item two")
|
||||
}
|
||||
}
|
||||
section {
|
||||
item {
|
||||
TextPreference(text = "next section started")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user