Modern Apps git

← Modern-Apps.git

photos: start including videos

Commit
9c94903efda79a9f304e0b17bec300ce14d1f79c
Author
Vayun Mathur <[email protected]>
When
2026-02-16 21:56 UTC (9 months ago)
Parents
df85cd4

Diff

photos/schemas/com.vayunmathur.photos.data.PhotoDatabase/1.json
diff --git a/photos/schemas/com.vayunmathur.photos.data.PhotoDatabase/1.json b/photos/schemas/com.vayunmathur.photos.data.PhotoDatabase/1.jsonnew file mode 100644index 0000000000..dfd026ae01--- /dev/null+++ b/photos/schemas/com.vayunmathur.photos.data.PhotoDatabase/1.json@@ -0,0 +1,88 @@+{+  "formatVersion": 1,+  "database": {+    "version": 1,+    "identityHash": "d8797eb490c4ac6ab35c015b82d611c9",+    "entities": [+      {+        "tableName": "Photo",+        "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `name` TEXT NOT NULL, `uri` TEXT NOT NULL, `date` INTEGER NOT NULL, `width` INTEGER NOT NULL, `height` INTEGER NOT NULL, `exifSet` INTEGER NOT NULL, `lat` REAL, `long` REAL, `position` REAL NOT NULL, `duration` INTEGER)",+        "fields": [+          {+            "fieldPath": "id",+            "columnName": "id",+            "affinity": "INTEGER",+            "notNull": true+          },+          {+            "fieldPath": "name",+            "columnName": "name",+            "affinity": "TEXT",+            "notNull": true+          },+          {+            "fieldPath": "uri",+            "columnName": "uri",+            "affinity": "TEXT",+            "notNull": true+          },+          {+            "fieldPath": "date",+            "columnName": "date",+            "affinity": "INTEGER",+            "notNull": true+          },+          {+            "fieldPath": "width",+            "columnName": "width",+            "affinity": "INTEGER",+            "notNull": true+          },+          {+            "fieldPath": "height",+            "columnName": "height",+            "affinity": "INTEGER",+            "notNull": true+          },+          {+            "fieldPath": "exifSet",+            "columnName": "exifSet",+            "affinity": "INTEGER",+            "notNull": true+          },+          {+            "fieldPath": "lat",+            "columnName": "lat",+            "affinity": "REAL"+          },+          {+            "fieldPath": "long",+            "columnName": "long",+            "affinity": "REAL"+          },+          {+            "fieldPath": "position",+            "columnName": "position",+            "affinity": "REAL",+            "notNull": true+          },+          {+            "fieldPath": "videoData.duration",+            "columnName": "duration",+            "affinity": "INTEGER"+          }+        ],+        "primaryKey": {+          "autoGenerate": true,+          "columnNames": [+            "id"+          ]+        }+      }+    ],+    "setupQueries": [+      "CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",+      "INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, 'd8797eb490c4ac6ab35c015b82d611c9')"+    ]+  }+}\ No newline at end of file
photos/src/main/java/com/vayunmathur/photos/data/Photo.kt
diff --git a/photos/src/main/java/com/vayunmathur/photos/data/Photo.kt b/photos/src/main/java/com/vayunmathur/photos/data/Photo.ktindex b0c7b14a03..8d1c00564f 100644--- a/photos/src/main/java/com/vayunmathur/photos/data/Photo.kt+++ b/photos/src/main/java/com/vayunmathur/photos/data/Photo.kt@@ -1,10 +1,14 @@ package com.vayunmathur.photos.data +import androidx.room.Embedded import androidx.room.Entity import androidx.room.PrimaryKey import com.vayunmathur.library.util.DatabaseItem import kotlinx.serialization.Serializable +@Serializable+data class VideoData(val duration: Long)+ @Serializable @Entity data class Photo(@@ -17,6 +21,8 @@ data class Photo(     val exifSet: Boolean,     val lat: Double?,     val long: Double?,+    @Embedded+    val videoData: VideoData?,     override val position: Double = 0.0 ) : DatabaseItem<Photo>() {     override fun withPosition(position: Double): Photo = copy(position = position)
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 434091ed60..0c59bfef24 100644--- a/photos/src/main/java/com/vayunmathur/photos/ui/GalleryPage.kt+++ b/photos/src/main/java/com/vayunmathur/photos/ui/GalleryPage.kt@@ -31,6 +31,7 @@ import com.vayunmathur.photos.ImageLoader import com.vayunmathur.photos.NavigationBar import com.vayunmathur.photos.Route import com.vayunmathur.photos.data.Photo+import com.vayunmathur.photos.data.VideoData import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.async@@ -96,6 +97,8 @@ fun GalleryPage(backStack: NavBackStack<Route>, viewModel: DatabaseViewModel) {   fun setAllPhotos(context: Context, viewModel: DatabaseViewModel) {+    val photos = mutableListOf<Photo>()+     val projection = arrayOf(         MediaStore.Images.Media._ID,         MediaStore.Images.Media.DISPLAY_NAME,@@ -109,8 +112,6 @@ fun setAllPhotos(context: Context, viewModel: DatabaseViewModel) {         null, null, null     ) -    val photos = mutableListOf<Photo>()-     query?.use { cursor ->         val idColumn = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID)         val nameColumn = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DISPLAY_NAME)@@ -126,9 +127,45 @@ fun setAllPhotos(context: Context, viewModel: DatabaseViewModel) {             val width = cursor.getInt(widthColumn)             val height = cursor.getInt(heightColumn) -            photos += Photo(id, name, contentUri.toString(), date, width, height, false, null, null)+            photos += Photo(id, name, contentUri.toString(), date, width, height, false, null, null, null)+        }+    }++    val projectionVideo = arrayOf(+        MediaStore.Video.Media._ID,+        MediaStore.Video.Media.DISPLAY_NAME,+        MediaStore.Video.Media.INFERRED_DATE,+        MediaStore.Video.Media.WIDTH,+        MediaStore.Video.Media.HEIGHT,+        MediaStore.Video.Media.DURATION+    )+    val queryVideo = context.contentResolver.query(+        MediaStore.Video.Media.EXTERNAL_CONTENT_URI,+        projectionVideo,+        null, null, null+    )++    queryVideo?.use { cursor ->+        val idColumn = cursor.getColumnIndexOrThrow(MediaStore.Video.Media._ID)+        val nameColumn = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME)+        val dateColumn = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.INFERRED_DATE)+        val widthColumn = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.WIDTH)+        val heightColumn = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.HEIGHT)+        val durationColumn = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DURATION)++        while (cursor.moveToNext()) {+            val id = cursor.getLong(idColumn)+            val contentUri = ContentUris.withAppendedId(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, id)+            val name = cursor.getString(nameColumn)+            val date = cursor.getLong(dateColumn)+            val width = cursor.getInt(widthColumn)+            val height = cursor.getInt(heightColumn)+            val duration = cursor.getLong(durationColumn)++            photos += Photo(id, name, contentUri.toString(), date, width, height, false, null, null, VideoData(duration))         }     }+     viewModel.replaceAll(photos.sortedByDescending { it.date }) }