photos: load faster & load image immediately if recieved intent from camera
- Commit
7c2babe8685501b4b15dee4e6ed78307d8f7e5b3- Author
- Vayun Mathur <[email protected]>
- When
- 2026-07-16 17:57 UTC (4 months ago)
- Parents
d7436b5
Diff
photos/src/main/java/com/vayunmathur/photos/MainActivity.kt
diff --git a/photos/src/main/java/com/vayunmathur/photos/MainActivity.kt b/photos/src/main/java/com/vayunmathur/photos/MainActivity.ktindex 5ad16f0946..44e3dcf566 100644--- a/photos/src/main/java/com/vayunmathur/photos/MainActivity.kt+++ b/photos/src/main/java/com/vayunmathur/photos/MainActivity.kt@@ -1,6 +1,7 @@ package com.vayunmathur.photos import android.Manifest+import android.content.ContentUris import android.content.Context import android.content.Intent import android.net.Uri@@ -223,7 +224,7 @@ sealed interface Route: NavKey { data object Gallery: Route @Serializable- data class PhotoPage(val id: Long, val overridePhotosList: List<Photo>?): Route+ data class PhotoPage(val id: Long, val overridePhotosList: List<Photo>?, val pendingUri: String? = null): Route @Serializable data object Map: Route@@ -249,14 +250,19 @@ fun Navigation( val vaultPhotoDao by secureFolderViewModel.vaultPhotoDao.collectAsState() val vaultPassword by secureFolderViewModel.vaultPassword.collectAsState() - // Opened via ACTION_VIEW from another app: index the URI into the gallery- // DB, then open it in the regular swipeable PhotoPage (full gallery as the- // pager so the user can swipe to other photos). Done once per URI.+ // Opened via ACTION_VIEW from another app (e.g. the camera): open the+ // swipeable PhotoPage immediately, rendering the incoming URI directly so+ // there's no wait. The MediaStore _id in a content URI equals Photo.id, so+ // once the background index writes the row (and the full sync populates the+ // rest of the library for swiping), PhotoPage reconciles to the DB-backed+ // pager. Done once per URI, even across configuration changes.+ var handledViewUri by rememberSaveable { mutableStateOf(false) } LaunchedEffect(viewUri) {- if (viewUri != null) {- galleryViewModel.resolveAndIndex(viewUri) { id ->- if (id != null) backStack.add(Route.PhotoPage(id, null))- }+ if (viewUri != null && !handledViewUri) {+ handledViewUri = true+ val parsedId = runCatching { ContentUris.parseId(viewUri) }.getOrNull() ?: -1L+ backStack.add(Route.PhotoPage(parsedId, null, viewUri.toString()))+ galleryViewModel.resolveAndIndex(viewUri) {} } } @@ -274,7 +280,7 @@ fun Navigation( } entry<Route.PhotoPage> {- PhotoPage(galleryViewModel, photoMapViewModel, it.id, it.overridePhotosList)+ PhotoPage(galleryViewModel, photoMapViewModel, it.id, it.overridePhotosList, it.pendingUri) } entry<Route.Trash> {
photos/src/main/java/com/vayunmathur/photos/data/PhotoDatabase.kt
diff --git a/photos/src/main/java/com/vayunmathur/photos/data/PhotoDatabase.kt b/photos/src/main/java/com/vayunmathur/photos/data/PhotoDatabase.ktindex 5dbe842ec1..60fbde0ac1 100644--- a/photos/src/main/java/com/vayunmathur/photos/data/PhotoDatabase.kt+++ b/photos/src/main/java/com/vayunmathur/photos/data/PhotoDatabase.kt@@ -11,7 +11,11 @@ import kotlinx.coroutines.flow.Flow @Dao interface PhotoDao {- @Query("SELECT * FROM Photo")+ // Excludes the heavy clipEmbedding BLOB and ocrText, which the gallery/photo+ // UI never reads, so the first emission on cold start is fast (loading those+ // for a large library is what left the grid blank). Semantic search reads+ // embeddings via getClipEmbeddings(); OCR text is read via getAll().+ @Query("SELECT id, name, uri, date, width, height, dateModified, exifSet, lat, `long`, duration, fullWidth, fullHeight, croppedWidth, croppedHeight, croppedLeft, croppedTop, isTrashed, faceScanned, ocrScanned, clipScanned FROM Photo") fun getAllFlow(): Flow<List<Photo>> @Query("SELECT * FROM Photo WHERE id = :id")
photos/src/main/java/com/vayunmathur/photos/ui/PhotoPage.kt
diff --git a/photos/src/main/java/com/vayunmathur/photos/ui/PhotoPage.kt b/photos/src/main/java/com/vayunmathur/photos/ui/PhotoPage.ktindex f0b126778e..5376555cfd 100644--- a/photos/src/main/java/com/vayunmathur/photos/ui/PhotoPage.kt+++ b/photos/src/main/java/com/vayunmathur/photos/ui/PhotoPage.kt@@ -87,19 +87,35 @@ import kotlinx.datetime.toLocalDateTime data class ZoomState(val scale: Float = 1f, val offset: Offset = Offset.Zero) @Composable-fun PhotoPage(galleryViewModel: GalleryViewModel, photoMapViewModel: PhotoMapViewModel, id: Long, overridePhotosList: List<Photo>?) {+fun PhotoPage(galleryViewModel: GalleryViewModel, photoMapViewModel: PhotoMapViewModel, id: Long, overridePhotosList: List<Photo>?, pendingUri: String? = null) { val photosAll by galleryViewModel.photos.collectAsState() val photos = overridePhotosList ?: photosAll.filter { !it.isTrashed } val context = LocalContext.current val photosSorted = remember(photos) { photos.sortedByDescending { it.date } } val matchedCounts by galleryViewModel.faceCountByPhoto.collectAsState() + // Resolve the page to open by id, falling back to the incoming view-intent+ // URI. A freshly received photo may not be indexed into the DB yet, so it+ // won't be in the list until the background sync writes its row. val initialIndex =- remember(photosSorted, id) {- val index = photosSorted.indexOfFirst { it.id == id }- if (index == -1) 0 else index+ remember(photosSorted, id, pendingUri) {+ var index = photosSorted.indexOfFirst { it.id == id }+ if (index == -1 && pendingUri != null) {+ index = photosSorted.indexOfFirst { it.uri == pendingUri }+ }+ index } + // Not in the library yet: show the incoming image directly so the viewer+ // opens instantly. Once indexing adds the row, this recomposes into the+ // swipeable pager below (initialIndex becomes valid).+ if (initialIndex == -1) {+ if (pendingUri != null) {+ PendingPhotoView(uri = pendingUri, context = context)+ }+ return+ }+ var isMetadataVisible by remember { mutableStateOf(true) } var refreshKey by remember { mutableIntStateOf(0) }@@ -159,6 +175,24 @@ fun PhotoPage(galleryViewModel: GalleryViewModel, photoMapViewModel: PhotoMapVie } } +/**+ * Minimal full-screen viewer for a photo that just arrived via ACTION_VIEW but+ * isn't in the gallery DB yet. Renders the URI directly so the app opens on the+ * image with no delay; [PhotoPage] swaps to the full swipeable pager as soon as+ * the background index writes the row.+ */+@Composable+private fun PendingPhotoView(uri: String, context: Context) {+ Scaffold(containerColor = Color.Black) { paddingValues ->+ AsyncImage(+ model = ImageRequest.Builder(context).data(uri.toUri()).build(),+ contentDescription = null,+ modifier = Modifier.fillMaxSize().padding(paddingValues),+ contentScale = ContentScale.Fit,+ )+ }+}+ @Composable fun PhotoDetailView( photo: Photo,