photos: map page
- Commit
4afbafaf6723dbd7ef63d49115bd288088ec195c- Author
- Vayun <[email protected]>
- When
- 2026-01-19 22:34 UTC (10 months ago)
- Parents
9951826
Diff
gradle/libs.versions.toml
diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.tomlindex 0f713deb11..23605d255e 100644--- a/gradle/libs.versions.toml+++ b/gradle/libs.versions.toml@@ -55,6 +55,10 @@ androidx-pdf-compose = { module = "androidx.pdf:pdf-compose", version.ref = "pdf androidx-pdf-document-service = { module = "androidx.pdf:pdf-document-service", version.ref = "pdfViewer" } androidx-pdf-viewer = { module = "androidx.pdf:pdf-viewer", version.ref = "pdfViewer" } +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" }
photos/build.gradle.kts
diff --git a/photos/build.gradle.kts b/photos/build.gradle.ktsindex 0b27181c4f..3322f227dc 100644--- a/photos/build.gradle.kts+++ b/photos/build.gradle.kts@@ -13,6 +13,8 @@ android { dependencies { implementation(libs.androidx.navigation3.runtime) + implementation(libs.maplibre.compose)+ implementation(libs.maplibre.composeMaterial3) implementation(libs.coil.compose) implementation(libs.androidx.exifinterface)
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 e67acaf406..5593de6cef 100644--- a/photos/src/main/java/com/vayunmathur/photos/MainActivity.kt+++ b/photos/src/main/java/com/vayunmathur/photos/MainActivity.kt@@ -4,8 +4,14 @@ import android.os.Bundle import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.activity.enableEdgeToEdge+import androidx.compose.material3.Icon+import androidx.compose.material3.ShortNavigationBar+import androidx.compose.material3.ShortNavigationBarItem+import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect+import androidx.compose.ui.res.painterResource+import androidx.navigation3.runtime.NavBackStack import androidx.navigation3.runtime.NavKey import com.vayunmathur.library.ui.DynamicTheme import com.vayunmathur.library.util.DatabaseViewModel@@ -15,6 +21,7 @@ import com.vayunmathur.library.util.rememberNavBackStack import com.vayunmathur.photos.data.Photo import com.vayunmathur.photos.data.PhotoDatabase import com.vayunmathur.photos.ui.GalleryPage+import com.vayunmathur.photos.ui.MapPage import com.vayunmathur.photos.ui.PhotoPage import kotlinx.coroutines.delay import kotlinx.serialization.Serializable@@ -40,6 +47,9 @@ sealed interface Route: NavKey { @Serializable data class Photo(val id: Long): Route++ @Serializable+ data object Map: Route } @Composable@@ -50,8 +60,30 @@ fun Navigation(viewModel: DatabaseViewModel) { GalleryPage(backStack, viewModel) } + entry<Route.Map> {+ MapPage(backStack, viewModel)+ }+ entry<Route.Photo>() { PhotoPage(backStack, viewModel, it.id) } }+}++private enum class MainRoute(val route: Route, val title: String, val icon: Int) {+ Gallery(Route.Gallery, "Gallery", R.drawable.gallery_thumbnail_24px),+ Map(Route.Map, "Map", R.drawable.map_24px)+}++@Composable+fun NavigationBar(currentRoute: Route, backStack: NavBackStack<Route>) {+ ShortNavigationBar {+ MainRoute.entries.forEach {+ ShortNavigationBarItem(it.route == currentRoute, { backStack.add(it.route) }, {+ Icon(painterResource(it.icon), null)+ }, {+ Text(it.title)+ })+ }+ } }\ No newline at end of file
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 0adaa2d71d..5a08de9af5 100644--- a/photos/src/main/java/com/vayunmathur/photos/ui/GalleryPage.kt+++ b/photos/src/main/java/com/vayunmathur/photos/ui/GalleryPage.kt@@ -17,6 +17,7 @@ import androidx.compose.foundation.lazy.grid.items import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Scaffold+import androidx.compose.material3.ShortNavigationBar import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect@@ -39,6 +40,7 @@ import coil.disk.DiskCache import coil.memory.MemoryCache import coil.request.ImageRequest import com.vayunmathur.library.util.DatabaseViewModel+import com.vayunmathur.photos.NavigationBar import com.vayunmathur.photos.Route import com.vayunmathur.photos.data.Photo import kotlinx.coroutines.CoroutineScope@@ -91,7 +93,7 @@ fun GalleryPage(backStack: NavBackStack<Route>, viewModel: DatabaseViewModel) { } } }- Scaffold() { paddingValues ->+ Scaffold(bottomBar = { NavigationBar(Route.Gallery, backStack) }) { paddingValues -> LazyVerticalGrid( GridCells.Adaptive(40.dp), Modifier.padding(paddingValues),
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.ktnew file mode 100644index 0000000000..0278cd71e7--- /dev/null+++ b/photos/src/main/java/com/vayunmathur/photos/ui/MapPage.kt@@ -0,0 +1,174 @@+package com.vayunmathur.photos.ui++import androidx.compose.foundation.background+import androidx.compose.foundation.border+import androidx.compose.foundation.layout.Box+import androidx.compose.foundation.layout.fillMaxSize+import androidx.compose.foundation.layout.offset+import androidx.compose.foundation.layout.padding+import androidx.compose.foundation.layout.size+import androidx.compose.foundation.shape.CircleShape+import androidx.compose.material3.MaterialTheme+import androidx.compose.material3.Scaffold+import androidx.compose.material3.Text+import androidx.compose.runtime.Composable+import androidx.compose.runtime.collectAsState+import androidx.compose.runtime.getValue+import androidx.compose.runtime.mutableStateOf+import androidx.compose.runtime.remember+import androidx.compose.runtime.setValue+import androidx.compose.ui.Alignment+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.LocalDensity+import androidx.compose.ui.text.font.FontWeight+import androidx.compose.ui.unit.Dp+import androidx.compose.ui.unit.DpOffset+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.util.DatabaseViewModel+import com.vayunmathur.photos.NavigationBar+import com.vayunmathur.photos.Route+import com.vayunmathur.photos.data.Photo+import org.maplibre.compose.camera.rememberCameraState+import org.maplibre.compose.map.MaplibreMap+import org.maplibre.spatialk.geojson.Position+import kotlin.math.pow+import kotlin.math.sqrt++// Helper class to hold cluster data+data class MapCluster(+ val position: DpOffset,+ val coverPhoto: Photo,+ val count: Int+)++@Composable+fun MapPage(backStack: NavBackStack<Route>, viewModel: DatabaseViewModel) {+ val photos by viewModel.data<Photo>().collectAsState()++ // Shuffle and take subset+ val photosFiltered = remember(photos) { photos.shuffled().take(100) }++ // Prepare raw GPS positions+ val positions = remember(photosFiltered) {+ photosFiltered.filter { it.lat != null && it.long != null }+ .map { (it.lat!! to it.long!!) to it }+ }++ val cameraState = rememberCameraState()+ val density = LocalDensity.current++ // State now holds Clusters instead of raw offsets+ var clusters: List<MapCluster> by remember { mutableStateOf(listOf()) }++ Scaffold(bottomBar = { NavigationBar(Route.Map, backStack) }) { paddingValues ->+ Box(Modifier.padding(paddingValues).fillMaxSize()) {+ MaplibreMap(+ cameraState = cameraState,+ onFrame = {+ // 1. Calculate Screen Locations (Pixels)+ val projection = cameraState.projection+ if (projection != null) {+ val rawLocations = positions.map { (gps, photo) ->+ val screenLoc = projection.screenLocationFromPosition(+ Position(gps.second, gps.first)+ )+ screenLoc to photo+ }++ // 3. Cluster them based on 50.dp threshold+ clusters = clusterPhotos(rawLocations, 50.dp)+ }+ }+ ) {}++ // Layer: Markers+ Box(Modifier.fillMaxSize()) {+ clusters.forEach { cluster ->+ // Main Marker Box+ Box(+ Modifier+ .offset(cluster.position.x, cluster.position.y)+ .size(50.dp)+ // Optional: Add shadow or border for better visibility against map+ .background(Color.White, shape = MaterialTheme.shapes.small)+ .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+ )++ // The "Bubble" Count Indicator+ if (cluster.count > 1) {+ Box(+ modifier = Modifier+ .align(Alignment.TopEnd)+ .offset(x = 8.dp, y = (-8).dp) // Shift slightly outside/corner+ .size(22.dp)+ .background(Color.Red, CircleShape)+ .border(1.dp, Color.White, CircleShape),+ contentAlignment = Alignment.Center+ ) {+ Text(+ text = cluster.count.toString(),+ color = Color.White,+ fontSize = 10.sp,+ fontWeight = FontWeight.Bold+ )+ }+ }+ }+ }+ }+ }+ }+}++/**+ * Greedy clustering algorithm.+ * Iterates through items and adds them to an existing cluster if within threshold,+ * otherwise creates a new cluster.+ */+fun clusterPhotos(+ items: List<Pair<DpOffset, Photo>>,+ threshold: Dp+): List<MapCluster> {+ val result = ArrayList<MapCluster>()++ // We need threshold in raw float value for distance check (Unit doesn't matter as long as X/Y are same unit)+ val thresholdVal = threshold.value++ for ((pos, photo) in items) {+ // Find the first cluster that is close enough to this photo+ val existingIndex = result.indexOfFirst { cluster ->+ calculateDistance(cluster.position, pos) < thresholdVal+ }++ if (existingIndex >= 0) {+ // "Absorb" into existing cluster+ val existing = result[existingIndex]+ result[existingIndex] = existing.copy(count = existing.count + 1)+ } else {+ // Create new cluster+ result.add(MapCluster(pos, photo, 1))+ }+ }+ return result+}++private fun calculateDistance(p1: DpOffset, p2: DpOffset): Float {+ val dx = p1.x.value - p2.x.value+ val dy = p1.y.value - p2.y.value+ return sqrt(dx.pow(2) + dy.pow(2))+}\ No newline at end of file
photos/src/main/res/drawable/gallery_thumbnail_24px.xml
diff --git a/photos/src/main/res/drawable/gallery_thumbnail_24px.xml b/photos/src/main/res/drawable/gallery_thumbnail_24px.xmlnew file mode 100644index 0000000000..4dbcc140f4--- /dev/null+++ b/photos/src/main/res/drawable/gallery_thumbnail_24px.xml@@ -0,0 +1,5 @@+<vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="24dp" android:tint="#000000" android:viewportHeight="960" android:viewportWidth="960" android:width="24dp">+ + <path android:fillColor="@android:color/white" android:pathData="M120,760Q87,760 63.5,736.5Q40,713 40,680L40,280Q40,247 63.5,223.5Q87,200 120,200L520,200Q553,200 576.5,223.5Q600,247 600,280L600,680Q600,713 576.5,736.5Q553,760 520,760L120,760ZM720,440Q703,440 691.5,428.5Q680,417 680,400L680,240Q680,223 691.5,211.5Q703,200 720,200L880,200Q897,200 908.5,211.5Q920,223 920,240L920,400Q920,417 908.5,428.5Q897,440 880,440L720,440ZM760,360L840,360L840,280L760,280L760,360ZM120,680L520,680Q520,680 520,680Q520,680 520,680L520,280Q520,280 520,280Q520,280 520,280L120,280Q120,280 120,280Q120,280 120,280L120,680Q120,680 120,680Q120,680 120,680ZM160,600L480,600L375,460L300,560L245,487L160,600ZM720,760Q703,760 691.5,748.5Q680,737 680,720L680,560Q680,543 691.5,531.5Q703,520 720,520L880,520Q897,520 908.5,531.5Q920,543 920,560L920,720Q920,737 908.5,748.5Q897,760 880,760L720,760ZM760,680L840,680L840,600L760,600L760,680ZM120,680Q120,680 120,680Q120,680 120,680L120,280Q120,280 120,280Q120,280 120,280L120,280Q120,280 120,280Q120,280 120,280L120,680Q120,680 120,680Q120,680 120,680ZM760,360L760,280L760,280L760,360ZM760,680L760,600L760,600L760,680Z"/>+ +</vector>
photos/src/main/res/drawable/map_24px.xml
diff --git a/photos/src/main/res/drawable/map_24px.xml b/photos/src/main/res/drawable/map_24px.xmlnew file mode 100644index 0000000000..59941e6d78--- /dev/null+++ b/photos/src/main/res/drawable/map_24px.xml@@ -0,0 +1,5 @@+<vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="24dp" android:tint="#000000" android:viewportHeight="960" android:viewportWidth="960" android:width="24dp">+ + <path android:fillColor="@android:color/white" android:pathData="M600,840L360,756L174,828Q154,836 137,823.5Q120,811 120,790L120,230Q120,217 127.5,207Q135,197 148,192L360,120L600,204L786,132Q806,124 823,136.5Q840,149 840,170L840,730Q840,743 832.5,753Q825,763 812,768L600,840ZM560,742L560,274L400,218L400,686L560,742ZM640,742L760,702L760,228L640,274L640,742ZM200,732L320,686L320,218L200,258L200,732ZM640,274L640,274L640,742L640,742L640,274ZM320,218L320,218L320,686L320,686L320,218Z"/>+ +</vector>