From 8a90864e941660c58262457bea3ae30937a363ca Mon Sep 17 00:00:00 2001 From: shimeoki Date: Sat, 4 Apr 2026 00:19:41 +0300 Subject: [PATCH] feat: gallery video duration --- .../features/gallery/GalleryMediaQueries.kt | 10 ++++++---- .../features/gallery/GalleryModels.kt | 7 +++++-- .../gallery/components/GalleryMediaGrid.kt | 15 ++++++++++++++- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/presentation/src/main/java/org/monogram/presentation/features/gallery/GalleryMediaQueries.kt b/presentation/src/main/java/org/monogram/presentation/features/gallery/GalleryMediaQueries.kt index 6c21143d..1f596e9b 100644 --- a/presentation/src/main/java/org/monogram/presentation/features/gallery/GalleryMediaQueries.kt +++ b/presentation/src/main/java/org/monogram/presentation/features/gallery/GalleryMediaQueries.kt @@ -33,7 +33,7 @@ fun queryImages(context: Context): List { cursor.getLong(idColumn) ), dateAdded = cursor.getLong(dateColumn), - isVideo = false, + duration = 0, bucketName = bucket, relativePath = relative, isCamera = isCameraBucket(bucket, relative), @@ -51,7 +51,8 @@ fun queryVideos(context: Context): List { MediaStore.Video.Media._ID, MediaStore.Video.Media.DATE_ADDED, MediaStore.Video.Media.BUCKET_DISPLAY_NAME, - MediaStore.Video.Media.RELATIVE_PATH + MediaStore.Video.Media.RELATIVE_PATH, + MediaStore.Video.Media.DURATION ) context.contentResolver.query( MediaStore.Video.Media.EXTERNAL_CONTENT_URI, @@ -64,6 +65,7 @@ fun queryVideos(context: Context): List { val dateColumn = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATE_ADDED) val bucketColumn = cursor.getColumnIndex(MediaStore.Video.Media.BUCKET_DISPLAY_NAME) val relColumn = cursor.getColumnIndex(MediaStore.Video.Media.RELATIVE_PATH) + val durationColumn = cursor.getColumnIndex(MediaStore.Video.Media.DURATION) while (cursor.moveToNext()) { val bucket = if (bucketColumn != -1) cursor.getString(bucketColumn).orEmpty() else "" val relative = if (relColumn != -1) cursor.getString(relColumn).orEmpty() else "" @@ -74,7 +76,7 @@ fun queryVideos(context: Context): List { cursor.getLong(idColumn) ), dateAdded = cursor.getLong(dateColumn), - isVideo = true, + duration = cursor.getLong(durationColumn), bucketName = bucket, relativePath = relative, isCamera = isCameraBucket(bucket, relative), @@ -96,4 +98,4 @@ private fun isScreenshotsBucket(bucket: String, relativePath: String): Boolean { val b = bucket.lowercase() val p = relativePath.lowercase() return b.contains("screenshot") || p.contains("screenshots") -} \ No newline at end of file +} diff --git a/presentation/src/main/java/org/monogram/presentation/features/gallery/GalleryModels.kt b/presentation/src/main/java/org/monogram/presentation/features/gallery/GalleryModels.kt index 7d877dcd..5aa953ad 100644 --- a/presentation/src/main/java/org/monogram/presentation/features/gallery/GalleryModels.kt +++ b/presentation/src/main/java/org/monogram/presentation/features/gallery/GalleryModels.kt @@ -18,9 +18,12 @@ sealed class BucketFilter(val key: String) { data class GalleryMediaItem( val uri: Uri, val dateAdded: Long, - val isVideo: Boolean, + val duration: Long, val bucketName: String, val relativePath: String, val isCamera: Boolean, val isScreenshot: Boolean -) \ No newline at end of file +) { + val isVideo: Boolean + get() = duration > 0 +} diff --git a/presentation/src/main/java/org/monogram/presentation/features/gallery/components/GalleryMediaGrid.kt b/presentation/src/main/java/org/monogram/presentation/features/gallery/components/GalleryMediaGrid.kt index f16b729c..7df52f8a 100644 --- a/presentation/src/main/java/org/monogram/presentation/features/gallery/components/GalleryMediaGrid.kt +++ b/presentation/src/main/java/org/monogram/presentation/features/gallery/components/GalleryMediaGrid.kt @@ -29,6 +29,19 @@ import coil3.compose.AsyncImage import org.monogram.presentation.R import org.monogram.presentation.features.gallery.GalleryMediaItem +private fun formatDuration(ms: Long): String { + val sec = ms / 1000 + val h = sec / 3600 + val m = (sec % 3600) / 60 + val s = sec % 60 + + return if (h > 0) { + "%d:%02d:%02d".format(h, m, s) + } else { + "%02d:%02d".format(m, s) + } +} + @Composable fun GalleryGrid( media: List, @@ -97,7 +110,7 @@ fun GalleryGrid( color = MaterialTheme.colorScheme.surface.copy(alpha = 0.85f) ) { Text( - text = stringResource(R.string.media_type_video), + text = formatDuration(item.duration), style = MaterialTheme.typography.labelSmall, modifier = Modifier.padding(horizontal = 6.dp, vertical = 2.dp) )