Repository navigation
feat(map): add the Map tab - #392
Conversation
New MapMarker (#341): a listing's id, title, type (item or service, so the map can draw them with different icons), coordinates and price, formatted by formatChf like the Browse cards. Listing.toMapMarker() builds it, or returns null for a listing that can't be placed: not saved yet, or without a location, which owners may not have set (#345 made it optional). Tests (MapMarkerTest): an item at its location, a service, no marker without a location, and none for an unsaved listing.
MapViewModel now also loads the markers for the map tab (#341): - MapUiState gets markers, isLoading and error. - loadListings(), called on creation and again on Retry or when the tab comes back, shows the same listings as Browse: every listing that isn't paused (getAllListings), except the signed-in user's own (currentUid), and only those with a location (toMapMarker). - Offline, Firestore answers from its cache; on failure the markers already shown are kept and error says why. A load while one runs is ignored, cancellation is rethrown and isLoading is cleared in a finally, as in BrowseViewModel. - The constructor takes the ListingRepository and the current uid; the existing tests pass a FakeListingRepository. Tests: 6 more in MapViewModelTest (21 in total): only other users' listings with a location (fails without the uid filter), isLoading, an error and its retry, a failed reload keeping the markers, a second load while one runs (fails without the guard), and a cancelled load.
New MapScreen for the Map tab (#341): - ListingsMap draws the Google map: the camera from cameraFor(), one MarkerComposable per listing with its icon (a box for items, a cap for skills, so they differ by icon and not only colour, #353), the renter's approximate circle (150 m) or their campus marker. The camera position is set rather than animated, since CameraUpdateFactory needs the map to be initialised first. - Every decision is in plain functions so it can be tested without Google Maps, which can't run in unit tests: cameraFor() (close on the renter or the campus, an overview of both campuses before that), markerIcon() and markerDescription() (read by TalkBack as "Bosch drill set, item, CHF 8 per day"). - MapContent draws the map, then the listings' state at the bottom (a loading pill on the first load, the error with a 48dp Retry, or "No listings with a location nearby yet"; nothing during a reload with markers, so the map doesn't flicker), then the location layer (#340) on top. MapScreen(viewModel) wires it; its map is a parameter, the Google map by default. Resources: ic_marker_item, ic_marker_service and 5 map_* strings. Tests (MapScreenTest, 14): the camera for each centre, different icons and descriptions per type, the marker label and pin, each listings state, Retry, and the screen with the real MapViewModel.
ListingsMap can't run under Robolectric ("CameraUpdateFactory is not
initialized"), so its lines would stay uncovered. The CI emulator is a
google_apis image with Google Play services, and the coverage report
already includes the connected tests, so ListingsMapTest (instrumented)
builds the map with an item and a skill marker around the renter, then
on a campus, then while choosing. Tiles need a valid key, so it checks
that the map and its markers are built without crashing, not what the
tiles show.
The map screen (#391) gets its tab (#341): - Routes.MAP and a Map tab in the bottom bar, second between Browse and Create listing as in the Figma "Map — …" frames, with a folded-map icon (ic_nav_map) and the "Map" label. - AppNavHost builds MapViewModel for the signed-in user (keyed by uid, like Browse), with the location repository, the listing repository and createSavedStateHandle(), and reloads the listings when the tab comes back (ON_RESUME), as the Toolbox does. - AppNavHost takes a locationRepository, which MainActivity builds as FusedLocationRepository(LocationServices.getFusedLocationProviderClient), and a listingsMap (the Google map by default) that Robolectric tests replace with a stand-in, since Google Maps can't run there. - Every AppNavHost caller passes a FakeLocationRepository in tests (AppNavHostTest, EndToEndFlowTest, and the instrumented PolyBazaarTestCase, which keeps the real map on the emulator). Tests (AppNavHostTest): the bottom bar on the Map tab, Map second between Browse and Create listing, and the map showing other users' listings with a location, then a listing added meanwhile once the renter comes back to the tab (fails without the ON_RESUME reload).
ListingsMapTest crashed on the CI emulator with NoClassDefFoundError org/apache/http/ProtocolVersion, thrown from Play Services' Maps module, which still uses Apache HTTP; Android 9+ no longer loads it by default, so the map could crash in the app too. The manifest now declares <uses-library android:name="org.apache.http.legacy" android:required="false"/>, as Google's Maps SDK documentation asks; not required, so devices without it can still install the app.
# Conflicts: # app/src/androidTest/java/com/android/polybazaar/PolyBazaarTestCase.kt # app/src/main/java/com/android/polybazaar/MainActivity.kt # app/src/main/java/com/android/polybazaar/ui/navigation/AppNavHost.kt # app/src/test/java/com/android/polybazaar/ui/navigation/AppNavHostTest.kt # app/src/test/java/com/android/polybazaar/ui/navigation/EndToEndFlowTest.kt
Inside the forEach, rememberMarkerState's state was tied to the marker's position in the list (its key only names the saved state, and the position only counts the first time). After a reload that dropped the first listing, the second took its state and was drawn where the first had been, and a listing its owner moved kept its old pin (Mamel0uk on #391; rememberMarkerState is deprecated in maps-compose 9 for this reason). Each listing marker, and the campus marker, now sits in key(id) { } and uses rememberUpdatedMarkerState(position), so it keeps its own state and follows its current position.
ListingsMapTest slept 3 x 3 s and asserted nothing, so it was slow and could still be flaky on a slow emulator (Mamel0uk on #391). ListingsMap takes an onMapReady callback, called from MapEffect once the map is created, and the test waits up to 20 s for it, then checks the campus and overview states recompose. It waits for "created" rather than onMapLoaded, which waits for the tiles: our Maps key doesn't accept debug builds, so onMapLoaded might never fire on CI.
screen_showsTheViewModelsMarkers_andRetryReloads never tapped Retry (Mamel0uk on #391). The first load now fails, as offline without a cache, so no marker is shown; Retry is tapped and the listing appears, and the error goes away. A campus is picked first, so the location card doesn't cover the bottom of the map where Retry is.
Mamel0uk
left a comment
There was a problem hiding this comment.
lgtm, could you open an issue for the two nitpicks below?
| locationRepository = FakeLocationRepository(), | ||
| locationRepository = locationRepository, | ||
| // Google Maps can't run under Robolectric: a stand-in that shows the markers. | ||
| listingsMap = { |
There was a problem hiding this comment.
Nitpick: this stand-in is copied in both setContent helpers (here and l.231), and l.714/1548/1627 use another one, { mapState = it }. Also no test ever looks up MAP_STAND_IN, and the comment says it shows the markers while it's an empty box. Could it be one shared private val mapStandIn, with the unused tag dropped or the comment fixed?
There was a problem hiding this comment.
Good points, both are in #394 so they don't reset the approval here.
| locationRepository: LocationRepository, | ||
| connectivityRepository: ConnectivityRepository, | ||
| navController: NavHostController = rememberNavController(), | ||
| listingsMap: @Composable (MapUiState) -> Unit = { ListingsMap(it.centre, it.markers) }, |
There was a problem hiding this comment.
Nitpick: this default copies the map default of MapScreen. If one of them changes (say ListingsMap gets a new param), the app could quietly stop matching what MapScreen does. Could listingsMap be nullable here and only be passed on to MapScreen when it's set?
There was a problem hiding this comment.
Good points, both are in #394 so they don't reset the approval here.
|



Summary
Closes #341 (story #21). The map is now a tab: Map sits second in the bottom bar, between Browse and Create listing, and opens the map screen (#391) with the listings of #390 and the location fallback of #340.
What
Routes.MAPand a Map tab inBottomBar, second as in the Figma "Map — …" frames, with a folded-map icon (ic_nav_map) and the "Map" label.AppNavHost:MapViewModelfor the signed-in user (keyed by uid, like Browse, since the map leaves out their own listings) with the location repository, the listing repository andcreateSavedStateHandle();ON_RESUME), as the Toolbox does, so listings created or paused meanwhile show; on opening, the ViewModel ignores the call while its first load runs;locationRepositoryit already receives for the listing form's map picker (feat(listing): let the listing form set and name a location #366), whichMainActivitybuilds asFusedLocationRepository, and takes alistingsMap(the Google map by default) that Robolectric tests replace with a stand-in, since Google Maps can't run there.AppNavHostTestandEndToEndFlowTestpass a stand-inlistingsMap; the instrumentedPolyBazaarTestCasekeeps the real map on the emulator.ic_nav_mapandnav_map.AppNavHostTest(104 in total) and the Map tab added tobottomBar_isShownOnEveryTab: Map second between Browse and Create listing, and the map showing other users' listings with a location, then a listing added meanwhile once the renter comes back to the tab.Why
#341 (from #21): a Map tab with item and skill markers, matching Browse, with Retry on a load error and the cached listings offline. #390 loads the markers, #391 draws the map; this PR puts it on the bottom bar, so it closes the issue.
Notes for the reviewer
main.listingsMapparameter exists only so tests can avoid the Google map; the app always uses the default.Validation
./gradlew ktfmtCheck: passed./gradlew check(with lint): 1,652 tests, 0 failures (same JIT exclusion as before for JDK 21.0.2 on Apple Silicon; CI is not affected).ON_RESUMEreload makesmapTab_showsOtherUsersListingsWithALocation_andReloadsWhenComingBackfail.connectedChecklocally (no emulator), and the app on a device with a valid Maps key.Follow-ups
AI use
Claude Code helped write the Map tab, its navigation and their tests; I reviewed, ran and tested all of it.