Modern Apps git

← Modern-Apps.git

photos: advance to next photo after deleting from the viewer

Commit
c00af15d8cf12a0e3c032d96c4289980ee17391d
Author
Vayun Mathur <[email protected]>
When
2026-07-30 18:13 UTC (4 months ago)
Parents
5b7842c

Diff

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 2214b5276c..671bd19381 100644--- a/photos/src/main/java/com/vayunmathur/photos/data/PhotoDatabase.kt+++ b/photos/src/main/java/com/vayunmathur/photos/data/PhotoDatabase.kt@@ -36,6 +36,9 @@ interface PhotoDao {     @Query("DELETE FROM Photo WHERE id IN (:ids)")     suspend fun deleteByIds(ids: List<Long>) +    @Query("UPDATE Photo SET isTrashed = 1 WHERE id = :id")+    suspend fun setTrashed(id: Long)+     @Query("SELECT * FROM Photo WHERE isTrashed = 0 AND (ocrText LIKE '%' || :query || '%' OR name LIKE '%' || :query || '%') ORDER BY date DESC")     suspend fun searchPhotos(query: String): List<Photo> 
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 dafc0f5344..b415b1c55f 100644--- a/photos/src/main/java/com/vayunmathur/photos/ui/PhotoPage.kt+++ b/photos/src/main/java/com/vayunmathur/photos/ui/PhotoPage.kt@@ -44,6 +44,7 @@ import androidx.compose.runtime.derivedStateOf import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableFloatStateOf import androidx.compose.runtime.mutableIntStateOf+import androidx.compose.runtime.mutableStateListOf import androidx.compose.runtime.mutableStateMapOf import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember@@ -103,22 +104,36 @@ fun PhotoPage(galleryViewModel: GalleryViewModel, photoMapViewModel: PhotoMapVie     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 } }+    // Photos deleted from within this viewer, hidden immediately so the pager+    // advances to the next photo without waiting for the MediaStore resync.+    val locallyDeleted = remember { mutableStateListOf<Long>() }+    val photosSorted = remember(photos, locallyDeleted.toList()) {+        photos.asSequence()+            .filter { it.id !in locallyDeleted }+            .sortedByDescending { it.date }+            .toList()+    }     val matchedCounts by galleryViewModel.faceCountByPhoto.collectAsState()      // In-viewer delete: move the current photo to the system trash via the same     // MediaStore IntentSender flow the grid uses. MANAGE_MEDIA (enforced at app-    // start) means no per-item confirmation popup. On success we resync; if the-    // photo just deleted was the only one on screen, leave the viewer.+    // start) means no per-item confirmation popup. On success we hide it locally+    // (the pager falls through to the next photo) and trash it in the DB.+    var pendingDelete by remember { mutableStateOf<Photo?>(null) }     val deleteLauncher = rememberLauncherForActivityResult(         ActivityResultContracts.StartIntentSenderForResult()     ) { result ->         if (result.resultCode == Activity.RESULT_OK) {+            pendingDelete?.let { p ->+                locallyDeleted.add(p.id)+                galleryViewModel.trashPhotoLocally(p)+            }             galleryViewModel.runSync()-            if (photosSorted.size <= 1) backStack?.pop()         }+        pendingDelete = null     }     val onDeletePhoto: (Photo) -> Unit = { p ->+        pendingDelete = p         val pendingIntent = MediaStore.createTrashRequest(             context.contentResolver, listOf(p.uri.toUri()), true         )@@ -137,16 +152,27 @@ fun PhotoPage(galleryViewModel: GalleryViewModel, photoMapViewModel: PhotoMapVie                 index             } +    // Once the pager is showing, stay in it even if the originally-opened photo+    // leaves the list (e.g. the user just deleted it) — otherwise deleting the+    // current photo would blank the screen instead of advancing to the next.+    var hasEntered by remember { mutableStateOf(false) }+    if (initialIndex != -1) hasEntered = true+     // 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 (!hasEntered) {         if (pendingUri != null) {             PendingPhotoView(uri = pendingUri, context = context)         }         return     } +    // Deleting the last remaining photo empties the list — leave the viewer.+    LaunchedEffect(photosSorted.isEmpty()) {+        if (photosSorted.isEmpty()) backStack?.pop()+    }+     var isMetadataVisible by remember { mutableStateOf(true) }      var refreshKey by remember { mutableIntStateOf(0) }@@ -168,7 +194,7 @@ fun PhotoPage(galleryViewModel: GalleryViewModel, photoMapViewModel: PhotoMapVie      if (photosSorted.isNotEmpty()) {         val pagerState =-                rememberPagerState(initialPage = initialIndex, pageCount = { photosSorted.size })+                rememberPagerState(initialPage = initialIndex.coerceAtLeast(0), pageCount = { photosSorted.size })          Scaffold(containerColor = Color.Black) { paddingValues ->             HorizontalPager(
photos/src/main/java/com/vayunmathur/photos/util/GalleryViewModel.kt
diff --git a/photos/src/main/java/com/vayunmathur/photos/util/GalleryViewModel.kt b/photos/src/main/java/com/vayunmathur/photos/util/GalleryViewModel.ktindex fb43e3eecb..1a023c1e72 100644--- a/photos/src/main/java/com/vayunmathur/photos/util/GalleryViewModel.kt+++ b/photos/src/main/java/com/vayunmathur/photos/util/GalleryViewModel.kt@@ -245,6 +245,17 @@ class GalleryViewModel(         }     } +    /**+     * Move a photo to trash in the DB immediately (so the viewer/grid drop it+     * without waiting for the MediaStore resync). The file itself is trashed via+     * the MediaStore request in the UI; [runSync] later reconciles the rest.+     */+    fun trashPhotoLocally(photo: Photo) {+        viewModelScope.launch(Dispatchers.IO) {+            photoDao.setTrashed(photo.id)+        }+    }+     fun runSync() {         SyncWorker.runOnce(getApplication())     }