Skip to content

Commit 9abd23d

Browse files
committed
refactor: GroupedLazyList支持item
1 parent a4897cc commit 9abd23d

File tree

2 files changed

+37
-9
lines changed

2 files changed

+37
-9
lines changed

app/src/main/kotlin/ui/composable/GroupedLazyList.kt

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,23 @@ class GroupedLazyListScope(
134134
}
135135
}
136136

137+
fun item(
138+
key: Any? = null,
139+
contentType: Any? = null,
140+
content: @Composable LazyItemScope.() -> Unit,
141+
) {
142+
group { item(key, contentType, content) }
143+
}
144+
145+
inline fun <T> items(
146+
items: List<T>,
147+
crossinline key: (item: T) -> Any? = { null },
148+
crossinline contentType: (item: T) -> Any? = { null },
149+
crossinline itemContent: @Composable LazyItemScope.(T) -> Unit,
150+
) {
151+
group { items(items, key, contentType, itemContent) }
152+
}
153+
137154
fun LazyListScope.build() {
138155
val scope = this@GroupedLazyListScope
139156
scope.lock()
@@ -164,7 +181,20 @@ class GroupedLazyListScope(
164181
@LazyScopeMarker
165182
class LazyListGroupScope(
166183
private val scope: LazyListScope,
167-
) : LazyListScope by scope
184+
) : LazyListScope by scope {
185+
inline fun <T> items(
186+
items: List<T>,
187+
key: (item: T) -> Any? = { null },
188+
contentType: (item: T) -> Any? = { null },
189+
crossinline itemContent: @Composable LazyItemScope.(T) -> Unit,
190+
) {
191+
items.forEach { item ->
192+
item(key(item), contentType(item)) {
193+
itemContent(item)
194+
}
195+
}
196+
}
197+
}
168198

169199
private data class StickyHeaderContent(val contentType: Any?)
170200

app/src/main/kotlin/ui/page/Overview.kt

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,12 @@ object Overview : Main() {
7777
contentPadding = contentPadding,
7878
spacing = 16.dp,
7979
) {
80-
group {
81-
item {
82-
StatusCard(
83-
modifier = Modifier.padding(horizontal = 16.dp),
84-
isPreferencesReady = isPreferencesReady,
85-
isServiceConnected = service != null,
86-
)
87-
}
80+
item {
81+
StatusCard(
82+
modifier = Modifier.padding(horizontal = 16.dp),
83+
isPreferencesReady = isPreferencesReady,
84+
isServiceConnected = service != null,
85+
)
8886
}
8987

9088
categoryCards(

0 commit comments

Comments
 (0)