Modern Apps git

← Modern-Apps.git

photo: open photo from map

Commit
b53b7783d0a2cbd3e32d9c273b8f07ce8b5f06ef
Author
Vayun <[email protected]>
When
2026-01-20 00:40 UTC (10 months ago)
Parents
2837d37

Diff

.idea/inspectionProfiles/Project_Default.xml
diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xmlnew file mode 100644index 0000000000..f0c6ad08eb--- /dev/null+++ b/.idea/inspectionProfiles/Project_Default.xml@@ -0,0 +1,50 @@+<component name="InspectionProjectProfileManager">+  <profile version="1.0">+    <option name="myName" value="Project Default" />+    <inspection_tool class="ComposePreviewDimensionRespectsLimit" enabled="true" level="WARNING" enabled_by_default="true">+      <option name="composableFile" value="true" />+    </inspection_tool>+    <inspection_tool class="ComposePreviewMustBeTopLevelFunction" enabled="true" level="ERROR" enabled_by_default="true">+      <option name="composableFile" value="true" />+    </inspection_tool>+    <inspection_tool class="ComposePreviewNeedsComposableAnnotation" enabled="true" level="ERROR" enabled_by_default="true">+      <option name="composableFile" value="true" />+    </inspection_tool>+    <inspection_tool class="ComposePreviewNotSupportedInUnitTestFiles" enabled="true" level="ERROR" enabled_by_default="true">+      <option name="composableFile" value="true" />+    </inspection_tool>+    <inspection_tool class="GlancePreviewDimensionRespectsLimit" enabled="true" level="WARNING" enabled_by_default="true">+      <option name="composableFile" value="true" />+    </inspection_tool>+    <inspection_tool class="GlancePreviewMustBeTopLevelFunction" enabled="true" level="ERROR" enabled_by_default="true">+      <option name="composableFile" value="true" />+    </inspection_tool>+    <inspection_tool class="GlancePreviewNeedsComposableAnnotation" enabled="true" level="ERROR" enabled_by_default="true">+      <option name="composableFile" value="true" />+    </inspection_tool>+    <inspection_tool class="GlancePreviewNotSupportedInUnitTestFiles" enabled="true" level="ERROR" enabled_by_default="true">+      <option name="composableFile" value="true" />+    </inspection_tool>+    <inspection_tool class="PreviewAnnotationInFunctionWithParameters" enabled="true" level="ERROR" enabled_by_default="true">+      <option name="composableFile" value="true" />+    </inspection_tool>+    <inspection_tool class="PreviewApiLevelMustBeValid" enabled="true" level="ERROR" enabled_by_default="true">+      <option name="composableFile" value="true" />+    </inspection_tool>+    <inspection_tool class="PreviewDeviceShouldUseNewSpec" enabled="true" level="WEAK WARNING" enabled_by_default="true">+      <option name="composableFile" value="true" />+    </inspection_tool>+    <inspection_tool class="PreviewFontScaleMustBeGreaterThanZero" enabled="true" level="ERROR" enabled_by_default="true">+      <option name="composableFile" value="true" />+    </inspection_tool>+    <inspection_tool class="PreviewMultipleParameterProviders" enabled="true" level="ERROR" enabled_by_default="true">+      <option name="composableFile" value="true" />+    </inspection_tool>+    <inspection_tool class="PreviewParameterProviderOnFirstParameter" enabled="true" level="ERROR" enabled_by_default="true">+      <option name="composableFile" value="true" />+    </inspection_tool>+    <inspection_tool class="PreviewPickerAnnotation" enabled="true" level="ERROR" enabled_by_default="true">+      <option name="composableFile" value="true" />+    </inspection_tool>+  </profile>+</component>\ No newline at end of file
gradle/libs.versions.toml
diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.tomlindex 23605d255e..4e071c9341 100644--- a/gradle/libs.versions.toml+++ b/gradle/libs.versions.toml@@ -58,7 +58,6 @@ androidx-pdf-viewer = { module = "androidx.pdf:pdf-viewer", version.ref = "pdfVi maplibre-compose = { module = "org.maplibre.compose:maplibre-compose", version = "0.12.1" } maplibre-composeMaterial3 = { module = "org.maplibre.compose:maplibre-compose-material3", version = "0.12.1" } - # Compose (BOM Managed) androidx-compose-bom = { group = "androidx.compose", name = "compose-bom", version.ref = "composeBom" } androidx-compose-ui = { group = "androidx.compose.ui", name = "ui" }
library/src/main/java/com/vayunmathur/library/ui/Modifier.kt
diff --git a/library/src/main/java/com/vayunmathur/library/ui/Modifier.kt b/library/src/main/java/com/vayunmathur/library/ui/Modifier.ktnew file mode 100644index 0000000000..02f87376c9--- /dev/null+++ b/library/src/main/java/com/vayunmathur/library/ui/Modifier.kt@@ -0,0 +1,11 @@+package com.vayunmathur.library.ui++import androidx.compose.foundation.clickable+import androidx.compose.foundation.gestures.awaitEachGesture+import androidx.compose.foundation.gestures.awaitFirstDown+import androidx.compose.foundation.gestures.waitForUpOrCancellation+import androidx.compose.ui.Modifier+import androidx.compose.ui.input.pointer.PointerEventPass+import androidx.compose.ui.input.pointer.pointerInput++fun Modifier.invisibleClickable(onClick: () -> Unit) = clickable(null, null) {onClick()}\ No newline at end of file
photos/src/main/java/com/vayunmathur/photos/ImageLoader.kt
diff --git a/photos/src/main/java/com/vayunmathur/photos/ImageLoader.kt b/photos/src/main/java/com/vayunmathur/photos/ImageLoader.ktnew file mode 100644index 0000000000..c3ab48f3b5--- /dev/null+++ b/photos/src/main/java/com/vayunmathur/photos/ImageLoader.kt@@ -0,0 +1,65 @@+package com.vayunmathur.photos++import android.content.Context+import androidx.compose.foundation.background+import androidx.compose.foundation.layout.aspectRatio+import androidx.compose.foundation.layout.fillMaxWidth+import androidx.compose.foundation.shape.RoundedCornerShape+import androidx.compose.runtime.Composable+import androidx.compose.runtime.remember+import androidx.compose.ui.Modifier+import androidx.compose.ui.draw.clip+import androidx.compose.ui.graphics.Color+import androidx.compose.ui.layout.ContentScale+import androidx.compose.ui.platform.LocalContext+import androidx.compose.ui.unit.dp+import androidx.core.net.toUri+import androidx.navigation3.runtime.NavBackStack+import coil.ImageLoader+import coil.compose.AsyncImage+import coil.disk.DiskCache+import coil.memory.MemoryCache+import coil.request.ImageRequest+import com.vayunmathur.library.ui.invisibleClickable+import com.vayunmathur.photos.data.Photo++object ImageLoader {+    private lateinit var imageLoader: ImageLoader;++    fun init(context: Context) {+        imageLoader = ImageLoader.Builder(context)+            .memoryCache {+                MemoryCache.Builder(context)+                    .maxSizePercent(0.25) // Use 25% of available RAM+                    .build()+            }+            .diskCache {+                DiskCache.Builder()+                    .directory(context.cacheDir.resolve("image_cache"))+                    .maxSizePercent(0.05) // Use 5% of disk space (or a fixed size like 512MB)+                    .build()+            }+            .respectCacheHeaders(false) // Important for local files/mediastore+            .build()+    }++    @Composable+    fun PhotoItem(photo: Photo, modifier: Modifier, onClick: () -> Unit) {+        AsyncImage(+            model = ImageRequest.Builder(LocalContext.current)+                .data(photo.uri.toUri())+                .diskCacheKey(photo.id.toString())+                .memoryCacheKey(photo.id.toString())+                .crossfade(true)+                .size(128) // Coil will handle the downsampling for you+                .build(),+            imageLoader = imageLoader,+            contentDescription = null,+            contentScale = ContentScale.Crop,+            modifier = modifier+                .clip(RoundedCornerShape(8.dp))+                .invisibleClickable(onClick)+                .background(Color.LightGray) // Placeholder while loading+        )+    }+}\ No newline at end of file
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 5593de6cef..a7c230e955 100644--- a/photos/src/main/java/com/vayunmathur/photos/MainActivity.kt+++ b/photos/src/main/java/com/vayunmathur/photos/MainActivity.kt@@ -32,6 +32,7 @@ class MainActivity : ComponentActivity() {         enableEdgeToEdge()         val db = buildDatabase<PhotoDatabase>()         val viewModel = DatabaseViewModel(Photo::class to db.noteDao())+        ImageLoader.init(this)         setContent {             DynamicTheme {                 Navigation(viewModel)
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 5a08de9af5..02dacc7352 100644--- a/photos/src/main/java/com/vayunmathur/photos/ui/GalleryPage.kt+++ b/photos/src/main/java/com/vayunmathur/photos/ui/GalleryPage.kt@@ -34,12 +34,13 @@ import androidx.compose.ui.unit.dp import androidx.core.net.toUri import androidx.exifinterface.media.ExifInterface import androidx.navigation3.runtime.NavBackStack-import coil.ImageLoader import coil.compose.AsyncImage import coil.disk.DiskCache import coil.memory.MemoryCache import coil.request.ImageRequest+import com.vayunmathur.library.ui.invisibleClickable import com.vayunmathur.library.util.DatabaseViewModel+import com.vayunmathur.photos.ImageLoader import com.vayunmathur.photos.NavigationBar import com.vayunmathur.photos.Route import com.vayunmathur.photos.data.Photo@@ -59,20 +60,7 @@ import kotlin.time.Instant fun GalleryPage(backStack: NavBackStack<Route>, viewModel: DatabaseViewModel) {     val photos by viewModel.data<Photo>().collectAsState()     val context = LocalContext.current-    val imageLoader = remember { ImageLoader.Builder(context)-        .memoryCache {-            MemoryCache.Builder(context)-                .maxSizePercent(0.25) // Use 25% of available RAM-                .build()-        }-        .diskCache {-            DiskCache.Builder()-                .directory(context.cacheDir.resolve("image_cache"))-                .maxSizePercent(0.05) // Use 5% of disk space (or a fixed size like 512MB)-                .build()-        }-        .respectCacheHeaders(false) // Important for local files/mediastore-        .build() }+     LaunchedEffect(Unit) {         delay(1000)         withContext(Dispatchers.IO) {@@ -111,36 +99,16 @@ fun GalleryPage(backStack: NavBackStack<Route>, viewModel: DatabaseViewModel) {                     )                 }                 items(photos, { it.id }, contentType = { "photo_thumbnail" }) {-                    PhotoItem(it, imageLoader, backStack)+                    ImageLoader.PhotoItem(it, Modifier.fillMaxWidth().aspectRatio(1f)) {+                        backStack.add(Route.Photo(it.id))+                    }                 }             }         }     } } -@Composable-fun PhotoItem(photo: Photo, imageLoader: ImageLoader, backStack: NavBackStack<Route>) {-    AsyncImage(-        model = ImageRequest.Builder(LocalContext.current)-            .data(photo.uri.toUri())-            .diskCacheKey(photo.id.toString())-            .memoryCacheKey(photo.id.toString())-            .crossfade(true)-            .size(128) // Coil will handle the downsampling for you-            .build(),-        imageLoader = imageLoader,-        contentDescription = null,-        contentScale = ContentScale.Crop,-        modifier = Modifier-            .fillMaxWidth()-            .aspectRatio(1f)-            .clip(RoundedCornerShape(8.dp))-            .clickable(interactionSource = remember { MutableInteractionSource() }, indication = null) {-                backStack.add(Route.Photo(photo.id))-            }-            .background(Color.LightGray) // Placeholder while loading-    )-}+  fun addAllPhotos(context: Context, viewModel: DatabaseViewModel) {     val projection = arrayOf(
photos/src/main/java/com/vayunmathur/photos/ui/MapPage.kt
diff --git a/photos/src/main/java/com/vayunmathur/photos/ui/MapPage.kt b/photos/src/main/java/com/vayunmathur/photos/ui/MapPage.ktindex c2fe3140d0..ea5348542b 100644--- a/photos/src/main/java/com/vayunmathur/photos/ui/MapPage.kt+++ b/photos/src/main/java/com/vayunmathur/photos/ui/MapPage.kt@@ -2,14 +2,27 @@ package com.vayunmathur.photos.ui  import androidx.compose.foundation.background import androidx.compose.foundation.border+import androidx.compose.foundation.clickable+import androidx.compose.foundation.horizontalScroll+import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Box+import androidx.compose.foundation.layout.Row+import androidx.compose.foundation.layout.Spacer+import androidx.compose.foundation.layout.aspectRatio+import androidx.compose.foundation.layout.fillMaxHeight import androidx.compose.foundation.layout.fillMaxSize+import androidx.compose.foundation.layout.fillMaxWidth+import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.offset import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.size+import androidx.compose.foundation.lazy.LazyRow+import androidx.compose.foundation.lazy.items+import androidx.compose.foundation.rememberScrollState import androidx.compose.foundation.shape.CircleShape import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Scaffold+import androidx.compose.material3.Surface import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect@@ -18,6 +31,9 @@ import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableLongStateOf import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember+import androidx.compose.runtime.rememberCoroutineScope+import androidx.compose.runtime.retain.retain+import androidx.compose.runtime.saveable.rememberSaveable import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier@@ -32,18 +48,22 @@ import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import androidx.navigation3.runtime.NavBackStack import coil.compose.AsyncImage+import com.vayunmathur.library.ui.invisibleClickable import com.vayunmathur.library.util.DatabaseViewModel+import com.vayunmathur.photos.ImageLoader import com.vayunmathur.photos.NavigationBar import com.vayunmathur.photos.Route import com.vayunmathur.photos.data.Photo import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers+import kotlinx.coroutines.coroutineScope import kotlinx.coroutines.delay import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import org.maplibre.compose.camera.rememberCameraState import org.maplibre.compose.map.MaplibreMap import org.maplibre.compose.style.BaseStyle+import org.maplibre.compose.util.ClickResult import org.maplibre.spatialk.geojson.Position import kotlin.math.pow import kotlin.math.sqrt@@ -52,6 +72,7 @@ import kotlin.math.sqrt data class MapCluster(     val position: DpOffset,     val coverPhoto: Photo,+    val allPhotos: List<Photo>,     val count: Int ) @@ -70,11 +91,11 @@ fun MapPage(backStack: NavBackStack<Route>, viewModel: DatabaseViewModel) {     // State now holds Clusters instead of raw offsets     var generatedClusters: List<MapCluster> by remember { mutableStateOf(listOf()) }     var clusters: List<MapCluster> by remember { mutableStateOf(listOf()) }+    var selectedCluster: MapCluster? by remember { mutableStateOf(null) }      val dpsize = LocalWindowInfo.current.containerDpSize      LaunchedEffect(photos) {-        val now = System.currentTimeMillis()         cameraState.awaitProjection()         while (true) {             val projection = cameraState.projection ?: continue@@ -89,8 +110,11 @@ fun MapPage(backStack: NavBackStack<Route>, viewModel: DatabaseViewModel) {             withContext(Dispatchers.IO) {                 // Update groupings                 generatedClusters = clusterPhotos(rawLocations, 50.dp)+                if(selectedCluster != null) {+                    selectedCluster = generatedClusters.find { selectedCluster!!.coverPhoto.id in it.allPhotos.map { it.id } }+                }             }-            delay(100)+            delay(200)         }     } @@ -99,6 +123,10 @@ fun MapPage(backStack: NavBackStack<Route>, viewModel: DatabaseViewModel) {             MaplibreMap(                 baseStyle = BaseStyle.Uri("https://tiles.openfreemap.org/styles/liberty"),                 cameraState = cameraState,+                onMapClick = { _, _ ->+                    selectedCluster = null+                    ClickResult.Pass+                },                 onFrame = {                     val projection = cameraState.projection @@ -111,7 +139,7 @@ fun MapPage(backStack: NavBackStack<Route>, viewModel: DatabaseViewModel) {                             val lat = cluster.coverPhoto.lat                             val long = cluster.coverPhoto.long -                            if (lat != null && long != null) {+                            val nc = if (lat != null && long != null) {                                 val newOffset = projection.screenLocationFromPosition(                                     Position(long, lat)                                 )@@ -119,11 +147,17 @@ fun MapPage(backStack: NavBackStack<Route>, viewModel: DatabaseViewModel) {                             } else {                                 null                             }++                            if(cluster == selectedCluster) {+                                selectedCluster = nc+                            }++                            nc                         }                         clusters = updatedClusters                     }                 }-            ) {}+            )              // Layer: Markers             Box(Modifier.fillMaxSize()) {@@ -138,14 +172,9 @@ fun MapPage(backStack: NavBackStack<Route>, viewModel: DatabaseViewModel) {                             .padding(2.dp) // creates a small white border effect                     ) {                         // The Image-                        AsyncImage(-                            model = cluster.coverPhoto.uri,-                            contentDescription = null,-                            modifier = Modifier-                                .fillMaxSize()-                                .clip(MaterialTheme.shapes.extraSmall),-                            contentScale = ContentScale.Crop-                        )+                        ImageLoader.PhotoItem(cluster.coverPhoto, Modifier.fillMaxSize()) {+                            selectedCluster = cluster+                        }                          // The "Bubble" Count Indicator                         if (cluster.count > 1) {@@ -168,6 +197,25 @@ fun MapPage(backStack: NavBackStack<Route>, viewModel: DatabaseViewModel) {                         }                     }                 }+                selectedCluster?.let {+                    Surface(Modifier.align(Alignment.BottomCenter), color = MaterialTheme.colorScheme.background, ) {+                        LazyRow(+                            Modifier.height(100.dp).padding(vertical = 8.dp).fillMaxWidth(), horizontalArrangement = Arrangement.spacedBy(8.dp)+                        ) {+                            item {+                                Spacer(Modifier.padding(8.dp))+                            }+                            items(it.allPhotos, key = {it.id}, contentType = { "photo_thumbnail" }) {+                                ImageLoader.PhotoItem(it, Modifier.fillMaxHeight().aspectRatio(1f)) {+                                    backStack.add(Route.Photo(it.id))+                                }+                            }+                            item {+                                Spacer(Modifier.padding(8.dp))+                            }+                        }+                    }+                }             }         }     }@@ -196,10 +244,10 @@ fun clusterPhotos(         if (existingIndex >= 0) {             // "Absorb" into existing cluster             val existing = result[existingIndex]-            result[existingIndex] = existing.copy(count = existing.count + 1)+            result[existingIndex] = existing.copy(count = existing.count + 1, allPhotos = existing.allPhotos + photo)         } else {             // Create new cluster-            result.add(MapCluster(pos, photo, 1))+            result.add(MapCluster(pos, photo, listOf(photo), 1))         }     }     return result