Skip to content

Commit

Permalink
Make SharedApp backwards compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
kmadsen committed Sep 9, 2022
1 parent 31f0d6a commit 80d1dd4
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 36 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Mapbox welcomes participation and contributions from everyone.
## Unreleased
#### Features
#### Bug fixes and improvements
- Fixed backwards compatibility issue with `SharedApp`. [#6303](https://github.com/mapbox/mapbox-navigation-android/pull/6303)

## Mapbox Navigation SDK 2.8.0-beta.3 - 09 September, 2022
### Changelog
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,10 @@ object MapboxCarApp {
}

/**
* Setup android auto from your [Application.onCreate]
*
* @param application used to detect when activities are foregrounded
* Set up services that are used by Android Auto.
*/
fun setup(application: Application) {
SharedApp.setup(application)
fun setup() {
MapboxNavigationApp.registerObserver(SharedApp)
MapboxNavigationApp.registerObserver(CarAppLocationImpl())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.mapbox.navigation.ui.app.internal

import android.content.Context
import com.mapbox.navigation.base.ExperimentalPreviewMapboxNavigationAPI
import com.mapbox.navigation.core.MapboxNavigation
import com.mapbox.navigation.core.internal.extensions.attachCreated
import com.mapbox.navigation.core.lifecycle.MapboxNavigationApp
import com.mapbox.navigation.core.lifecycle.MapboxNavigationObserver
Expand All @@ -23,9 +24,7 @@ import com.mapbox.navigation.ui.voice.internal.impl.MapboxAudioGuidanceImpl
import java.util.concurrent.atomic.AtomicBoolean

@OptIn(ExperimentalPreviewMapboxNavigationAPI::class)
object SharedApp {
private var isSetup = false

object SharedApp : MapboxNavigationObserver {
val store = Store()
val state get() = store.state.value

Expand All @@ -42,6 +41,10 @@ object SharedApp {
val destinationStateController = DestinationStateController(store)
val routeStateController = RouteStateController(store)
val routePreviewStateController = RoutePreviewStateController(store)
internal val stateResetController = StateResetController(store, ignoreTripSessionUpdates)
private val routeAlternativeComponent = RouteAlternativeComponent {
RouteAlternativeComponentImpl(store)
}
private val navigationObservers: Array<MapboxNavigationObserver> = arrayOf(
routeStateController,
cameraStateController,
Expand All @@ -51,26 +54,14 @@ object SharedApp {
routePreviewStateController,
audioGuidanceStateController,
tripSessionStarterStateController,
stateResetController,
routeAlternativeComponent,
)

@JvmOverloads
fun setup(
context: Context,
audioGuidance: MapboxAudioGuidance? = null,
routeAlternativeContract: RouteAlternativeContract? = null
) {
if (isSetup) return
isSetup = true

MapboxNavigationApp.registerObserver(StateResetController(store, ignoreTripSessionUpdates))
MapboxNavigationApp.registerObserver(
RouteAlternativeComponent {
routeAlternativeContract ?: RouteAlternativeComponentImpl(store)
}
)
MapboxNavigationApp.lifecycleOwner.attachCreated(*navigationObservers)
MapboxNavigationApp.registerObserver(audioGuidance ?: defaultAudioGuidance(context))
}
/**
* Requires the [Context] so will be initialized when attached.
*/
private lateinit var mapboxAudioGuidance: MapboxAudioGuidance

fun tripSessionTransaction(updateSession: () -> Unit) {
// Any changes to MapboxNavigation TripSession should be done within `tripSessionTransaction { }` block.
Expand All @@ -87,5 +78,17 @@ object SharedApp {
}
}

override fun onAttached(mapboxNavigation: MapboxNavigation) {
val context = mapboxNavigation.navigationOptions.applicationContext
val audioGuidance: MapboxAudioGuidance = defaultAudioGuidance(context)
MapboxNavigationApp.lifecycleOwner.attachCreated(*navigationObservers)
MapboxNavigationApp.registerObserver(audioGuidance)
}

override fun onDetached(mapboxNavigation: MapboxNavigation) {
navigationObservers.forEach { MapboxNavigationApp.unregisterObserver(it) }
MapboxNavigationApp.unregisterObserver(mapboxAudioGuidance)
}

private const val DEFAULT_DATA_STORE_NAME = "mapbox_navigation_preferences"
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class NavigationView @JvmOverloads constructor(
keepScreenOn = true
captureSystemBarsInsets()

SharedApp.setup(context.applicationContext as Application)
MapboxNavigationApp.registerObserver(SharedApp)
if (!MapboxNavigationApp.isSetup()) {
MapboxNavigationApp.setup(
NavigationOptions.Builder(context)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
package com.mapbox.navigation.qa_test_app

import android.app.Application
import com.mapbox.androidauto.MapboxCarApp

class QaTestApplication : Application() {

override fun onCreate() {
super.onCreate()

MapboxCarApp.setup(this)
}
}
class QaTestApplication : Application()
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import com.mapbox.android.core.permissions.PermissionsManager
import com.mapbox.androidauto.MapboxCarApp
import com.mapbox.androidauto.MapboxCarNavigationManager
import com.mapbox.androidauto.car.MainCarContext
import com.mapbox.androidauto.car.MainScreenManager
Expand Down Expand Up @@ -47,6 +48,7 @@ class MainCarSession : Session() {

init {
logAndroidAuto("MainCarSession constructor")
MapboxCarApp.setup()
val logoSurfaceRenderer = CarLogoSurfaceRenderer()
val compassSurfaceRenderer = CarCompassSurfaceRenderer()
MapboxNavigationApp.attach(lifecycleOwner = this)
Expand Down

0 comments on commit 80d1dd4

Please sign in to comment.