Skip to content

Commit 534103d

Browse files
committed
refactor: optimize LazyColumn items with key and contentType parameters
1 parent c76baaa commit 534103d

3 files changed

Lines changed: 17 additions & 11 deletions

File tree

‎core/ui/src/commonMain/kotlin/com/softartdev/notedelight/ui/files/FileListScreen.kt‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,11 @@ fun FileList(
122122
) {
123123
val listState = rememberLazyListState()
124124
LazyColumn(modifier = modifier, state = listState) {
125-
items(items = fileList) { fileName: String ->
125+
items(
126+
items = fileList,
127+
key = { fileName: String -> fileName },
128+
contentType = { "file" },
129+
) { fileName: String ->
126130
FileItem(fileName, onItemClicked)
127131
HorizontalDivider()
128132
}

‎core/ui/src/commonMain/kotlin/com/softartdev/notedelight/ui/main/NoteList.kt‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import androidx.compose.ui.tooling.preview.Preview
1111
import androidx.paging.PagingData
1212
import androidx.paging.compose.LazyPagingItems
1313
import androidx.paging.compose.collectAsLazyPagingItems
14+
import androidx.paging.compose.itemContentType
15+
import androidx.paging.compose.itemKey
1416
import com.softartdev.notedelight.db.TestSchema.firstNote
1517
import com.softartdev.notedelight.db.TestSchema.secondNote
1618
import com.softartdev.notedelight.db.TestSchema.thirdNote
@@ -24,7 +26,11 @@ fun NoteList(
2426
selectedNoteId: Long?
2527
) {
2628
LazyColumn {
27-
items(count = pagingItems.itemCount) { index ->
29+
items(
30+
count = pagingItems.itemCount,
31+
key = pagingItems.itemKey(Note::id),
32+
contentType = pagingItems.itemContentType { "note" },
33+
) { index ->
2834
when (val note: Note? = pagingItems[index]) {
2935
null -> LinearProgressIndicator(modifier = Modifier.fillMaxWidth())
3036
else -> NoteItem(note = note, onItemClicked = onItemClicked, selected = note.id == selectedNoteId)

‎core/ui/src/commonMain/kotlin/com/softartdev/notedelight/ui/settings/detail/SettingsDetailScreen.kt‎

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ import androidx.compose.runtime.LaunchedEffect
4141
import androidx.compose.runtime.State
4242
import androidx.compose.runtime.collectAsState
4343
import androidx.compose.runtime.derivedStateOf
44-
import androidx.compose.runtime.getValue
45-
import androidx.compose.runtime.mutableStateOf
4644
import androidx.compose.runtime.remember
4745
import androidx.compose.ui.Modifier
4846
import androidx.compose.ui.graphics.vector.ImageVector
@@ -311,13 +309,11 @@ private fun Preference(
311309
private fun AppVersionItem(appVersion: String?, onAction: (SettingsAction) -> Unit) {
312310
if (appVersion != null) {
313311
val defaultColors: ListItemColors = ListItemDefaults.colors()
314-
val disabledColors: ListItemColors by remember(defaultColors) {
315-
mutableStateOf(
316-
defaultColors.copy(
317-
headlineColor = defaultColors.disabledHeadlineColor,
318-
leadingIconColor = defaultColors.disabledLeadingIconColor,
319-
supportingTextColor = defaultColors.disabledHeadlineColor
320-
)
312+
val disabledColors: ListItemColors = remember(defaultColors) {
313+
defaultColors.copy(
314+
headlineColor = defaultColors.disabledHeadlineColor,
315+
leadingIconColor = defaultColors.disabledLeadingIconColor,
316+
supportingTextColor = defaultColors.disabledHeadlineColor
321317
)
322318
}
323319
ListItem(

0 commit comments

Comments
 (0)