Browse Source

ImageViewer, simplify pictures.indexOf(picture) (#3024)

pull/3027/head
dima.avdeev 2 years ago committed by GitHub
parent
commit
9961674078
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      examples/imageviewer/shared/src/commonMain/kotlin/example/imageviewer/ImageViewer.common.kt
  2. 52
      examples/imageviewer/shared/src/commonMain/kotlin/example/imageviewer/view/MemoryScreen.kt

4
examples/imageviewer/shared/src/commonMain/kotlin/example/imageviewer/ImageViewer.common.kt

@ -94,8 +94,8 @@ fun ImageViewerWithProvidedDependencies(
MemoryScreen( MemoryScreen(
pictures = pictures, pictures = pictures,
memoryPage = page, memoryPage = page,
onSelectRelatedMemory = { picture: PictureData -> onSelectRelatedMemory = { pictureIndex ->
navigationStack.push(MemoryPage(pictures.indexOf(picture))) navigationStack.push(MemoryPage(pictureIndex))
}, },
onBack = { resetNavigation -> onBack = { resetNavigation ->
if (resetNavigation) { if (resetNavigation) {

52
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.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.* import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyRow 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.foundation.shape.RoundedCornerShape
import androidx.compose.material.* import androidx.compose.material.*
import androidx.compose.material.icons.Icons import androidx.compose.material.icons.Icons
@ -43,7 +43,7 @@ import org.jetbrains.compose.resources.ExperimentalResourceApi
fun MemoryScreen( fun MemoryScreen(
pictures: SnapshotStateList<PictureData>, pictures: SnapshotStateList<PictureData>,
memoryPage: MemoryPage, memoryPage: MemoryPage,
onSelectRelatedMemory: (picture: PictureData) -> Unit, onSelectRelatedMemory: (pictureIndex: Int) -> Unit,
onBack: (resetNavigation: Boolean) -> Unit, onBack: (resetNavigation: Boolean) -> Unit,
onHeaderClick: (index: Int) -> Unit, onHeaderClick: (index: Int) -> Unit,
) { ) {
@ -87,10 +87,26 @@ fun MemoryScreen(
Headliner("Note") Headliner("Note")
Collapsible(picture.description, onEdit = { edit = true }) Collapsible(picture.description, onEdit = { edit = true })
Headliner("Related memories") Headliner("Related memories")
RelatedMemoriesVisualizer( val shuffledIndices = remember {
pictures = remember { (pictures - picture).shuffled().take(8) }, (pictures.indices.toList() - memoryPage.pictureIndex).shuffled().take(8)
onSelectRelatedMemory = onSelectRelatedMemory }
) 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") Headliner("Place")
val locationShape = RoundedCornerShape(10.dp) val locationShape = RoundedCornerShape(10.dp)
LocationVisualizer( LocationVisualizer(
@ -271,27 +287,3 @@ fun Headliner(s: String) {
modifier = Modifier.padding(start = 12.dp, top = 32.dp, end = 12.dp, bottom = 16.dp) modifier = Modifier.padding(start = 12.dp, top = 32.dp, end = 12.dp, bottom = 16.dp)
) )
} }
@Composable
fun RelatedMemoriesVisualizer(
pictures: List<PictureData>,
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) })
}
}
}
}
}

Loading…
Cancel
Save