maps: fix rail lines vanishing after zoom-out/zoom-in
- Commit
7d0dbc77338fe1ae78e89cf0fb9802e1d5640781- Author
- vayun-mathur <[email protected]>
- When
- 2026-09-14 17:02 UTC (2 months ago)
- Parents
fd70ba3
Diff
maps/src/main/java/com/vayunmathur/maps/ui/map/RailLines.kt
diff --git a/maps/src/main/java/com/vayunmathur/maps/ui/map/RailLines.kt b/maps/src/main/java/com/vayunmathur/maps/ui/map/RailLines.ktindex d07ddaf858..d59179483f 100644--- a/maps/src/main/java/com/vayunmathur/maps/ui/map/RailLines.kt+++ b/maps/src/main/java/com/vayunmathur/maps/ui/map/RailLines.kt@@ -154,6 +154,52 @@ internal fun offsetPolyline( } } +/**+ * What the rail collector should do on a settled camera. The refetch /+ * re-fan / restore decision, factored pure for tests. `CLEAR` drops the+ * overlay at the zoom floor but keeps the fetch cache, so zooming back in+ * restores via `REFAN` instead of re-querying the pack.+ */+internal enum class RailRefresh {+ CLEAR,+ REUSE,+ REFAN,+ REFETCH,+}++/**+ * Pure form of the settle logic in [rememberRailLines]: below+ * [RAIL_MIN_ZOOM] clear; outside the fetched footprint (or on first load)+ * refetch; inside it re-fan when the lane count stepped, or when the overlay+ * is null with cached spans (the zoom gate cleared it).+ */+internal fun railRefreshDecision(+ zoom: Double,+ centre: Pair<Double, Double>,+ fetchedCentre: Pair<Double, Double>?,+ fetchedZoom: Double,+ fannedZoom: Double,+ overlayNull: Boolean,+ spansEmpty: Boolean,+): RailRefresh {+ if (zoom < RAIL_MIN_ZOOM) return RailRefresh.CLEAR+ val last = fetchedCentre+ if (last == null ||+ kotlin.math.abs(centre.first - last.first) >= RAIL_RECENTER_DEGREES ||+ kotlin.math.abs(centre.second - last.second) >= RAIL_RECENTER_DEGREES ||+ kotlin.math.abs(zoom - fetchedZoom) >= 1.0+ ) {+ return RailRefresh.REFETCH+ }+ if (spansEmpty) return RailRefresh.REUSE+ if (overlayNull) return RailRefresh.REFAN+ return if (railLaneCount(zoom) != railLaneCount(fannedZoom)) {+ RailRefresh.REFAN+ } else {+ RailRefresh.REUSE+ }+}+ /** * The pack-driven rail-lines network for the viewport, as a [RouteOverlay * ], or null when the transit layer is off, zoomed too far out, or the pack@@ -227,24 +273,23 @@ fun rememberRailLines( .collectLatest { val bounds = camera.visibleBoundsOrWorld() val zoom = camera.position.zoom- if (zoom < RAIL_MIN_ZOOM) {- overlay = null- return@collectLatest- } val centre = (bounds.south + bounds.north) / 2.0 to (bounds.west + bounds.east) / 2.0- val last = fetchedCentre- if (last != null &&- kotlin.math.abs(centre.first - last.first) < RAIL_RECENTER_DEGREES &&- kotlin.math.abs(centre.second - last.second) < RAIL_RECENTER_DEGREES &&- kotlin.math.abs(zoom - fetchedZoom) < 1.0- ) {- // Inside the fetched footprint: re-fan only when the- // lane count stepped.- if (railLaneCount(zoom) != railLaneCount(fannedZoom)) {+ when (railRefreshDecision(+ zoom, centre, fetchedCentre, fetchedZoom,+ fannedZoom, overlay == null, fetchedSpans.isEmpty(),+ )) {+ RailRefresh.CLEAR -> {+ overlay = null+ fannedZoom = Double.NaN+ return@collectLatest+ }+ RailRefresh.REFAN -> { fan(fetchedSpans, zoom, centre.first)+ return@collectLatest }- return@collectLatest+ RailRefresh.REUSE -> return@collectLatest+ RailRefresh.REFETCH -> Unit } // Pad the query past the viewport so small pans stay covered. val padLat = (bounds.north - bounds.south) * 0.25 + 0.05
maps/src/test/java/com/vayunmathur/maps/ui/map/RailFanTest.kt
diff --git a/maps/src/test/java/com/vayunmathur/maps/ui/map/RailFanTest.kt b/maps/src/test/java/com/vayunmathur/maps/ui/map/RailFanTest.ktindex 904fde9262..3dda9b9c31 100644--- a/maps/src/test/java/com/vayunmathur/maps/ui/map/RailFanTest.kt+++ b/maps/src/test/java/com/vayunmathur/maps/ui/map/RailFanTest.kt@@ -70,4 +70,50 @@ class RailFanTest { val line = listOf(GeoPoint(-122.4, 37.7), GeoPoint(-122.4, 37.71)) assertEquals(line, offsetPolyline(line, 0.0)) }++ @Test+ fun `zoom out and back restores lines from cache`() {+ val centre = 37.7 to -122.4+ // First load: no fetch yet, so refetch.+ assertEquals(+ RailRefresh.REFETCH,+ railRefreshDecision(10.0, centre, null, Double.NaN, Double.NaN, true, true),+ )+ // Zoomed out below the floor: clear, even with a warm cache.+ assertEquals(+ RailRefresh.CLEAR,+ railRefreshDecision(8.0, centre, centre, 10.0, 10.0, false, false),+ )+ // Back at the fetched zoom with the overlay cleared: re-fan the+ // cached spans even though the lane count never stepped.+ assertEquals(+ RailRefresh.REFAN,+ railRefreshDecision(10.0, centre, centre, 10.0, Double.NaN, true, false),+ )+ }++ @Test+ fun `steady camera inside the footprint reuses without refan`() {+ val centre = 37.7 to -122.4+ // Same zoom, overlay live: nothing to do.+ assertEquals(+ RailRefresh.REUSE,+ railRefreshDecision(10.0, centre, centre, 10.0, 10.0, false, false),+ )+ // Lane count stepped (z10 -> z12): re-fan cached geometry.+ assertEquals(+ RailRefresh.REFAN,+ railRefreshDecision(12.0, centre, centre, 11.5, 10.0, false, false),+ )+ // Empty fetch stays empty: no pointless refan.+ assertEquals(+ RailRefresh.REUSE,+ railRefreshDecision(10.0, centre, centre, 10.0, 10.0, true, true),+ )+ // Drifted past the recenter window: refetch the pack.+ assertEquals(+ RailRefresh.REFETCH,+ railRefreshDecision(10.0, 37.8 to -122.4, centre, 10.0, 10.0, false, false),+ )+ } }