From 9961674078cc33162cb4e16247918e64402cf89d Mon Sep 17 00:00:00 2001 From: "dima.avdeev" <99798741+dima-avdeev-jb@users.noreply.github.com> Date: Mon, 10 Apr 2023 14:29:20 +0300 Subject: [PATCH] ImageViewer, simplify pictures.indexOf(picture) (#3024) --- .../example/imageviewer/ImageViewer.common.kt | 4 +- .../example/imageviewer/view/MemoryScreen.kt | 52 ++++++++----------- 2 files changed, 24 insertions(+), 32 deletions(-) diff --git a/examples/imageviewer/shared/src/commonMain/kotlin/example/imageviewer/ImageViewer.common.kt b/examples/imageviewer/shared/src/commonMain/kotlin/example/imageviewer/ImageViewer.common.kt index 24ca0c24c7..3901bc53e7 100644 --- a/examples/imageviewer/shared/src/commonMain/kotlin/example/imageviewer/ImageViewer.common.kt +++ b/examples/imageviewer/shared/src/commonMain/kotlin/example/imageviewer/ImageViewer.common.kt @@ -94,8 +94,8 @@ fun ImageViewerWithProvidedDependencies( MemoryScreen( pictures = pictures, memoryPage = page, - onSelectRelatedMemory = { picture: PictureData -> - navigationStack.push(MemoryPage(pictures.indexOf(picture))) + onSelectRelatedMemory = { pictureIndex -> + navigationStack.push(MemoryPage(pictureIndex)) }, onBack = { resetNavigation -> if (resetNavigation) { diff --git a/examples/imageviewer/shared/src/commonMain/kotlin/example/imageviewer/view/MemoryScreen.kt b/examples/imageviewer/shared/src/commonMain/kotlin/example/imageviewer/view/MemoryScreen.kt index 61bb2eb13a..30dcc37ffe 100644 --- a/examples/imageviewer/shared/src/commonMain/kotlin/example/imageviewer/view/MemoryScreen.kt +++ b/examples/imageviewer/shared/src/commonMain/kotlin/example/imageviewer/view/MemoryScreen.kt @@ -7,7 +7,7 @@ import androidx.compose.foundation.* import androidx.compose.foundation.interaction.MutableInteractionSource import androidx.compose.foundation.layout.* import androidx.compose.foundation.lazy.LazyRow -import androidx.compose.foundation.lazy.itemsIndexed +import androidx.compose.foundation.lazy.items import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.material.* import androidx.compose.material.icons.Icons @@ -43,7 +43,7 @@ import org.jetbrains.compose.resources.ExperimentalResourceApi fun MemoryScreen( pictures: SnapshotStateList, memoryPage: MemoryPage, - onSelectRelatedMemory: (picture: PictureData) -> Unit, + onSelectRelatedMemory: (pictureIndex: Int) -> Unit, onBack: (resetNavigation: Boolean) -> Unit, onHeaderClick: (index: Int) -> Unit, ) { @@ -87,10 +87,26 @@ fun MemoryScreen( Headliner("Note") Collapsible(picture.description, onEdit = { edit = true }) Headliner("Related memories") - RelatedMemoriesVisualizer( - pictures = remember { (pictures - picture).shuffled().take(8) }, - onSelectRelatedMemory = onSelectRelatedMemory - ) + val shuffledIndices = remember { + (pictures.indices.toList() - memoryPage.pictureIndex).shuffled().take(8) + } + LazyRow( + modifier = Modifier + .padding(10.dp, 0.dp) + .clip(RoundedCornerShape(10.dp)) + .fillMaxWidth(), + horizontalArrangement = Arrangement.spacedBy(8.dp) + ) { + items(items = shuffledIndices) { index -> + Box(Modifier.size(130.dp).clip(RoundedCornerShape(8.dp))) { + SquareThumbnail( + picture = pictures[index], + isHighlighted = false, + onClick = { onSelectRelatedMemory(index) } + ) + } + } + } Headliner("Place") val locationShape = RoundedCornerShape(10.dp) LocationVisualizer( @@ -271,27 +287,3 @@ fun Headliner(s: String) { modifier = Modifier.padding(start = 12.dp, top = 32.dp, end = 12.dp, bottom = 16.dp) ) } - -@Composable -fun RelatedMemoriesVisualizer( - pictures: List, - onSelectRelatedMemory: (picture: PictureData) -> Unit -) { - Box( - modifier = Modifier.padding(10.dp, 0.dp).clip(RoundedCornerShape(10.dp)).fillMaxWidth() - ) { - LazyRow( - modifier = Modifier.fillMaxSize(), - horizontalArrangement = Arrangement.spacedBy(8.dp) - ) { - itemsIndexed(pictures) { index, item -> - Box(Modifier.size(130.dp).clip(RoundedCornerShape(8.dp))) { - SquareThumbnail( - picture = item, - isHighlighted = false, - onClick = { onSelectRelatedMemory(item) }) - } - } - } - } -}