Skip to content

Commit

Permalink
Migrate InactiveRouteStylingActivity
Browse files Browse the repository at this point in the history
  • Loading branch information
kmadsen committed Aug 25, 2022
1 parent 9bf5554 commit 1ba0694
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 52 deletions.
8 changes: 4 additions & 4 deletions libnavigation-core/api/current.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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<androidx.lifecycle.LifecycleOwner,com.mapbox.navigation.core.MapboxNavigation> {
ctor public RequireMapboxNavigationDelegate(com.mapbox.navigation.core.lifecycle.MapboxNavigationObserver? observer = null, kotlin.jvm.functions.Function0<kotlin.Unit>? onInitialize = null);
public final class RequireMapboxNavigationProperty implements kotlin.properties.ReadOnlyProperty<androidx.lifecycle.LifecycleOwner,com.mapbox.navigation.core.MapboxNavigation> {
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<kotlin.Unit>? 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<kotlin.Unit> 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<kotlin.Unit> onInitialize);
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -95,42 +88,63 @@ 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
}
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() {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 1ba0694

Please sign in to comment.