Modern Apps git

← Modern-Apps.git

maps P2: map-screen layout to match Vela (scale bar, compass, chips, FAB stack)

Commit
35190ed90ef096798e694fbfe0bd74f9031bc122
Author
vayun-mathur <[email protected]>
When
2026-08-16 22:41 UTC (3 months ago)
Parents
48c9556

Diff

maps/src/main/java/com/vayunmathur/maps/MainActivity.kt
diff --git a/maps/src/main/java/com/vayunmathur/maps/MainActivity.kt b/maps/src/main/java/com/vayunmathur/maps/MainActivity.ktindex 01fd03bcd0..8a62396660 100644--- a/maps/src/main/java/com/vayunmathur/maps/MainActivity.kt+++ b/maps/src/main/java/com/vayunmathur/maps/MainActivity.kt@@ -98,7 +98,7 @@ sealed interface Route: NavKey {     data object DownloadedMapsPage: Route      @Serializable-    data class SearchPage(val idx: Int?, val east: Double, val west: Double, val north: Double, val south: Double): Route+    data class SearchPage(val idx: Int?, val east: Double, val west: Double, val north: Double, val south: Double, val query: String? = null): Route }  @Composable@@ -118,7 +118,7 @@ fun Navigation(             DownloadedMapsPage(backStack, zonesViewModel)         }         entry<Route.SearchPage> {-            SearchPage(backStack, viewModel, searchViewModel, it.idx, it.east, it.west, it.north, it.south)+            SearchPage(backStack, viewModel, searchViewModel, it.idx, it.east, it.west, it.north, it.south, it.query)         }     } }
maps/src/main/java/com/vayunmathur/maps/ui/CategoryChips.kt
diff --git a/maps/src/main/java/com/vayunmathur/maps/ui/CategoryChips.kt b/maps/src/main/java/com/vayunmathur/maps/ui/CategoryChips.ktnew file mode 100644index 0000000000..6c5b94d00a--- /dev/null+++ b/maps/src/main/java/com/vayunmathur/maps/ui/CategoryChips.kt@@ -0,0 +1,50 @@+package com.vayunmathur.maps.ui++import androidx.compose.foundation.horizontalScroll+import androidx.compose.foundation.layout.Arrangement+import androidx.compose.foundation.layout.Row+import androidx.compose.foundation.rememberScrollState+import androidx.compose.runtime.Composable+import androidx.compose.ui.Modifier+import androidx.compose.ui.res.stringResource+import androidx.compose.ui.unit.dp+import com.vayunmathur.library.ui.AssistChip+import com.vayunmathur.library.ui.Text+import com.vayunmathur.maps.R++/** One quick-search category chip: a display label and the query it runs. */+data class MapCategory(val labelRes: Int, val query: String)++/**+ * Shared quick-category definitions, reused by both the browse map screen and+ * the search page so the two stay in lock-step (Vela's `CategoryChips`). Each+ * chip runs the same Google search query wired up in P3.+ */+val MAP_CATEGORIES: List<MapCategory> = listOf(+    MapCategory(R.string.search_category_restaurants, "restaurants"),+    MapCategory(R.string.search_category_coffee, "coffee"),+    MapCategory(R.string.search_category_gas, "gas station"),+    MapCategory(R.string.search_category_groceries, "groceries"),+    MapCategory(R.string.search_category_hotels, "hotels"),+    MapCategory(R.string.search_category_atms, "atm"),+)++/**+ * Horizontally-scrolling row of quick category chips. [onCategory] is invoked+ * with the chip's Google search query — on browse this opens the search page+ * pre-filled with that query; on the search page it runs the query in place.+ */+@Composable+fun CategoryChips(onCategory: (String) -> Unit, modifier: Modifier = Modifier) {+    Row(+        modifier = modifier.horizontalScroll(rememberScrollState()),+        horizontalArrangement = Arrangement.spacedBy(8.dp),+    ) {+        MAP_CATEGORIES.forEach { category ->+            AssistChip(+                onClick = { onCategory(category.query) },+                label = { Text(stringResource(category.labelRes)) },+            )+        }+    }+}
maps/src/main/java/com/vayunmathur/maps/ui/CompassButton.kt
diff --git a/maps/src/main/java/com/vayunmathur/maps/ui/CompassButton.kt b/maps/src/main/java/com/vayunmathur/maps/ui/CompassButton.ktnew file mode 100644index 0000000000..477bea7606--- /dev/null+++ b/maps/src/main/java/com/vayunmathur/maps/ui/CompassButton.kt@@ -0,0 +1,27 @@+package com.vayunmathur.maps.ui++import androidx.compose.runtime.Composable+import androidx.compose.ui.Modifier+import androidx.compose.ui.graphics.graphicsLayer+import com.vayunmathur.library.ui.IconCompass+import com.vayunmathur.library.ui.SmallFloatingActionButton+import kotlin.math.abs++/**+ * Heading-up / north-up toggle bound to the camera [bearing] (degrees clockwise+ * from north). The compass rose rotates to keep pointing at true north; tapping+ * it calls [onResetNorth] to snap the camera back to a north-up orientation.+ *+ * Port of Vela's `onCompassTap`: the control only surfaces while the map is+ * rotated — at (near) north-up there's nothing to reset, so it hides itself.+ */+@Composable+fun CompassButton(bearing: Double, onResetNorth: () -> Unit, modifier: Modifier = Modifier) {+    // Normalize to (-180, 180] so a bearing of e.g. 359° reads as -1°.+    val normalized = ((bearing + 180.0) % 360.0 + 360.0) % 360.0 - 180.0+    if (abs(normalized) < 1.0) return++    SmallFloatingActionButton(onClick = onResetNorth, modifier = modifier) {+        IconCompass(Modifier.graphicsLayer { rotationZ = -normalized.toFloat() })+    }+}
maps/src/main/java/com/vayunmathur/maps/ui/LayersButton.kt
diff --git a/maps/src/main/java/com/vayunmathur/maps/ui/LayersButton.kt b/maps/src/main/java/com/vayunmathur/maps/ui/LayersButton.ktnew file mode 100644index 0000000000..c19d45e2d5--- /dev/null+++ b/maps/src/main/java/com/vayunmathur/maps/ui/LayersButton.kt@@ -0,0 +1,18 @@+package com.vayunmathur.maps.ui++import androidx.compose.runtime.Composable+import androidx.compose.ui.Modifier+import com.vayunmathur.library.ui.IconStyle+import com.vayunmathur.library.ui.SmallFloatingActionButton++/**+ * Minimal map-layers entry point in the browse FAB stack. For P2 this is just+ * the button surface (Vela's `LayersButton`); the full layer-toggle sheet+ * (satellite / traffic / transit) lands in P6, which wires [onClick] to open it.+ */+@Composable+fun LayersButton(onClick: () -> Unit, modifier: Modifier = Modifier) {+    SmallFloatingActionButton(onClick = onClick, modifier = modifier) {+        IconStyle()+    }+}
maps/src/main/java/com/vayunmathur/maps/ui/MapPage.kt
diff --git a/maps/src/main/java/com/vayunmathur/maps/ui/MapPage.kt b/maps/src/main/java/com/vayunmathur/maps/ui/MapPage.ktindex 53338fefc3..d9a81dc9e8 100644--- a/maps/src/main/java/com/vayunmathur/maps/ui/MapPage.kt+++ b/maps/src/main/java/com/vayunmathur/maps/ui/MapPage.kt@@ -10,11 +10,14 @@ import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.Spacer+import androidx.compose.foundation.layout.WindowInsets import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.size+import androidx.compose.foundation.layout.systemBars+import androidx.compose.foundation.layout.windowInsetsPadding import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.itemsIndexed import androidx.compose.foundation.lazy.rememberLazyListState@@ -29,8 +32,10 @@ import com.vayunmathur.library.ui.Card import com.vayunmathur.library.ui.CardDefaults import com.vayunmathur.library.ui.CompassCalibrationBanner import com.vayunmathur.library.ui.ExperimentalMaterial3Api+import com.vayunmathur.library.ui.FloatingActionButton import com.vayunmathur.library.ui.Icon import com.vayunmathur.library.ui.IconButton+import com.vayunmathur.library.ui.IconMyLocation import com.vayunmathur.library.ui.ListItem import com.vayunmathur.library.ui.ListItemDefaults import com.vayunmathur.library.ui.SheetValue@@ -250,6 +255,21 @@ fun MapPage(backStack: NavBackStack<Route>, viewModel: SelectedFeatureViewModel,         )     } +    // Browse category chip → open search pre-filled with the category query.+    fun openCategorySearch(query: String) {+        val bbox = camera.projection?.queryVisibleBoundingBox()+        backStack.add(+            Route.SearchPage(+                null,+                bbox?.east ?: 180.0,+                bbox?.west ?: -180.0,+                bbox?.north ?: 85.0,+                bbox?.south ?: -85.0,+                query,+            )+        )+    }+     // Tapping a saved Home/Work chip recenters onto the place and opens its     // bottom sheet, from which the user can start Directions or remove the slot.     fun showSavedPlace(place: SavedPlace) {@@ -504,6 +524,13 @@ fun MapPage(backStack: NavBackStack<Route>, viewModel: SelectedFeatureViewModel,                             )                         }                         Spacer(Modifier.height(8.dp))+                        // Quick category chips (Vela's browse CategoryChips),+                        // wired to the P3 Google search categories.+                        CategoryChips(+                            onCategory = { openCategorySearch(it) },+                            modifier = Modifier.fillMaxWidth(),+                        )+                        Spacer(Modifier.height(8.dp))                         // Compass calibration hint for the heading puck; self-hides at HIGH accuracy.                         CompassCalibrationBanner(userHeadingAccuracy)                     }@@ -537,6 +564,59 @@ fun MapPage(backStack: NavBackStack<Route>, viewModel: SelectedFeatureViewModel,                     )                 } +                // Browse map controls (Decision D6): a scale bar plus the FAB+                // stack (my-location, layers, compass). Surfaced only while+                // browsing — hidden during navigation (the nav overlay owns its+                // own controls) and while a place/route is selected (the bottom+                // sheet takes over the lower half of the screen).+                if (selectedFeature == null && inactiveNavigation == null && !isNavigating) {+                    MapScaleBar(+                        zoom = camera.position.zoom,+                        latitude = camera.position.target.latitude,+                        modifier = Modifier+                            .align(Alignment.BottomStart)+                            .windowInsetsPadding(WindowInsets.systemBars)+                            .padding(16.dp),+                    )+                    Column(+                        modifier = Modifier+                            .align(Alignment.BottomEnd)+                            .windowInsetsPadding(WindowInsets.systemBars)+                            .padding(16.dp),+                        horizontalAlignment = Alignment.End,+                        verticalArrangement = Arrangement.spacedBy(12.dp),+                    ) {+                        // Compass sits on top and only shows when the map is+                        // rotated; layers in the middle; my-location is the+                        // primary action, closest to the thumb.+                        CompassButton(+                            bearing = camera.position.bearing,+                            onResetNorth = {+                                coroutineScope.launch {+                                    camera.animateTo(+                                        camera.position.copy(bearing = 0.0, tilt = 0.0)+                                    )+                                }+                            },+                        )+                        LayersButton(onClick = { /* P6: open the layers sheet */ })+                        FloatingActionButton(+                            onClick = {+                                coroutineScope.launch {+                                    camera.animateTo(+                                        camera.position.copy(+                                            target = userPosition,+                                            zoom = maxOf(camera.position.zoom, 15.0),+                                        )+                                    )+                                }+                            }+                        ) {+                            IconMyLocation()+                        }+                    }+                }+                 // Live navigation overlay (top maneuver card, bottom ETA strip,                 // recenter FAB, arrival card). Hidden when nav is Idle.                 NavigationOverlay(
maps/src/main/java/com/vayunmathur/maps/ui/MapScaleBar.kt
diff --git a/maps/src/main/java/com/vayunmathur/maps/ui/MapScaleBar.kt b/maps/src/main/java/com/vayunmathur/maps/ui/MapScaleBar.ktnew file mode 100644index 0000000000..cba6a6b8fb--- /dev/null+++ b/maps/src/main/java/com/vayunmathur/maps/ui/MapScaleBar.kt@@ -0,0 +1,81 @@+package com.vayunmathur.maps.ui++import androidx.compose.foundation.background+import androidx.compose.foundation.layout.Box+import androidx.compose.foundation.layout.Column+import androidx.compose.foundation.layout.height+import androidx.compose.foundation.layout.padding+import androidx.compose.foundation.layout.width+import androidx.compose.foundation.shape.RoundedCornerShape+import androidx.compose.runtime.Composable+import androidx.compose.ui.Modifier+import androidx.compose.ui.platform.LocalDensity+import androidx.compose.ui.unit.dp+import com.vayunmathur.library.ui.MaterialTheme+import com.vayunmathur.library.ui.Surface+import com.vayunmathur.library.ui.Text+import kotlin.math.PI+import kotlin.math.cos+import kotlin.math.floor+import kotlin.math.log10+import kotlin.math.pow+import kotlin.math.roundToInt++/**+ * Web-Mercator scale bar bound to the map camera. Given the current [zoom] and+ * [latitude] (Mercator ground resolution depends on both), it picks the largest+ * "nice" round distance (1 / 2 / 5 × 10ⁿ) that fits within a fixed on-screen+ * width and draws a labelled bar for it.+ *+ * Port of Vela's `ScaleBar`/`ScaleBarReader` — kept declarative so it just reads+ * `camera.position.zoom` / `camera.position.target.latitude` from the caller.+ */+@Composable+fun MapScaleBar(zoom: Double, latitude: Double, modifier: Modifier = Modifier) {+    val density = LocalDensity.current+    val maxBarPx = with(density) { 96.dp.toPx() }++    // Ground resolution (meters per screen pixel) at this latitude and zoom.+    val metersPerPixel = 156543.03392 * cos(latitude * PI / 180.0) / 2.0.pow(zoom)+    if (!metersPerPixel.isFinite() || metersPerPixel <= 0.0) return++    val niceMeters = niceRoundDistance(metersPerPixel * maxBarPx)+    val barWidth = with(density) { (niceMeters / metersPerPixel).toFloat().toDp() }++    Surface(+        modifier = modifier,+        shape = RoundedCornerShape(4.dp),+        color = MaterialTheme.colorScheme.surface.copy(alpha = 0.75f),+    ) {+        Column(Modifier.padding(horizontal = 8.dp, vertical = 4.dp)) {+            Text(+                text = scaleLabel(niceMeters),+                style = MaterialTheme.typography.labelSmall,+                color = MaterialTheme.colorScheme.onSurface,+            )+            Box(+                Modifier+                    .padding(top = 2.dp)+                    .width(barWidth)+                    .height(3.dp)+                    .background(MaterialTheme.colorScheme.onSurface),+            )+        }+    }+}++/** Largest value of the form 1/2/5 × 10ⁿ that is ≤ [maxMeters]. */+private fun niceRoundDistance(maxMeters: Double): Double {+    if (maxMeters <= 0.0) return 1.0+    val magnitude = 10.0.pow(floor(log10(maxMeters)))+    var best = magnitude+    for (factor in listOf(1.0, 2.0, 5.0, 10.0)) {+        val candidate = factor * magnitude+        if (candidate <= maxMeters) best = candidate+    }+    return best+}++/** Nice distances are always whole, so integer m / km rendering is exact. */+private fun scaleLabel(meters: Double): String =+    if (meters >= 1000.0) "${(meters / 1000.0).roundToInt()} km" else "${meters.roundToInt()} m"
maps/src/main/java/com/vayunmathur/maps/ui/SearchPage.kt
diff --git a/maps/src/main/java/com/vayunmathur/maps/ui/SearchPage.kt b/maps/src/main/java/com/vayunmathur/maps/ui/SearchPage.ktindex 91b84af454..576f7564a3 100644--- a/maps/src/main/java/com/vayunmathur/maps/ui/SearchPage.kt+++ b/maps/src/main/java/com/vayunmathur/maps/ui/SearchPage.kt@@ -1,7 +1,6 @@ package com.vayunmathur.maps.ui  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.Column@@ -11,8 +10,6 @@ import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.padding import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.items-import androidx.compose.foundation.rememberScrollState-import com.vayunmathur.library.ui.AssistChip import com.vayunmathur.library.ui.AppScaffold import com.vayunmathur.library.ui.ExperimentalMaterial3Api import com.vayunmathur.library.ui.HorizontalDivider@@ -22,6 +19,7 @@ import com.vayunmathur.library.ui.Text import com.vayunmathur.library.ui.TextField import com.vayunmathur.library.ui.TextFieldDefaults import androidx.compose.runtime.Composable+import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.remember import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue@@ -58,7 +56,8 @@ fun SearchPage(     east: Double,     west: Double,     north: Double,-    south: Double+    south: Double,+    query: String? = null, ) {     val searchQuery by searchViewModel.query.collectAsState()     val results by searchViewModel.results.collectAsState()@@ -68,6 +67,13 @@ fun SearchPage(     val nearLat = (north + south) / 2.0     val nearLon = (east + west) / 2.0 +    // Pre-fill from a browse-screen category chip tap (Route carries the query).+    LaunchedEffect(query) {+        if (!query.isNullOrBlank()) {+            searchViewModel.setQuery(query, nearLat, nearLon)+        }+    }+     val actions = remember(nearLat, nearLon, idx) {         object : SearchActions {             override fun setQuery(query: String) {@@ -111,18 +117,6 @@ fun SearchPage(     SearchScreen(SearchUiState(searchQuery, results, recents), actions) } -/** One quick-search category chip: a display label and the query it runs. */-private data class SearchCategory(val labelRes: Int, val query: String)--private val SEARCH_CATEGORIES = listOf(-    SearchCategory(R.string.search_category_restaurants, "restaurants"),-    SearchCategory(R.string.search_category_coffee, "coffee"),-    SearchCategory(R.string.search_category_gas, "gas station"),-    SearchCategory(R.string.search_category_groceries, "groceries"),-    SearchCategory(R.string.search_category_hotels, "hotels"),-    SearchCategory(R.string.search_category_atms, "atm"),-)- /** The rendered half of [SearchPage]: query text in, results out, no ViewModel. */ @OptIn(ExperimentalMaterial3Api::class) @Composable@@ -146,7 +140,12 @@ fun SearchScreen(state: SearchUiState, actions: SearchActions) {         onNavigateBack = { actions.back() },     ) { paddingValues ->         Column(modifier = Modifier.padding(paddingValues).fillMaxSize()) {-            CategoryChips(actions)+            CategoryChips(+                onCategory = { actions.setQuery(it) },+                modifier = Modifier+                    .fillMaxWidth()+                    .padding(horizontal = 16.dp, vertical = 8.dp),+            )             Box(modifier = Modifier.fillMaxSize()) {                 when {                     state.query.length >= 2 && state.results.isEmpty() -> {@@ -176,24 +175,6 @@ fun SearchScreen(state: SearchUiState, actions: SearchActions) {     } } -@Composable-private fun CategoryChips(actions: SearchActions) {-    Row(-        modifier = Modifier-            .fillMaxWidth()-            .horizontalScroll(rememberScrollState())-            .padding(horizontal = 16.dp, vertical = 8.dp),-        horizontalArrangement = Arrangement.spacedBy(8.dp),-    ) {-        SEARCH_CATEGORIES.forEach { category ->-            AssistChip(-                onClick = { actions.setQuery(category.query) },-                label = { Text(stringResource(category.labelRes)) },-            )-        }-    }-}- @Composable private fun ResultsList(results: List<SearchResult>, actions: SearchActions) {     LazyColumn(modifier = Modifier.fillMaxSize()) {