@@ -7,12 +7,13 @@ import androidx.lifecycle.Lifecycle
77import androidx.lifecycle.LifecycleOwner
88import com.mapbox.navigation.base.ExperimentalPreviewMapboxNavigationAPI
99import com.mapbox.navigation.core.MapboxNavigation
10+ import com.mapbox.navigation.core.lifecycle.MapboxNavigationApp.lifecycleOwner
1011import kotlin.properties.ReadOnlyProperty
1112import kotlin.reflect.KProperty
1213
1314/* *
14- * Extension function to make it simple to create the [RequireMapboxNavigationDelegate]. Below are
15- * a couple examples of how you may use the delegate.
15+ * Extension function to make it simple to create the [RequireMapboxNavigationDelegate].
16+ * Below are a couple examples of how you may use the delegate.
1617 *
1718 * Default can be used when [MapboxNavigationApp] is setup elsewhere.
1819 * ```
@@ -51,12 +52,13 @@ import kotlin.reflect.KProperty
5152 * @see [RequireMapboxNavigationDelegate] for more details.
5253 */
5354@ExperimentalPreviewMapboxNavigationAPI
54- fun requireMapboxNavigation (
55+ fun LifecycleOwner. requireMapboxNavigation (
5556 onCreatedObserver : MapboxNavigationObserver ? = null,
5657 onStartedObserver : MapboxNavigationObserver ? = null,
5758 onResumedObserver : MapboxNavigationObserver ? = null,
5859 onInitialize : (() -> Unit )? = null,
59- ): ReadOnlyProperty <LifecycleOwner , MapboxNavigation > = RequireMapboxNavigationDelegate (
60+ ): ReadOnlyProperty <Any , MapboxNavigation > = RequireMapboxNavigationDelegate (
61+ lifecycleOwner = this ,
6062 onCreatedObserver = onCreatedObserver,
6163 onStartedObserver = onStartedObserver,
6264 onResumedObserver = onResumedObserver,
@@ -74,22 +76,23 @@ fun requireMapboxNavigation(
7476 * [MapboxNavigation] instance can be re-created with [MapboxNavigationApp.disable], or if all
7577 * [MapboxNavigationApp.attach] lifecycles are destroyed.
7678 *
79+ * @param lifecycleOwner: LifecycleOwner
7780 * @param onCreatedObserver registered to the [Lifecycle.State.CREATED] lifecycle
7881 * @param onStartedObserver registered to the [Lifecycle.State.STARTED] lifecycle
7982 * @param onResumedObserver registered to the [Lifecycle.State.RESUMED] lifecycle
80- * @param onInitialize called when the property is read for the first time
83+ * @param onInitialize called when the [lifecycleOwner] is [Lifecycle.State.CREATED]
8184 */
8285internal class RequireMapboxNavigationDelegate (
86+ lifecycleOwner : LifecycleOwner ,
8387 private val onCreatedObserver : MapboxNavigationObserver ? = null ,
8488 private val onStartedObserver : MapboxNavigationObserver ? = null ,
8589 private val onResumedObserver : MapboxNavigationObserver ? = null ,
8690 private val onInitialize : (() -> Unit )? = null
87- ) : ReadOnlyProperty<LifecycleOwner, MapboxNavigation> {
88-
89- private lateinit var lifecycleOwner: LifecycleOwner
91+ ) : ReadOnlyProperty<Any, MapboxNavigation> {
9092
9193 private val lifecycleObserver = object : DefaultLifecycleObserver {
9294 override fun onCreate (owner : LifecycleOwner ) {
95+ onInitialize?.invoke()
9396 onCreatedObserver?.let { MapboxNavigationApp .registerObserver(it) }
9497 }
9598
@@ -114,21 +117,19 @@ internal class RequireMapboxNavigationDelegate(
114117 }
115118 }
116119
120+ init {
121+ MapboxNavigationApp .attach(lifecycleOwner)
122+ lifecycleOwner.lifecycle.addObserver(lifecycleObserver)
123+ }
124+
117125 /* *
118- * Returns an instance of [MapboxNavigation], the first retrieval will attach the [thisRef]
119- * to [MapboxNavigationApp.attach]. If [MapboxNavigationApp.isSetup] is false after all
120- * observers and initializers, this property getter will crash.
126+ * Returns an instance of [MapboxNavigation]. If [MapboxNavigationApp.isSetup] is false after
127+ * all observers and initializers, this property getter will crash.
121128 *
122129 * @param thisRef - the [LifecycleOwner] that needs access to [MapboxNavigation].
123130 * @param property - ignored
124131 */
125- override fun getValue (thisRef : LifecycleOwner , property : KProperty <* >): MapboxNavigation {
126- if (! this ::lifecycleOwner.isInitialized) {
127- onInitialize?.invoke()
128- this .lifecycleOwner = thisRef
129- MapboxNavigationApp .attach(lifecycleOwner)
130- lifecycleOwner.lifecycle.addObserver(lifecycleObserver)
131- }
132+ override fun getValue (thisRef : Any , property : KProperty <* >): MapboxNavigation {
132133 val mapboxNavigation = MapboxNavigationApp .current()
133134 checkNotNull(mapboxNavigation) {
134135 " MapboxNavigation cannot be null. Ensure that MapboxNavigationApp is setup and an" +
0 commit comments