-
Notifications
You must be signed in to change notification settings - Fork 319
[Android Auto] Remove mapbox navigation from PlaceListOnMapScreen #6371
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
kmadsen
merged 1 commit into
main
from
km-remove-mapbox-navigation-PlaceListOnMapScreen
Sep 27, 2022
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
140 changes: 140 additions & 0 deletions
140
...idauto/src/main/java/com/mapbox/androidauto/car/placeslistonmap/PlacesListOnMapManager.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| package com.mapbox.androidauto.car.placeslistonmap | ||
|
|
||
| import androidx.car.app.model.ItemList | ||
| import com.mapbox.androidauto.car.search.PlaceRecord | ||
| import com.mapbox.androidauto.internal.car.extensions.handleStyleOnAttached | ||
| import com.mapbox.androidauto.internal.car.extensions.handleStyleOnDetached | ||
| import com.mapbox.androidauto.internal.car.extensions.mapboxNavigationForward | ||
| import com.mapbox.androidauto.internal.logAndroidAuto | ||
| import com.mapbox.androidauto.navigation.location.CarAppLocation | ||
| import com.mapbox.geojson.Feature | ||
| import com.mapbox.geojson.FeatureCollection | ||
| import com.mapbox.geojson.Point | ||
| import com.mapbox.maps.MapboxExperimental | ||
| import com.mapbox.maps.extension.androidauto.MapboxCarMapObserver | ||
| import com.mapbox.maps.extension.androidauto.MapboxCarMapSurface | ||
| import com.mapbox.maps.plugin.delegates.listeners.OnStyleLoadedListener | ||
| import com.mapbox.navigation.core.MapboxNavigation | ||
| import com.mapbox.navigation.core.lifecycle.MapboxNavigationApp | ||
| import kotlinx.coroutines.CoroutineScope | ||
| import kotlinx.coroutines.Dispatchers | ||
| import kotlinx.coroutines.MainScope | ||
| import kotlinx.coroutines.cancel | ||
| import kotlinx.coroutines.flow.MutableStateFlow | ||
| import kotlinx.coroutines.flow.StateFlow | ||
| import kotlinx.coroutines.flow.asStateFlow | ||
| import kotlinx.coroutines.launch | ||
| import kotlinx.coroutines.withContext | ||
|
|
||
| @OptIn(MapboxExperimental::class) | ||
| class PlacesListOnMapManager( | ||
| private val placesListOnMapProvider: PlacesListOnMapProvider, | ||
| ) : MapboxCarMapObserver { | ||
|
|
||
| private var carMapSurface: MapboxCarMapSurface? = null | ||
| private var coroutineScope: CoroutineScope? = null | ||
| private var styleLoadedListener: OnStyleLoadedListener? = null | ||
| private var placesListItemMapper: PlacesListItemMapper? = null | ||
| private val placesLayerUtil: PlacesListOnMapLayerUtil = PlacesListOnMapLayerUtil() | ||
| private val navigationObserver = mapboxNavigationForward(this::onAttached) { onDetached() } | ||
|
|
||
| private val _placeRecords = MutableStateFlow(listOf<PlaceRecord>()) | ||
| val placeRecords: StateFlow<List<PlaceRecord>> = _placeRecords.asStateFlow() | ||
|
|
||
| private val _placeSelected = MutableStateFlow<PlaceRecord?>(null) | ||
| val placeSelected: StateFlow<PlaceRecord?> = _placeSelected.asStateFlow() | ||
|
|
||
| private val placeClickListener = object : PlacesListItemClickListener { | ||
| override fun onItemClick(placeRecord: PlaceRecord) { | ||
| logAndroidAuto("PlacesListOnMapScreen request $placeRecord") | ||
| _placeSelected.value = placeRecord | ||
| } | ||
| } | ||
|
|
||
| fun currentItemList(): ItemList? { | ||
| val carAppLocation = MapboxNavigationApp.getObserver(CarAppLocation::class) | ||
| val currentLocation = carAppLocation.navigationLocationProvider.lastLocation | ||
| ?: return null | ||
| return placesListItemMapper?.mapToItemList( | ||
| currentLocation, | ||
| placeRecords.value, | ||
| placeClickListener | ||
| ) | ||
| } | ||
|
|
||
| override fun onAttached(mapboxCarMapSurface: MapboxCarMapSurface) { | ||
| super.onAttached(mapboxCarMapSurface) | ||
| carMapSurface = mapboxCarMapSurface | ||
| coroutineScope = MainScope() | ||
| MapboxNavigationApp.registerObserver(navigationObserver) | ||
|
|
||
| styleLoadedListener = mapboxCarMapSurface.handleStyleOnAttached { | ||
| placesLayerUtil.initializePlacesListOnMapLayer( | ||
| it, | ||
| mapboxCarMapSurface.carContext.resources | ||
| ) | ||
| loadPlaceRecords() | ||
| } | ||
| } | ||
|
|
||
| override fun onDetached(mapboxCarMapSurface: MapboxCarMapSurface) { | ||
| super.onDetached(mapboxCarMapSurface) | ||
| mapboxCarMapSurface.handleStyleOnDetached(styleLoadedListener)?.let { | ||
| placesLayerUtil.removePlacesListOnMapLayer(it) | ||
| } | ||
| styleLoadedListener = null | ||
| MapboxNavigationApp.unregisterObserver(navigationObserver) | ||
| carMapSurface = null | ||
| } | ||
|
|
||
| private fun onAttached(mapboxNavigation: MapboxNavigation) { | ||
| placesListItemMapper = PlacesListItemMapper( | ||
| PlaceMarkerRenderer(carMapSurface?.carContext!!), | ||
| mapboxNavigation | ||
| .navigationOptions | ||
| .distanceFormatterOptions | ||
| .unitType | ||
| ) | ||
| } | ||
|
|
||
| private fun onDetached() { | ||
| placesListItemMapper = null | ||
| coroutineScope?.cancel() | ||
| coroutineScope = null | ||
kmadsen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| private fun loadPlaceRecords() { | ||
| coroutineScope?.launch { | ||
| val expectedPlaceRecords = withContext(Dispatchers.IO) { | ||
| placesListOnMapProvider.getPlaces() | ||
| } | ||
| _placeRecords.value = emptyList() | ||
| expectedPlaceRecords.fold( | ||
| { | ||
| logAndroidAuto( | ||
| "PlacesListOnMapScreen ${it.errorMessage}, ${it.throwable?.stackTrace}" | ||
| ) | ||
| }, | ||
| { | ||
| _placeRecords.value = it | ||
| addPlaceIconsToMap(it) | ||
| } | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| private fun addPlaceIconsToMap(places: List<PlaceRecord>) { | ||
| logAndroidAuto("PlacesListOnMapScreen addPlaceIconsToMap with ${places.size} places.") | ||
| carMapSurface?.mapSurface?.getMapboxMap()?.let { mapboxMap -> | ||
| val features = places.filter { it.coordinate != null }.map { | ||
| Feature.fromGeometry( | ||
| Point.fromLngLat(it.coordinate!!.longitude(), it.coordinate.latitude()) | ||
| ) | ||
| } | ||
| val featureCollection = FeatureCollection.fromFeatures(features) | ||
| mapboxMap.getStyle()?.let { | ||
| placesLayerUtil.updatePlacesListOnMapLayer(it, featureCollection) | ||
| } | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's this dependency that needs to be moved, getting the unit type to become observable is a bit complicated