From 1ba069475f5286c70d3fbf1ddc295e5c8050e391 Mon Sep 17 00:00:00 2001 From: Kyle Madsen Date: Thu, 25 Aug 2022 14:53:36 -0700 Subject: [PATCH] Migrate InactiveRouteStylingActivity --- libnavigation-core/api/current.txt | 8 +- .../RequireMapboxNavigationProperty.kt | 7 +- .../qa_test_app/view/FeedbackActivity.kt | 2 +- .../view/InactiveRouteStylingActivity.kt | 92 ++++++++++--------- 4 files changed, 57 insertions(+), 52 deletions(-) diff --git a/libnavigation-core/api/current.txt b/libnavigation-core/api/current.txt index d1a64ec4617..879f10729bf 100644 --- a/libnavigation-core/api/current.txt +++ b/libnavigation-core/api/current.txt @@ -337,13 +337,13 @@ package com.mapbox.navigation.core.lifecycle { method public com.mapbox.navigation.base.options.NavigationOptions createNavigationOptions(); } - public final class RequireMapboxNavigationDelegate implements kotlin.properties.ReadOnlyProperty { - ctor public RequireMapboxNavigationDelegate(com.mapbox.navigation.core.lifecycle.MapboxNavigationObserver? observer = null, kotlin.jvm.functions.Function0? onInitialize = null); + public final class RequireMapboxNavigationProperty implements kotlin.properties.ReadOnlyProperty { + ctor public RequireMapboxNavigationProperty(com.mapbox.navigation.core.lifecycle.MapboxNavigationObserver? onCreatedObserver = null, com.mapbox.navigation.core.lifecycle.MapboxNavigationObserver? onStartedObserver = null, com.mapbox.navigation.core.lifecycle.MapboxNavigationObserver? onResumedObserver = null, kotlin.jvm.functions.Function0? onInitialize = null); method public com.mapbox.navigation.core.MapboxNavigation getValue(androidx.lifecycle.LifecycleOwner thisRef, kotlin.reflect.KProperty property); } - public final class RequireMapboxNavigationDelegateKt { - method public static com.mapbox.navigation.core.lifecycle.RequireMapboxNavigationDelegate requireMapboxNavigation(com.mapbox.navigation.core.lifecycle.MapboxNavigationObserver? observer = null, kotlin.jvm.functions.Function0 onInitialize); + public final class RequireMapboxNavigationPropertyKt { + method public static com.mapbox.navigation.core.lifecycle.RequireMapboxNavigationProperty requireMapboxNavigation(com.mapbox.navigation.core.lifecycle.MapboxNavigationObserver? onCreatedObserver = null, com.mapbox.navigation.core.lifecycle.MapboxNavigationObserver? onStartedObserver = null, com.mapbox.navigation.core.lifecycle.MapboxNavigationObserver? onResumedObserver = null, kotlin.jvm.functions.Function0 onInitialize); } } diff --git a/libnavigation-core/src/main/java/com/mapbox/navigation/core/lifecycle/RequireMapboxNavigationProperty.kt b/libnavigation-core/src/main/java/com/mapbox/navigation/core/lifecycle/RequireMapboxNavigationProperty.kt index 34ba2945272..3dfd99a7667 100644 --- a/libnavigation-core/src/main/java/com/mapbox/navigation/core/lifecycle/RequireMapboxNavigationProperty.kt +++ b/libnavigation-core/src/main/java/com/mapbox/navigation/core/lifecycle/RequireMapboxNavigationProperty.kt @@ -1,6 +1,7 @@ package com.mapbox.navigation.core.lifecycle import androidx.lifecycle.DefaultLifecycleObserver +import androidx.lifecycle.Lifecycle import androidx.lifecycle.LifecycleOwner import com.mapbox.navigation.core.MapboxNavigation import kotlin.properties.ReadOnlyProperty @@ -70,9 +71,9 @@ fun requireMapboxNavigation( * [MapboxNavigation] instance can be re-created with [MapboxNavigationApp.disable], or if all * [MapboxNavigationApp.attach] lifecycles are destroyed. * - * @param onCreatedObserver to setup any subscriptions to [MapboxNavigation] - * @param onStartedObserver to setup any subscriptions to [MapboxNavigation] - * @param onResumedObserver to setup any subscriptions to [MapboxNavigation] + * @param onCreatedObserver to setup any subscriptions when the lifecycle [Lifecycle.State.CREATED] + * @param onStartedObserver to setup any subscriptions when the lifecycle [Lifecycle.State.STARTED] + * @param onResumedObserver to setup any subscriptions when the lifecycle [Lifecycle.State.RESUMED] * @param onInitialize called after the lifecycle has been attached */ class RequireMapboxNavigationProperty( diff --git a/qa-test-app/src/main/java/com/mapbox/navigation/qa_test_app/view/FeedbackActivity.kt b/qa-test-app/src/main/java/com/mapbox/navigation/qa_test_app/view/FeedbackActivity.kt index c8fef3c8970..f0a700a91ed 100644 --- a/qa-test-app/src/main/java/com/mapbox/navigation/qa_test_app/view/FeedbackActivity.kt +++ b/qa-test-app/src/main/java/com/mapbox/navigation/qa_test_app/view/FeedbackActivity.kt @@ -130,7 +130,7 @@ class FeedbackActivity : AppCompatActivity() { } private val mapboxNavigation by requireMapboxNavigation( - observer = object : MapboxNavigationObserver { + onCreatedObserver = object : MapboxNavigationObserver { override fun onAttached(mapboxNavigation: MapboxNavigation) { binding.mapView.location.apply { setLocationProvider(navigationLocationProvider) diff --git a/qa-test-app/src/main/java/com/mapbox/navigation/qa_test_app/view/InactiveRouteStylingActivity.kt b/qa-test-app/src/main/java/com/mapbox/navigation/qa_test_app/view/InactiveRouteStylingActivity.kt index 663f3aaaa3c..3b9bf8283d8 100644 --- a/qa-test-app/src/main/java/com/mapbox/navigation/qa_test_app/view/InactiveRouteStylingActivity.kt +++ b/qa-test-app/src/main/java/com/mapbox/navigation/qa_test_app/view/InactiveRouteStylingActivity.kt @@ -17,7 +17,9 @@ import com.mapbox.maps.plugin.animation.camera import com.mapbox.maps.plugin.locationcomponent.location import com.mapbox.navigation.base.options.NavigationOptions import com.mapbox.navigation.core.MapboxNavigation -import com.mapbox.navigation.core.MapboxNavigationProvider +import com.mapbox.navigation.core.lifecycle.MapboxNavigationApp +import com.mapbox.navigation.core.lifecycle.MapboxNavigationObserver +import com.mapbox.navigation.core.lifecycle.requireMapboxNavigation import com.mapbox.navigation.core.replay.MapboxReplayer import com.mapbox.navigation.core.replay.ReplayLocationEngine import com.mapbox.navigation.core.replay.route.ReplayProgressObserver @@ -56,15 +58,6 @@ class InactiveRouteStylingActivity : AppCompatActivity() { binding.mapView.camera } - private val mapboxNavigation: MapboxNavigation by lazy { - MapboxNavigationProvider.create( - NavigationOptions.Builder(this) - .accessToken(Utils.getMapboxAccessToken(this)) - .locationEngine(ReplayLocationEngine(mapboxReplayer)) - .build() - ) - } - private val routeLineColorResources by lazy { RouteLineColorResources.Builder() .inActiveRouteLegsColor(Color.YELLOW) @@ -95,32 +88,30 @@ class InactiveRouteStylingActivity : AppCompatActivity() { MapboxRouteLineApi(options) } - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) - setContentView(binding.root) - - initNavigation() - initStyle() - initListeners() - } - - override fun onStop() { - super.onStop() - mapboxNavigation.unregisterRouteProgressObserver(replayProgressObserver) - mapboxNavigation.unregisterRouteProgressObserver(routeProgressObserver) - mapboxNavigation.unregisterLocationObserver(locationObserver) - mapboxNavigation.stopTripSession() - } + private val mapboxNavigation by requireMapboxNavigation( + onCreatedObserver = object : MapboxNavigationObserver { + override fun onAttached(mapboxNavigation: MapboxNavigation) { + initListeners() + } - override fun onDestroy() { - super.onDestroy() - routeLineApi.cancel() - routeLineView.cancel() - mapboxReplayer.finish() - mapboxNavigation.onDestroy() + override fun onDetached(mapboxNavigation: MapboxNavigation) { + mapboxNavigation.unregisterRouteProgressObserver(replayProgressObserver) + mapboxNavigation.unregisterRouteProgressObserver(routeProgressObserver) + mapboxNavigation.unregisterLocationObserver(locationObserver) + mapboxNavigation.stopTripSession() + } + } + ) { + MapboxNavigationApp.setup( + NavigationOptions.Builder(this) + .accessToken(Utils.getMapboxAccessToken(this)) + .locationEngine(ReplayLocationEngine(mapboxReplayer)) + .build() + ) } - private fun initNavigation() { + @SuppressLint("MissingPermission") + private fun initListeners() { binding.mapView.location.apply { setLocationProvider(navigationLocationProvider) enabled = true @@ -128,9 +119,32 @@ class InactiveRouteStylingActivity : AppCompatActivity() { mapboxNavigation.setRoutes(listOf(getRoute())) mapboxNavigation.registerLocationObserver(locationObserver) mapboxNavigation.registerRouteProgressObserver(replayProgressObserver) - mapboxReplayer.pushRealLocation(this, 0.0) + mapboxReplayer.pushRealLocation( + this@InactiveRouteStylingActivity, + 0.0 + ) mapboxReplayer.playbackSpeed(1.5) mapboxReplayer.play() + binding.startNavigation.setOnClickListener { + mapboxNavigation.registerRouteProgressObserver(routeProgressObserver) + mapboxNavigation.startTripSession() + binding.startNavigation.visibility = View.GONE + startSimulation(mapboxNavigation.getRoutes()[0]) + } + } + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(binding.root) + + initStyle() + } + + override fun onDestroy() { + super.onDestroy() + routeLineApi.cancel() + routeLineView.cancel() + mapboxReplayer.finish() } private fun initStyle() { @@ -177,16 +191,6 @@ class InactiveRouteStylingActivity : AppCompatActivity() { ) } - @SuppressLint("MissingPermission") - private fun initListeners() { - binding.startNavigation.setOnClickListener { - mapboxNavigation.registerRouteProgressObserver(routeProgressObserver) - mapboxNavigation.startTripSession() - binding.startNavigation.visibility = View.GONE - startSimulation(mapboxNavigation.getRoutes()[0]) - } - } - private val routeProgressObserver = RouteProgressObserver { routeProgress -> // This is the most important part of this example. The route progress will be used to