Skip to content

Commit e92feb7

Browse files
committed
Create a delegate for MapboxNavigation
1 parent f68ed19 commit e92feb7

6 files changed

Lines changed: 559 additions & 71 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Mapbox welcomes participation and contributions from everyone.
44

55
## Unreleased
66
#### Features
7+
- Added `RequireMapboxNavigationDelegate` and `requireMapboxNavigation` to offer a simple way to use `MapboxNavigationApp`. [#6233](https://github.com/mapbox/mapbox-navigation-android/pull/6233)
78
#### Bug fixes and improvements
89

910
## Mapbox Navigation SDK 2.8.0-beta.1 - 25 August, 2022

libnavigation-core/api/current.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,15 @@ package com.mapbox.navigation.core.lifecycle {
337337
method public com.mapbox.navigation.base.options.NavigationOptions createNavigationOptions();
338338
}
339339

340+
public final class RequireMapboxNavigationProperty implements kotlin.properties.ReadOnlyProperty<androidx.lifecycle.LifecycleOwner,com.mapbox.navigation.core.MapboxNavigation> {
341+
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);
342+
method public com.mapbox.navigation.core.MapboxNavigation getValue(androidx.lifecycle.LifecycleOwner thisRef, kotlin.reflect.KProperty<?> property);
343+
}
344+
345+
public final class RequireMapboxNavigationPropertyKt {
346+
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);
347+
}
348+
340349
}
341350

342351
package com.mapbox.navigation.core.navigator {
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package com.mapbox.navigation.core.lifecycle
2+
3+
import androidx.lifecycle.DefaultLifecycleObserver
4+
import androidx.lifecycle.Lifecycle
5+
import androidx.lifecycle.LifecycleOwner
6+
import com.mapbox.navigation.core.MapboxNavigation
7+
import kotlin.properties.ReadOnlyProperty
8+
import kotlin.reflect.KProperty
9+
10+
/**
11+
* Extension function to make it simple to create the [RequireMapboxNavigationProperty]. Below are
12+
* a couple examples of how you may use the delegate.
13+
*
14+
* Default can be used when [MapboxNavigationApp] is setup elsewhere.
15+
* ```
16+
* val mapboxNavigation by requireMapboxNavigation()
17+
* ```
18+
*
19+
* Initialize the [MapboxNavigationApp] when you are ready to use it
20+
* ```
21+
* val mapboxNavigation by requireMapboxNavigation {
22+
* MapboxNavigationApp.setup(..)
23+
* }
24+
* ```
25+
*
26+
* Register subscriptions and setup MapboxNavigationApp
27+
* ```
28+
* private val mapboxNavigation by requireMapboxNavigation(
29+
* onResumedObserver = object : MapboxNavigationObserver {
30+
* override fun onAttached(mapboxNavigation: MapboxNavigation) {
31+
* mapboxNavigation.registerLocationObserver(locationObserver)
32+
* mapboxNavigation.registerRoutesObserver(routesObserver)
33+
* }
34+
* override fun onDetached(mapboxNavigation: MapboxNavigation) {
35+
* mapboxNavigation.unregisterLocationObserver(locationObserver)
36+
* mapboxNavigation.unregisterRoutesObserver(routesObserver)
37+
* }
38+
* }
39+
* ) {
40+
* MapboxNavigationApp.setup(
41+
* NavigationOptions.Builder(this)
42+
* .accessToken(accessToken)
43+
* .build()
44+
* )
45+
* }
46+
* ```
47+
*
48+
* @see [RequireMapboxNavigationProperty] for more details.
49+
*/
50+
fun requireMapboxNavigation(
51+
onCreatedObserver: MapboxNavigationObserver? = null,
52+
onStartedObserver: MapboxNavigationObserver? = null,
53+
onResumedObserver: MapboxNavigationObserver? = null,
54+
onInitialize: () -> Unit
55+
) = RequireMapboxNavigationProperty(
56+
onCreatedObserver = onCreatedObserver,
57+
onStartedObserver = onStartedObserver,
58+
onResumedObserver = onResumedObserver,
59+
onInitialize = onInitialize
60+
)
61+
62+
/**
63+
* Creates a simple way to use the [MapboxNavigationApp] inside a [LifecycleOwner] and provides
64+
* access to [MapboxNavigation].
65+
*
66+
* You can choose to call [MapboxNavigationApp.setup] in the [onInitialize]. You can also setup in
67+
* the onCreate calls, or any call that happens before this delegate is accessed. The delegate will
68+
* crash if the app is not setup and an attached has been created.
69+
*
70+
* You can use the observers parameter to setup any subscriptions. This is important because the
71+
* [MapboxNavigation] instance can be re-created with [MapboxNavigationApp.disable], or if all
72+
* [MapboxNavigationApp.attach] lifecycles are destroyed.
73+
*
74+
* @param onCreatedObserver to setup any subscriptions when the lifecycle [Lifecycle.State.CREATED]
75+
* @param onStartedObserver to setup any subscriptions when the lifecycle [Lifecycle.State.STARTED]
76+
* @param onResumedObserver to setup any subscriptions when the lifecycle [Lifecycle.State.RESUMED]
77+
* @param onInitialize called after the lifecycle has been attached
78+
*/
79+
class RequireMapboxNavigationProperty(
80+
private val onCreatedObserver: MapboxNavigationObserver? = null,
81+
private val onStartedObserver: MapboxNavigationObserver? = null,
82+
private val onResumedObserver: MapboxNavigationObserver? = null,
83+
private val onInitialize: (() -> Unit)? = null
84+
) : ReadOnlyProperty<LifecycleOwner, MapboxNavigation> {
85+
86+
private lateinit var lifecycleOwner: LifecycleOwner
87+
88+
private val lifecycleObserver = object : DefaultLifecycleObserver {
89+
override fun onCreate(owner: LifecycleOwner) {
90+
onCreatedObserver?.let { MapboxNavigationApp.registerObserver(it) }
91+
}
92+
93+
override fun onDestroy(owner: LifecycleOwner) {
94+
onCreatedObserver?.let { MapboxNavigationApp.unregisterObserver(it) }
95+
}
96+
97+
override fun onStart(owner: LifecycleOwner) {
98+
onStartedObserver?.let { MapboxNavigationApp.registerObserver(it) }
99+
}
100+
101+
override fun onStop(owner: LifecycleOwner) {
102+
onStartedObserver?.let { MapboxNavigationApp.unregisterObserver(it) }
103+
}
104+
105+
override fun onResume(owner: LifecycleOwner) {
106+
onResumedObserver?.let { MapboxNavigationApp.registerObserver(it) }
107+
}
108+
109+
override fun onPause(owner: LifecycleOwner) {
110+
onResumedObserver?.let { MapboxNavigationApp.unregisterObserver(it) }
111+
}
112+
}
113+
114+
override fun getValue(thisRef: LifecycleOwner, property: KProperty<*>): MapboxNavigation {
115+
if (!this::lifecycleOwner.isInitialized) {
116+
onInitialize?.invoke()
117+
this.lifecycleOwner = thisRef
118+
MapboxNavigationApp.attach(lifecycleOwner)
119+
lifecycleOwner.lifecycle.addObserver(lifecycleObserver)
120+
}
121+
val mapboxNavigation = MapboxNavigationApp.current()
122+
checkNotNull(mapboxNavigation) {
123+
"MapboxNavigation cannot be null. Ensure that MapboxNavigationApp is setup and an" +
124+
" attached lifecycle is at least CREATED."
125+
}
126+
return mapboxNavigation
127+
}
128+
}

0 commit comments

Comments
 (0)