photos: zoom in and out of gallery grid
- Commit
62ae491cae4779e17132591d7dffe9be800333f7- Author
- Vayun Mathur <[email protected]>
- When
- 2026-04-12 04:24 UTC (7 months ago)
- Parents
f2b6fce
Diff
photos/src/main/java/com/vayunmathur/photos/ui/GalleryPage.kt
diff --git a/photos/src/main/java/com/vayunmathur/photos/ui/GalleryPage.kt b/photos/src/main/java/com/vayunmathur/photos/ui/GalleryPage.ktindex 23e67bd68b..0550697f76 100644--- a/photos/src/main/java/com/vayunmathur/photos/ui/GalleryPage.kt+++ b/photos/src/main/java/com/vayunmathur/photos/ui/GalleryPage.kt@@ -3,8 +3,13 @@ package com.vayunmathur.photos.ui import android.content.ContentUris import android.content.Context import android.provider.MediaStore+import androidx.compose.foundation.gestures.awaitEachGesture+import androidx.compose.foundation.gestures.calculateZoom+import androidx.compose.foundation.gestures.detectTransformGestures import androidx.compose.foundation.layout.Arrangement+import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.aspectRatio+import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.padding import androidx.compose.foundation.lazy.grid.GridCells@@ -19,8 +24,13 @@ import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.collectAsState import androidx.compose.runtime.derivedStateOf import androidx.compose.runtime.getValue+import androidx.compose.runtime.mutableFloatStateOf import androidx.compose.runtime.remember+import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier+import androidx.compose.ui.input.pointer.PointerEventPass+import androidx.compose.ui.input.pointer.changedToUp+import androidx.compose.ui.input.pointer.pointerInput import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.unit.dp import androidx.core.database.getLongOrNull@@ -43,12 +53,14 @@ import kotlinx.datetime.LocalDate import kotlinx.datetime.TimeZone import kotlinx.datetime.format.MonthNames import kotlinx.datetime.toLocalDateTime+import kotlin.math.roundToInt import kotlin.time.Instant @Composable fun GalleryPage(backStack: NavBackStack<Route>, viewModel: DatabaseViewModel) { val photos by viewModel.data<Photo>().collectAsState() val context = LocalContext.current+ var columnCount by remember { mutableFloatStateOf(3f) } LaunchedEffect(Unit) { withContext(Dispatchers.IO) {@@ -68,25 +80,55 @@ fun GalleryPage(backStack: NavBackStack<Route>, viewModel: DatabaseViewModel) { } } Scaffold(bottomBar = { NavigationBar(Route.Gallery, backStack) }) { paddingValues ->- LazyVerticalGrid(- GridCells.Adaptive(100.dp),- Modifier.padding(paddingValues),- horizontalArrangement = Arrangement.spacedBy(4.dp),- verticalArrangement = Arrangement.spacedBy(4.dp)- ) {- photosGroupedByMonth.forEach { (month, photos) ->- item(span = {- GridItemSpan(maxLineSpan)- }) {- Text(- month,- Modifier.padding(top = 16.dp, bottom = 8.dp),- style = MaterialTheme.typography.titleLarge- )+ Box(+ modifier = Modifier+ .fillMaxSize()+ .pointerInput(Unit) {+ awaitEachGesture {+ // Initial pass lets the parent intercept events+ // BEFORE the clickable items in the grid consume them+ while (true) {+ val event = awaitPointerEvent(PointerEventPass.Initial)++ if (event.changes.size > 1) {+ val zoom = event.calculateZoom()+ if (zoom != 1f) {+ // Update your state+ columnCount = (columnCount / zoom).coerceIn(2f, 8f)++ // Consume the changes so the grid doesn't scroll+ // while you are zooming+ event.changes.forEach { it.consume() }+ }+ }++ // Break the loop if all pointers are up+ if (event.changes.all { it.changedToUp() }) break+ }+ } }- items(photos, { it.id }, contentType = { "photo_thumbnail" }) {- ImageLoader.PhotoItem(it, Modifier.fillMaxWidth().aspectRatio(1f)) {- backStack.add(Route.PhotoPage(it.id, null))+ ) {+ LazyVerticalGrid(+ GridCells.Fixed(columnCount.roundToInt().coerceIn(2, 8)),+ Modifier+ .padding(paddingValues),+ horizontalArrangement = Arrangement.spacedBy(4.dp),+ verticalArrangement = Arrangement.spacedBy(4.dp)+ ) {+ photosGroupedByMonth.forEach { (month, photos) ->+ item(span = {+ GridItemSpan(maxLineSpan)+ }) {+ Text(+ month,+ Modifier.padding(top = 16.dp, bottom = 8.dp),+ style = MaterialTheme.typography.titleLarge+ )+ }+ items(photos, { it.id }, contentType = { "photo_thumbnail" }) {+ ImageLoader.PhotoItem(it, Modifier.fillMaxWidth().aspectRatio(1f)) {+ backStack.add(Route.PhotoPage(it.id, null))+ } } } }