From 1b66be15c9ad4f4674884fcb8c00f7e1c40be4cc Mon Sep 17 00:00:00 2001 From: Devota Aabel Date: Fri, 12 Apr 2019 17:37:45 -0400 Subject: [PATCH 1/5] Example activity refactor for simplicity/stability --- .../testapp/example/ui/ExampleActivity.kt | 6 -- .../testapp/example/ui/ExamplePresenter.kt | 87 ++++++++++--------- .../testapp/example/ui/ExampleView.kt | 2 - .../testapp/example/ui/ExampleViewModel.kt | 25 +++++- .../testapp/example/ui/PresenterState.kt | 5 +- .../ui/navigation/ExampleRouteFinder.kt | 2 + .../example/ui/navigation/RouteFinder.kt | 5 +- app/src/main/res/layout/activity_example.xml | 7 -- app/src/main/res/values/arrays.xml | 6 +- app/src/main/res/values/strings.xml | 4 + .../xml/fragment_navigation_preferences.xml | 1 + 11 files changed, 83 insertions(+), 67 deletions(-) diff --git a/app/src/main/java/com/mapbox/services/android/navigation/testapp/example/ui/ExampleActivity.kt b/app/src/main/java/com/mapbox/services/android/navigation/testapp/example/ui/ExampleActivity.kt index db1904290ad..046f1c3abc9 100644 --- a/app/src/main/java/com/mapbox/services/android/navigation/testapp/example/ui/ExampleActivity.kt +++ b/app/src/main/java/com/mapbox/services/android/navigation/testapp/example/ui/ExampleActivity.kt @@ -39,7 +39,6 @@ private const val ZERO_PADDING = 0 private const val BOTTOMSHEET_MULTIPLIER = 4 class ExampleActivity : HistoryActivity(), ExampleView { - private var map: NavigationMapboxMap? = null private val viewModel by lazy(mode = LazyThreadSafetyMode.NONE) { ViewModelProviders.of(this).get(ExampleViewModel::class.java) @@ -193,10 +192,6 @@ class ExampleActivity : HistoryActivity(), ExampleView { locationFab.visibility = visibility } - override fun updateDirectionsFabVisibility(visibility: Int) { - directionsFab.visibility = visibility - } - override fun updateNavigationFabVisibility(visibility: Int) { navigationFab.visibility = visibility } @@ -293,7 +288,6 @@ class ExampleActivity : HistoryActivity(), ExampleView { settingsFab.setOnClickListener { presenter.onSettingsFabClick() } locationFab.setOnClickListener { presenter.onLocationFabClick() } - directionsFab.setOnClickListener { presenter.onDirectionsFabClick() } navigationFab.setOnClickListener { presenter.onNavigationFabClick() } cancelFab.setOnClickListener { presenter.onCancelFabClick() } attribution.setOnClickListener { presenter.onAttributionsClick(it) } diff --git a/app/src/main/java/com/mapbox/services/android/navigation/testapp/example/ui/ExamplePresenter.kt b/app/src/main/java/com/mapbox/services/android/navigation/testapp/example/ui/ExamplePresenter.kt index ccd2e31d9aa..8da1cd0936e 100644 --- a/app/src/main/java/com/mapbox/services/android/navigation/testapp/example/ui/ExamplePresenter.kt +++ b/app/src/main/java/com/mapbox/services/android/navigation/testapp/example/ui/ExamplePresenter.kt @@ -35,7 +35,7 @@ private const val ONE_SECOND = 1000 class ExamplePresenter(private val view: ExampleView, private val viewModel: ExampleViewModel) { - private var state: PresenterState = PresenterState.SHOW_LOCATION + private var presenterState: PresenterState = PresenterState.SHOW_LOCATION fun onPermissionsGranted(granted: Boolean) { if (granted) { @@ -57,7 +57,6 @@ class ExamplePresenter(private val view: ExampleView, private val viewModel: Exa fun onAutocompleteClick() { view.selectAllAutocompleteText() view.updateLocationFabVisibility(INVISIBLE) - view.updateSettingsFabVisibility(INVISIBLE) view.updateAutocompleteBottomSheetState(BottomSheetBehavior.STATE_EXPANDED) } @@ -75,14 +74,9 @@ class ExamplePresenter(private val view: ExampleView, private val viewModel: Exa } } - fun onDirectionsFabClick() { - state = PresenterState.FIND_ROUTE - viewModel.findRouteToDestination() - } - fun onNavigationFabClick() { if (viewModel.canNavigate()) { - state = PresenterState.NAVIGATE + presenterState = PresenterState.NAVIGATE view.showAlternativeRoutes(false) view.addMapProgressChangeListener(viewModel.retrieveNavigation()) view.updateNavigationFabVisibility(INVISIBLE) @@ -97,7 +91,11 @@ class ExamplePresenter(private val view: ExampleView, private val viewModel: Exa } fun onCancelFabClick() { - state = PresenterState.SHOW_LOCATION + clearView() + } + + private fun clearView() { + presenterState = PresenterState.SHOW_LOCATION viewModel.stopNavigation() view.removeRoute() view.clearMarkers() @@ -111,30 +109,28 @@ class ExamplePresenter(private val view: ExampleView, private val viewModel: Exa view.updateAutocompleteBottomSheetState(BottomSheetBehavior.STATE_COLLAPSED) } - fun onAutocompleteBottomSheetStateChange(state: Int) { - when (state) { + fun onAutocompleteBottomSheetStateChange(newState: Int) { + when (newState) { BottomSheetBehavior.STATE_COLLAPSED -> { - viewModel.collapsedBottomSheet = true view.hideSoftKeyboard() - if (this.state == PresenterState.SHOW_LOCATION) { - view.updateLocationFabVisibility(VISIBLE) - view.updateSettingsFabVisibility(VISIBLE) - } + presenterState = PresenterState.SHOW_LOCATION + + view.updateLocationFabVisibility(VISIBLE) + view.updateSettingsFabVisibility(VISIBLE) } BottomSheetBehavior.STATE_EXPANDED -> { - viewModel.collapsedBottomSheet = false + presenterState = PresenterState.SEARCH } } } fun onDestinationFound(feature: CarmenFeature) { feature.center()?.let { - if (state == PresenterState.ROUTE_FOUND) { + if (presenterState == PresenterState.SHOW_ROUTE) { view.removeRoute() viewModel.primaryRoute = null view.updateNavigationFabVisibility(INVISIBLE) } - state = PresenterState.SELECTED_DESTINATION viewModel.destination.value = it view.clearMarkers() view.hideSoftKeyboard() @@ -143,13 +139,14 @@ class ExamplePresenter(private val view: ExampleView, private val viewModel: Exa view.updateMapCamera(buildCameraUpdateFrom(it), TWO_SECONDS) view.updateLocationFabVisibility(INVISIBLE) view.updateSettingsFabVisibility(INVISIBLE) - view.updateDirectionsFabVisibility(VISIBLE) + view.updateNavigationFabVisibility(VISIBLE) + viewModel.findRouteToDestination() } } fun onLocationUpdate(location: Location?) { location?.let { - if (state == PresenterState.SHOW_LOCATION) { + if (presenterState == PresenterState.SHOW_LOCATION) { view.updateMapCamera(buildCameraUpdateFrom(location), TWO_SECONDS) } view.updateAutocompleteProximity(location) @@ -159,25 +156,17 @@ class ExamplePresenter(private val view: ExampleView, private val viewModel: Exa fun onRouteFound(routes: List?) { routes?.let { directionsRoutes -> - when (state) { - PresenterState.FIND_ROUTE -> { - state = PresenterState.ROUTE_FOUND - view.transition() - view.showAlternativeRoutes(true) - view.updateRoutes(directionsRoutes) - view.updateDirectionsFabVisibility(INVISIBLE) - view.updateNavigationFabVisibility(VISIBLE) - viewModel.destination.value?.let { destination -> - moveCameraToInclude(destination) - } - } - PresenterState.NAVIGATE -> { - view.updateRoutes(directionsRoutes) - } - else -> { - // TODO no impl + if (presenterState != PresenterState.NAVIGATE) { + view.transition() + view.showAlternativeRoutes(true) + view.updateNavigationFabVisibility(VISIBLE) + viewModel.destination.value?.let { destination -> + moveCameraToInclude(destination) } + presenterState = PresenterState.SHOW_ROUTE } + + view.updateRoutes(directionsRoutes) } } @@ -199,15 +188,29 @@ class ExamplePresenter(private val view: ExampleView, private val viewModel: Exa fun onMapLongClick(point: LatLng): Boolean { viewModel.reverseGeocode(point) + viewModel.destination.value = Point.fromLngLat(point.longitude, point.latitude) + viewModel.findRouteToDestination() return true } fun onBackPressed(): Boolean { - if (!viewModel.collapsedBottomSheet) { - view.updateAutocompleteBottomSheetState(BottomSheetBehavior.STATE_COLLAPSED) - return false + when (presenterState) { + PresenterState.SEARCH -> { + view.updateAutocompleteBottomSheetState(BottomSheetBehavior.STATE_COLLAPSED) + return false + } + PresenterState.SHOW_ROUTE -> { + clearView() + return false + } + + PresenterState.NAVIGATE -> { + clearView() + return false + } + + else -> return true } - return true } fun subscribe(owner: LifecycleOwner) { diff --git a/app/src/main/java/com/mapbox/services/android/navigation/testapp/example/ui/ExampleView.kt b/app/src/main/java/com/mapbox/services/android/navigation/testapp/example/ui/ExampleView.kt index 99e4f06e14c..0301ee47209 100644 --- a/app/src/main/java/com/mapbox/services/android/navigation/testapp/example/ui/ExampleView.kt +++ b/app/src/main/java/com/mapbox/services/android/navigation/testapp/example/ui/ExampleView.kt @@ -45,8 +45,6 @@ interface ExampleView: PermissionsListener, OnMapReadyCallback, fun updateLocationFabVisibility(visibility: Int) - fun updateDirectionsFabVisibility(visibility: Int) - fun updateNavigationFabVisibility(visibility: Int) fun updateCancelFabVisibility(visibility: Int) diff --git a/app/src/main/java/com/mapbox/services/android/navigation/testapp/example/ui/ExampleViewModel.kt b/app/src/main/java/com/mapbox/services/android/navigation/testapp/example/ui/ExampleViewModel.kt index 1cb02ce0daf..d3aaf4da497 100644 --- a/app/src/main/java/com/mapbox/services/android/navigation/testapp/example/ui/ExampleViewModel.kt +++ b/app/src/main/java/com/mapbox/services/android/navigation/testapp/example/ui/ExampleViewModel.kt @@ -26,6 +26,7 @@ import com.mapbox.services.android.navigation.ui.v5.camera.DynamicCamera import com.mapbox.services.android.navigation.ui.v5.voice.NavigationSpeechPlayer import com.mapbox.services.android.navigation.ui.v5.voice.SpeechPlayerProvider import com.mapbox.services.android.navigation.ui.v5.voice.VoiceInstructionLoader +import com.mapbox.services.android.navigation.v5.location.replay.ReplayRouteLocationEngine import com.mapbox.services.android.navigation.v5.milestone.Milestone import com.mapbox.services.android.navigation.v5.navigation.MapboxNavigation import com.mapbox.services.android.navigation.v5.routeprogress.RouteProgress @@ -52,7 +53,6 @@ class ExampleViewModel(application: Application) : AndroidViewModel(application) val geocode: MutableLiveData = MutableLiveData() var primaryRoute: DirectionsRoute? = null - var collapsedBottomSheet: Boolean = false var isOffRoute: Boolean = false private val locationEngine: LocationEngine @@ -64,7 +64,8 @@ class ExampleViewModel(application: Application) : AndroidViewModel(application) private val routeFinder: RouteFinder init { - routeFinder = RouteFinder(this, routes, accessToken, retrieveOfflineVersionFromPreferences()) + routeFinder = RouteFinder(this, routes, accessToken, retrieveOfflineVersionFromPreferences(), + retrieveProfileFromPreferences()) // Initialize the location engine locationEngine = LocationEngineProvider.getBestLocationEngine(getApplication()) locationEngineCallback = ExampleLocationEngineCallback(location) @@ -112,11 +113,24 @@ class ExampleViewModel(application: Application) : AndroidViewModel(application) fun startNavigation() { primaryRoute?.let { + if (shouldSimulateRoute()) { + val replayRouteLocationEngine = ReplayRouteLocationEngine() + replayRouteLocationEngine.assign(it) + navigation.locationEngine = replayRouteLocationEngine + } else { + navigation.locationEngine = locationEngine + } + navigation.startNavigation(it) removeLocation() } } + private fun shouldSimulateRoute(): Boolean { + val context = getApplication() + return PreferenceManager.getDefaultSharedPreferences(context).getBoolean(context.getString(R.string.simulate_route_key), false) + } + fun stopNavigation() { requestLocation() navigation.stopNavigation() @@ -174,6 +188,13 @@ class ExampleViewModel(application: Application) : AndroidViewModel(application) .getString(context.getString(R.string.offline_version_key), "") } + private fun retrieveProfileFromPreferences(): String { + val context = getApplication() + return PreferenceManager.getDefaultSharedPreferences(context) + .getString(context.getString(R.string.route_profile_key), context.getString(R.string + .default_route_profile)) + } + private fun buildEngineRequest(): LocationEngineRequest { return LocationEngineRequest.Builder(UPDATE_INTERVAL_IN_MILLISECONDS) .setPriority(LocationEngineRequest.PRIORITY_HIGH_ACCURACY) diff --git a/app/src/main/java/com/mapbox/services/android/navigation/testapp/example/ui/PresenterState.kt b/app/src/main/java/com/mapbox/services/android/navigation/testapp/example/ui/PresenterState.kt index 8903a2c3d29..aab72c417d5 100644 --- a/app/src/main/java/com/mapbox/services/android/navigation/testapp/example/ui/PresenterState.kt +++ b/app/src/main/java/com/mapbox/services/android/navigation/testapp/example/ui/PresenterState.kt @@ -2,8 +2,7 @@ package com.mapbox.services.android.navigation.testapp.example.ui enum class PresenterState { SHOW_LOCATION, - SELECTED_DESTINATION, - FIND_ROUTE, - ROUTE_FOUND, + SEARCH, + SHOW_ROUTE, NAVIGATE } \ No newline at end of file diff --git a/app/src/main/java/com/mapbox/services/android/navigation/testapp/example/ui/navigation/ExampleRouteFinder.kt b/app/src/main/java/com/mapbox/services/android/navigation/testapp/example/ui/navigation/ExampleRouteFinder.kt index e08959c4df3..aeac5fe045a 100644 --- a/app/src/main/java/com/mapbox/services/android/navigation/testapp/example/ui/navigation/ExampleRouteFinder.kt +++ b/app/src/main/java/com/mapbox/services/android/navigation/testapp/example/ui/navigation/ExampleRouteFinder.kt @@ -13,6 +13,7 @@ import timber.log.Timber private const val BEARING_TOLERANCE = 90.0 class ExampleRouteFinder(private val accessToken: String, + private val profile: String, private val callback: OnRoutesFoundCallback): Callback { fun findRoute(location: Location, destination: Point) { @@ -33,6 +34,7 @@ class ExampleRouteFinder(private val accessToken: String, NavigationRoute.builder(NavigationApplication.instance) .accessToken(accessToken) .origin(origin, bearing, BEARING_TOLERANCE) + .profile(profile) .destination(destination) .alternatives(true) .build() diff --git a/app/src/main/java/com/mapbox/services/android/navigation/testapp/example/ui/navigation/RouteFinder.kt b/app/src/main/java/com/mapbox/services/android/navigation/testapp/example/ui/navigation/RouteFinder.kt index 7368691a9ad..aa6c279df10 100644 --- a/app/src/main/java/com/mapbox/services/android/navigation/testapp/example/ui/navigation/RouteFinder.kt +++ b/app/src/main/java/com/mapbox/services/android/navigation/testapp/example/ui/navigation/RouteFinder.kt @@ -14,9 +14,10 @@ import timber.log.Timber class RouteFinder(private val viewModel: ExampleViewModel, private val routes: MutableLiveData>, accessToken: String, - private var tileVersion: String) : OnRoutesFoundCallback { + private var tileVersion: String, + private var profile: String) : OnRoutesFoundCallback { - private val routeFinder: ExampleRouteFinder = ExampleRouteFinder(accessToken, this) + private val routeFinder: ExampleRouteFinder = ExampleRouteFinder(accessToken, profile, this) private val offlineRouteFinder = OfflineRouteFinder(obtainOfflineDirectory(), tileVersion, this) internal fun findRoute(location: Location, destination: Point) { diff --git a/app/src/main/res/layout/activity_example.xml b/app/src/main/res/layout/activity_example.xml index 185b65cd0ad..b4e9203e5e5 100644 --- a/app/src/main/res/layout/activity_example.xml +++ b/app/src/main/res/layout/activity_example.xml @@ -75,13 +75,6 @@ android:layout_marginBottom="@dimen/fab_margin_bottom" app:srcCompat="@drawable/ic_my_location"/> - - - Driving - Cycling - Walking + @string/route_profile_driving + @string/route_profile_cycling + @string/route_profile_walking diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 83c014d369f..1608d543742 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -50,6 +50,10 @@ simulate_route language route_profile + Driving + Cycling + Walking + @string/route_profile_driving view_examples git_hash offline_preference_key diff --git a/app/src/main/res/xml/fragment_navigation_preferences.xml b/app/src/main/res/xml/fragment_navigation_preferences.xml index 939eae54744..8c0c34ff9b3 100644 --- a/app/src/main/res/xml/fragment_navigation_preferences.xml +++ b/app/src/main/res/xml/fragment_navigation_preferences.xml @@ -40,6 +40,7 @@ android:title="@string/unit_type"/> Date: Mon, 15 Apr 2019 11:55:05 -0400 Subject: [PATCH 2/5] Updated with onActivityResult to refresh profile when it changes in settings. --- .../testapp/example/ui/ExampleActivity.kt | 13 ++++++++++++- .../testapp/example/ui/ExamplePresenter.kt | 5 +---- .../testapp/example/ui/ExampleViewModel.kt | 19 ++++++++++++++++--- .../ui/navigation/ExampleRouteFinder.kt | 2 +- .../example/ui/navigation/RouteFinder.kt | 6 +++++- 5 files changed, 35 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/com/mapbox/services/android/navigation/testapp/example/ui/ExampleActivity.kt b/app/src/main/java/com/mapbox/services/android/navigation/testapp/example/ui/ExampleActivity.kt index 046f1c3abc9..d4cd8e16b1f 100644 --- a/app/src/main/java/com/mapbox/services/android/navigation/testapp/example/ui/ExampleActivity.kt +++ b/app/src/main/java/com/mapbox/services/android/navigation/testapp/example/ui/ExampleActivity.kt @@ -1,6 +1,7 @@ package com.mapbox.services.android.navigation.testapp.example.ui import android.Manifest +import android.app.Activity import android.arch.lifecycle.ViewModelProviders import android.content.Intent import android.content.pm.PackageManager @@ -34,9 +35,12 @@ import com.mapbox.services.android.navigation.v5.milestone.Milestone import com.mapbox.services.android.navigation.v5.navigation.MapboxNavigation import com.mapbox.services.android.navigation.v5.routeprogress.RouteProgress import kotlinx.android.synthetic.main.activity_example.* +import org.jetbrains.anko.startActivityForResult private const val ZERO_PADDING = 0 private const val BOTTOMSHEET_MULTIPLIER = 4 +private const val CHANGE_SETTING_REQUEST_CODE = 1 + class ExampleActivity : HistoryActivity(), ExampleView { private var map: NavigationMapboxMap? = null @@ -237,7 +241,14 @@ class ExampleActivity : HistoryActivity(), ExampleView { } override fun showSettings() { - startActivity(Intent(this, NavigationSettingsActivity::class.java)) + startActivityForResult(Intent(this, NavigationSettingsActivity::class.java), CHANGE_SETTING_REQUEST_CODE) + } + + override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { + super.onActivityResult(requestCode, resultCode, data) + if (requestCode == CHANGE_SETTING_REQUEST_CODE && resultCode == Activity.RESULT_OK) { + viewModel.updateProfile() + } } override fun adjustMapPaddingForNavigation() { diff --git a/app/src/main/java/com/mapbox/services/android/navigation/testapp/example/ui/ExamplePresenter.kt b/app/src/main/java/com/mapbox/services/android/navigation/testapp/example/ui/ExamplePresenter.kt index 8da1cd0936e..7b33a76ebdd 100644 --- a/app/src/main/java/com/mapbox/services/android/navigation/testapp/example/ui/ExamplePresenter.kt +++ b/app/src/main/java/com/mapbox/services/android/navigation/testapp/example/ui/ExamplePresenter.kt @@ -103,6 +103,7 @@ class ExamplePresenter(private val view: ExampleView, private val viewModel: Exa view.updateLocationRenderMode(RenderMode.NORMAL) view.updateCameraTrackingMode(NavigationCamera.NAVIGATION_TRACKING_MODE_NONE) view.updateLocationFabVisibility(VISIBLE) + view.updateNavigationFabVisibility(INVISIBLE) view.updateSettingsFabVisibility(VISIBLE) view.updateCancelFabVisibility(INVISIBLE) view.updateInstructionViewVisibility(INVISIBLE) @@ -114,9 +115,6 @@ class ExamplePresenter(private val view: ExampleView, private val viewModel: Exa BottomSheetBehavior.STATE_COLLAPSED -> { view.hideSoftKeyboard() presenterState = PresenterState.SHOW_LOCATION - - view.updateLocationFabVisibility(VISIBLE) - view.updateSettingsFabVisibility(VISIBLE) } BottomSheetBehavior.STATE_EXPANDED -> { presenterState = PresenterState.SEARCH @@ -165,7 +163,6 @@ class ExamplePresenter(private val view: ExampleView, private val viewModel: Exa } presenterState = PresenterState.SHOW_ROUTE } - view.updateRoutes(directionsRoutes) } } diff --git a/app/src/main/java/com/mapbox/services/android/navigation/testapp/example/ui/ExampleViewModel.kt b/app/src/main/java/com/mapbox/services/android/navigation/testapp/example/ui/ExampleViewModel.kt index d3aaf4da497..64ed1b1605d 100644 --- a/app/src/main/java/com/mapbox/services/android/navigation/testapp/example/ui/ExampleViewModel.kt +++ b/app/src/main/java/com/mapbox/services/android/navigation/testapp/example/ui/ExampleViewModel.kt @@ -61,7 +61,7 @@ class ExampleViewModel(application: Application) : AndroidViewModel(application) private val navigation: MapboxNavigation private val accessToken: String = instance.resources.getString(R.string.mapbox_access_token) - private val routeFinder: RouteFinder + val routeFinder: RouteFinder init { routeFinder = RouteFinder(this, routes, accessToken, retrieveOfflineVersionFromPreferences(), @@ -86,6 +86,10 @@ class ExampleViewModel(application: Application) : AndroidViewModel(application) navigation.addOffRouteListener(ExampleOffRouteListener(this)) } + internal fun updateProfile() { + routeFinder.updateProfile(retrieveProfileFromPreferences()) + } + override fun onCleared() { super.onCleared() shutdown() @@ -190,9 +194,18 @@ class ExampleViewModel(application: Application) : AndroidViewModel(application) private fun retrieveProfileFromPreferences(): String { val context = getApplication() - return PreferenceManager.getDefaultSharedPreferences(context) + return normalizeForTraffic(PreferenceManager.getDefaultSharedPreferences(context) .getString(context.getString(R.string.route_profile_key), context.getString(R.string - .default_route_profile)) + .default_route_profile))) + } + + private fun normalizeForTraffic(string: String): String { + var normalizedString = string.toLowerCase() + if (string.equals("driving")) { + return "driving-traffic" + } else { + return normalizedString + } } private fun buildEngineRequest(): LocationEngineRequest { diff --git a/app/src/main/java/com/mapbox/services/android/navigation/testapp/example/ui/navigation/ExampleRouteFinder.kt b/app/src/main/java/com/mapbox/services/android/navigation/testapp/example/ui/navigation/ExampleRouteFinder.kt index aeac5fe045a..3f32bf0b9e3 100644 --- a/app/src/main/java/com/mapbox/services/android/navigation/testapp/example/ui/navigation/ExampleRouteFinder.kt +++ b/app/src/main/java/com/mapbox/services/android/navigation/testapp/example/ui/navigation/ExampleRouteFinder.kt @@ -13,7 +13,7 @@ import timber.log.Timber private const val BEARING_TOLERANCE = 90.0 class ExampleRouteFinder(private val accessToken: String, - private val profile: String, + var profile: String, private val callback: OnRoutesFoundCallback): Callback { fun findRoute(location: Location, destination: Point) { diff --git a/app/src/main/java/com/mapbox/services/android/navigation/testapp/example/ui/navigation/RouteFinder.kt b/app/src/main/java/com/mapbox/services/android/navigation/testapp/example/ui/navigation/RouteFinder.kt index aa6c279df10..dacf607736e 100644 --- a/app/src/main/java/com/mapbox/services/android/navigation/testapp/example/ui/navigation/RouteFinder.kt +++ b/app/src/main/java/com/mapbox/services/android/navigation/testapp/example/ui/navigation/RouteFinder.kt @@ -15,7 +15,7 @@ class RouteFinder(private val viewModel: ExampleViewModel, private val routes: MutableLiveData>, accessToken: String, private var tileVersion: String, - private var profile: String) : OnRoutesFoundCallback { + profile: String) : OnRoutesFoundCallback { private val routeFinder: ExampleRouteFinder = ExampleRouteFinder(accessToken, profile, this) private val offlineRouteFinder = OfflineRouteFinder(obtainOfflineDirectory(), tileVersion, this) @@ -35,6 +35,10 @@ class RouteFinder(private val viewModel: ExampleViewModel, } } + internal fun updateProfile(profile: String) { + routeFinder.profile = profile + } + override fun onRoutesFound(routes: List) { updateRoutes(routes) } From 7089b2a71a93cff6a0aa2c0f2e611b65f931acd8 Mon Sep 17 00:00:00 2001 From: Devota Aabel Date: Mon, 15 Apr 2019 13:30:37 -0400 Subject: [PATCH 3/5] Remove reverse-geocoding --- .../testapp/example/ui/ExamplePresenter.kt | 14 +--------- .../testapp/example/ui/ExampleViewModel.kt | 27 ------------------- 2 files changed, 1 insertion(+), 40 deletions(-) diff --git a/app/src/main/java/com/mapbox/services/android/navigation/testapp/example/ui/ExamplePresenter.kt b/app/src/main/java/com/mapbox/services/android/navigation/testapp/example/ui/ExamplePresenter.kt index 7b33a76ebdd..14eee97c10e 100644 --- a/app/src/main/java/com/mapbox/services/android/navigation/testapp/example/ui/ExamplePresenter.kt +++ b/app/src/main/java/com/mapbox/services/android/navigation/testapp/example/ui/ExamplePresenter.kt @@ -10,7 +10,6 @@ import android.view.View.INVISIBLE import android.view.View.VISIBLE import com.mapbox.api.directions.v5.models.DirectionsRoute import com.mapbox.api.geocoding.v5.models.CarmenFeature -import com.mapbox.api.geocoding.v5.models.GeocodingResponse import com.mapbox.geojson.Point import com.mapbox.mapboxsdk.camera.CameraPosition import com.mapbox.mapboxsdk.camera.CameraUpdate @@ -162,6 +161,7 @@ class ExamplePresenter(private val view: ExampleView, private val viewModel: Exa moveCameraToInclude(destination) } presenterState = PresenterState.SHOW_ROUTE + view.updateSettingsFabVisibility(INVISIBLE) } view.updateRoutes(directionsRoutes) } @@ -184,7 +184,6 @@ class ExamplePresenter(private val view: ExampleView, private val viewModel: Exa } fun onMapLongClick(point: LatLng): Boolean { - viewModel.reverseGeocode(point) viewModel.destination.value = Point.fromLngLat(point.longitude, point.latitude) viewModel.findRouteToDestination() return true @@ -215,7 +214,6 @@ class ExamplePresenter(private val view: ExampleView, private val viewModel: Exa viewModel.routes.observe(owner, Observer { onRouteFound(it) }) viewModel.progress.observe(owner, Observer { onProgressUpdate(it) }) viewModel.milestone.observe(owner, Observer { onMilestoneUpdate(it) }) - viewModel.geocode.observe(owner, Observer { onGeocodingResponse(it) }) viewModel.activateLocationEngine() } @@ -258,14 +256,4 @@ class ExamplePresenter(private val view: ExampleView, private val viewModel: Exa view.updateMapCameraFor(bounds, padding, TWO_SECONDS) } } - - private fun onGeocodingResponse(it: GeocodingResponse?) { - val features = it?.features() - val isValidFeatureList = features?.isNotEmpty() ?: false - if (isValidFeatureList) { - features?.first()?.let { firstFeature -> - onDestinationFound(firstFeature) - } - } - } } \ No newline at end of file diff --git a/app/src/main/java/com/mapbox/services/android/navigation/testapp/example/ui/ExampleViewModel.kt b/app/src/main/java/com/mapbox/services/android/navigation/testapp/example/ui/ExampleViewModel.kt index 64ed1b1605d..4f3a72a6e57 100644 --- a/app/src/main/java/com/mapbox/services/android/navigation/testapp/example/ui/ExampleViewModel.kt +++ b/app/src/main/java/com/mapbox/services/android/navigation/testapp/example/ui/ExampleViewModel.kt @@ -10,12 +10,7 @@ import com.mapbox.android.core.location.LocationEngine import com.mapbox.android.core.location.LocationEngineProvider import com.mapbox.android.core.location.LocationEngineRequest import com.mapbox.api.directions.v5.models.DirectionsRoute -import com.mapbox.api.geocoding.v5.GeocodingCriteria -import com.mapbox.api.geocoding.v5.MapboxGeocoding -import com.mapbox.api.geocoding.v5.models.GeocodingResponse import com.mapbox.geojson.Point -import com.mapbox.mapboxsdk.Mapbox -import com.mapbox.mapboxsdk.geometry.LatLng import com.mapbox.services.android.navigation.testapp.NavigationApplication.Companion.instance import com.mapbox.services.android.navigation.testapp.R import com.mapbox.services.android.navigation.testapp.example.ui.navigation.ExampleMilestoneEventListener @@ -31,10 +26,6 @@ import com.mapbox.services.android.navigation.v5.milestone.Milestone import com.mapbox.services.android.navigation.v5.navigation.MapboxNavigation import com.mapbox.services.android.navigation.v5.routeprogress.RouteProgress import okhttp3.Cache -import retrofit2.Call -import retrofit2.Callback -import retrofit2.Response -import timber.log.Timber import java.io.File import java.util.Locale.US @@ -50,7 +41,6 @@ class ExampleViewModel(application: Application) : AndroidViewModel(application) val progress: MutableLiveData = MutableLiveData() val milestone: MutableLiveData = MutableLiveData() val destination: MutableLiveData = MutableLiveData() - val geocode: MutableLiveData = MutableLiveData() var primaryRoute: DirectionsRoute? = null var isOffRoute: Boolean = false @@ -144,23 +134,6 @@ class ExampleViewModel(application: Application) : AndroidViewModel(application) return navigation } - fun reverseGeocode(point: LatLng) { - val reverseGeocode = MapboxGeocoding.builder() - .accessToken(Mapbox.getAccessToken()!!) - .query(Point.fromLngLat(point.longitude, point.latitude)) - .geocodingTypes(GeocodingCriteria.TYPE_ADDRESS) - .build() - reverseGeocode.enqueueCall(object : Callback { - override fun onResponse(call: Call, response: Response) { - geocode.value = response.body() - } - - override fun onFailure(call: Call, throwable: Throwable) { - Timber.e(throwable, "Geocoding request failed") - } - }) - } - fun refreshOfflineVersionFromPreferences() { val version = retrieveOfflineVersionFromPreferences() routeFinder.updateOfflineVersion(version) From e21cadd66e0af3e6b6794ea40f4f101efd414954 Mon Sep 17 00:00:00 2001 From: Devota Aabel Date: Thu, 18 Apr 2019 13:50:03 -0400 Subject: [PATCH 4/5] Fixed double centering on route --- .../android/navigation/testapp/example/ui/ExampleActivity.kt | 2 -- .../android/navigation/testapp/example/ui/ExamplePresenter.kt | 1 - 2 files changed, 3 deletions(-) diff --git a/app/src/main/java/com/mapbox/services/android/navigation/testapp/example/ui/ExampleActivity.kt b/app/src/main/java/com/mapbox/services/android/navigation/testapp/example/ui/ExampleActivity.kt index d4cd8e16b1f..dbc27217182 100644 --- a/app/src/main/java/com/mapbox/services/android/navigation/testapp/example/ui/ExampleActivity.kt +++ b/app/src/main/java/com/mapbox/services/android/navigation/testapp/example/ui/ExampleActivity.kt @@ -35,13 +35,11 @@ import com.mapbox.services.android.navigation.v5.milestone.Milestone import com.mapbox.services.android.navigation.v5.navigation.MapboxNavigation import com.mapbox.services.android.navigation.v5.routeprogress.RouteProgress import kotlinx.android.synthetic.main.activity_example.* -import org.jetbrains.anko.startActivityForResult private const val ZERO_PADDING = 0 private const val BOTTOMSHEET_MULTIPLIER = 4 private const val CHANGE_SETTING_REQUEST_CODE = 1 - class ExampleActivity : HistoryActivity(), ExampleView { private var map: NavigationMapboxMap? = null private val viewModel by lazy(mode = LazyThreadSafetyMode.NONE) { diff --git a/app/src/main/java/com/mapbox/services/android/navigation/testapp/example/ui/ExamplePresenter.kt b/app/src/main/java/com/mapbox/services/android/navigation/testapp/example/ui/ExamplePresenter.kt index 14eee97c10e..4b2e8a6dec0 100644 --- a/app/src/main/java/com/mapbox/services/android/navigation/testapp/example/ui/ExamplePresenter.kt +++ b/app/src/main/java/com/mapbox/services/android/navigation/testapp/example/ui/ExamplePresenter.kt @@ -133,7 +133,6 @@ class ExamplePresenter(private val view: ExampleView, private val viewModel: Exa view.hideSoftKeyboard() view.updateAutocompleteBottomSheetState(BottomSheetBehavior.STATE_COLLAPSED) view.updateDestinationMarker(it) - view.updateMapCamera(buildCameraUpdateFrom(it), TWO_SECONDS) view.updateLocationFabVisibility(INVISIBLE) view.updateSettingsFabVisibility(INVISIBLE) view.updateNavigationFabVisibility(VISIBLE) From 2b3fcfe8960de58b313f4d51a8a5a4f4cb162a0f Mon Sep 17 00:00:00 2001 From: Devota Aabel Date: Wed, 24 Apr 2019 11:28:25 -0400 Subject: [PATCH 5/5] Updated to fix bug where destination marker wasn't showing up --- .../testapp/example/ui/ExamplePresenter.kt | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/com/mapbox/services/android/navigation/testapp/example/ui/ExamplePresenter.kt b/app/src/main/java/com/mapbox/services/android/navigation/testapp/example/ui/ExamplePresenter.kt index 4b2e8a6dec0..236e36bf6fb 100644 --- a/app/src/main/java/com/mapbox/services/android/navigation/testapp/example/ui/ExamplePresenter.kt +++ b/app/src/main/java/com/mapbox/services/android/navigation/testapp/example/ui/ExamplePresenter.kt @@ -55,7 +55,6 @@ class ExamplePresenter(private val view: ExampleView, private val viewModel: Exa fun onAutocompleteClick() { view.selectAllAutocompleteText() - view.updateLocationFabVisibility(INVISIBLE) view.updateAutocompleteBottomSheetState(BottomSheetBehavior.STATE_EXPANDED) } @@ -123,19 +122,9 @@ class ExamplePresenter(private val view: ExampleView, private val viewModel: Exa fun onDestinationFound(feature: CarmenFeature) { feature.center()?.let { - if (presenterState == PresenterState.SHOW_ROUTE) { - view.removeRoute() - viewModel.primaryRoute = null - view.updateNavigationFabVisibility(INVISIBLE) - } viewModel.destination.value = it - view.clearMarkers() view.hideSoftKeyboard() view.updateAutocompleteBottomSheetState(BottomSheetBehavior.STATE_COLLAPSED) - view.updateDestinationMarker(it) - view.updateLocationFabVisibility(INVISIBLE) - view.updateSettingsFabVisibility(INVISIBLE) - view.updateNavigationFabVisibility(VISIBLE) viewModel.findRouteToDestination() } } @@ -153,14 +142,17 @@ class ExamplePresenter(private val view: ExampleView, private val viewModel: Exa fun onRouteFound(routes: List?) { routes?.let { directionsRoutes -> if (presenterState != PresenterState.NAVIGATE) { + view.clearMarkers() view.transition() view.showAlternativeRoutes(true) + view.updateLocationFabVisibility(INVISIBLE) + view.updateSettingsFabVisibility(INVISIBLE) view.updateNavigationFabVisibility(VISIBLE) viewModel.destination.value?.let { destination -> + view.updateDestinationMarker(destination) moveCameraToInclude(destination) } presenterState = PresenterState.SHOW_ROUTE - view.updateSettingsFabVisibility(INVISIBLE) } view.updateRoutes(directionsRoutes) }