diff --git a/app/src/main/java/com/android/polybazaar/ui/navigation/AppNavHost.kt b/app/src/main/java/com/android/polybazaar/ui/navigation/AppNavHost.kt index a3ce4073..3b077936 100644 --- a/app/src/main/java/com/android/polybazaar/ui/navigation/AppNavHost.kt +++ b/app/src/main/java/com/android/polybazaar/ui/navigation/AppNavHost.kt @@ -85,6 +85,10 @@ import com.android.polybazaar.listing.ui.ListingDetailScreen import com.android.polybazaar.listing.viewmodel.ListingDetailViewModel import com.android.polybazaar.listing.viewmodel.ListingFormViewModel import com.android.polybazaar.map.model.LocationRepository +import com.android.polybazaar.map.ui.ListingsMap +import com.android.polybazaar.map.ui.MapScreen +import com.android.polybazaar.map.viewmodel.MapUiState +import com.android.polybazaar.map.viewmodel.MapViewModel import com.android.polybazaar.profile.model.ProfileRepository import com.android.polybazaar.profile.ui.EditProfileActions import com.android.polybazaar.profile.ui.EditProfileContent @@ -107,8 +111,8 @@ object AppNavHostTags { /** * Navigation graph of the app. It starts on a loading screen until the persisted session has been * checked, then routes a signed-in user to Browse, or to the verify email screen while their email - * is not verified, and everyone else to sign-in. Browse, Create listing, Conversations and Profile - * share a bottom bar. + * is not verified, and everyone else to sign-in. Browse, Map, Create listing, Conversations and + * Profile share a bottom bar. * * All auth screens share [authViewModel], so a sign-in made on one of them is seen by the others. * Signing out from any signed-in screen returns to sign-in and clears the back stack. @@ -118,10 +122,13 @@ object AppNavHostTags { * @param listingRepository gives the Browse screen the listings to show, and saves new ones. * @param chatRepository gives the Conversations screen the signed-in user's conversations. * @param bookingRepository sends the booking requests made from a listing's "Choose dates". + * @param listingsMap draws the Map tab's map: the Google map, or a stand-in in tests, where Google + * Maps can't run. * @param photoStorage keeps the photos of the listing being created. * @param editPhotoStorage keeps the photos added to the listing being edited. It must use another * folder than [photoStorage]: opening a form deletes the photos its folder holds for other forms. - * @param locationRepository finds the lender, to open the listing form's map picker where they are. + * @param locationRepository reads the device's approximate location: to centre the Map tab on the + * renter, and to open the listing form's map picker where the lender is. * @param connectivityRepository tells the listing forms whether the device is online. */ @Composable @@ -137,6 +144,7 @@ fun AppNavHost( locationRepository: LocationRepository, connectivityRepository: ConnectivityRepository, navController: NavHostController = rememberNavController(), + listingsMap: @Composable (MapUiState) -> Unit = { ListingsMap(it.centre, it.markers) }, ) { val uiState by authViewModel.uiState.collectAsState() val snackbarHostState = remember { SnackbarHostState() } @@ -536,6 +544,29 @@ fun AppNavHost( onBack = { navController.popBackStack() }, ) } + composable(Routes.MAP) { + val uid = uiState.user?.uid ?: return@composable + // Keyed by uid, like Browse: the map leaves out the signed-in user's own listings. + val mapViewModel: MapViewModel = + viewModel( + key = "map_$uid", + factory = + viewModelFactory { + initializer { + MapViewModel( + locationRepository = locationRepository, + listingRepository = listingRepository, + currentUid = uid, + savedStateHandle = createSavedStateHandle(), + ) + } + }, + ) + // Coming back to the tab shows listings created or paused meanwhile. On opening, the + // ViewModel ignores this call while its first load runs. + LifecycleEventEffect(Lifecycle.Event.ON_RESUME) { mapViewModel.loadListings() } + MapScreen(viewModel = mapViewModel, map = listingsMap) + } composable(Routes.INBOX) { // Renders nothing for the frame between sign-out and the redirect to sign-in. val uid = uiState.user?.uid ?: return@composable @@ -682,6 +713,7 @@ private val signedInRoutes = setOf( Routes.VERIFY_EMAIL, Routes.BROWSE, + Routes.MAP, Routes.CREATE_LISTING, Routes.EDIT_LISTING, Routes.LISTING_DETAIL, diff --git a/app/src/main/java/com/android/polybazaar/ui/navigation/BottomBar.kt b/app/src/main/java/com/android/polybazaar/ui/navigation/BottomBar.kt index ae557e20..541813fa 100644 --- a/app/src/main/java/com/android/polybazaar/ui/navigation/BottomBar.kt +++ b/app/src/main/java/com/android/polybazaar/ui/navigation/BottomBar.kt @@ -41,6 +41,7 @@ import com.android.polybazaar.auth.ui.AuthColors object BottomBarTags { const val BAR = "bottom_bar" const val BROWSE_TAB = "bottom_bar_browse_tab" + const val MAP_TAB = "bottom_bar_map_tab" const val CREATE_LISTING_TAB = "bottom_bar_create_listing_tab" const val CONVERSATIONS_TAB = "bottom_bar_conversations_tab" const val PROFILE_TAB = "bottom_bar_profile_tab" @@ -61,6 +62,13 @@ private val tabs = R.drawable.ic_nav_browse, BottomBarTags.BROWSE_TAB, ), + // Second, next to Browse: the same listings, on a map (Figma "Map — …" frames, #341). + BottomBarTab( + Routes.MAP, + R.string.nav_map, + R.drawable.ic_nav_map, + BottomBarTags.MAP_TAB, + ), BottomBarTab( Routes.CREATE_LISTING, R.string.nav_create_listing, diff --git a/app/src/main/java/com/android/polybazaar/ui/navigation/Routes.kt b/app/src/main/java/com/android/polybazaar/ui/navigation/Routes.kt index b76b96bc..9524aad1 100644 --- a/app/src/main/java/com/android/polybazaar/ui/navigation/Routes.kt +++ b/app/src/main/java/com/android/polybazaar/ui/navigation/Routes.kt @@ -14,6 +14,7 @@ object Routes { const val FORGOT_PASSWORD = "forgot_password" const val VERIFY_EMAIL = "verify_email" const val BROWSE = "browse" + const val MAP = "map" const val CREATE_LISTING = "create_listing" const val LISTING_ID = "listingId" const val EDIT_LISTING = "edit_listing/{$LISTING_ID}" diff --git a/app/src/main/res/drawable/ic_nav_map.xml b/app/src/main/res/drawable/ic_nav_map.xml new file mode 100644 index 00000000..cffac933 --- /dev/null +++ b/app/src/main/res/drawable/ic_nav_map.xml @@ -0,0 +1,15 @@ + + + + + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 9fcd6438..f6e920d0 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -6,6 +6,7 @@ Browse + Map Create listing Conversations Profile diff --git a/app/src/test/java/com/android/polybazaar/ui/navigation/AppNavHostTest.kt b/app/src/test/java/com/android/polybazaar/ui/navigation/AppNavHostTest.kt index e7d0a579..93368d49 100644 --- a/app/src/test/java/com/android/polybazaar/ui/navigation/AppNavHostTest.kt +++ b/app/src/test/java/com/android/polybazaar/ui/navigation/AppNavHostTest.kt @@ -7,8 +7,12 @@ package com.android.polybazaar.ui.navigation import android.content.Context import android.net.Uri import androidx.activity.compose.LocalActivityResultRegistryOwner +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.runtime.CompositionLocalProvider import androidx.compose.runtime.remember +import androidx.compose.ui.Modifier +import androidx.compose.ui.platform.testTag import androidx.compose.ui.semantics.Role import androidx.compose.ui.semantics.SemanticsActions import androidx.compose.ui.semantics.SemanticsProperties @@ -80,11 +84,14 @@ import com.android.polybazaar.listing.model.FakeListingRepository import com.android.polybazaar.listing.model.FilePhotoStorage import com.android.polybazaar.listing.model.ItemListing import com.android.polybazaar.listing.model.ListingCategory +import com.android.polybazaar.listing.model.ListingLocation import com.android.polybazaar.listing.model.ServiceListing import com.android.polybazaar.listing.ui.CreateListingScreenTags import com.android.polybazaar.listing.ui.EditListingScreenTags import com.android.polybazaar.listing.ui.ListingDetailScreenTags import com.android.polybazaar.map.model.FakeLocationRepository +import com.android.polybazaar.map.ui.MapScreenTags +import com.android.polybazaar.map.viewmodel.MapUiState import com.android.polybazaar.profile.model.ProfileRepository import com.android.polybazaar.profile.ui.EditProfileScreenTags import com.android.polybazaar.profile.ui.LenderProfileScreenTags @@ -134,6 +141,10 @@ class AppNavHostTest { private val listingRepository = FakeListingRepository(ownerUid = user.uid) private val chatRepository = FakeChatRepository() private val bookingRepository = FakeBookingRepository() + private val locationRepository = FakeLocationRepository() + + /** What the Map tab last gave its map: the stand-in records it, since it draws no markers. */ + private var mapState: MapUiState? = null private val photoStorage = FilePhotoStorage( ApplicationProvider.getApplicationContext(), @@ -166,7 +177,12 @@ class AppNavHostTest { editPhotoStorage = editPhotoStorage, chatRepository = chatRepository, bookingRepository = bookingRepository, - locationRepository = FakeLocationRepository(), + locationRepository = locationRepository, + // Google Maps can't run under Robolectric: a stand-in that shows the markers. + listingsMap = { + mapState = it + Box(Modifier.fillMaxSize().testTag(MAP_STAND_IN)) + }, connectivityRepository = FakeConnectivityRepository(), navController = navController, ) @@ -210,7 +226,12 @@ class AppNavHostTest { editPhotoStorage = editPhotoStorage, chatRepository = chatRepository, bookingRepository = bookingRepository, - locationRepository = FakeLocationRepository(), + locationRepository = locationRepository, + // Google Maps can't run under Robolectric: a stand-in that shows the markers. + listingsMap = { + mapState = it + Box(Modifier.fillMaxSize().testTag(MAP_STAND_IN)) + }, connectivityRepository = FakeConnectivityRepository(), navController = navController, ) @@ -260,6 +281,12 @@ class AppNavHostTest { assertEquals(Routes.INBOX, navController.currentBackStackEntry?.destination?.route) } + private fun openMap() { + composeTestRule.onNodeWithTag(BottomBarTags.MAP_TAB).performClick() + composeTestRule.onNodeWithTag(MapScreenTags.SCREEN).assertIsDisplayed() + assertEquals(Routes.MAP, navController.currentBackStackEntry?.destination?.route) + } + private fun openBrowse() { composeTestRule.onNodeWithTag(BottomBarTags.BROWSE_TAB).performClick() composeTestRule.onNodeWithTag(BrowseScreenTags.SCREEN).assertIsDisplayed() @@ -681,9 +708,10 @@ class AppNavHostTest { editPhotoStorage = editPhotoStorage, chatRepository = chatRepository, bookingRepository = bookingRepository, - locationRepository = FakeLocationRepository(), + locationRepository = locationRepository, connectivityRepository = FakeConnectivityRepository(), navController = navController, + listingsMap = { mapState = it }, ) } @@ -1514,9 +1542,10 @@ class AppNavHostTest { editPhotoStorage = editPhotoStorage, chatRepository = chatRepository, bookingRepository = bookingRepository, - locationRepository = FakeLocationRepository(), + locationRepository = locationRepository, connectivityRepository = FakeConnectivityRepository(), navController = navController, + listingsMap = { mapState = it }, ) } openProfile() @@ -1592,9 +1621,10 @@ class AppNavHostTest { editPhotoStorage = editPhotoStorage, chatRepository = chatRepository, bookingRepository = bookingRepository, - locationRepository = FakeLocationRepository(), + locationRepository = locationRepository, connectivityRepository = FakeConnectivityRepository(), navController = navController, + listingsMap = { mapState = it }, ) } composeTestRule.onNodeWithTag(VerifyEmailScreenTags.SCREEN).assertIsDisplayed() @@ -1664,6 +1694,9 @@ class AppNavHostTest { composeTestRule.onNodeWithTag(BrowseScreenTags.SCREEN).assertIsDisplayed() composeTestRule.onNodeWithTag(BottomBarTags.BAR).assertIsDisplayed() + openMap() + composeTestRule.onNodeWithTag(BottomBarTags.BAR).assertIsDisplayed() + openCreateListing() composeTestRule.onNodeWithTag(BottomBarTags.BAR).assertIsDisplayed() @@ -1674,6 +1707,39 @@ class AppNavHostTest { composeTestRule.onNodeWithTag(BottomBarTags.BAR).assertIsDisplayed() } + @Test + fun mapTab_isSecond_betweenBrowseAndCreateListing() { + setContent(FakeAuthRepository(currentUser = user)) + + fun left(tag: String) = composeTestRule.onNodeWithTag(tag).getBoundsInRoot().left + assertTrue(left(BottomBarTags.BROWSE_TAB) < left(BottomBarTags.MAP_TAB)) + assertTrue(left(BottomBarTags.MAP_TAB) < left(BottomBarTags.CREATE_LISTING_TAB)) + } + + @Test + fun mapTab_showsOtherUsersListingsWithALocation_andReloadsWhenComingBack() { + val rolex = ListingLocation(46.5185, 6.5683, "Rolex Learning Center") + listingRepository.allListings = + listOf( + gracesDrill.copy(location = rolex), + gracesDrill.copy(listingId = "nowhere", title = "No location"), + ) + setContent(FakeAuthRepository(currentUser = user)) + + openMap() + composeTestRule.waitForIdle() + assertEquals(listOf("drill"), mapState?.markers?.map { it.id }) + + // A listing placed meanwhile shows when the renter comes back to the tab. + listingRepository.allListings = + listingRepository.allListings + gracesDrill.copy(listingId = "tent", location = rolex) + openBrowse() + openMap() + composeTestRule.waitForIdle() + + assertEquals(listOf("drill", "tent"), mapState?.markers?.map { it.id }) + } + @Test fun bottomBar_isHiddenOnLoading() { setContent(FakeAuthRepository(currentUserGate = CompletableDeferred())) @@ -2228,3 +2294,6 @@ class AppNavHostTest { } private const val NETWORK_MESSAGE = "No connection. Check your internet and try again." + +/** Tag of the stand-in drawn instead of the Google map, which can't run under Robolectric. */ +private const val MAP_STAND_IN = "map_stand_in" diff --git a/app/src/test/java/com/android/polybazaar/ui/navigation/EndToEndFlowTest.kt b/app/src/test/java/com/android/polybazaar/ui/navigation/EndToEndFlowTest.kt index e66ca495..dd6697fe 100644 --- a/app/src/test/java/com/android/polybazaar/ui/navigation/EndToEndFlowTest.kt +++ b/app/src/test/java/com/android/polybazaar/ui/navigation/EndToEndFlowTest.kt @@ -89,6 +89,7 @@ class EndToEndFlowTest { chatRepository = FakeChatRepository(), bookingRepository = FakeBookingRepository(), locationRepository = FakeLocationRepository(), + listingsMap = {}, connectivityRepository = FakeConnectivityRepository(), navController = navController, )