Skip to content

feat(map): show the map with item and skill markers - #391

Merged
Julkzein merged 9 commits into
mainfrom
feature/map-screen
Nov 19, 2026
Merged

Julkzein merged 9 commits into
mainfrom
feature/map-screen

Conversation

@Julkzein

@Julkzein Julkzein commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Part of #341 (story #21). Adds the map screen: a Google map centred on the renter or their campus, with the listings as markers that differ for items and skills, the listings' loading, error and empty states, and the location layer (#340) on top. The Map tab and its navigation come in the next PR.

What

  • ListingsMap (Google map), kept to drawing only:
    • the camera from cameraFor(), set rather than animated, since CameraUpdateFactory needs the map to be initialised first;
    • one MarkerComposable per listing with its icon: a box for items, a cap for skills, so they differ by icon and not only by colour (Add the map screens in Figma #353);
    • the renter's approximate circle (150 m, Figma "Map — Centred on you") or their campus marker (Figma "Map — Centred on campus").
  • Plain functions, tested without Google Maps:
    • cameraFor(centre): close on the renter or the campus, an overview of both campuses before the centre is known;
    • markerIcon(type) and markerDescription(type): TalkBack reads a marker as "Bosch drill set, item, CHF 8 per day".
  • MapContent: the map, then the listings' state at the bottom, then the location layer on top:
    • first load: a "Loading listings…" pill; a reload while markers are shown (the tab coming back) shows nothing, so the map doesn't flicker;
    • error: the reason with a 48dp Retry, also when older markers are still shown;
    • no listing with a location: "No listings with a location nearby yet".
  • MapScreen(viewModel) wires it to MapViewModel (feat(map): load the listings to show on the map #390); the map is a parameter, the Google map by default, so the screen can be tested with a stand-in.
  • AndroidManifest.xml: <uses-library android:name="org.apache.http.legacy" android:required="false"/>. Google Maps' Play Services module still uses Apache HTTP, which Android 9+ no longer loads by default; ListingsMapTest crashed on the CI emulator with NoClassDefFoundError org/apache/http/ProtocolVersion without it.
  • Resources: ic_marker_item, ic_marker_service and 5 map_* strings.
  • Tests:
    • 14 in MapScreenTest (Robolectric): 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.
    • ListingsMapTest (instrumented): Google Maps can't run under Robolectric, so this runs ListingsMap on the CI emulator (a google_apis image with Play services), whose coverage the report already includes. It checks that the map and its markers are built without crashing, around the renter, on a campus and while choosing.

Why

#341 (from #21): the Map tab shows items and skills with distinct markers, matching Browse, with Retry on a load error and the cached listings offline. #390 loads the markers; this PR draws them; the next one adds the tab.

Notes for the reviewer

Validation

  • ./gradlew ktfmtCheck: passed
  • ./gradlew check (with lint): 1,650 tests, 0 failures (same JIT exclusion as before for JDK 21.0.2 on Apple Silicon; CI is not affected).
  • JaCoCo locally: MapScreen.kt 111/111 lines. ListingsMap.kt is only covered by the instrumented test, so on CI.
  • Not run: connectedCheck locally (no emulator); CI runs ListingsMapTest.

Follow-ups

AI use

Claude Code helped write the map screen, its markers and their tests; I reviewed, ran and tested all of it.

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.
@Julkzein Julkzein self-assigned this Nov 19, 2026
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.
Base automatically changed from feature/map-listing-markers to main November 19, 2026 16:33

@Mamel0uk Mamel0uk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really nice split: with cameraFor, markerIcon and the swappable map, almost every decision is tested without Google Maps. One important point and two nitpicks below.

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 Mamel0uk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@sonarqubecloud

Copy link
Copy Markdown

@Julkzein
Julkzein merged commit 9963760 into main Nov 19, 2026
3 checks passed
@Julkzein
Julkzein deleted the feature/map-screen branch November 19, 2026 17:40
Julkzein added a commit that referenced this pull request Nov 19, 2026
* feat(map): turn listings into map markers

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.

* feat(map): load the listings to show on the map

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.

* feat(map): show the map with item and skill markers

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.

* test(map): run the Google map on the CI emulator

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.

* feat(map): add the Map tab

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).

* fix(map): load Apache HTTP for Google Maps on Android 9+

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.

* fix(map): key each marker by its listing

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.

* test(map): wait for the Google map instead of sleeping

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.

* test(map): tap Retry in the map screen's Retry test

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants